package com.ruoyi.sange.controller;
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
import com.ruoyi.common.core.domain.AjaxResult;
|
import com.ruoyi.common.core.domain.R;
|
import com.ruoyi.sange.domain.SystemUser;
|
import com.ruoyi.sange.service.ISystemUserService;
|
import com.ruoyi.sange.util.JwtUtils;
|
import com.ruoyi.sange.util.SecurityConstants;
|
import com.ruoyi.sange.util.ServletUtils;
|
import com.ruoyi.sange.util.TokenService;
|
import com.ruoyi.sange.warpper.LoginVo;
|
import com.ruoyi.sange.warpper.TokenVo;
|
import com.ruoyi.web.tool.StringUtils;
|
import io.swagger.annotations.Api;
|
import io.swagger.annotations.ApiOperation;
|
import org.apache.shiro.SecurityUtils;
|
import org.springframework.data.redis.core.RedisTemplate;
|
import org.springframework.web.bind.annotation.PostMapping;
|
import org.springframework.web.bind.annotation.RequestBody;
|
import org.springframework.web.bind.annotation.RequestMapping;
|
import org.springframework.web.bind.annotation.RestController;
|
|
import javax.annotation.Resource;
|
import java.util.HashMap;
|
import java.util.Map;
|
import java.util.UUID;
|
import java.util.concurrent.TimeUnit;
|
|
/**
|
* @author zhibing.pu
|
* @Date 2025/2/18 22:30
|
*/
|
@Api
|
@RestController
|
@RequestMapping("/api/systemUser")
|
public class SystemUserController {
|
|
@Resource
|
private ISystemUserService systemUserService;
|
|
@Resource
|
private RedisTemplate redisTemplate;
|
|
private final Integer max_err = 5;
|
|
@Resource
|
private TokenService tokenService;
|
|
|
@PostMapping("/login")
|
@ApiOperation(value = "登录", tags = {"管理后台-登录"})
|
public R<TokenVo> login(@RequestBody LoginVo vo){
|
String key = "login:" + vo.getPhone();
|
Integer size = (Integer) redisTemplate.opsForValue().get(key);
|
if(null != size && max_err.equals(size)){
|
return R.fail("连续登录失败,请稍后重试。");
|
}
|
if(null == size){
|
size = 0;
|
}
|
SystemUser systemUser = systemUserService.getOne(new LambdaQueryWrapper<SystemUser>()
|
.eq(SystemUser::getPhone, vo.getPhone()).ne(SystemUser::getStatus, 3));
|
if(null == systemUser){
|
size++;
|
redisTemplate.opsForValue().set(key, size, 5, TimeUnit.MINUTES);
|
return R.fail("登录失败,手机号/密码错误。");
|
}
|
if(!vo.getPassword().equals(systemUser.getPassword())){
|
size++;
|
redisTemplate.opsForValue().set(key, size, 5, TimeUnit.MINUTES);
|
return R.fail("登录失败,手机号/密码错误。");
|
}
|
if(2 == systemUser.getStatus()){
|
return R.fail("当前账号已冻结。");
|
}
|
//创建token
|
String userKey = UUID.randomUUID().toString();
|
Map<String, Object> claims = new HashMap<>();
|
claims.put(SecurityConstants.USER_KEY, userKey);
|
String token = JwtUtils.createToken(claims);
|
//token有效期2小时
|
redisTemplate.opsForValue().set("token:" + userKey, token, SecurityConstants.expireTime, TimeUnit.MILLISECONDS);
|
redisTemplate.delete(key);
|
TokenVo tokenVo = new TokenVo();
|
tokenVo.setToken(token);
|
tokenVo.setExpireTime(SecurityConstants.expireTime);
|
return R.ok(tokenVo);
|
}
|
|
|
@PostMapping("/logout")
|
@ApiOperation(value = "退出登录", tags = {"管理后台-登录"})
|
public R logout(){
|
String token = tokenService.getToken(ServletUtils.getRequest());
|
if(StringUtils.isEmpty(token)){
|
return R.fail("请先登录");
|
}
|
String userKey = JwtUtils.getUserKey(token);
|
redisTemplate.delete("token:" + userKey);
|
return R.ok();
|
}
|
}
|