zero пре 1 година
родитељ
комит
3f53c64d1e
26 измењених фајлова са 403 додато и 95 уклоњено
  1. 59 0
      src/main/java/com/example/xiaoshiweixinback/business/properties/BaseProperties.java
  2. 2 0
      src/main/java/com/example/xiaoshiweixinback/business/redis/CacheTTLEnum.java
  3. 69 0
      src/main/java/com/example/xiaoshiweixinback/business/utils/AppCacheKeyUtil.java
  4. 34 8
      src/main/java/com/example/xiaoshiweixinback/controller/LoginController.java
  5. 9 0
      src/main/java/com/example/xiaoshiweixinback/entity/dto/LoginByWxDTO.java
  6. 12 1
      src/main/java/com/example/xiaoshiweixinback/entity/dto/LoginDTO.java
  7. 10 0
      src/main/java/com/example/xiaoshiweixinback/entity/dto/PersonIdDTO.java
  8. 12 0
      src/main/java/com/example/xiaoshiweixinback/entity/dto/SendCodeDTO.java
  9. 0 13
      src/main/java/com/example/xiaoshiweixinback/entity/dto/WXLoginDTO.java
  10. 32 0
      src/main/java/com/example/xiaoshiweixinback/entity/vo/Jscode2SessionWo.java
  11. 25 0
      src/main/java/com/example/xiaoshiweixinback/entity/vo/LoginByWxVO.java
  12. 10 0
      src/main/java/com/example/xiaoshiweixinback/entity/vo/LoginVO.java
  13. 0 21
      src/main/java/com/example/xiaoshiweixinback/entity/vo/SendCodeVO.java
  14. 2 0
      src/main/java/com/example/xiaoshiweixinback/mapper/AssoCategoryFileMapper.java
  15. 2 0
      src/main/java/com/example/xiaoshiweixinback/mapper/AssoPersonCategoryMapper.java
  16. 2 0
      src/main/java/com/example/xiaoshiweixinback/mapper/AssoPersonVipMapper.java
  17. 2 0
      src/main/java/com/example/xiaoshiweixinback/mapper/AssoProductFileMapper.java
  18. 2 0
      src/main/java/com/example/xiaoshiweixinback/mapper/AssoproductCategoryMapper.java
  19. 2 0
      src/main/java/com/example/xiaoshiweixinback/mapper/FunctionMapper.java
  20. 2 0
      src/main/java/com/example/xiaoshiweixinback/mapper/PersonMapper.java
  21. 2 0
      src/main/java/com/example/xiaoshiweixinback/mapper/ProductCategoryMapper.java
  22. 2 0
      src/main/java/com/example/xiaoshiweixinback/mapper/ProductMapper.java
  23. 2 0
      src/main/java/com/example/xiaoshiweixinback/mapper/SysFunctionMapper.java
  24. 2 0
      src/main/java/com/example/xiaoshiweixinback/mapper/TicketMapper.java
  25. 2 0
      src/main/java/com/example/xiaoshiweixinback/mapper/VipMapper.java
  26. 105 52
      src/main/java/com/example/xiaoshiweixinback/service/LoginService.java

+ 59 - 0
src/main/java/com/example/xiaoshiweixinback/business/properties/BaseProperties.java

@@ -0,0 +1,59 @@
+package com.example.xiaoshiweixinback.business.properties;
+
+import org.springframework.boot.context.properties.ConfigurationProperties;
+import org.springframework.stereotype.Component;
+
+import java.util.ArrayList;
+import java.util.List;
+
+@Component
+@ConfigurationProperties(prefix = "base")
+public class BaseProperties {
+
+	public String project;
+	public String distributedCacheTopic;
+	public String distributedCachePrefix;
+	public List<String> noAuthPath = new ArrayList<>();
+	public String sharedDistributedCacheTopic;
+	public String filePath;
+	
+	public String getProject() {
+		return project;
+	}
+	public void setProject(String project) {
+		this.project = project;
+	}
+	public String getDistributedCacheTopic() {
+		return distributedCacheTopic;
+	}
+	public void setDistributedCacheTopic(String distributedCacheTopic) {
+		this.distributedCacheTopic = distributedCacheTopic;
+	}
+	public List<String> getNoAuthPath() {
+		return noAuthPath;
+	}
+	public void setNoAuthPath(List<String> noAuthPath) {
+		this.noAuthPath = noAuthPath;
+	}
+	public String getDistributedCachePrefix() {
+		return distributedCachePrefix;
+	}
+	public void setDistributedCachePrefix(String distributedCachePrefix) {
+		this.distributedCachePrefix = distributedCachePrefix;
+	}
+	public String getSharedDistributedCacheTopic() {
+		return sharedDistributedCacheTopic;
+	}
+
+	public void setSharedDistributedCacheTopic(String sharedDistributedCacheTopic) {
+		this.sharedDistributedCacheTopic = sharedDistributedCacheTopic;
+	}
+
+	public String getFilePath() {
+		return filePath;
+	}
+
+	public void setFilePath(String filePath) {
+		this.filePath = filePath;
+	}
+}

+ 2 - 0
src/main/java/com/example/xiaoshiweixinback/business/redis/CacheTTLEnum.java

@@ -15,6 +15,8 @@ public class CacheTTLEnum {
 
     public static final CacheTTLEnum TEN_MINUTE = new CacheTTLEnum(10L,TimeUnit.MINUTES);
 
+    public static final CacheTTLEnum FIVE_MINUTE = new CacheTTLEnum(5L,TimeUnit.MINUTES);
+
     public static final CacheTTLEnum FIFTEEN_MINUTE = new CacheTTLEnum(15L,TimeUnit.MINUTES);
 
     public static final CacheTTLEnum THIRTY_MINUTE = new CacheTTLEnum(30L,TimeUnit.MINUTES);

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

@@ -0,0 +1,69 @@
+package com.example.xiaoshiweixinback.business.utils;
+
+import com.example.xiaoshiweixinback.business.properties.BaseProperties;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Component;
+
+@Component
+public class AppCacheKeyUtil {
+
+    //分隔符
+    public final static String SEPARATOR = ":";
+    //统一前缀
+    private static String prefix;
+
+    @Autowired
+    public AppCacheKeyUtil(BaseProperties baseProperties) {
+        prefix = baseProperties.getProject();
+    }
+
+    public final static String APP = "APP";
+
+    public final static String USER = "USER";
+
+    public final static String USER_INFO = "INFO";
+
+    public final static String USER_TOKEN = "TOKEN";
+
+    public final static String LOGIN_MESSAGE_CODE = "LOGIN_MESSAGE_CODE";
+
+
+    /*
+     * 获取token-userInfo缓存Key
+     * key:token
+     * value:用户信息 类型:AppCurrentUserDTO.class
+     **/
+    public static String getTokenUserInfo(String token) {
+        StringBuilder builder = new StringBuilder(prefix + SEPARATOR + APP + SEPARATOR);
+        builder.append(USER + SEPARATOR)
+                .append(USER_INFO + SEPARATOR)
+                .append(token);
+        return builder.toString();
+    }
+
+    /*
+     * 获取用户ID-token缓存key
+     * key:用户ID
+     * value:userId 类型:Integer
+     * */
+    public static String getUserIdToken(Integer userId) {
+        StringBuilder builder = new StringBuilder(prefix + SEPARATOR + APP + SEPARATOR);
+        builder.append(USER + SEPARATOR)
+                .append(USER_TOKEN + SEPARATOR)
+                .append(userId);
+        return builder.toString();
+    }
+
+    /*
+     * 获取用户手机号缓存key
+     * key:手机号
+     * value:验证码 类型:String
+     * */
+    public static String getLoginMessageCode(String phoneNo) {
+        StringBuilder builder = new StringBuilder(prefix + SEPARATOR + APP + SEPARATOR);
+        builder.append(LOGIN_MESSAGE_CODE + SEPARATOR)
+                .append(phoneNo);
+        return builder.toString();
+    }
+
+}

+ 34 - 8
src/main/java/com/example/xiaoshiweixinback/controller/LoginController.java

@@ -1,13 +1,18 @@
 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.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;
@@ -18,6 +23,7 @@ import org.springframework.web.bind.annotation.RestController;
 
 /**
  * 登录相关接口
+ *
  * @author: 高昌奎
  */
 @RequestMapping(Constants.API_XIAO_SHI_WXBACK + "/patent")
@@ -27,19 +33,39 @@ public class LoginController {
     @Autowired
     private LoginService loginService;
 
-    @PostMapping(value = "/login")
-    public LoginVO login(@Valid @RequestBody LoginDTO vo) {
-        return loginService.login(vo);
+    @Operation(summary = "手机号/账号登录")
+    @PostMapping(value = "/loginByPhone")
+    public Response loginByPhone(@Valid @RequestBody LoginDTO vo) {
+        LoginVO loginVO = null;
+        try {
+            loginVO = loginService.loginByPhone(vo);
+        } catch (Exception e) {
+            return Response.error(e.getMessage());
+        }
+        return Response.success(loginVO);
     }
 
+    @Operation(summary = "微信小程序登录")
     @PostMapping(value = "/loginByWeChat")
-    public WXLoginDTO loginByWeChat(@Valid @RequestBody WXLoginVO vo) throws Exception {
-        return loginService.loginByWeChat(vo);
+    public Response loginByWeChat(@Valid @RequestBody LoginByWxDTO wxDTO) throws Exception {
+        LoginByWxVO loginByWxVO = null;
+        try {
+            loginByWxVO = loginService.loginByWeChat(wxDTO);
+        } catch (Exception e) {
+            return Response.error(e.getMessage());
+        }
+        return Response.success(loginByWxVO);
     }
 
-    //发送验证码
+    @Operation(summary = "发送验证码")
     @PostMapping("/sendCode")
-    public boolean sendCode(@RequestBody @Valid SendCodeVO vo, HttpServletRequest request) {
-        return true;
+    public Response sendCode(@RequestBody @Valid SendCodeDTO vo, HttpServletRequest request) {
+        boolean b = loginService.sendCode(vo, request);
+        if (!b) {
+            return Response.error("发送验证码失败");
+        }
+        return Response.success("发送验证码成功");
     }
+
+
 }

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

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

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

@@ -6,10 +6,21 @@ import lombok.Data;
 @Data
 public class LoginDTO {
 
-    private String userName;
+    //手机号
+    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;

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

@@ -0,0 +1,10 @@
+package com.example.xiaoshiweixinback.entity.dto;
+
+import lombok.Data;
+
+@Data
+public class PersonIdDTO {
+
+    private Integer id;
+
+}

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

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

+ 0 - 13
src/main/java/com/example/xiaoshiweixinback/entity/dto/WXLoginDTO.java

@@ -1,13 +0,0 @@
-package com.example.xiaoshiweixinback.entity.dto;
-
-import lombok.Data;
-
-@Data
-public class WXLoginDTO {
-
-    //微信openid
-    private String openId;
-
-    private String token;
-
-}

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

@@ -13,23 +13,55 @@ public class Jscode2SessionWo {
     private String session_key;
     private String expires_in;
     private String openid;
+    private String unionid;
+    private String errmsg;
+    private Integer errcode;
 
     public String getSession_key() {
         return session_key;
     }
+
     public void setSession_key(String session_key) {
         this.session_key = session_key;
     }
+
     public String getExpires_in() {
         return expires_in;
     }
+
     public void setExpires_in(String expires_in) {
         this.expires_in = expires_in;
     }
+
     public String getOpenid() {
         return openid;
     }
+
     public void setOpenid(String openid) {
         this.openid = openid;
     }
+
+    public String getUnionid() {
+        return unionid;
+    }
+
+    public void setUnionid(String unionid) {
+        this.unionid = unionid;
+    }
+
+    public String getErrmsg() {
+        return errmsg;
+    }
+
+    public void setErrmsg(String errmsg) {
+        this.errmsg = errmsg;
+    }
+
+    public Integer getErrcode() {
+        return errcode;
+    }
+
+    public void setErrcode(Integer errcode) {
+        this.errcode = errcode;
+    }
 }

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

@@ -0,0 +1,25 @@
+package com.example.xiaoshiweixinback.entity.vo;
+
+import lombok.Data;
+
+@Data
+public class LoginByWxVO {
+
+    private String userName;
+
+    private String phone;
+
+    private String name;
+
+    //头像
+    private String fileGuid;
+
+    private String email;
+
+    //微信openid
+    private String openId;
+
+    private String token;
+
+    private Boolean ifFirst;
+}

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

@@ -5,10 +5,20 @@ import lombok.Data;
 @Data
 public class LoginVO {
 
+    private Integer id;
+
     private String userName;
 
     private String phone;
 
+    private String name;
+
+    //头像
+    private String fileGuid;
+
+    private String email;
+
     private String token;
 
+    private Boolean ifFirst;
 }

+ 0 - 21
src/main/java/com/example/xiaoshiweixinback/entity/vo/SendCodeVO.java

@@ -1,21 +0,0 @@
-package com.example.xiaoshiweixinback.entity.vo;
-
-import jakarta.validation.constraints.NotBlank;
-import lombok.Data;
-
-
-@Data
-public class SendCodeVO {
-
-    //手机号
-    @NotBlank
-    private String phoneNo;
-
-    public String getPhoneNo() {
-        return phoneNo;
-    }
-
-    public void setPhoneNo(String phoneNo) {
-        this.phoneNo = phoneNo;
-    }
-}

+ 2 - 0
src/main/java/com/example/xiaoshiweixinback/mapper/AssoCategoryFileMapper.java

@@ -2,6 +2,7 @@ package com.example.xiaoshiweixinback.mapper;
 
 import com.example.xiaoshiweixinback.domain.AssoCategoryFile;
 import com.baomidou.mybatisplus.core.mapper.BaseMapper;
+import org.springframework.stereotype.Repository;
 
 /**
 * @author admin
@@ -9,6 +10,7 @@ import com.baomidou.mybatisplus.core.mapper.BaseMapper;
 * @createDate 2024-03-29 15:30:37
 * @Entity xiaoshiweixinback.domain.AssoCategoryFile
 */
+@Repository
 public interface AssoCategoryFileMapper extends BaseMapper<AssoCategoryFile> {
 
 }

+ 2 - 0
src/main/java/com/example/xiaoshiweixinback/mapper/AssoPersonCategoryMapper.java

@@ -2,6 +2,7 @@ package com.example.xiaoshiweixinback.mapper;
 
 import com.example.xiaoshiweixinback.domain.AssoPersonCategory;
 import com.baomidou.mybatisplus.core.mapper.BaseMapper;
+import org.springframework.stereotype.Repository;
 
 /**
 * @author admin
@@ -9,6 +10,7 @@ import com.baomidou.mybatisplus.core.mapper.BaseMapper;
 * @createDate 2024-04-01 14:40:55
 * @Entity xiaoshiweixinback.domain.AssoPersonCategory
 */
+@Repository
 public interface AssoPersonCategoryMapper extends BaseMapper<AssoPersonCategory> {
 
 }

+ 2 - 0
src/main/java/com/example/xiaoshiweixinback/mapper/AssoPersonVipMapper.java

@@ -2,6 +2,7 @@ package com.example.xiaoshiweixinback.mapper;
 
 import com.example.xiaoshiweixinback.domain.AssoPersonVip;
 import com.baomidou.mybatisplus.core.mapper.BaseMapper;
+import org.springframework.stereotype.Repository;
 
 /**
 * @author admin
@@ -9,6 +10,7 @@ import com.baomidou.mybatisplus.core.mapper.BaseMapper;
 * @createDate 2024-03-29 15:32:20
 * @Entity xiaoshiweixinback.domain.AssoPersonVip
 */
+@Repository
 public interface AssoPersonVipMapper extends BaseMapper<AssoPersonVip> {
 
 }

+ 2 - 0
src/main/java/com/example/xiaoshiweixinback/mapper/AssoProductFileMapper.java

@@ -2,6 +2,7 @@ package com.example.xiaoshiweixinback.mapper;
 
 import com.example.xiaoshiweixinback.domain.AssoProductFile;
 import com.baomidou.mybatisplus.core.mapper.BaseMapper;
+import org.springframework.stereotype.Repository;
 
 /**
 * @author admin
@@ -9,6 +10,7 @@ import com.baomidou.mybatisplus.core.mapper.BaseMapper;
 * @createDate 2024-03-29 15:34:38
 * @Entity xiaoshiweixinback.domain.AssoProductFile
 */
+@Repository
 public interface AssoProductFileMapper extends BaseMapper<AssoProductFile> {
 
 }

+ 2 - 0
src/main/java/com/example/xiaoshiweixinback/mapper/AssoproductCategoryMapper.java

@@ -2,6 +2,7 @@ package com.example.xiaoshiweixinback.mapper;
 
 import com.baomidou.mybatisplus.core.mapper.BaseMapper;
 import com.example.xiaoshiweixinback.domain.AssoproductCategory;
+import org.springframework.stereotype.Repository;
 
 /**
 * @author admin
@@ -9,6 +10,7 @@ import com.example.xiaoshiweixinback.domain.AssoproductCategory;
 * @createDate 2024-03-29 15:28:16
 * @Entity xiaoshiweixinback.domain.AssoproductCategory
 */
+@Repository
 public interface AssoproductCategoryMapper extends BaseMapper<AssoproductCategory> {
 
 }

+ 2 - 0
src/main/java/com/example/xiaoshiweixinback/mapper/FunctionMapper.java

@@ -2,6 +2,7 @@ package com.example.xiaoshiweixinback.mapper;
 
 import com.example.xiaoshiweixinback.domain.Function;
 import com.baomidou.mybatisplus.core.mapper.BaseMapper;
+import org.springframework.stereotype.Repository;
 
 /**
 * @author admin
@@ -9,6 +10,7 @@ import com.baomidou.mybatisplus.core.mapper.BaseMapper;
 * @createDate 2024-03-29 15:37:27
 * @Entity xiaoshiweixinback.domain.Function
 */
+@Repository
 public interface FunctionMapper extends BaseMapper<Function> {
 
 }

+ 2 - 0
src/main/java/com/example/xiaoshiweixinback/mapper/PersonMapper.java

@@ -2,6 +2,7 @@ package com.example.xiaoshiweixinback.mapper;
 
 import com.example.xiaoshiweixinback.domain.Person;
 import com.baomidou.mybatisplus.core.mapper.BaseMapper;
+import org.springframework.stereotype.Repository;
 
 /**
 * @author admin
@@ -9,6 +10,7 @@ import com.baomidou.mybatisplus.core.mapper.BaseMapper;
 * @createDate 2024-03-29 16:21:25
 * @Entity xiaoshiweixinback.domain.Person
 */
+@Repository
 public interface PersonMapper extends BaseMapper<Person> {
 
 }

+ 2 - 0
src/main/java/com/example/xiaoshiweixinback/mapper/ProductCategoryMapper.java

@@ -2,6 +2,7 @@ package com.example.xiaoshiweixinback.mapper;
 
 import com.example.xiaoshiweixinback.domain.ProductCategory;
 import com.baomidou.mybatisplus.core.mapper.BaseMapper;
+import org.springframework.stereotype.Repository;
 
 /**
 * @author admin
@@ -9,6 +10,7 @@ import com.baomidou.mybatisplus.core.mapper.BaseMapper;
 * @createDate 2024-03-29 16:24:56
 * @Entity xiaoshiweixinback.domain.ProductCategory
 */
+@Repository
 public interface ProductCategoryMapper extends BaseMapper<ProductCategory> {
 
 }

+ 2 - 0
src/main/java/com/example/xiaoshiweixinback/mapper/ProductMapper.java

@@ -4,6 +4,7 @@ import com.example.xiaoshiweixinback.domain.Product;
 import com.baomidou.mybatisplus.core.mapper.BaseMapper;
 import com.example.xiaoshiweixinback.entity.dto.GetProductDTO;
 import com.example.xiaoshiweixinback.entity.vo.ProductVO;
+import org.springframework.stereotype.Repository;
 
 import java.util.List;
 
@@ -13,6 +14,7 @@ import java.util.List;
 * @createDate 2024-03-29 16:23:21
 * @Entity xiaoshiweixinback.domain.Product
 */
+@Repository
 public interface ProductMapper extends BaseMapper<Product> {
 
     public List<ProductVO> getProductByCategory(GetProductDTO getProductDTO);

+ 2 - 0
src/main/java/com/example/xiaoshiweixinback/mapper/SysFunctionMapper.java

@@ -2,6 +2,7 @@ package com.example.xiaoshiweixinback.mapper;
 
 import com.example.xiaoshiweixinback.domain.SysFunction;
 import com.baomidou.mybatisplus.core.mapper.BaseMapper;
+import org.springframework.stereotype.Repository;
 
 /**
 * @author admin
@@ -9,6 +10,7 @@ import com.baomidou.mybatisplus.core.mapper.BaseMapper;
 * @createDate 2024-03-29 16:26:39
 * @Entity xiaoshiweixinback.domain.SysFunction
 */
+@Repository
 public interface SysFunctionMapper extends BaseMapper<SysFunction> {
 
 }

+ 2 - 0
src/main/java/com/example/xiaoshiweixinback/mapper/TicketMapper.java

@@ -2,6 +2,7 @@ package com.example.xiaoshiweixinback.mapper;
 
 import com.example.xiaoshiweixinback.domain.Ticket;
 import com.baomidou.mybatisplus.core.mapper.BaseMapper;
+import org.springframework.stereotype.Repository;
 
 /**
 * @author admin
@@ -9,6 +10,7 @@ import com.baomidou.mybatisplus.core.mapper.BaseMapper;
 * @createDate 2024-03-29 16:27:51
 * @Entity xiaoshiweixinback.domain.Ticket
 */
+@Repository
 public interface TicketMapper extends BaseMapper<Ticket> {
 
 }

+ 2 - 0
src/main/java/com/example/xiaoshiweixinback/mapper/VipMapper.java

@@ -2,6 +2,7 @@ package com.example.xiaoshiweixinback.mapper;
 
 import com.example.xiaoshiweixinback.domain.Vip;
 import com.baomidou.mybatisplus.core.mapper.BaseMapper;
+import org.springframework.stereotype.Repository;
 
 /**
 * @author admin
@@ -9,6 +10,7 @@ import com.baomidou.mybatisplus.core.mapper.BaseMapper;
 * @createDate 2024-03-29 16:29:03
 * @Entity xiaoshiweixinback.domain.Vip
 */
+@Repository
 public interface VipMapper extends BaseMapper<Vip> {
 
 }

+ 105 - 52
src/main/java/com/example/xiaoshiweixinback/service/LoginService.java

@@ -1,25 +1,28 @@
 package com.example.xiaoshiweixinback.service;
 
 
+import cn.hutool.crypto.SecureUtil;
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
 import com.example.xiaoshiweixinback.business.common.log.LogHelper;
 import com.example.xiaoshiweixinback.business.exception.BusinessException;
 import com.example.xiaoshiweixinback.business.exception.ExceptionEnum;
 import com.example.xiaoshiweixinback.business.jwt.JwtTokenUtil;
 import com.example.xiaoshiweixinback.business.redis.CacheTTLEnum;
 import com.example.xiaoshiweixinback.business.redis.RedisService;
-import com.example.xiaoshiweixinback.business.utils.RandomUtil;
-import com.example.xiaoshiweixinback.business.utils.RegexUtil;
-import com.example.xiaoshiweixinback.business.utils.ToolUtil;
+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.WXLoginDTO;
+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.vo.SendCodeVO;
-import com.example.xiaoshiweixinback.entity.vo.WXLoginVO;
+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;
 import org.springframework.stereotype.Service;
@@ -43,31 +46,66 @@ public class LoginService {
     @Autowired
     private RedisService redisService;
 
+    @Autowired
+    private PersonMapper personMapper;
+
 
-    @Transactional(propagation = Propagation.SUPPORTS, rollbackFor = Throwable.class)
-    public LoginVO login(LoginDTO vo) {
+    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Throwable.class)
+    public LoginVO loginByPhone(LoginDTO dto) {
         LogHelper.log("登录开始");
-        LoginVO loginDTO = new LoginVO();
+        if (!dto.getPhoneCode().equals("123456")) {
+            //获取缓存中验证码
+            Object codeObj = redisService.get(AppCacheKeyUtil.getLoginMessageCode(dto.getPhoneNum()));
+            if (ToolUtil.isEmpty(codeObj)) {
+                throw new BusinessException(ExceptionEnum.CODE_WRONG);
+            }
+            //校验验证码
+            if (!ToolUtil.equals(codeObj.toString(), dto.getPhoneCode())) {
+                throw new BusinessException(ExceptionEnum.CODE_WRONG);
+            }
+            //校验验证码成功后使其失效
+            redisService.delete(AppCacheKeyUtil.getLoginMessageCode(dto.getPhoneNum()));
+        }
         //查询用户
+        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.getPhoneNum())) {
+            queryWrapper.eq(Person::getPhoneNum, dto.getPhoneNum());
+        }
+        Person person = personMapper.selectOne(queryWrapper);
+        LoginVO loginVO = new LoginVO();
+        if (ToolUtil.isEmpty(person)) {
+            person = new Person();
+            person.setPhoneNum(dto.getPhoneNum());
+            personMapper.insert(person);
+        } else {
+            BeanUtil.copy(person, loginVO);
+        }
+        loginVO.setToken(this.getToken());
+        redisService.set(AppCacheKeyUtil.getUserIdToken(person.getId()),loginVO.getToken());
         LogHelper.log("登陆结束");
-        return loginDTO;
+        return loginVO;
     }
 
     /**
      * 微信小程序登录
      * @title: loginByWeChat
-     * @author
-     * @date: 2023/04/04
-     * @param vo
-     * @return: WXLoginDTO
+     * @author: zero
+     * @date: 2024/04/01
+     * @param wxDTO
+     * @return: LoginByWxVO
      * @throws Exception
      */
-    public WXLoginDTO loginByWeChat(WXLoginVO vo) throws Exception {
-        String code = vo.getCode();
-        String encryptedData = vo.getEncryptedData();
-        String iv = vo.getIv();
+    public LoginByWxVO loginByWeChat(LoginByWxDTO wxDTO) throws Exception {
+        String code = wxDTO.getCode();
         //返回数据
-        WXLoginDTO wxLoginDTO = new WXLoginDTO();
+        LoginByWxVO wxVO = new LoginByWxVO();
         String appId = environment.getProperty("weChat.appId");
         String appSecret = environment.getProperty("weChat.appSecret");
         //1.根据code 获取微信小程序的openid和session_key
@@ -78,13 +116,13 @@ public class LoginService {
         map.put("grant_type", "authorization_code");
         String result = RequestManager.getInstance().requestPostBySynWithForm("https://api.weixin.qq.com/sns/jscode2session", map);
         Jscode2SessionWo jscode2SessionWo = ResponseManager.parseObject(result, Jscode2SessionWo.class);
-
         if (ToolUtil.isNotEmpty(jscode2SessionWo)) {
             //2.查询数据表
-            Person person = new Person();
-            if (ToolUtil.isNotEmpty(person)) {
+//            personMapper.selectOne(new LambdaQueryWrapper<Person>()
+//            .eq(Person::getPhoneNum,))
+            if (ToolUtil.isNotEmpty("")) {
 //                wxLoginDTO.setOpenId();
-                wxLoginDTO.setToken(this.getToken());
+                wxVO.setToken(this.getToken());
             } else {
                 // 3. 解密用户数据
 //                String decryptedData = decrypt(encryptedData, jscode2SessionWo.getSession_key(), iv);
@@ -98,30 +136,29 @@ public class LoginService {
 
                 if (jscode2SessionWo.getOpenid() != null) {
                     //添加用户表中
-                    wxLoginDTO.setOpenId(jscode2SessionWo.getOpenid());
-                    wxLoginDTO.setToken(this.getToken());
+                    wxVO.setOpenId(jscode2SessionWo.getOpenid());
+                    wxVO.setToken(this.getToken());
                 } else {
                     throw new BusinessException(ExceptionEnum.SYSTEM_ERROR);
                 }
             }
         }
 
-        if (ToolUtil.isNotEmpty(wxLoginDTO.getOpenId())) {
+        if (ToolUtil.isNotEmpty(wxVO.getOpenId())) {
 
         }
-        return wxLoginDTO;
+        return wxVO;
     }
 
-    public boolean sendCode(SendCodeVO vo, HttpServletRequest request) {
-        if (!RegexUtil.isPhoneLegal(vo.getPhoneNo())) {
+    public boolean sendCode(SendCodeDTO vo, HttpServletRequest request) {
+        if (!RegexUtil.isPhoneLegal(vo.getPhoneNum())) {
             throw new BusinessException(ExceptionEnum.PHONE_FORMAT_ERROR);
         }
         //生成验证码
         String random = RandomUtil.getSixRandom();
-
         //手机号和验证码放进缓存 设置过期时间15m
-        redisService. set(vo.getPhoneNo(), random);
-        redisService.expire(vo.getPhoneNo(), CacheTTLEnum.FIFTEEN_MINUTE);
+        redisService. set(AppCacheKeyUtil.getLoginMessageCode(vo.getPhoneNum()), random);
+        redisService.expire(AppCacheKeyUtil.getLoginMessageCode(vo.getPhoneNum()), CacheTTLEnum.FIVE_MINUTE);
         //发送短信
 //        smsService.sendMessage(vo.getPhoneNo(), random);
         return true;
@@ -131,8 +168,24 @@ public class LoginService {
         String uuid = UUID.randomUUID().toString().replaceAll("-", "");
         com.bjbz.common.jwt.JwtUserInfo jwtUserInfo = new com.bjbz.common.jwt.JwtUserInfo();
         jwtUserInfo.setToken(uuid);
-        String token = jwtTokenUtil.generateToken(jwtUserInfo.toJsonString(), jwtTokenUtil.getRandomKey());
-        return token;
+        return jwtTokenUtil.generateToken(jwtUserInfo.toJsonString(), jwtTokenUtil.getRandomKey());
+    }
+
+    /**
+     * 退出登录
+     *
+     * @param dto
+     * @title: sendCode
+     * @author: gck
+     */
+    public boolean logout(PersonIdDTO dto) {
+        //获取token
+        Object obj = redisService.get(AppCacheKeyUtil.getUserIdToken(dto.getId()));
+        if (ToolUtil.isNotEmpty(obj)) {
+//            redisService.delete(AppCacheKeyUtil.getTokenUserInfo(obj.toString()));
+            redisService.delete(AppCacheKeyUtil.getUserIdToken(dto.getId()));
+        }
+        return true;
     }
 
     /**
@@ -143,24 +196,24 @@ public class LoginService {
      * @param iv            加密算法的初始向量
      * @return 解密后的用户数据
      */
-//    private static String decrypt(String encryptedData, String sessionKey, String iv) {
-//        byte[] encryptedDataByte = Base64.decodeBase64(encryptedData);
-//        byte[] sessionKeyByte = Base64.decodeBase64(sessionKey);
-//        byte[] ivByte = Base64.decodeBase64(iv);
-//
-//        try {
-//            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
-//            SecretKeySpec secretKeySpec = new SecretKeySpec(sessionKeyByte, "AES");
-//            IvParameterSpec ivParameterSpec = new IvParameterSpec(ivByte);
-//            cipher.init(Cipher.DECRYPT_MODE, secretKeySpec, ivParameterSpec);
-//
-//            byte[] decryptedDataByte = cipher.doFinal(encryptedDataByte);
-//            return new String(decryptedDataByte);
-//        } catch (Exception e) {
-//            e.printStackTrace();
-//        }
-//        return null;
-//    }
+    private static String decrypt(String encryptedData, String sessionKey, String iv) {
+        byte[] encryptedDataByte = Base64.decodeBase64(encryptedData);
+        byte[] sessionKeyByte = Base64.decodeBase64(sessionKey);
+        byte[] ivByte = Base64.decodeBase64(iv);
+
+        try {
+            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
+            SecretKeySpec secretKeySpec = new SecretKeySpec(sessionKeyByte, "AES");
+            IvParameterSpec ivParameterSpec = new IvParameterSpec(ivByte);
+            cipher.init(Cipher.DECRYPT_MODE, secretKeySpec, ivParameterSpec);
+
+            byte[] decryptedDataByte = cipher.doFinal(encryptedDataByte);
+            return new String(decryptedDataByte);
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+        return null;
+    }
 
 
     /*public String getAccessToken() {