zero преди 1 година
родител
ревизия
ef9d177744

+ 14 - 0
src/main/java/com/example/xiaoshiweixinback/business/utils/AppCacheKeyUtil.java

@@ -27,6 +27,8 @@ public class AppCacheKeyUtil {
 
     public final static String LOGIN_MESSAGE_CODE = "LOGIN_MESSAGE_CODE";
 
+    public final static String LOGIN_CHECK_CODE = "LOGIN_CHECK_CODE";
+
 
     /*
      * 获取token-userInfo缓存Key
@@ -66,4 +68,16 @@ public class AppCacheKeyUtil {
         return builder.toString();
     }
 
+    /*
+     * 获取用户手机号缓存key
+     * key:手机号
+     * value:验证码 类型:String
+     * */
+    public static String getCheckCode(String phoneNo) {
+        StringBuilder builder = new StringBuilder(prefix + SEPARATOR + APP + SEPARATOR);
+        builder.append(LOGIN_CHECK_CODE + SEPARATOR)
+                .append(phoneNo);
+        return builder.toString();
+    }
+
 }

+ 35 - 16
src/main/java/com/example/xiaoshiweixinback/controller/LoginController.java

@@ -3,30 +3,21 @@ package com.example.xiaoshiweixinback.controller;
 
 import com.example.xiaoshiweixinback.business.common.Response;
 import com.example.xiaoshiweixinback.business.common.base.Constants;
-import com.example.xiaoshiweixinback.entity.dto.LoginByWxDTO;
-import com.example.xiaoshiweixinback.entity.dto.LoginDTO;
-import com.example.xiaoshiweixinback.entity.dto.SendCodeDTO;
-import com.example.xiaoshiweixinback.entity.dto.WXLoginDTO;
-import com.example.xiaoshiweixinback.entity.vo.LoginByWxVO;
-import com.example.xiaoshiweixinback.entity.vo.LoginVO;
-import com.example.xiaoshiweixinback.entity.vo.SendCodeVO;
-import com.example.xiaoshiweixinback.entity.vo.WXLoginVO;
+import com.example.xiaoshiweixinback.entity.dto.person.*;
+import com.example.xiaoshiweixinback.entity.vo.person.LoginByWxVO;
+import com.example.xiaoshiweixinback.entity.vo.person.LoginVO;
 import com.example.xiaoshiweixinback.service.LoginService;
 import io.swagger.v3.oas.annotations.Operation;
-import jakarta.servlet.http.HttpServletRequest;
 import jakarta.validation.Valid;
 import org.springframework.beans.factory.annotation.Autowired;
-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 org.springframework.web.bind.annotation.*;
 
 /**
  * 登录相关接口
  *
  * @author: 高昌奎
  */
-@RequestMapping(Constants.API_XIAO_SHI_WXBACK + "/patent")
+@RequestMapping(Constants.API_XIAO_SHI_WXBACK + "/account")
 @RestController
 public class LoginController {
 
@@ -59,13 +50,41 @@ public class LoginController {
 
     @Operation(summary = "发送验证码")
     @PostMapping("/sendCode")
-    public Response sendCode(@RequestBody @Valid SendCodeDTO vo, HttpServletRequest request) {
-        boolean b = loginService.sendCode(vo, request);
+    public Response sendCode(@RequestBody @Valid SendCodeDTO vo) {
+        boolean b = loginService.sendCode(vo);
         if (!b) {
             return Response.error("发送验证码失败");
         }
         return Response.success("发送验证码成功");
     }
 
+    @GetMapping("/verifyCode")
+    @Operation(summary = "生成校验码")
+    public Response verifyCode(@RequestBody @Valid PersonPhoneDTO vo) throws Exception {
+        boolean b = true;
+        try {
+            b = loginService.verifyCode(vo);
+        } catch (Exception e) {
+            return Response.success(e.getMessage());
+        }
+        return Response.success(b);
+    }
+
+    @GetMapping("/editPerson")
+    @Operation(summary = "修改个人信息")
+    public Response editPerson(@RequestBody EditPersonDTO vo) throws Exception {
+        boolean b = true;
+        try {
+            b = loginService.editPerson(vo);
+        } catch (Exception e) {
+            return Response.success(e.getMessage());
+        }
+        return Response.success(b);
+    }
 
+    @GetMapping("/logout")
+    @Operation(summary = "退出登录")
+    public Response logout(PersonIdDTO dto) {
+        return Response.success(loginService.logout(dto));
+    }
 }

+ 0 - 9
src/main/java/com/example/xiaoshiweixinback/entity/dto/LoginByWxDTO.java

@@ -1,9 +0,0 @@
-package com.example.xiaoshiweixinback.entity.dto;
-
-import lombok.Data;
-
-@Data
-public class LoginByWxDTO {
-
-    private String code;
-}

+ 0 - 36
src/main/java/com/example/xiaoshiweixinback/entity/dto/LoginDTO.java

@@ -1,36 +0,0 @@
-package com.example.xiaoshiweixinback.entity.dto;
-
-
-import lombok.Data;
-
-@Data
-public class LoginDTO {
-
-    //手机号
-    private String phoneNum;
-
-    //验证码
-    private String phoneCode;
-
-    //账号/邮箱
-    private String account;
-
-    //密码
-    private String password;
-
-    public String getUserName() {
-        return userName;
-    }
-
-    public void setUserName(String userName) {
-        this.userName = userName;
-    }
-
-    public String getPassword() {
-        return password;
-    }
-
-    public void setPassword(String password) {
-        this.password = password;
-    }
-}

+ 28 - 0
src/main/java/com/example/xiaoshiweixinback/entity/dto/person/EditPersonDTO.java

@@ -0,0 +1,28 @@
+package com.example.xiaoshiweixinback.entity.dto.person;
+
+import lombok.Data;
+
+@Data
+public class EditPersonDTO {
+
+    private Integer id;
+
+    //姓名
+    private String name;
+
+    /**
+     * 头像
+     */
+    private String fileGuid;
+
+    /**
+     * 电话号码
+     */
+    private String phoneNum;
+
+    /**
+     * 邮箱
+     */
+    private String email;
+
+}

+ 2 - 3
src/main/java/com/example/xiaoshiweixinback/entity/vo/WXLoginVO.java

@@ -1,9 +1,9 @@
-package com.example.xiaoshiweixinback.entity.vo;
+package com.example.xiaoshiweixinback.entity.dto.person;
 
 import lombok.Data;
 
 @Data
-public class WXLoginVO {
+public class LoginByWxDTO {
 
     //code
     private String code;
@@ -13,5 +13,4 @@ public class WXLoginVO {
 
     //加密算法的初始向量
     private String iv;
-
 }

+ 21 - 0
src/main/java/com/example/xiaoshiweixinback/entity/dto/person/LoginDTO.java

@@ -0,0 +1,21 @@
+package com.example.xiaoshiweixinback.entity.dto.person;
+
+
+import lombok.Data;
+
+@Data
+public class LoginDTO {
+
+    //手机号
+    private String phoneNum;
+
+    //验证码
+    private String phoneCode;
+
+    //账号/邮箱
+    private String account;
+
+    //密码
+    private String password;
+
+}

+ 1 - 1
src/main/java/com/example/xiaoshiweixinback/entity/dto/PersonIdDTO.java

@@ -1,4 +1,4 @@
-package com.example.xiaoshiweixinback.entity.dto;
+package com.example.xiaoshiweixinback.entity.dto.person;
 
 import lombok.Data;
 

+ 12 - 0
src/main/java/com/example/xiaoshiweixinback/entity/dto/person/PersonPhoneDTO.java

@@ -0,0 +1,12 @@
+package com.example.xiaoshiweixinback.entity.dto.person;
+
+import jakarta.validation.constraints.NotBlank;
+import lombok.Data;
+
+@Data
+public class PersonPhoneDTO {
+
+    //手机号
+    @NotBlank
+    private String phoneNum;
+}

+ 4 - 1
src/main/java/com/example/xiaoshiweixinback/entity/dto/SendCodeDTO.java

@@ -1,4 +1,4 @@
-package com.example.xiaoshiweixinback.entity.dto;
+package com.example.xiaoshiweixinback.entity.dto.person;
 
 import jakarta.validation.constraints.NotBlank;
 import lombok.Data;
@@ -9,4 +9,7 @@ public class SendCodeDTO {
     //手机号
     @NotBlank
     private String phoneNum;
+
+    private String checkCode;
+
 }

+ 1 - 1
src/main/java/com/example/xiaoshiweixinback/entity/vo/Jscode2SessionWo.java

@@ -1,4 +1,4 @@
-package com.example.xiaoshiweixinback.entity.vo;
+package com.example.xiaoshiweixinback.entity.vo.person;
 
 import lombok.Data;
 

+ 1 - 1
src/main/java/com/example/xiaoshiweixinback/entity/vo/LoginByWxVO.java

@@ -1,4 +1,4 @@
-package com.example.xiaoshiweixinback.entity.vo;
+package com.example.xiaoshiweixinback.entity.vo.person;
 
 import lombok.Data;
 

+ 1 - 1
src/main/java/com/example/xiaoshiweixinback/entity/vo/LoginVO.java

@@ -1,4 +1,4 @@
-package com.example.xiaoshiweixinback.entity.vo;
+package com.example.xiaoshiweixinback.entity.vo.person;
 
 import lombok.Data;
 

+ 65 - 22
src/main/java/com/example/xiaoshiweixinback/service/LoginService.java

@@ -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;
     }