60 lines
2.1 KiB
Java
60 lines
2.1 KiB
Java
|
|
package com.yupi.springbootinit.controller;
|
||
|
|
|
||
|
|
import cn.dev33.satoken.stp.StpUtil;
|
||
|
|
import cn.hutool.core.bean.BeanUtil;
|
||
|
|
import com.yupi.springbootinit.common.BaseResponse;
|
||
|
|
import com.yupi.springbootinit.common.ErrorCode;
|
||
|
|
import com.yupi.springbootinit.common.ResultUtils;
|
||
|
|
import com.yupi.springbootinit.exception.BusinessException;
|
||
|
|
import com.yupi.springbootinit.model.dto.user.SystemUsersDTO;
|
||
|
|
import com.yupi.springbootinit.model.entity.SystemUsers;
|
||
|
|
import com.yupi.springbootinit.model.enums.CommonStatusEnum;
|
||
|
|
import com.yupi.springbootinit.model.vo.user.SystemUsersVO;
|
||
|
|
import com.yupi.springbootinit.service.SystemUsersService;
|
||
|
|
import lombok.extern.slf4j.Slf4j;
|
||
|
|
import org.springframework.web.bind.annotation.*;
|
||
|
|
|
||
|
|
import javax.annotation.Resource;
|
||
|
|
|
||
|
|
/*
|
||
|
|
* @author: ziin
|
||
|
|
* @date: 2025/6/11 19:50
|
||
|
|
*/
|
||
|
|
@RestController
|
||
|
|
@RequestMapping("/user")
|
||
|
|
@Slf4j
|
||
|
|
@CrossOrigin
|
||
|
|
public class UserController {
|
||
|
|
|
||
|
|
|
||
|
|
@Resource
|
||
|
|
private SystemUsersService usersService;
|
||
|
|
|
||
|
|
// 测试登录,浏览器访问: http://localhost:8081/user/doLogin?username=zhang&password=123456
|
||
|
|
@PostMapping("doLogin")
|
||
|
|
public BaseResponse<SystemUsersVO> doLogin(@RequestBody SystemUsersDTO usersDTO) {
|
||
|
|
SystemUsers user = usersService.getUserByUserName(usersDTO.getUsername());
|
||
|
|
if (user == null) {
|
||
|
|
throw new BusinessException(ErrorCode.USERNAME_OR_PASSWORD_ERROR);
|
||
|
|
}
|
||
|
|
|
||
|
|
if (!usersService.isPasswordMatch(usersDTO.getPassword(), user.getPassword())) {
|
||
|
|
throw new BusinessException(ErrorCode.USERNAME_OR_PASSWORD_ERROR);
|
||
|
|
}
|
||
|
|
|
||
|
|
if (CommonStatusEnum.isDisable(user.getStatus())) {
|
||
|
|
throw new BusinessException(ErrorCode.USER_DISABLE);
|
||
|
|
}
|
||
|
|
SystemUsersVO systemUsersVO = new SystemUsersVO();
|
||
|
|
BeanUtil.copyProperties(user, systemUsersVO);
|
||
|
|
StpUtil.login(user.getId());
|
||
|
|
return ResultUtils.success(systemUsersVO);
|
||
|
|
}
|
||
|
|
|
||
|
|
// // 查询登录状态,浏览器访问: http://localhost:8081/user/isLogin
|
||
|
|
// @RequestMapping("isLogin")
|
||
|
|
// public String isLogin() {
|
||
|
|
// return "当前会话是否登录:" + StpUtil.isLogin();
|
||
|
|
// }
|
||
|
|
}
|