88 lines
3.1 KiB
Java
88 lines
3.1 KiB
Java
package cn.bunny.service.impl;
|
||
|
||
|
||
import cn.bunny.exception.BunnyException;
|
||
import cn.bunny.mapper.SysUserMapper;
|
||
import cn.bunny.service.SysUserService;
|
||
import cn.bunny.spzx.model.dto.system.LoginDto;
|
||
import cn.bunny.spzx.model.entity.system.SysUser;
|
||
import cn.bunny.spzx.model.vo.common.ResultCodeEnum;
|
||
import cn.bunny.spzx.model.vo.system.LoginVo;
|
||
import cn.hutool.core.util.StrUtil;
|
||
import com.alibaba.fastjson.JSON;
|
||
import lombok.extern.log4j.Log4j2;
|
||
import org.springframework.beans.factory.annotation.Autowired;
|
||
import org.springframework.data.redis.core.RedisTemplate;
|
||
import org.springframework.stereotype.Service;
|
||
import org.springframework.util.DigestUtils;
|
||
|
||
import java.util.UUID;
|
||
import java.util.concurrent.TimeUnit;
|
||
|
||
@Log4j2
|
||
@Service
|
||
public class SysUserServiceImpl implements SysUserService {
|
||
@Autowired
|
||
private SysUserMapper sysUserMapper;
|
||
|
||
@Autowired
|
||
private RedisTemplate<String, String> redisTemplate;
|
||
|
||
// 用户登录
|
||
@Override
|
||
public LoginVo login(LoginDto loginDto) {
|
||
// 判断验证码是否正确
|
||
String captcha = loginDto.getCaptcha();
|
||
String codeKey = loginDto.getCodeKey();
|
||
String redisCode = redisTemplate.opsForValue().get("user:validate" + codeKey);
|
||
|
||
// 比较验证码
|
||
if (StrUtil.isEmpty(redisCode) || !StrUtil.equalsIgnoreCase(redisCode, captcha)) {
|
||
redisTemplate.delete("user:validate" + codeKey);
|
||
throw new BunnyException(ResultCodeEnum.VALIDATECODE_ERROR);
|
||
}
|
||
// 如果一致,删除Redis中验证码
|
||
redisTemplate.delete("user:validate" + codeKey);
|
||
|
||
// 获取用户提交用户名,LoginDto获取到
|
||
String userName = loginDto.getUserName();
|
||
|
||
// 根据用户名查询数据表 sys_user
|
||
SysUser sysUser = sysUserMapper.selectUserInfoByUserName(userName);
|
||
|
||
// 如果用户名查询不到对应信息,用户不存在,返回错误信息
|
||
if (sysUser == null) {
|
||
throw new BunnyException(ResultCodeEnum.LOGIN_ERROR_USERNAME);// 用户名不存在
|
||
}
|
||
|
||
// 获取输入密码,比较输入密码和数据库密码是否一致
|
||
String databases_password = sysUser.getPassword();
|
||
String input_password = DigestUtils.md5DigestAsHex(loginDto.getPassword().getBytes());
|
||
if (!input_password.equals(databases_password)) {
|
||
throw new BunnyException(ResultCodeEnum.LOGIN_ERROR_PASSWORD);// 密码不正确
|
||
}
|
||
|
||
|
||
String token = UUID.randomUUID().toString().replaceAll("-", "");
|
||
redisTemplate.opsForValue().set("user:login" + token, JSON.toJSONString(sysUser), 7, TimeUnit.DAYS);
|
||
LoginVo loginVo = new LoginVo();
|
||
loginVo.setToken(token);
|
||
|
||
return loginVo;
|
||
}
|
||
|
||
// 获取用户token
|
||
@Override
|
||
public SysUser getUserInfo(String token) {
|
||
String userJson = redisTemplate.opsForValue().get("user:login" + token);
|
||
SysUser sysUser = JSON.parseObject(userJson, SysUser.class);
|
||
return sysUser;
|
||
}
|
||
|
||
// 用户退出
|
||
@Override
|
||
public void logout(String token) {
|
||
redisTemplate.delete("user:login" + token);
|
||
}
|
||
}
|