|
|
|
@ -2,9 +2,9 @@ package cn.zyp.stusystem.controller;
|
|
|
|
|
|
|
|
|
|
|
|
import cn.dev33.satoken.stp.StpUtil;
|
|
|
|
import cn.dev33.satoken.stp.StpUtil;
|
|
|
|
import cn.zyp.stusystem.common.Result;
|
|
|
|
import cn.zyp.stusystem.common.Result;
|
|
|
|
import cn.zyp.stusystem.constant.PermissionConstants;
|
|
|
|
|
|
|
|
import cn.zyp.stusystem.dto.LoginDTO;
|
|
|
|
import cn.zyp.stusystem.dto.LoginDTO;
|
|
|
|
import cn.zyp.stusystem.entity.User;
|
|
|
|
import cn.zyp.stusystem.entity.User;
|
|
|
|
|
|
|
|
import cn.zyp.stusystem.service.RoleService;
|
|
|
|
import cn.zyp.stusystem.service.UserService;
|
|
|
|
import cn.zyp.stusystem.service.UserService;
|
|
|
|
import cn.zyp.stusystem.vo.LoginResult;
|
|
|
|
import cn.zyp.stusystem.vo.LoginResult;
|
|
|
|
import cn.zyp.stusystem.vo.UserVO;
|
|
|
|
import cn.zyp.stusystem.vo.UserVO;
|
|
|
|
@ -23,6 +23,9 @@ public class LoginController {
|
|
|
|
@Autowired
|
|
|
|
@Autowired
|
|
|
|
private UserService userService;
|
|
|
|
private UserService userService;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
|
|
|
|
private RoleService roleService;
|
|
|
|
|
|
|
|
|
|
|
|
@PostMapping("/login")
|
|
|
|
@PostMapping("/login")
|
|
|
|
public Result<LoginResult> login(@RequestBody LoginDTO loginDTO) {
|
|
|
|
public Result<LoginResult> login(@RequestBody LoginDTO loginDTO) {
|
|
|
|
// 1. 根据用户名查询用户信息
|
|
|
|
// 1. 根据用户名查询用户信息
|
|
|
|
@ -36,16 +39,28 @@ public class LoginController {
|
|
|
|
// 3. SaToken登录(自动生成token)
|
|
|
|
// 3. SaToken登录(自动生成token)
|
|
|
|
StpUtil.login(user.getId());
|
|
|
|
StpUtil.login(user.getId());
|
|
|
|
|
|
|
|
|
|
|
|
// 4. 在Session中存储角色和权限
|
|
|
|
// 4. 获取用户权限(从数据库获取,而不是从常量)
|
|
|
|
|
|
|
|
List<String> permissions = userService.getUserPermissions(user.getId());
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 5. 获取角色ID(如果user中没有roleId,则根据role查找)
|
|
|
|
|
|
|
|
Long roleId = user.getRoleId();
|
|
|
|
|
|
|
|
if (roleId == null && user.getRole() != null) {
|
|
|
|
|
|
|
|
roleId = roleService.getRoleIdByRoleCode(user.getRole());
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 6. 在Session中存储角色和权限
|
|
|
|
StpUtil.getSession().set("role", user.getRole());
|
|
|
|
StpUtil.getSession().set("role", user.getRole());
|
|
|
|
List<String> permissions = PermissionConstants.ROLE_PERMISSIONS.get(user.getRole());
|
|
|
|
|
|
|
|
StpUtil.getSession().set("permissions", permissions);
|
|
|
|
StpUtil.getSession().set("permissions", permissions);
|
|
|
|
|
|
|
|
if (roleId != null) {
|
|
|
|
|
|
|
|
StpUtil.getSession().set("roleId", roleId);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 5. 构建返回数据
|
|
|
|
// 7. 构建返回数据
|
|
|
|
UserVO userVO = new UserVO();
|
|
|
|
UserVO userVO = new UserVO();
|
|
|
|
userVO.setId(user.getId());
|
|
|
|
userVO.setId(user.getId());
|
|
|
|
userVO.setUsername(user.getUsername());
|
|
|
|
userVO.setUsername(user.getUsername());
|
|
|
|
userVO.setRole(user.getRole());
|
|
|
|
userVO.setRole(user.getRole());
|
|
|
|
|
|
|
|
userVO.setRoleId(roleId != null ? roleId.toString() : null); // 转换为字符串,前端需要
|
|
|
|
userVO.setName(user.getName());
|
|
|
|
userVO.setName(user.getName());
|
|
|
|
userVO.setPermissions(permissions);
|
|
|
|
userVO.setPermissions(permissions);
|
|
|
|
|
|
|
|
|
|
|
|
|