package com.example.controller;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import com.example.common.Result;
import com.example.common.ResultCode;
import com.example.entity.Account;
import com.example.entity.AuthorityInfo;
import com.example.exception.CustomException;
import com.example.entity.TeacherInfo;
import com.example.entity.StudentInfo;
import com.example.service.TeacherInfoService;
import com.example.service.StudentInfoService;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Value;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import cn.hutool.json.JSONUtil;
import java.util.*;
import java.util.stream.Collectors;
@RestController
public class AccountController {
@Value("${authority.info}")
private String authorityStr;
@Resource
private TeacherInfoService teacherInfoService;
@Resource
private StudentInfoService studentInfoService;
@PostMapping("/login")
public Result<Account> login(@RequestBody Account account, HttpServletRequest request) {
if (StrUtil.isBlank(account.getName()) || StrUtil.isBlank(account.getPassword()) || account.getLevel() == null) {
throw new CustomException(ResultCode.PARAM_LOST_ERROR);
}
Integer level = account.getLevel();
Account login = new Account();
if (1 == level) {
login = teacherInfoService.login(account.getName(), account.getPassword());
}
if (2 == level) {
login = studentInfoService.login(account.getName(), account.getPassword());
}
request.getSession().setAttribute("user", login);
return Result.success(login);
}
@PostMapping("/register")
public Result<Account> register(@RequestBody Account account) {
Integer level = account.getLevel();
Account login = new Account();
if (1 == level) {
TeacherInfo info = new TeacherInfo();
BeanUtils.copyProperties(account, info);
login = teacherInfoService.add(info);
}
if (2 == level) {
StudentInfo info = new StudentInfo();
BeanUtils.copyProperties(account, info);
login = studentInfoService.add(info);
}
return Result.success(login);
}
@GetMapping("/logout")
public Result logout(HttpServletRequest request) {
request.getSession().setAttribute("user", null);
return Result.success();
}
@GetMapping("/auth")
public Result getAuth(HttpServletRequest request) {
Object user = request.getSession().getAttribute("user");
if(user == null) {
return Result.error("401", "未登录");
}
return Result.success(user);
}
@GetMapping("/getAccountInfo")
public Result<Object> getAccountInfo(HttpServletRequest request) {
Account account = (Account) request.getSession().getAttribute("user");
if (account == null) {
return Result.success(new Object());
}
Integer level = account.getLevel();
if (1 == level) {
return Result.success(teacherInfoService.findById(account.getId()));
}
if (2 == level) {
return Result.success(studentInfoService.findById(account.getId()));
}
return Result.success(new Object());
}
@GetMapping("/getSession")
public Result<Map<String, String>> getSession(HttpServletRequest request) {
Account account = (Account) request.getSession().getAttribute("user");
if (account == null) {
return Result.success(new HashMap<>(1));
}
Map<String, String> map = new HashMap<>(1);
map.put("username", account.getName());
return Result.success(map);
}
@GetMapping("/getAuthority")
public Result<List<AuthorityInfo>> getAuthorityInfo() {
List<AuthorityInfo> authorityInfoList = JSONUtil.toList(JSONUtil.parseArray(authorityStr), AuthorityInfo.class);
return Result.success(authorityInfoList);
}
/**
* 获取当前用户所能看到的模块信息
* @param request
* @return
*/
@GetMapping("/authority")
public Result<List<Integer>> getAuthorityInfo(HttpServletRequest request) {
Account user = (Account) request.getSession().getAttribute("user");
if (user == null) {
return Result.success(new ArrayList<>());
}
JSONArray objects = JSONUtil.parseArray(authorityStr);
for (Object object : objects) {
JSONObject jsonObject = (JSONObject) object;
if (user.getLevel().equals(jsonObject.getInt("level"))) {
JSONArray array = JSONUtil.parseArray(jsonObject.getStr("models"));
List<Integer> modelIdList = array.stream().map((o -> {
JSONObject obj = (JSONObject) o;
return obj.getInt("modelId");
})).collect(Collectors.toList());
return Result.success(modelIdList);
}
}
return Result.success(new ArrayList<>());
}
@GetMapping("/permission/{modelId}")
public Result<List<Integer>> getPermission(@PathVariable Integer modelId, HttpServletRequest request) {
List<AuthorityInfo> authorityInfoList = JSONUtil.toList(JSONUtil.parseArray(authorityStr), AuthorityInfo.class);
Account user = (Account) request.getSession().getAttribute("user");
if (user == null) {
return Result.success(new ArrayList<>());
}
Optional<AuthorityInfo> optional = authorityInfoList.stream().filter(x -> x.getLevel().equals(user.getLevel())).findFirst();
if (optional.isPresent()) {
Optional<AuthorityInfo.Model> firstOption = optional.get().getModels().stream().filter(x -> x.getModelId().equals(modelId)).findFirst();
if (firstOption.isPresent()) {
List<Integer> info = firstOption.get().getOperation();
return Result.success(info);
}
}
return Result.success(new ArrayList<>());
}
@PutMapping("/updatePassword")
public Result updatePassword(@RequestBody Account info, HttpServletRequest request) {
Account account = (Account) request.getSession().getAttribute("user");
if (account == null) {
return Result.error(ResultCode.USER_NOT_EXIST_ERROR.code, ResultCode.USER_NOT_EXIST_ERROR.msg);
}
String oldPassword = SecureUtil.md5(info.getPassword());
if (!oldPassword.equals(account.getPassword())) {
return Result.error(ResultCode.PARAM_PASSWORD_ERROR.code, ResultCode.PARAM_PASSWORD_ERROR.msg);
}
info.setPassword(SecureUtil.md5(info.getNewPassword()));
Integer level = account.getLevel();
if (1 == level) {
TeacherInfo teacherInfo = new TeacherInfo();
BeanUtils.copyProperties(info, teacherInfo);
teacherInfoService.update(teacherInfo);
}
if (2 == level) {
StudentInfo studentInfo = new StudentInfo();
BeanUtils.copyProperties(info, studentInfo);
studentInfoService.update(studentInfo);
}
info.setLevel(level);
info.setName(account.getName());
// 清空session,让用户重新登录
request.getSession().setAttribute("user", null);
return Result.success();
}
@PostMapping("/resetPassword")
public Result resetPassword(@RequestBody Account account) {
Integer level = account.getLevel();
if (1 == level) {
TeacherInfo info = teacherInfoService.findByUserName(account.getName());
if (info == null) {
return Result.error(ResultCode.USER_NOT_EXIST_ERROR.code, ResultCode.USER_NOT_EXIST_ERROR.msg);
}
info.setPassword(SecureUtil.md5("123456"));
teacherInfoService.update(info);
}
if (2 == level) {
Stu
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
(基于ssm框架的Java毕业设计)学生平时成绩管理系统(源码) 【项目技术】 开发语言:Java 框架:ssm 架构:B/S 数据库:mysql 【实现功能】 (1)学生 该模块供大学生群体使用,大学生提供系统注册、登陆、个人信息修改等账户及登陆功能,可以查看下载老师发布的学习资料、编辑上传个人作业、查看老师作业评语以及成绩查询; (2)老师 该模块供老师使用,老师提供系统注册、登陆、个人信息修改等账户及登陆功能,并可以编辑发布学习资料,对学生上传的作业进行审核、批注,上传学生成绩等; (3)管理员模块 管理员使用管理员账户登陆系统,拥有系统最高权限,可以对系统数据进行管理和维护,包括编辑发布系统公告、查看老师信息、学生信息、成绩信息等,并可以图形化的查看上述数据信息。
资源推荐
资源详情
资源评论
收起资源包目录
(基于ssm框架的Java毕业设计)学生平时成绩管理系统(源码).rar (416个子文件)
$PRODUCT_WORKSPACE_FILE$ 489B
AccountController.class 12KB
EchartsController.class 10KB
TeacherInfoController.class 9KB
StudentInfoController.class 9KB
NxSystemFileController.class 8KB
AdvertiserInfoController.class 8KB
ChengjiInfoController.class 8KB
KechengInfoController.class 8KB
FileInfoController.class 5KB
MenuController.class 5KB
SubmitInfoController.class 5KB
SubmitInfoCommentService.class 4KB
FileInfoCommentService.class 4KB
StudentInfoService.class 4KB
TeacherInfoService.class 4KB
SubmitInfoCommentController.class 4KB
FileInfoCommentController.class 4KB
TeacherInfo.class 3KB
KechengInfoService.class 3KB
ChengjiInfoService.class 3KB
AdvertiserInfoService.class 3KB
SubmitInfoService.class 3KB
StudentInfo.class 3KB
SubmitInfo.class 3KB
Account.class 3KB
NxSystemFileInfoService.class 3KB
FileInfoService.class 3KB
Result.class 2KB
FileInfo.class 2KB
EchartsData.class 2KB
ResultCode.class 2KB
SubmitInfoComment.class 2KB
FileInfoComment.class 2KB
MyInterceptor.class 2KB
KechengInfo.class 2KB
GlobalExceptionHandler.class 2KB
EchartsData$Data.class 2KB
WebMvcConfig.class 2KB
AdvertiserInfo.class 2KB
ChengjiInfo.class 2KB
EchartsData$Series.class 1KB
AuthorityInfo.class 1KB
MyInterceptorConfig.class 1KB
NxSystemFileInfo.class 1KB
AuthorityInfo$Model.class 1KB
SubmitInfoCommentVo.class 1KB
FileInfoCommentVo.class 1KB
CustomException.class 1KB
TeacherInfoDao.class 930B
StudentInfoDao.class 930B
ChengjiInfoDao.class 904B
KechengInfoDao.class 904B
SubmitInfoCommentDao.class 898B
FileInfoCommentDao.class 888B
SubmitInfoDao.class 865B
Application.class 793B
NxSystemFileInfoDao.class 773B
FileInfoDao.class 682B
AdvertiserInfoDao.class 649B
KechengInfoVo.class 609B
ChengjiInfoVo.class 609B
AdvertiserInfoVo.class 320B
StudentInfoVo.class 308B
TeacherInfoVo.class 308B
SubmitInfoVo.class 304B
FileInfoVo.class 296B
index.css 227KB
index.css 227KB
index.css 227KB
index.css 227KB
bootstrap.min.css 119KB
bootstrap.min.css 119KB
bootstrap.min.css 119KB
bootstrap.min.css 119KB
font-awesome.css 28KB
font-awesome.css 28KB
font-awesome.css 26KB
font-awesome.css 26KB
quill.snow.css 24KB
quill.snow.css 24KB
swiper.min.css 13KB
swiper.min.css 13KB
basic.css 5KB
basic.css 5KB
my.css 1KB
my.css 1KB
custom.css 1KB
custom.css 1KB
my.css 1KB
my.css 1KB
index.css 480B
index.css 480B
notice.css 475B
notice.css 475B
fontawesome-webfont.eot 55KB
fontawesome-webfont.eot 55KB
fontawesome-webfont.eot 55KB
fontawesome-webfont.eot 55KB
glyphicons-halflings-regular.eot 20KB
共 416 条
- 1
- 2
- 3
- 4
- 5
资源评论
岛上程序猿
- 粉丝: 2235
- 资源: 4154
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功