|
@@ -1,8 +1,13 @@
|
|
|
package com.example.xiaoshiweixinback.service;
|
|
|
|
|
|
|
|
|
+import cn.hutool.captcha.CaptchaUtil;
|
|
|
+import cn.hutool.captcha.CircleCaptcha;
|
|
|
+import cn.hutool.core.img.ImgUtil;
|
|
|
import cn.hutool.crypto.SecureUtil;
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
+import com.example.xiaoshiweixinback.business.common.Response;
|
|
|
+import com.example.xiaoshiweixinback.business.common.base.RedisConf;
|
|
|
import com.example.xiaoshiweixinback.business.common.log.LogHelper;
|
|
|
import com.example.xiaoshiweixinback.business.exception.BusinessException;
|
|
|
import com.example.xiaoshiweixinback.business.exception.ExceptionEnum;
|
|
@@ -11,17 +16,13 @@ import com.example.xiaoshiweixinback.business.redis.CacheTTLEnum;
|
|
|
import com.example.xiaoshiweixinback.business.redis.RedisService;
|
|
|
import com.example.xiaoshiweixinback.business.utils.*;
|
|
|
import com.example.xiaoshiweixinback.domain.Person;
|
|
|
-import com.example.xiaoshiweixinback.entity.dto.LoginByWxDTO;
|
|
|
-import com.example.xiaoshiweixinback.entity.dto.LoginDTO;
|
|
|
-import com.example.xiaoshiweixinback.entity.dto.PersonIdDTO;
|
|
|
-import com.example.xiaoshiweixinback.entity.dto.SendCodeDTO;
|
|
|
-import com.example.xiaoshiweixinback.entity.vo.Jscode2SessionWo;
|
|
|
-import com.example.xiaoshiweixinback.entity.vo.LoginByWxVO;
|
|
|
-import com.example.xiaoshiweixinback.entity.vo.LoginVO;
|
|
|
+import com.example.xiaoshiweixinback.entity.dto.person.*;
|
|
|
+import com.example.xiaoshiweixinback.entity.vo.person.Jscode2SessionWo;
|
|
|
+import com.example.xiaoshiweixinback.entity.vo.person.LoginByWxVO;
|
|
|
+import com.example.xiaoshiweixinback.entity.vo.person.LoginVO;
|
|
|
import com.example.xiaoshiweixinback.mapper.PersonMapper;
|
|
|
import com.example.xiaoshiweixinback.okhttp.RequestManager;
|
|
|
import com.example.xiaoshiweixinback.okhttp.ResponseManager;
|
|
|
-import jakarta.servlet.http.HttpServletRequest;
|
|
|
import org.apache.commons.codec.binary.Base64;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.core.env.Environment;
|
|
@@ -33,6 +34,7 @@ import javax.crypto.Cipher;
|
|
|
import javax.crypto.spec.IvParameterSpec;
|
|
|
import javax.crypto.spec.SecretKeySpec;
|
|
|
import java.util.*;
|
|
|
+import java.util.concurrent.TimeUnit;
|
|
|
|
|
|
@Service
|
|
|
public class LoginService {
|
|
@@ -47,6 +49,9 @@ public class LoginService {
|
|
|
private RedisService redisService;
|
|
|
|
|
|
@Autowired
|
|
|
+ private RedisUtil redisUtil;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
private PersonMapper personMapper;
|
|
|
|
|
|
|
|
@@ -68,13 +73,13 @@ public class LoginService {
|
|
|
}
|
|
|
//查询用户
|
|
|
LambdaQueryWrapper<Person> queryWrapper = new LambdaQueryWrapper<>();
|
|
|
- if (ToolUtil.isNotEmpty(dto.getAccount())) {
|
|
|
- queryWrapper.eq(Person::getEmail, dto.getAccount());
|
|
|
- }
|
|
|
- if (ToolUtil.isNotEmpty(dto.getPassword())) {
|
|
|
- String password = SecureUtil.md5(dto.getPassword());
|
|
|
- queryWrapper.eq(Person::getUserPassword, password);
|
|
|
- }
|
|
|
+// if (ToolUtil.isNotEmpty(dto.getAccount())) {
|
|
|
+// queryWrapper.eq(Person::getEmail, dto.getAccount());
|
|
|
+// }
|
|
|
+// if (ToolUtil.isNotEmpty(dto.getPassword())) {
|
|
|
+// String password = SecureUtil.md5(dto.getPassword());
|
|
|
+// queryWrapper.eq(Person::getUserPassword, password);
|
|
|
+// }
|
|
|
if (ToolUtil.isNotEmpty(dto.getPhoneNum())) {
|
|
|
queryWrapper.eq(Person::getPhoneNum, dto.getPhoneNum());
|
|
|
}
|
|
@@ -84,6 +89,9 @@ public class LoginService {
|
|
|
person = new Person();
|
|
|
person.setPhoneNum(dto.getPhoneNum());
|
|
|
personMapper.insert(person);
|
|
|
+
|
|
|
+ loginVO.setId(person.getId());
|
|
|
+ loginVO.setPhone(person.getPhoneNum());
|
|
|
} else {
|
|
|
BeanUtil.copy(person, loginVO);
|
|
|
}
|
|
@@ -150,17 +158,52 @@ public class LoginService {
|
|
|
return wxVO;
|
|
|
}
|
|
|
|
|
|
- public boolean sendCode(SendCodeDTO vo, HttpServletRequest request) {
|
|
|
+ public boolean sendCode(SendCodeDTO vo) {
|
|
|
if (!RegexUtil.isPhoneLegal(vo.getPhoneNum())) {
|
|
|
throw new BusinessException(ExceptionEnum.PHONE_FORMAT_ERROR);
|
|
|
}
|
|
|
- //生成验证码
|
|
|
- String random = RandomUtil.getSixRandom();
|
|
|
- //手机号和验证码放进缓存 设置过期时间15m
|
|
|
- redisService. set(AppCacheKeyUtil.getLoginMessageCode(vo.getPhoneNum()), random);
|
|
|
- redisService.expire(AppCacheKeyUtil.getLoginMessageCode(vo.getPhoneNum()), CacheTTLEnum.FIVE_MINUTE);
|
|
|
- //发送短信
|
|
|
+ String checkCode = redisService.get(AppCacheKeyUtil.getCheckCode(vo.getPhoneNum())).toString();
|
|
|
+ if (ToolUtil.isNotEmpty(checkCode) && ToolUtil.equals(checkCode,vo.getCheckCode())) {
|
|
|
+ //生成验证码
|
|
|
+ String random = RandomUtil.getSixRandom();
|
|
|
+ //手机号和验证码放进缓存 设置过期时间15m
|
|
|
+ redisService. set(AppCacheKeyUtil.getLoginMessageCode(vo.getPhoneNum()), random);
|
|
|
+ redisService.expire(AppCacheKeyUtil.getLoginMessageCode(vo.getPhoneNum()), CacheTTLEnum.FIVE_MINUTE);
|
|
|
+ //发送短信
|
|
|
// smsService.sendMessage(vo.getPhoneNo(), random);
|
|
|
+ return true;
|
|
|
+ } else {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 生成验证码
|
|
|
+ *
|
|
|
+ * @return 1.生成验证码的base64转码 2.生成的UUID 与Redis里面的验证码KEY值一致
|
|
|
+ * @date: 20240401
|
|
|
+ */
|
|
|
+ public boolean verifyCode(PersonPhoneDTO vo) {
|
|
|
+ if (!RegexUtil.isPhoneLegal(vo.getPhoneNum())) {
|
|
|
+ throw new BusinessException(ExceptionEnum.PHONE_FORMAT_ERROR);
|
|
|
+ }
|
|
|
+ //1.定义图形验证码的长、宽、验证码字符数、干扰元素个数
|
|
|
+ CircleCaptcha captcha = CaptchaUtil.createCircleCaptcha(200, 100, 4, 20);
|
|
|
+ //2.生成UUID
|
|
|
+ String uuid = cn.hutool.core.lang.UUID.fastUUID().toString();
|
|
|
+ //3.创建返回参数 并用base64将图片转码 与UUID一起填充
|
|
|
+ Map<String, String> result = new HashMap<>();
|
|
|
+ result.put("captcha", ImgUtil.toBase64DataUri(captcha.getImage(), "png"));
|
|
|
+ result.put("uuid", uuid);
|
|
|
+ //4.将验证码存放到Redis里面并设置过期时间为 60 单位:秒 KEY值格式为: 验证码:UUID VALUE值为:验证码生成工具所生成的验证码
|
|
|
+ redisUtil.setEx(AppCacheKeyUtil.getCheckCode(vo.getPhoneNum()), captcha.getCode(), 60, TimeUnit.SECONDS);
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+
|
|
|
+ public boolean editPerson(EditPersonDTO vo) {
|
|
|
+ if (!RegexUtil.isPhoneLegal(vo.getPhoneNum())) {
|
|
|
+ throw new BusinessException(ExceptionEnum.PHONE_FORMAT_ERROR);
|
|
|
+ }
|
|
|
return true;
|
|
|
}
|
|
|
|