添加用户登录相关功能
parent
30ea11fc10
commit
4845c9717e
@ -0,0 +1,69 @@
|
|||||||
|
package com.supervision.knowsub.controller.system;
|
||||||
|
|
||||||
|
import cn.hutool.core.bean.BeanUtil;
|
||||||
|
import cn.hutool.core.util.ObjectUtil;
|
||||||
|
import cn.hutool.core.util.StrUtil;
|
||||||
|
import cn.hutool.json.JSONUtil;
|
||||||
|
import com.supervision.knowsub.domain.UserInfo;
|
||||||
|
import com.supervision.knowsub.exception.BusinessException;
|
||||||
|
import com.supervision.knowsub.model.SystemUser;
|
||||||
|
import com.supervision.knowsub.service.SystemUserService;
|
||||||
|
import com.supervision.knowsub.util.TokenUtil;
|
||||||
|
import com.supervision.knowsub.util.UserUtil;
|
||||||
|
import com.supervision.knowsub.vo.user.LoginReqVO;
|
||||||
|
import com.supervision.knowsub.vo.user.LoginResVO;
|
||||||
|
import com.supervision.knowsub.vo.user.UserInfoResVo;
|
||||||
|
import io.swagger.v3.oas.annotations.Operation;
|
||||||
|
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||||
|
import lombok.RequiredArgsConstructor;
|
||||||
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
|
@Tag(name = "用户管理")
|
||||||
|
@RestController
|
||||||
|
@RequestMapping("user")
|
||||||
|
@RequiredArgsConstructor
|
||||||
|
public class UserController {
|
||||||
|
|
||||||
|
private final SystemUserService systemUserService;
|
||||||
|
|
||||||
|
@Operation(summary = "登录")
|
||||||
|
@PostMapping("login")
|
||||||
|
public LoginResVO login(@RequestBody LoginReqVO reqVO) {
|
||||||
|
if (!StrUtil.isAllNotBlank(reqVO.getUserAccount(), reqVO.getPassword())) {
|
||||||
|
throw new BusinessException("用户名不能为空");
|
||||||
|
}
|
||||||
|
Optional<SystemUser> user = systemUserService.lambdaQuery().eq(SystemUser::getAccount, reqVO.getUserAccount()).last("limit 1").oneOpt();
|
||||||
|
if (user.isEmpty() || !UserUtil.checkUserPassword(reqVO.getPassword(), user.get().getUserPd())) {
|
||||||
|
throw new BusinessException("用户名或密码有误!");
|
||||||
|
}
|
||||||
|
// 更新用户最近的登录时间
|
||||||
|
String token = TokenUtil.creatToken(JSONUtil.toJsonStr(user.get()));
|
||||||
|
|
||||||
|
LoginResVO loginResVO = BeanUtil.toBean(user.get(), LoginResVO.class);
|
||||||
|
loginResVO.setToken(token);
|
||||||
|
loginResVO.setUsername(user.get().getUsername());
|
||||||
|
loginResVO.setRoleId(user.get().getRoleId());
|
||||||
|
return loginResVO;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@Operation(summary = "查看账号信息")
|
||||||
|
@GetMapping("getUserAccountInfo")
|
||||||
|
public UserInfoResVo getUserAccountInfo() {
|
||||||
|
UserInfo user = UserUtil.getUser();
|
||||||
|
SystemUser systemUser = systemUserService.getById(user.getId());
|
||||||
|
if (ObjectUtil.isEmpty(systemUser)){
|
||||||
|
throw new BusinessException("用户信息不存在");
|
||||||
|
}
|
||||||
|
return BeanUtil.toBean(systemUser, UserInfoResVo.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,10 @@
|
|||||||
|
package com.supervision.knowsub.vo.user;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
public class LoginReqVO {
|
||||||
|
|
||||||
|
private String userAccount;
|
||||||
|
private String password;
|
||||||
|
}
|
Loading…
Reference in New Issue