You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

416 lines
12 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

# 微信小程序辩论助手后端
## 项目介绍
本项目是一个微信小程序辩论助手的后端服务实现了微信小程序登录、获取用户信息、AI辅助立论、复盘分析、模拟辩论等功能并支持历史记录的保存与查询。
## 技术栈
- Spring Boot 3.0.12
- MyBatis 3.0.1
- MySQL 8.0
- JWT
- RestTemplate (AI接口调用)
## 项目结构
```
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脚本
# 用户表
CREATE TABLE `wx_user` (
`id` bigint NOT NULL AUTO_INCREMENT COMMENT '主键',
`openid` varchar(100) NOT NULL COMMENT '微信openid',
`nickname` varchar(50) DEFAULT NULL COMMENT '昵称',
`avatar_url` varchar(255) DEFAULT NULL COMMENT '头像',
`gender` tinyint DEFAULT NULL COMMENT '性别 0-未知 1-男 2-女',
`country` varchar(50) DEFAULT NULL COMMENT '国家',
`province` varchar(50) DEFAULT NULL COMMENT '省份',
`city` varchar(50) DEFAULT NULL COMMENT '城市',
`language` varchar(50) DEFAULT NULL COMMENT '语言',
`create_time` datetime DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
`update_time` datetime DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
PRIMARY KEY (`id`),
UNIQUE KEY `uk_openid` (`openid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='微信用户表';
# 对话活动表
CREATE TABLE `wx_conversation` (
`id` bigint NOT NULL AUTO_INCREMENT COMMENT '主键',
`user_id` bigint NOT NULL COMMENT '用户ID',
`type` varchar(20) NOT NULL COMMENT '对话类型(debate/argument/review)',
`title` varchar(255) DEFAULT NULL COMMENT '对话标题',
`preview` varchar(255) DEFAULT NULL COMMENT '预览内容(最后一次AI回复的前10个字符)',
`create_time` datetime DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
`update_time` datetime DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
PRIMARY KEY (`id`),
KEY `idx_user_id` (`user_id`),
KEY `idx_type` (`type`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='对话活动表';
# 辩论历史记录表
CREATE TABLE `wx_debate_record` (
`id` bigint NOT NULL AUTO_INCREMENT COMMENT '主键',
`conversation_id` bigint NOT NULL COMMENT '对话活动ID',
`content` text COMMENT 'AI回复内容',
`user_message` text COMMENT '用户消息',
`sequence` int NOT NULL COMMENT '消息序号',
`create_time` datetime DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
PRIMARY KEY (`id`),
KEY `idx_conversation_id` (`conversation_id`),
KEY `idx_sequence` (`sequence`),
CONSTRAINT `fk_debate_conversation` FOREIGN KEY (`conversation_id`) REFERENCES `wx_conversation` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='辩论历史记录表';
# 立论历史记录表
CREATE TABLE `wx_argument_record` (
`id` bigint NOT NULL AUTO_INCREMENT COMMENT '主键',
`conversation_id` bigint NOT NULL COMMENT '对话活动ID',
`topic` varchar(255) DEFAULT NULL COMMENT '辩题',
`stance` varchar(50) DEFAULT NULL COMMENT '立场',
`content` text COMMENT 'AI回复内容',
`user_message` text COMMENT '用户消息',
`sequence` int NOT NULL COMMENT '消息序号',
`create_time` datetime DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
PRIMARY KEY (`id`),
KEY `idx_conversation_id` (`conversation_id`),
KEY `idx_sequence` (`sequence`),
CONSTRAINT `fk_argument_conversation` FOREIGN KEY (`conversation_id`) REFERENCES `wx_conversation` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='立论历史记录表';
# 复盘历史记录表
CREATE TABLE `wx_review_record` (
`id` bigint NOT NULL AUTO_INCREMENT COMMENT '主键',
`conversation_id` bigint NOT NULL COMMENT '对话活动ID',
`content` text COMMENT 'AI回复内容',
`user_message` text COMMENT '用户消息',
`sequence` int NOT NULL COMMENT '消息序号',
`create_time` datetime DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
PRIMARY KEY (`id`),
KEY `idx_conversation_id` (`conversation_id`),
KEY `idx_sequence` (`sequence`),
CONSTRAINT `fk_review_conversation` FOREIGN KEY (`conversation_id`) REFERENCES `wx_conversation` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='复盘历史记录表';
```
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
```
## AI接口配置
修改`application.yml`中的AI接口配置
```yaml
ai:
api:
url: AI接口地址
key: API密钥(如果需要)
```
## 启动项目
```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": "语言"
}
}
```
### 3. AI接口
#### 3.1 立论接口
- URL: `/api/ai/argument`
- Method: POST
- Headers:
```
Authorization: 登录接口返回的token
```
- Body:
```json
{
"conversationId": "对话活动ID(可选,有则更新,无则新建)",
"topic": "辩题",
"stance": "立场(正方/反方)"
}
```
- Response:
```json
{
"success": true,
"code": 200,
"message": "操作成功",
"data": {
"content": "AI生成的立论内容",
"conversationId": "对话活动ID"
}
}
```
- 调用时机:用户在立论助手页面输入辩题和选择立场后,点击发送按钮时调用
- 数据库操作:
- 如果没有conversationId则在wx_conversation表中新建一条记录type为"argument"并返回新建的conversationId
- 在wx_argument_record表中插入一条记录包含用户消息和AI回复内容
- 更新wx_conversation表中对应记录的preview字段(取AI回复内容前10个字符)
#### 3.2 复盘接口
- URL: `/api/ai/review`
- Method: POST
- Headers:
```
Authorization: 登录接口返回的token
```
- Body:
```json
{
"conversationId": "对话活动ID(可选,有则更新,无则新建)",
"content": "用户输入的辩论内容或复盘需求"
}
```
- Response:
```json
{
"success": true,
"code": 200,
"message": "操作成功",
"data": {
"content": "AI生成的复盘分析内容",
"conversationId": "对话活动ID"
}
}
```
- 调用时机:用户在复盘分析页面输入辩论内容或复盘需求后,点击发送按钮时调用
- 数据库操作:
- 如果没有conversationId则在wx_conversation表中新建一条记录type为"review"并返回新建的conversationId
- 在wx_review_record表中插入一条记录包含用户消息和AI回复内容
- 更新wx_conversation表中对应记录的preview字段(取AI回复内容前10个字符)
#### 3.3 辩论接口
- URL: `/api/ai/debate`
- Method: POST
- Headers:
```
Authorization: 登录接口返回的token
```
- Body:
```json
{
"conversationId": "对话活动ID(可选,有则更新,无则新建)",
"userMessage": "用户当前输入的消息"
}
```
- Response:
```json
{
"success": true,
"code": 200,
"message": "操作成功",
"data": {
"content": "AI生成的辩论回复内容",
"conversationId": "对话活动ID"
}
}
```
- 调用时机:用户在模拟辩论页面输入消息后,点击发送按钮时调用
- 数据库操作:
- 如果没有conversationId则在wx_conversation表中新建一条记录type为"debate"并返回新建的conversationId
- 在wx_debate_record表中插入一条记录包含用户消息和AI回复内容
- 更新wx_conversation表中对应记录的preview字段(取AI回复内容前10个字符)
### 4. 历史记录接口
#### 4.1 获取对话活动列表
- URL: `/api/conversation/list`
- Method: GET
- Headers:
```
Authorization: 登录接口返回的token
```
- Response:
```json
{
"success": true,
"code": 200,
"message": "操作成功",
"data": [
{
"id": 1,
"type": "debate",
"title": "对话标题",
"preview": "AI回复的前10个字符",
"createTime": "2023-01-01 12:00:00"
}
]
}
```
- 调用时机用户进入历史记录列表页面时调用根据当前所在模块传入不同的type参数
- 数据库操作查询当前用户在wx_conversation表中指定type的所有记录按创建时间倒序排列
#### 4.2 获取辩论历史记录详情
- URL: `/api/conversation/debate/{conversationId}`
- Method: GET
- Headers:
```
Authorization: 登录接口返回的token
```
- Response:
```json
{
"success": true,
"code": 200,
"message": "操作成功",
"data":
{
"id": 1,
"userMessage": "用户消息",
"content": "AI回复内容",
"sequence": 1,
"createTime": "2023-01-01 12:00:00"
}
}
```
- 调用时机:用户点击辩论历史记录列表中的某一项时调用
- 数据库操作:
- 查询wx_conversation表中指定id的记录
- 查询wx_debate_record表中conversation_id等于指定id的所有记录按sequence排序
#### 4.3 获取立论历史记录详情
- URL: `/api/conversation/argument/{conversationId}`
- Method: GET
- Headers:
```
Authorization: 登录接口返回的token
```
- Response:
```json
{
"success": true,
"code": 200,
"message": "操作成功",
"data":
[
{
"id": 1,
"userMessage": "用户消息",
"content": "AI回复内容",
"sequence": 1,
"createTime": "2023-01-01 12:00:00"
}
]
}
```
- 调用时机:用户点击立论历史记录列表中的某一项时调用
- 数据库操作:
- 查询wx_conversation表中指定id的记录
- 查询wx_argument_record表中conversation_id等于指定id的所有记录按sequence排序
#### 4.4 获取复盘历史记录详情
- URL: `/api/conversation/review/{conversationId}`
- Method: GET
- Headers:
```
Authorization: 登录接口返回的token
```
- Response:
```json
{
"success": true,
"code": 200,
"message": "操作成功",
"data": {
[
{
"id": 1,
"userMessage": "用户消息",
"content": "AI回复内容",
"sequence": 1,
"createTime": "2023-01-01 12:00:00"
}
]
}
}
```
- 调用时机:用户点击复盘历史记录列表中的某一项时调用
- 数据库操作:
- 查询wx_conversation表中指定id的记录
- 查询wx_review_record表中conversation_id等于指定id的所有记录按sequence排序