commit 3a49cbd41ba02de3a8f5d05d1a52e198a8e8baa6
Author: Heife <2044384845@qq.com>
Date: Tue Apr 1 18:38:51 2025 +0800
yhf_4/1
diff --git a/Debate_backend/.idea/.gitignore b/Debate_backend/.idea/.gitignore
new file mode 100644
index 0000000..13566b8
--- /dev/null
+++ b/Debate_backend/.idea/.gitignore
@@ -0,0 +1,8 @@
+# Default ignored files
+/shelf/
+/workspace.xml
+# Editor-based HTTP Client requests
+/httpRequests/
+# Datasource local storage ignored files
+/dataSources/
+/dataSources.local.xml
diff --git a/Debate_backend/.idea/Debate_backend.iml b/Debate_backend/.idea/Debate_backend.iml
new file mode 100644
index 0000000..d6ebd48
--- /dev/null
+++ b/Debate_backend/.idea/Debate_backend.iml
@@ -0,0 +1,9 @@
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/Debate_backend/.idea/compiler.xml b/Debate_backend/.idea/compiler.xml
new file mode 100644
index 0000000..08f0a45
--- /dev/null
+++ b/Debate_backend/.idea/compiler.xml
@@ -0,0 +1,18 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/Debate_backend/.idea/dataSources.xml b/Debate_backend/.idea/dataSources.xml
new file mode 100644
index 0000000..4f92709
--- /dev/null
+++ b/Debate_backend/.idea/dataSources.xml
@@ -0,0 +1,17 @@
+
+
+
+
+ mysql.8
+ true
+ com.mysql.cj.jdbc.Driver
+ jdbc:mysql://localhost:3306/wx_miniapp
+
+
+
+
+
+ $ProjectFileDir$
+
+
+
\ No newline at end of file
diff --git a/Debate_backend/.idea/encodings.xml b/Debate_backend/.idea/encodings.xml
new file mode 100644
index 0000000..63e9001
--- /dev/null
+++ b/Debate_backend/.idea/encodings.xml
@@ -0,0 +1,6 @@
+
+
+
+
+
+
\ No newline at end of file
diff --git a/Debate_backend/.idea/jarRepositories.xml b/Debate_backend/.idea/jarRepositories.xml
new file mode 100644
index 0000000..712ab9d
--- /dev/null
+++ b/Debate_backend/.idea/jarRepositories.xml
@@ -0,0 +1,20 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/Debate_backend/.idea/misc.xml b/Debate_backend/.idea/misc.xml
new file mode 100644
index 0000000..9dc782b
--- /dev/null
+++ b/Debate_backend/.idea/misc.xml
@@ -0,0 +1,12 @@
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/Debate_backend/README.md b/Debate_backend/README.md
new file mode 100644
index 0000000..ad86e2f
--- /dev/null
+++ b/Debate_backend/README.md
@@ -0,0 +1,106 @@
+# 微信小程序登录Demo后端
+
+## 项目介绍
+本项目是一个微信小程序登录的后端Demo,实现了微信小程序登录、获取用户信息等功能。
+
+## 技术栈
+- Spring Boot 3.0.12
+- MyBatis 3.0.1
+- MySQL 8.0
+- JWT
+
+## 项目结构
+```
+src/main/java/com/learning/newdemo
+├── config // 配置类
+├── controller // 控制器
+├── entity // 实体类
+├── mapper // 数据访问层
+├── service // 服务层
+│ └── impl // 服务实现
+├── util // 工具类
+└── common // 通用类
+```
+
+## 运行环境
+- JDK 17+
+- MySQL 8.0+
+- Maven 3.6+
+
+## 数据库配置
+1. 创建数据库和表
+```sql
+# 执行src/main/resources/db/wx_miniapp.sql脚本
+```
+
+2. 修改数据库连接信息(`application.yml`)
+```yaml
+spring:
+ datasource:
+ url: jdbc:mysql://localhost:3306/wx_miniapp?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai
+ username: root
+ password: 1234
+```
+
+## 微信小程序配置
+修改`application.yml`中的微信小程序配置:
+```yaml
+wechat:
+ miniapp:
+ appid: 你的小程序APPID
+ secret: 你的小程序SECRET
+```
+
+## 启动项目
+```bash
+mvn spring-boot:run
+```
+
+## 接口说明
+### 1. 登录接口
+- URL: `/api/wx/login`
+- Method: POST
+- Body:
+```json
+{
+ "code": "微信临时登录凭证"
+}
+```
+- Response:
+```json
+{
+ "success": true,
+ "code": 200,
+ "message": "操作成功",
+ "data": {
+ "token": "JWT令牌"
+ }
+}
+```
+
+### 2. 获取用户信息接口
+- URL: `/api/wx/user`
+- Method: GET
+- Headers:
+```
+Authorization: 登录接口返回的token
+```
+- Response:
+```json
+{
+ "success": true,
+ "code": 200,
+ "message": "操作成功",
+ "data": {
+ "id": 1,
+ "openid": "用户openid",
+ "nickname": "用户昵称",
+ "avatarUrl": "头像URL",
+ "gender": 1,
+ "country": "国家",
+ "province": "省份",
+ "city": "城市",
+ "language": "语言"
+ }
+}
+```
\ No newline at end of file
diff --git a/Debate_backend/pom.xml b/Debate_backend/pom.xml
new file mode 100644
index 0000000..ff0631a
--- /dev/null
+++ b/Debate_backend/pom.xml
@@ -0,0 +1,104 @@
+
+
+ 4.0.0
+
+ org.springframework.boot
+ spring-boot-starter-parent
+ 3.0.12
+
+
+ com.learning
+ newdemo
+ 0.0.1-SNAPSHOT
+ newdemo
+ 微信小程序登录Demo
+
+
+ 17
+ 3.0.1
+ 8.0.33
+ 1.2.83
+ 0.11.5
+
+
+
+
+
+ org.springframework.boot
+ spring-boot-starter-web
+
+
+
+
+ org.mybatis.spring.boot
+ mybatis-spring-boot-starter
+ ${mybatis.version}
+
+
+
+
+ com.mysql
+ mysql-connector-j
+ ${mysql.version}
+ runtime
+
+
+
+
+ org.projectlombok
+ lombok
+ true
+
+
+
+
+ com.alibaba
+ fastjson
+ ${fastjson.version}
+
+
+
+
+ io.jsonwebtoken
+ jjwt-api
+ ${jwt.version}
+
+
+ io.jsonwebtoken
+ jjwt-impl
+ ${jwt.version}
+ runtime
+
+
+ io.jsonwebtoken
+ jjwt-jackson
+ ${jwt.version}
+ runtime
+
+
+
+
+ org.springframework.boot
+ spring-boot-starter-test
+ test
+
+
+
+
+
+
+ org.springframework.boot
+ spring-boot-maven-plugin
+
+
+
+ org.projectlombok
+ lombok
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/Debate_backend/src/main/java/com/learning/newdemo/NewDemoApplication.java b/Debate_backend/src/main/java/com/learning/newdemo/NewDemoApplication.java
new file mode 100644
index 0000000..304317f
--- /dev/null
+++ b/Debate_backend/src/main/java/com/learning/newdemo/NewDemoApplication.java
@@ -0,0 +1,14 @@
+package com.learning.newdemo;
+
+import org.mybatis.spring.annotation.MapperScan;
+import org.springframework.boot.SpringApplication;
+import org.springframework.boot.autoconfigure.SpringBootApplication;
+
+@SpringBootApplication
+@MapperScan("com.learning.newdemo.mapper")
+public class NewDemoApplication {
+
+ public static void main(String[] args) {
+ SpringApplication.run(NewDemoApplication.class, args);
+ }
+}
\ No newline at end of file
diff --git a/Debate_backend/src/main/java/com/learning/newdemo/common/Result.java b/Debate_backend/src/main/java/com/learning/newdemo/common/Result.java
new file mode 100644
index 0000000..38fcf5f
--- /dev/null
+++ b/Debate_backend/src/main/java/com/learning/newdemo/common/Result.java
@@ -0,0 +1,71 @@
+package com.learning.newdemo.common;
+
+import lombok.Data;
+
+import java.io.Serializable;
+
+/**
+ * 统一API返回结果封装
+ */
+@Data
+public class Result implements Serializable {
+
+ private static final long serialVersionUID = 1L;
+
+ /**
+ * 是否成功
+ */
+ private boolean success;
+
+ /**
+ * 状态码
+ */
+ private int code;
+
+ /**
+ * 提示信息
+ */
+ private String message;
+
+ /**
+ * 数据
+ */
+ private T data;
+
+ /**
+ * 成功结果
+ */
+ public static Result success() {
+ return success(null);
+ }
+
+ /**
+ * 成功结果(带数据)
+ */
+ public static Result success(T data) {
+ Result result = new Result<>();
+ result.setSuccess(true);
+ result.setCode(200);
+ result.setMessage("操作成功");
+ result.setData(data);
+ return result;
+ }
+
+ /**
+ * 失败结果
+ */
+ public static Result error(String message) {
+ return error(500, message);
+ }
+
+ /**
+ * 失败结果(带状态码)
+ */
+ public static Result error(int code, String message) {
+ Result result = new Result<>();
+ result.setSuccess(false);
+ result.setCode(code);
+ result.setMessage(message);
+ return result;
+ }
+}
\ No newline at end of file
diff --git a/Debate_backend/src/main/java/com/learning/newdemo/config/CorsConfig.java b/Debate_backend/src/main/java/com/learning/newdemo/config/CorsConfig.java
new file mode 100644
index 0000000..c38e7cf
--- /dev/null
+++ b/Debate_backend/src/main/java/com/learning/newdemo/config/CorsConfig.java
@@ -0,0 +1,32 @@
+package com.learning.newdemo.config;
+
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.web.cors.CorsConfiguration;
+import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
+import org.springframework.web.filter.CorsFilter;
+
+/**
+ * 跨域配置
+ */
+@Configuration
+public class CorsConfig {
+
+ @Bean
+ public CorsFilter corsFilter() {
+ CorsConfiguration config = new CorsConfiguration();
+ // 允许所有域名进行跨域调用
+ config.addAllowedOriginPattern("*");
+ // 允许跨域发送cookie
+ config.setAllowCredentials(true);
+ // 放行全部原始头信息
+ config.addAllowedHeader("*");
+ // 允许所有请求方法跨域调用
+ config.addAllowedMethod("*");
+
+ UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
+ source.registerCorsConfiguration("/**", config);
+
+ return new CorsFilter(source);
+ }
+}
\ No newline at end of file
diff --git a/Debate_backend/src/main/java/com/learning/newdemo/config/GlobalExceptionHandler.java b/Debate_backend/src/main/java/com/learning/newdemo/config/GlobalExceptionHandler.java
new file mode 100644
index 0000000..9cdd962
--- /dev/null
+++ b/Debate_backend/src/main/java/com/learning/newdemo/config/GlobalExceptionHandler.java
@@ -0,0 +1,36 @@
+package com.learning.newdemo.config;
+
+import com.learning.newdemo.common.Result;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.http.HttpStatus;
+import org.springframework.web.bind.annotation.ExceptionHandler;
+import org.springframework.web.bind.annotation.ResponseStatus;
+import org.springframework.web.bind.annotation.RestControllerAdvice;
+
+/**
+ * 全局异常处理器
+ */
+@Slf4j
+@RestControllerAdvice
+public class GlobalExceptionHandler {
+
+ /**
+ * 处理所有未捕获的异常
+ */
+ @ExceptionHandler(Exception.class)
+ @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
+ public Result handleException(Exception e) {
+ log.error("系统异常", e);
+ return Result.error("系统异常,请联系管理员");
+ }
+
+ /**
+ * 处理运行时异常
+ */
+ @ExceptionHandler(RuntimeException.class)
+ @ResponseStatus(HttpStatus.BAD_REQUEST)
+ public Result handleRuntimeException(RuntimeException e) {
+ log.error("运行时异常", e);
+ return Result.error(e.getMessage());
+ }
+}
\ No newline at end of file
diff --git a/Debate_backend/src/main/java/com/learning/newdemo/config/RestTemplateConfig.java b/Debate_backend/src/main/java/com/learning/newdemo/config/RestTemplateConfig.java
new file mode 100644
index 0000000..af2e3bb
--- /dev/null
+++ b/Debate_backend/src/main/java/com/learning/newdemo/config/RestTemplateConfig.java
@@ -0,0 +1,17 @@
+package com.learning.newdemo.config;
+
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.web.client.RestTemplate;
+
+/**
+ * RestTemplate配置类
+ */
+@Configuration
+public class RestTemplateConfig {
+
+ @Bean
+ public RestTemplate restTemplate() {
+ return new RestTemplate();
+ }
+}
\ No newline at end of file
diff --git a/Debate_backend/src/main/java/com/learning/newdemo/controller/WxLoginController.java b/Debate_backend/src/main/java/com/learning/newdemo/controller/WxLoginController.java
new file mode 100644
index 0000000..37305ec
--- /dev/null
+++ b/Debate_backend/src/main/java/com/learning/newdemo/controller/WxLoginController.java
@@ -0,0 +1,67 @@
+package com.learning.newdemo.controller;
+
+import com.learning.newdemo.common.Result;
+import com.learning.newdemo.entity.WxUser;
+import com.learning.newdemo.service.WxUserService;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.web.bind.annotation.*;
+
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ * 微信小程序登录控制器
+ */
+@Slf4j
+@RestController
+@RequestMapping("/api/wx")
+public class WxLoginController {
+
+ @Autowired
+ private WxUserService wxUserService;
+
+ /**
+ * 微信登录接口
+ */
+ @PostMapping("/login")
+ public Result