Browse Source

微信登录

feng_ting-ting 1 month ago
parent
commit
0a09dab3a1

+ 31 - 14
pom.xml

@@ -11,43 +11,60 @@
         <java.version>1.8</java.version>
         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
         <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
-        <spring-boot.version>2.6.13</spring-boot.version>
+        <spring-boot.version>2.7.6</spring-boot.version>
     </properties>
     <dependencies>
+        <!-- Spring Boot 基础依赖 -->
         <dependency>
             <groupId>org.springframework.boot</groupId>
-            <artifactId>spring-boot-starter</artifactId>
+            <artifactId>spring-boot-starter-web</artifactId>
         </dependency>
         <dependency>
             <groupId>org.springframework.boot</groupId>
-            <artifactId>spring-boot-starter-web</artifactId>
+            <artifactId>spring-boot-starter-data-redis</artifactId>
         </dependency>
         <dependency>
             <groupId>org.springframework.boot</groupId>
-            <artifactId>spring-boot-starter-data-redis</artifactId>
+            <artifactId>spring-boot-starter-validation</artifactId>
         </dependency>
+
+        <!-- 数据库相关 -->
         <dependency>
-            <groupId>org.apache.commons</groupId>
-            <artifactId>commons-pool2</artifactId>
+            <groupId>com.baomidou</groupId>
+            <artifactId>mybatis-plus-boot-starter</artifactId>
+            <version>3.5.3.1</version>
         </dependency>
         <dependency>
-            <groupId>com.alibaba</groupId>
-            <artifactId>fastjson</artifactId>
-            <version>1.2.72</version>
+            <groupId>mysql</groupId>
+            <artifactId>mysql-connector-java</artifactId>
+            <scope>runtime</scope>
         </dependency>
 
-        <!--使用hutool中对http封装工具类 调用 HTTP 请求-->
+        <!-- 工具库 -->
         <dependency>
-            <groupId>cn.hutool</groupId>
-            <artifactId>hutool-all</artifactId>
-            <version>5.6.5</version>
+            <groupId>com.alibaba</groupId>
+            <artifactId>fastjson</artifactId>
+            <version>2.0.24</version>
         </dependency>
         <dependency>
             <groupId>org.projectlombok</groupId>
             <artifactId>lombok</artifactId>
+            <optional>true</optional>
         </dependency>
-    </dependencies>
+        <dependency>
+            <groupId>commons-codec</groupId>
+            <artifactId>commons-codec</artifactId>
+            <version>1.15</version>
+        </dependency>
+
+        <!-- HTTP客户端 -->
+<!--        <dependency>-->
+<!--            <groupId>org.springframework.boot</groupId>-->
+<!--            <artifactId>spring-boot-starter-webclient</artifactId>-->
+<!--            <version>2.7.6</version>-->
+<!--        </dependency>-->
 
+    </dependencies>
     <dependencyManagement>
         <dependencies>
             <dependency>

+ 20 - 0
src/main/java/com/zhentao/common/AppConfig.java

@@ -0,0 +1,20 @@
+package com.zhentao.common;
+
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.web.client.RestTemplate;
+
+/**
+ * @date: 2025/5/20 11:10
+ * @author: ftt
+ */
+
+@Configuration
+public class AppConfig {
+
+    @Bean
+    public RestTemplate restTemplate() {
+        return new RestTemplate();
+    }
+}
+

+ 29 - 0
src/main/java/com/zhentao/common/RedisConfig.java

@@ -0,0 +1,29 @@
+package com.zhentao.common;
+
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.data.redis.connection.RedisConnectionFactory;
+import org.springframework.data.redis.core.RedisTemplate;
+import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
+import org.springframework.data.redis.serializer.StringRedisSerializer;
+
+/**
+ * @date: 2025/5/20 11:08
+ * @author: ftt
+ */
+
+@Configuration
+public class RedisConfig {
+
+    @Bean
+    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
+        RedisTemplate<String, Object> template = new RedisTemplate<>();
+        template.setConnectionFactory(factory);
+        template.setKeySerializer(new StringRedisSerializer());
+        template.setValueSerializer(new GenericJackson2JsonRedisSerializer());
+        template.setHashKeySerializer(new StringRedisSerializer());
+        template.setHashValueSerializer(new GenericJackson2JsonRedisSerializer());
+        return template;
+    }
+}
+

+ 0 - 11
src/main/java/com/zhentao/common/RedisKey.java

@@ -1,11 +0,0 @@
-package com.zhentao.common;
-
-/**
- * @date: 2025/5/19 11:45
- * @author: ftt
- */
-
-public class RedisKey {
-    public static final String WX_SESSION_ID = "wx_session_id";
-}
-

+ 59 - 0
src/main/java/com/zhentao/common/Result.java

@@ -0,0 +1,59 @@
+package com.zhentao.common;
+
+/**
+ * @date: 2025/5/19 11:49
+ * @author: ftt
+ */
+
+import lombok.Data;
+
+/**
+ * 统一响应结果集
+ * @author crush
+ */
+@Data
+public class Result<T> {
+
+    //操作代码
+    Integer code;
+
+    //提示信息
+    String message;
+
+    //结果数据
+    T data;
+
+    public Result() {
+    }
+
+    public Result(ResultCode resultCode) {
+        this.code = resultCode.code();
+        this.message = resultCode.message();
+    }
+
+    public Result(ResultCode resultCode, T data) {
+        this.code = resultCode.code();
+        this.message = resultCode.message();
+        this.data = data;
+    }
+
+    public Result(String message) {
+        this.message = message;
+    }
+
+    public static Result SUCCESS() {
+        return new Result(ResultCode.SUCCESS);
+    }
+
+    public static <T> Result SUCCESS(T data) {
+        return new Result(ResultCode.SUCCESS, data);
+    }
+
+    public static Result FAIL() {
+        return new Result(ResultCode.FAIL);
+    }
+
+    public static Result FAIL(String message) {
+        return new Result(message);
+    }
+}

+ 83 - 0
src/main/java/com/zhentao/common/ResultCode.java

@@ -0,0 +1,83 @@
+package com.zhentao.common;
+
+/**
+ * @date: 2025/5/19 11:50
+ * @author: ftt
+ */
+
+/**
+ * 通用响应状态
+ */
+public enum ResultCode {
+
+    /* 成功状态码 */
+    SUCCESS(0, "操作成功!"),
+
+    /* 错误状态码 */
+    FAIL(-1, "操作失败!"),
+
+    /* 参数错误:10001-19999 */
+    PARAM_IS_INVALID(10001, "参数无效"),
+    PARAM_IS_BLANK(10002, "参数为空"),
+    PARAM_TYPE_BIND_ERROR(10003, "参数格式错误"),
+    PARAM_NOT_COMPLETE(10004, "参数缺失"),
+
+    /* 用户错误:20001-29999*/
+    USER_NOT_LOGGED_IN(20001, "用户未登录,请先登录"),
+    USER_LOGIN_ERROR(20002, "账号不存在或密码错误"),
+    USER_ACCOUNT_FORBIDDEN(20003, "账号已被禁用"),
+    USER_NOT_EXIST(20004, "用户不存在"),
+    USER_HAS_EXISTED(20005, "用户已存在"),
+
+    /* 系统错误:40001-49999 */
+    FILE_MAX_SIZE_OVERFLOW(40003, "上传尺寸过大"),
+    FILE_ACCEPT_NOT_SUPPORT(40004, "上传文件格式不支持"),
+
+    /* 数据错误:50001-599999 */
+    RESULT_DATA_NONE(50001, "数据未找到"),
+    DATA_IS_WRONG(50002, "数据有误"),
+    DATA_ALREADY_EXISTED(50003, "数据已存在"),
+    AUTH_CODE_ERROR(50004, "验证码错误"),
+
+
+    /* 权限错误:70001-79999 */
+    PERMISSION_UNAUTHENTICATED(70001, "此操作需要登陆系统!"),
+
+    PERMISSION_UNAUTHORISE(70002, "权限不足,无权操作!"),
+
+    PERMISSION_EXPIRE(70003, "登录状态过期!"),
+
+    PERMISSION_TOKEN_EXPIRED(70004, "token已过期"),
+
+    PERMISSION_LIMIT(70005, "访问次数受限制"),
+
+    PERMISSION_TOKEN_INVALID(70006, "无效token"),
+
+    PERMISSION_SIGNATURE_ERROR(70007, "签名失败");
+
+    //操作代码
+    int code;
+    //提示信息
+    String message;
+
+    ResultCode(int code, String message) {
+        this.code = code;
+        this.message = message;
+    }
+
+    public int code() {
+        return code;
+    }
+
+    public String message() {
+        return message;
+    }
+
+    public void setCode(int code) {
+        this.code = code;
+    }
+
+    public void setMessage(String message) {
+        this.message = message;
+    }
+    }

+ 25 - 0
src/main/java/com/zhentao/user/controller/AuthController.java

@@ -0,0 +1,25 @@
+package com.zhentao.user.controller;
+
+import com.zhentao.user.dto.LoginResponseDTO;
+import com.zhentao.user.dto.WechatLoginDTO;
+import com.zhentao.user.service.UserService;
+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 javax.validation.Valid;
+
+@RestController
+@RequestMapping("/api/auth")
+public class AuthController {
+
+    @Autowired
+    private UserService userService;
+
+    @PostMapping("/wechat/login")
+    public LoginResponseDTO wechatLogin(@Valid @RequestBody WechatLoginDTO dto) {
+        return userService.wechatLogin(dto);
+    }
+}

+ 13 - 0
src/main/java/com/zhentao/user/dto/LoginResponseDTO.java

@@ -0,0 +1,13 @@
+package com.zhentao.user.dto;
+
+import lombok.Data;
+
+@Data
+public class LoginResponseDTO {
+    private Integer userId;
+    private String token;
+    private Integer loginType;
+    private String wxNickname;
+    private String wxAvatarUrl;
+    private Boolean isNewUser;
+}

+ 15 - 0
src/main/java/com/zhentao/user/dto/WechatLoginDTO.java

@@ -0,0 +1,15 @@
+package com.zhentao.user.dto;
+
+import lombok.Data;
+
+import javax.validation.constraints.NotBlank;
+
+@Data
+public class WechatLoginDTO {
+    @NotBlank(message = "code不能为空")
+    private String code;
+    private String encryptedData;
+    private String iv;
+    private String rawData;
+    private String signature;
+}

+ 19 - 8
src/main/resources/application.yml

@@ -1,15 +1,26 @@
 server:
-  port: 8081
+  port: 8080
 spring:
-  application:
-    name: springboot-weixin
+  datasource:
+    url: jdbc:mysql://localhost:3306/medical_inquiry_system?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=UTC
+    username: root
+    password: root
+    driver-class-name: com.mysql.cj.jdbc.Driver
+
   redis:
-    database: 0
-    port: 6379
     host: localhost
+    port: 6379
     password:
-weixin:
-  appid: 'appid'
-  secret: '应用密钥'
 
+mybatis-plus:
+  mapper-locations: classpath:mapper/*.xml
+  type-aliases-package: com.zhentao.*.domain
+  configuration:
+    map-underscore-to-camel-case: true
+
+# 微信小程序配置
+wx:
+  appid: wxa2a76a594be60e4d
+  secret: 73e228a4fea627fb74d5a9de8585d880
+  login-url: https://api.weixin.qq.com/sns/jscode2session