|
@@ -1,11 +1,15 @@
|
|
|
package cn.cslg.permission.service;
|
|
|
|
|
|
+import cn.cslg.permission.common.core.base.RedisConf;
|
|
|
+import cn.cslg.permission.common.model.vo.LoginVO;
|
|
|
import cn.cslg.permission.common.model.vo.PersonnelVO;
|
|
|
-import cn.cslg.permission.common.utils.Response;
|
|
|
-import cn.cslg.permission.common.utils.ResponseEnum;
|
|
|
-import cn.cslg.permission.common.utils.StpAdminUtil;
|
|
|
+import cn.cslg.permission.common.utils.*;
|
|
|
import cn.cslg.permission.domain.Personnel;
|
|
|
import cn.cslg.permission.mapper.PersonnelMapper;
|
|
|
+import cn.hutool.captcha.CaptchaUtil;
|
|
|
+import cn.hutool.captcha.CircleCaptcha;
|
|
|
+import cn.hutool.core.img.ImgUtil;
|
|
|
+import cn.hutool.core.lang.UUID;
|
|
|
import cn.hutool.crypto.SecureUtil;
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
@@ -13,6 +17,12 @@ import lombok.RequiredArgsConstructor;
|
|
|
import org.springframework.context.annotation.Lazy;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
+import javax.servlet.http.HttpServletRequest;
|
|
|
+import javax.servlet.http.HttpServletResponse;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.Map;
|
|
|
+import java.util.concurrent.TimeUnit;
|
|
|
+
|
|
|
/**
|
|
|
* @author 沈永艺
|
|
|
* @date 2022-8-2
|
|
@@ -22,21 +32,40 @@ import org.springframework.stereotype.Service;
|
|
|
@Service
|
|
|
@RequiredArgsConstructor(onConstructor_ = {@Lazy})
|
|
|
public class LoginService extends ServiceImpl<PersonnelMapper, Personnel> {
|
|
|
- public String login(PersonnelVO personnelVO) {
|
|
|
+ private final RedisUtil redisUtil;
|
|
|
+ private final CacheUtils cacheUtils;
|
|
|
+
|
|
|
+ public String login(LoginVO loginVO) {
|
|
|
+ String tempCode = redisUtil.get(RedisConf.VERIFY_CODE + RedisConf.SYMBOL_COLON + loginVO.getUuid());
|
|
|
+ if (Boolean.TRUE.equals(StringUtils.isEmpty(tempCode)) || !tempCode.equals(loginVO.getCode())) {
|
|
|
+ return Response.error(ResponseEnum.VERIFY_CODE_ERROR);
|
|
|
+ }
|
|
|
LambdaQueryWrapper<Personnel> queryWrapper = new LambdaQueryWrapper<>();
|
|
|
- queryWrapper.eq(Personnel::getPersonnelUserName, personnelVO.getUsername());
|
|
|
+ queryWrapper.eq(Personnel::getPersonnelUserName, loginVO.getUsername());
|
|
|
|
|
|
Personnel personnel = this.getOne(queryWrapper);
|
|
|
|
|
|
if (personnel == null) {
|
|
|
return Response.error(ResponseEnum.USERNAME_ERROR);
|
|
|
}
|
|
|
- boolean isPassword = SecureUtil.md5(personnelVO.getPassword()).equals(personnel.getPersonnelPassword());
|
|
|
+ boolean isPassword = SecureUtil.md5(loginVO.getPassword()).equals(personnel.getPersonnelPassword());
|
|
|
if (!isPassword) {
|
|
|
return Response.error(ResponseEnum.PASSWORD_ERROR);
|
|
|
}
|
|
|
StpAdminUtil.login(personnel.getId());
|
|
|
- System.out.println(StpAdminUtil.getLoginId());
|
|
|
+ cacheUtils.setLoginUser(personnel);
|
|
|
+ personnel.setPersonnelPassword(null);
|
|
|
return Response.success(StpAdminUtil.getTokenValue());
|
|
|
}
|
|
|
+
|
|
|
+ public String verifyCode(HttpServletRequest request, HttpServletResponse response) throws Exception {
|
|
|
+ //定义图形验证码的长、宽、验证码字符数、干扰元素个数
|
|
|
+ CircleCaptcha captcha = CaptchaUtil.createCircleCaptcha(200, 100, 4, 20);
|
|
|
+ String uuid = UUID.fastUUID().toString();
|
|
|
+ Map<String, String> result = new HashMap<>();
|
|
|
+ result.put("captcha", ImgUtil.toBase64DataUri(captcha.getImage(), "png"));
|
|
|
+ result.put("uuid", uuid);
|
|
|
+ redisUtil.setEx(RedisConf.VERIFY_CODE + RedisConf.SYMBOL_COLON + uuid, captcha.getCode(), 60, TimeUnit.SECONDS);
|
|
|
+ return Response.success(result);
|
|
|
+ }
|
|
|
}
|