diff --git a/WxCheckApi/.config/dotnet-tools.json b/WxCheckApi/.config/dotnet-tools.json
deleted file mode 100644
index d4937e0..0000000
--- a/WxCheckApi/.config/dotnet-tools.json
+++ /dev/null
@@ -1,13 +0,0 @@
-{
- "version": 1,
- "isRoot": true,
- "tools": {
- "dotnet-ef": {
- "version": "9.0.10",
- "commands": [
- "dotnet-ef"
- ],
- "rollForward": false
- }
- }
-}
\ No newline at end of file
diff --git a/WxCheckApi/.vscode/launch.json b/WxCheckApi/.vscode/launch.json
deleted file mode 100644
index e85d64d..0000000
--- a/WxCheckApi/.vscode/launch.json
+++ /dev/null
@@ -1,35 +0,0 @@
-{
- "version": "0.2.0",
- "configurations": [
- {
- // 使用 IntelliSense 找出 C# 调试存在哪些属性
- // 将悬停用于现有属性的说明
- // 有关详细信息,请访问 https://github.com/dotnet/vscode-csharp/blob/main/debugger-launchjson.md。
- "name": ".NET Core Launch (web)",
- "type": "coreclr",
- "request": "launch",
- "preLaunchTask": "build",
- // 如果已更改目标框架,请确保更新程序路径。
- "program": "${workspaceFolder}/bin/Debug/net8.0/WxCheckApi.dll",
- "args": [],
- "cwd": "${workspaceFolder}",
- "stopAtEntry": false,
- // 启用在启动 ASP.NET Core 时启动 Web 浏览器。有关详细信息: https://aka.ms/VSCode-CS-LaunchJson-WebBrowser
- "serverReadyAction": {
- "action": "openExternally",
- "pattern": "\\bNow listening on:\\s+(https?://\\S+)"
- },
- "env": {
- "ASPNETCORE_ENVIRONMENT": "Development"
- },
- "sourceFileMap": {
- "/Views": "${workspaceFolder}/Views"
- }
- },
- {
- "name": ".NET Core Attach",
- "type": "coreclr",
- "request": "attach"
- }
- ]
-}
\ No newline at end of file
diff --git a/WxCheckApi/.vscode/tasks.json b/WxCheckApi/.vscode/tasks.json
deleted file mode 100644
index aeffa9e..0000000
--- a/WxCheckApi/.vscode/tasks.json
+++ /dev/null
@@ -1,41 +0,0 @@
-{
- "version": "2.0.0",
- "tasks": [
- {
- "label": "build",
- "command": "dotnet",
- "type": "process",
- "args": [
- "build",
- "${workspaceFolder}/WxCheckApi.csproj",
- "/property:GenerateFullPaths=true",
- "/consoleloggerparameters:NoSummary;ForceNoAlign"
- ],
- "problemMatcher": "$msCompile"
- },
- {
- "label": "publish",
- "command": "dotnet",
- "type": "process",
- "args": [
- "publish",
- "${workspaceFolder}/WxCheckApi.csproj",
- "/property:GenerateFullPaths=true",
- "/consoleloggerparameters:NoSummary;ForceNoAlign"
- ],
- "problemMatcher": "$msCompile"
- },
- {
- "label": "watch",
- "command": "dotnet",
- "type": "process",
- "args": [
- "watch",
- "run",
- "--project",
- "${workspaceFolder}/WxCheckApi.csproj"
- ],
- "problemMatcher": "$msCompile"
- }
- ]
-}
\ No newline at end of file
diff --git a/WxCheckApi/API文档.md b/WxCheckApi/API文档.md
deleted file mode 100644
index 89e758f..0000000
--- a/WxCheckApi/API文档.md
+++ /dev/null
@@ -1,766 +0,0 @@
-# 微信小程序:语音信息打卡 API 接口文档
-
-本文档详细描述了WxCheck项目中Login控制器和Check控制器的所有API接口,包括接口功能、请求参数、响应格式以及调用示例。
-
-## 基础信息
-
-- **API基础路径**:`https://wx-xcx-check.blv-oa.com:4433/api/[controller]/[action]`
-- **请求方式**:POST
-
-## 统一响应格式
-
-所有接口返回以下统一格式的JSON响应:
-
-```json
-{
- "success": true/false,
- "message": "操作结果描述",
- "data": {...}, // 可选,返回的具体数据
- "error": "错误信息" // 可选,错误时返回
-}
-```
-
-## 1. Login控制器接口
-
-### 1.1 用户注册接口
-
-#### 接口描述
-用户信息更新功能,根据传入的UserKey更新用户的UserName、WeChatName和PhoneNumber信息。
-
-#### 接口路径
-`/api/Login/Register`
-
-#### 请求参数
-
-| 参数名 | 类型 | 必填 | 描述 |
-|--------|------|------|------|
-| UserName | string | 否 | 用户名(可以为空) |
-| UserKey | string | 是 | 用户唯一标识键 |
-| WeChatName | string | 否 | 微信名称 |
-| PhoneNumber | string | 否 | 电话号码 |
-| AvatarUrl | string | 否 | 头像地址 |
-
-#### 请求示例
-
-```json
-{
- "UserName": "张三",
- "UserKey": "openid_from_wechat",
- "WeChatName": "张三的微信",
- "PhoneNumber": "13800138000",
- "AvatarUrl": "https://example.com/avatar.jpg"
-}
-```
-
-#### 响应示例
-
-**成功响应:**
-```json
-{
- "success": true,
- "data": {
- "Id": 1,
- "UserName": "张三",
- "UserKey": "openid_from_wechat",
- "WeChatName": "张三的微信",
- "PhoneNumber": "13800138000",
- "AvatarUrl": "https://example.com/avatar.jpg",
- "FirstLoginTime": "2023-10-31T10:00:00",
- "IsDisabled": false,
- "CreateTime": "2023-10-31T10:00:00",
- "UpdateTime": "2023-10-31T15:30:00",
- "Token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9..."
- }
-}
-```
-
-**失败响应:**
-
-```json
-{
- "success": false,
- "message": "用户不存在"
-}
-```
-
-```json
-{
- "success": false,
- "message": "更新用户信息失败",
- "error": "数据库连接错误"
-}
-```
-
-### 1.2 用户登录接口
-
-#### 接口描述
-用户登录功能,将微信小程序code转换为OpenID并验证用户身份。如果用户不存在,则自动创建新用户记录。无论用户是否存在,都返回完整的用户信息和Token。
-
-#### 接口路径
-`/api/Login/Login`
-
-#### 请求参数
-
-| 参数名 | 类型 | 必填 | 描述 |
-|--------|------|------|------|
-| Code | string | 是 | 微信小程序登录凭证code,用于获取OpenID |
-
-#### 请求示例
-
-```json
-{
- "Code": "wx_login_code_here"
-}
-```
-
-#### 响应示例
-
-**成功响应(已存在用户):**
-```json
-{
- "success": true,
- "data": {
- "Id": 1,
- "UserName": "张三",
- "UserKey": "openid_from_wechat",
- "WeChatName": "张三的微信",
- "PhoneNumber": "13800138000",
- "AvatarUrl": "https://example.com/avatar.jpg",
- "FirstLoginTime": "2023-10-31T10:00:00",
- "IsDisabled": false,
- "CreateTime": "2023-10-31T10:00:00",
- "UpdateTime": "2023-10-31T10:00:00",
- "Token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9..."
- }
-}
-```
-
-**成功响应(新用户自动注册):**
-```json
-{
- "success": true,
- "data": {
- "Id": 2,
- "UserName": "",
- "UserKey": "new_openid_from_wechat",
- "WeChatName": "",
- "PhoneNumber": "",
- "AvatarUrl": "",
- "FirstLoginTime": "2023-10-31T16:00:00",
- "IsDisabled": false,
- "CreateTime": "2023-10-31T16:00:00",
- "UpdateTime": "2023-10-31T16:00:00",
- "Token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9..."
- }
-}
-```
-
-**失败响应:**
-```json
-{
- "success": false,
- "message": "用户已被禁用"
-}
-```
-
-```json
-{
- "success": false,
- "message": "获取微信OpenID失败",
- "error": "微信API调用失败"
-}
-```
-
-## 2. Check控制器接口
-
-### 2.1 检查地址接口
-
-#### 接口描述
-根据会话记录的GUID查询经纬度信息,并转换为详细地址更新到数据库中。
-
-#### 接口路径
-`/api/Check/CheckAddress`
-
-#### 请求参数
-
-| 参数名 | 类型 | 必填 | 描述 |
-|--------|------|------|------|
-| Guid | string | 是 | 会话唯一标识 |
-
-#### 请求示例
-
-```json
-{
- "Guid": "会话唯一标识"
-}
-```
-
-#### 响应示例
-
-**成功响应:**
-```json
-{
- "success": true,
- "message": "地址更新成功",
- "address": "北京市海淀区中关村南大街5号"
-}
-```
-
-**失败响应:**
-```json
-{
- "success": false,
- "message": "记录不存在或已被删除"
-}
-```
-
-```json
-{
- "success": false,
- "message": "更新失败",
- "error": "数据库操作错误"
-}
-```
-
-### 2.2 添加会话记录接口
-
-#### 接口描述
-添加新的会话记录到系统中。
-
-#### 接口路径
-`/api/Check/AddConversation`
-
-#### 请求参数
-
-| 参数名 | 类型 | 必填 | 描述 |
-|--------|------|------|------|
-| UserKey | string | 是 | 用户唯一标识键 |
-| ConversationContent | string | 是 | 会话内容 |
-| SendMethod | string | 是 | 发送方式 |
-| UserLocation | string | 否 | 用户定位信息(经纬度格式:"纬度,经度") |
-| MessageType | int | 否 | 1:公有消息,2:私有消息 |
-| Guid | string | 否 | 会话唯一标识(不提供则系统自动生成) |
-
-#### 请求示例
-
-```json
-{
- "UserKey": "user_123456",
- "ConversationContent": "这是一条测试消息",
- "SendMethod": "文本",
- "UserLocation": "39.9087,116.3975",
- "MessageType": 1
-}
-```
-
-#### 响应示例
-
-**成功响应:**
-```json
-{
- "success": true,
- "message": "收到!",
- "conversationGuid": "会话唯一标识",
- "receivedTime": "2023-10-31 10:05:00"
-}
-```
-
-**失败响应:**
-```json
-{
- "success": false,
- "message": "发送失败",
- "error": "数据库操作错误"
-}
-```
-
-### 2.3 查询会话记录接口
-
-#### 接口描述
-根据用户唯一标识键查询该用户的所有会话记录。
-
-#### 接口路径
-`https://wx-xcx-check.blv-oa.com/api/Check/GetConversations`
-
-#### 请求参数
-
-| 参数名 | 类型 | 必填 | 描述 |
-|--------|------|------|------|
-| UserKey | string | 是 | 用户唯一标识键 |
-| MessageType | int | 否 | 0:不判断消息类型(默认),1:只返回公有消息,2:只返回私有消息 |
-
-#### 请求示例
-
-```json
-{
- "UserKey": "user_123456",
- "MessageType": 0
-}
-```
-
-#### 响应示例
-
-**成功响应:**
-```json
-{
- "success": true,
- "data": [
- {
- "Id": 1,
- "Guid": "会话唯一标识",
- "UserKey": "user_123456",
- "ConversationContent": "这是一条测试消息",
- "SendMethod": "文本",
- "UserLocation": "北京市海淀区",
- "Latitude": "39.9087",
- "Longitude": "116.3975",
- "RecordTime": "2023-10-31T10:05:00",
- "RecordTimeUTCStamp": 1698732300000,
- "IsDeleted": false,
- "CreateTime": "2023-10-31T10:05:00",
- "MessageType": 1
- },
- {
- "Id": 2,
- "Guid": "会话唯一标识",
- "UserKey": "user_123456",
- "ConversationContent": "这是第二条测试消息",
- "SendMethod": "图片",
- "UserLocation": "北京市朝阳区",
- "Latitude": "39.9180",
- "Longitude": "116.4272",
- "RecordTime": "2023-10-31T10:06:00",
- "RecordTimeUTCStamp": 1698732360000,
- "IsDeleted": false,
- "CreateTime": "2023-10-31T10:06:00",
- "MessageType": 2
- }
- ]
-}
-```
-
-**失败响应:**
-```json
-{
- "success": false,
- "message": "查询失败",
- "error": "数据库连接错误"
-}
-```
-
-### 2.4 分页查询会话记录接口
-
-#### 接口描述
-分页查询用户的会话记录,每页默认10条,按时间戳从**最新到最旧**排序,支持根据消息类型进行过滤。
-
-#### 接口路径
-`https://wx-xcx-check.blv-oa.com:4433/api/Check/GetConversationsByPage`
-
-#### 请求参数
-
-| 参数名 | 类型 | 必填 | 描述 |
-|--------|------|------|------|
-| UserKey | string | 是 | 用户唯一标识键 |
-| Page | int | 否 | 页码,默认为1,小于1时自动设为1 |
-| PageSize | int | 否 | 每页数量,默认为10,最大100,小于1时自动设为10 |
-| MessageType | int | 否 | 0:不判断消息类型(默认),1:只返回公有消息,2:只返回私有消息 |
-
-#### 请求示例
-
-```json
-{
- "UserKey": "user_123456",
- "Page": 1,
- "PageSize": 10,
- "MessageType": 0
-}
-```
-
-#### 响应示例
-
-**成功响应:**
-```json
-{
- "success": true,
- "data": {
- "conversations": [
- {
- "Id": 1,
- "Guid": "会话唯一标识",
- "UserKey": "user_123456",
- "ConversationContent": "这是一条测试消息",
- "SendMethod": "文本",
- "UserLocation": "北京市海淀区",
- "Latitude": "39.9087",
- "Longitude": "116.3975",
- "RecordTime": "2023-10-31T10:00:00",
- "RecordTimeUTCStamp": 1698727200000,
- "IsDeleted": false,
- "CreateTime": "2023-10-31T10:00:00",
- "MessageType": 1
- },
- {
- "Id": 2,
- "Guid": "会话唯一标识",
- "UserKey": "user_123456",
- "ConversationContent": "这是一条私有消息",
- "SendMethod": "文本",
- "UserLocation": "北京市朝阳区",
- "Latitude": "39.9180",
- "Longitude": "116.4272",
- "RecordTime": "2023-10-31T09:55:00",
- "RecordTimeUTCStamp": 1698724500000,
- "IsDeleted": false,
- "CreateTime": "2023-10-31T09:55:00",
- "MessageType": 2
- }
- ],
- "totalCount": 25,
- "page": 1,
- "pageSize": 10,
- "totalPages": 3
- }
-}
-```
-
-**失败响应:**
-```json
-{
- "success": false,
- "message": "查询失败",
- "error": "数据库操作错误"
-}
-```
-
-### 2.5 根据GUID查询会话记录接口
-
-#### 接口描述
-根据会话唯一标识(GUID)查询会话记录详情,不考虑IsDeleted状态。
-
-#### 接口路径
-`/api/Check/GetConversationByGuid`
-
-#### 请求参数
-
-| 参数名 | 类型 | 必填 | 描述 |
-|--------|------|------|------|
-| Guid | string | 是 | 会话唯一标识 |
-
-#### 请求示例
-
-```json
-{
- "Guid": "会话唯一标识"
-}
-```
-
-#### 响应示例
-
-**成功响应:**
-```json
-{
- "success": true,
- "message": "查询成功",
- "data": {
- "Id": 1,
- "Guid": "会话唯一标识",
- "UserKey": "user_123456",
- "ConversationContent": "这是一条测试消息",
- "SendMethod": "文本",
- "UserLocation": "北京市海淀区",
- "Latitude": "39.9087",
- "Longitude": "116.3975",
- "RecordTime": "2023-10-31T10:05:00",
- "RecordTimeUTCStamp": 1698732300000,
- "IsDeleted": false,
- "CreateTime": "2023-10-31T10:05:00",
- "MessageType": 1
- }
-}
-```
-
-**失败响应:**
-```json
-{
- "success": false,
- "message": "未找到该记录"
-}
-```
-
-```json
-{
- "success": false,
- "message": "查询失败",
- "error": "数据库操作错误"
-}
-```
-
-### 2.6 更新会话记录接口
-
-#### 接口描述
-更新指定GUID的会话记录,需要验证UserKey的权限。每次更新时,RecordTime会自动更新为当前时间。
-
-#### 接口路径
-`/api/Check/UpdateConversation`
-
-#### 请求参数
-
-| 参数名 | 类型 | 必填 | 描述 |
-|--------|------|------|------|
-| Guid | string | 是 | 会话唯一标识 |
-| UserKey | string | 是 | 用户唯一标识键(用于权限验证) |
-| ConversationContent | string | 是 | 新的会话内容 |
-| SendMethod | string | 是 | 新的发送方式 |
-| UserLocation | string | 否 | 新的用户定位信息 |
-| MessageType | int | 否 | 1:公有消息,2:私有消息 |
-
-#### 请求示例
-
-```json
-{
- "Guid": "会话唯一标识",
- "UserKey": "user_123456",
- "ConversationContent": "更新后的会话内容",
- "SendMethod": "文本",
- "UserLocation": "北京市西城区",
- "MessageType": 1
-}
-```
-
-#### 响应示例
-
-**成功响应:**
-```json
-{
- "success": true,
- "message": "更新成功",
- "receivedTime": "2023-10-31 10:10:00"
-}
-```
-
-**失败响应:**
-```json
-{
- "success": false,
- "message": "记录不存在或无权限修改"
-}
-```
-
-```json
-{
- "success": false,
- "message": "更新失败",
- "error": "数据库操作错误"
-}
-```
-
-### 2.7 删除会话记录接口
-
-#### 接口描述
-软删除会话记录(将IsDeleted标记为1),需要验证UserKey的权限。
-
-#### 接口路径
-`/api/Check/DeleteConversation`
-
-#### 请求参数
-
-| 参数名 | 类型 | 必填 | 描述 |
-|--------|------|------|------|
-| Guid | string | 是 | 会话唯一标识 |
-| UserKey | string | 是 | 用户唯一标识键(用于权限验证) |
-
-#### 请求示例
-
-```json
-{
- "Guid": "会话唯一标识",
- "UserKey": "user_123456"
-}
-```
-
-#### 响应示例
-
-**成功响应:**
-```json
-{
- "success": true,
- "message": "删除成功"
-}
-```
-
-**失败响应:**
-```json
-{
- "success": false,
- "message": "记录不存在或已被删除"
-}
-```
-
-```json
-{
- "success": false,
- "message": "删除失败",
- "error": "数据库操作错误"
-}
-```
-
-### 2.8 从Redis Stream读取消息接口
-
-#### 接口描述
-从Redis Stream读取新的会话消息。
-
-#### 接口路径
-`/api/Check/ReadMessageFromRedis`
-
-#### 请求参数
-
-| 参数名 | 类型 | 必填 | 描述 |
-|--------|------|------|------|
-| GroupName | string | 否 | 消费组名称,默认:xcx_group |
-| ConsumerName | string | 否 | 消费者名称,默认:consumer_时间戳 |
-| Count | int | 否 | 读取消息数量,默认:1 |
-
-#### 请求示例
-
-```json
-{
- "GroupName": "xcx_group",
- "ConsumerName": "consumer_1",
- "Count": 5
-}
-```
-
-#### 响应示例
-
-**成功响应:**
-```json
-{
- "success": true,
- "message": "成功读取消息",
- "data": [
- {
- "MessageId": "1635739200000-0",
- "Id": "1",
- "Guid": "会话唯一标识",
- "UserKey": "user_123456",
- "ConversationContent": "测试消息",
- "SendMethod": "文本",
- "UserLocation": "北京市海淀区",
- "Latitude": "39.9087",
- "Longitude": "116.3975",
- "RecordTime": "2023-10-31T10:00:00",
- "RecordTimeUTCStamp": "1698727200000",
- "IsDeleted": "false",
- "CreateTime": "2023-10-31T10:00:00",
- "MessageType": "1",
- "UserName": "张三",
- "WeChatName": "张三的微信",
- "PhoneNumber": "13800138000",
- "AvatarUrl": "https://example.com/avatar.jpg"
- }
- ]
-}
-```
-
-**失败响应:**
-```json
-{
- "success": false,
- "message": "读取消息失败",
- "error": "Redis操作错误"
-}
-```
-
-## 3. 接口调用说明
-
-### 3.1 HTTP客户端调用示例(JavaScript)
-
-```javascript
-// 封装API请求函数
-async function callApi(endpoint, data) {
- try {
- const response = await fetch(`http://your-api-domain/api${endpoint}`, {
- method: 'POST',
- headers: {
- 'Content-Type': 'application/json'
- },
- body: JSON.stringify(data)
- });
-
- const result = await response.json();
- return result;
- } catch (error) {
- console.error('API调用失败:', error);
- throw error;
- }
-}
-
-// 用户信息更新示例
-async function updateUser() {
- const result = await callApi('/Login/Register', {
- UserName: '张三',
- UserKey: 'openid_from_wechat',
- WeChatName: '张三的微信',
- PhoneNumber: '13800138000'
- });
-
- if (result.success) {
- console.log('用户信息更新成功');
- } else {
- console.log('用户信息更新失败:', result.message);
- }
-}
-
-// 用户登录示例
-async function loginUser() {
- const result = await callApi('/Login/Login', {
- Code: 'wx_login_code_here'
- });
-
- if (result.success) {
- console.log('登录成功,用户信息:', result.data);
- // 保存用户信息和Token到本地存储
- localStorage.setItem('userInfo', JSON.stringify(result.data));
- localStorage.setItem('token', result.data.Token);
- } else {
- console.log('登录失败:', result.message);
- }
-}
-
-// 添加会话示例
-async function addConversation() {
- const result = await callApi('/Check/AddConversation', {
- UserKey: 'user_123456',
- ConversationContent: '测试会话内容',
- SendMethod: '文本',
- UserLocation: '北京',
- MessageType: 0
- });
-
- if (result.success) {
- console.log('会话添加成功');
- } else {
- console.log('会话添加失败:', result.message);
- }
-}
-```
-
-### 3.2 常见错误处理
-
-1. **数据库连接错误**:检查数据库服务是否正常运行,连接字符串是否正确
-2. **权限验证失败**:确保提供的UserKey与操作资源匹配
-3. **记录不存在**:在更新或删除前确认记录ID和UserKey的正确性
-4. **网络错误**:检查API服务是否正常运行,网络连接是否稳定
-
-## 4. 安全注意事项
-
-1. **参数验证**:所有接口都应在前端进行基本的数据格式验证
-2. **UserKey保护**:UserKey作为用户身份标识,应妥善保护,避免泄露
-3. **错误信息处理**:生产环境中应避免返回详细的错误信息,防止信息泄露
-4. **请求频率限制**:建议在生产环境中对API接口实施请求频率限制,防止滥用
-
-## 5. 接口维护信息
-
-- **最后更新时间**:2023-11-01
-- **维护人员**:系统管理员
-- **版本号**:v1.2.0
\ No newline at end of file
diff --git a/WxCheckApi/CSRedisCacheHelper.cs b/WxCheckApi/CSRedisCacheHelper.cs
deleted file mode 100644
index fef4abb..0000000
--- a/WxCheckApi/CSRedisCacheHelper.cs
+++ /dev/null
@@ -1,199 +0,0 @@
-using CSRedis;
-using System;
-using System.Collections.Generic;
-using System.Configuration;
-using System.Linq;
-using System.Text;
-using static CSRedis.CSRedisClient;
-
-namespace Common
-{
- ///
- /// Redis缓存辅助类
- ///
- public class CSRedisCacheHelper
- {
- public static CSRedisClient? redis;
- public static CSRedisClient? redis14;
- public static CSRedisClient? redis15;
-
- private const string ip = "127.0.0.1";
- //private const string port = "6379";
- private const string port = "6800";
- static CSRedisCacheHelper()
- {
- var redisHostStr = string.Format("{0}:{1}", ip, port);
- if (!string.IsNullOrEmpty(redisHostStr))
- {
- redis = new CSRedisClient(redisHostStr + ",password=,defaultDatabase=0");
- redis15 = new CSRedisClient(redisHostStr + ",password=,defaultDatabase=15");
- var DingYueMsg = ("CellCorelDRAWUser", new Action(async (args) =>
- {
- string body = args.Body;
- }));
-
- CSRedisCacheHelper.redis.Subscribe(DingYueMsg);
- }
- }
- ///
- /// 添加缓存
- ///
- ///
- ///
- ///
- public static void Set(string key, T value, int ExpireTime)
- {
- redis?.Set(key, value, ExpireTime * 60);
- }
-
- public static T Get(string key)
- {
- return redis.Get(key);
- }
-
- public static void Forever(string key, T value)
- {
- redis.Set(key, value, -1);
- }
- public static void Del(string key)
- {
- redis.Del(key);
- }
- public static void ListPush(string key, T value)
- {
- redis.LPush(key, value);
- }
-
- ///
- /// 判断是否存在
- ///
- ///
- ///
-
- public static bool Contains(string key)
- {
- bool result = redis.Exists(key);
- return result;
- }
-
- public static string? XAdd(string key, params (string, string)[] fieldValues)
- {
- try
- {
- var result = redis.XAdd(key, fieldValues);
- return result;
- }
- catch (Exception ex)
- {
- System.Diagnostics.Debug.WriteLine($"XAdd error: {ex.Message}");
- return null;
- }
- }
-
- public static string? XReadGroup(string key, string group, string consumer, int count = 1, string id = null)
- {
- try
- {
- id = id ?? ">";
- var result = redis.XReadGroup(group, consumer, count, 0, (key, id));
-
- if (result != null && result.Length > 0)
- {
- // 处理消息
- var messages = new List>();
-
- foreach (var streamResult in result)
- {
- foreach (var entry in streamResult.data)
- {
- var message = new Dictionary
- {
- ["Id"] = entry.id,
- ["Values"] = entry.items
- };
-
- messages.Add(message);
-
- // 确认消息已处理
- redis.XAck(key, group, entry.id);
- }
- }
-
- return System.Text.Json.JsonSerializer.Serialize(messages);
- }
-
- return null;
- }
- catch (Exception ex)
- {
- System.Diagnostics.Debug.WriteLine($"XReadGroup error: {ex.Message}");
- return null;
- }
- }
-
- public static (string key, (string id, string items)[] data)[] XReadGroup(string group, string consumer, long count, long block, params (string key, string id)[] streams)
- {
- try
- {
- var result = redis.XReadGroup(group, consumer, count, block, streams);
-
- if (result != null && result.Length > 0)
- {
- // 处理消息并确认已处理
- var processedResults = new List<(string key, (string id, string items)[] data)>();
-
- foreach (var streamResult in result)
- {
- var messages = new List<(string id, string items)>();
-
- foreach (var entry in streamResult.data)
- {
- // 确认消息已处理
- redis.XAck(streamResult.key, group, entry.id);
-
- // entry是一个元组 (string id, string[] items)
- // 我们需要将string[] items转换为string items
- var itemsArray = entry.items;
- var itemsString = string.Join(",", itemsArray);
- messages.Add((entry.id, itemsString));
- }
-
- processedResults.Add((streamResult.key, messages.ToArray()));
- }
-
- return processedResults.ToArray();
- }
-
- return Array.Empty<(string key, (string id, string items)[] data)>();
- }
- catch (Exception ex)
- {
- System.Diagnostics.Debug.WriteLine($"XReadGroup error: {ex.Message}");
- return Array.Empty<(string key, (string id, string items)[] data)>();
- }
- }
-
- public static bool XGroupCreate(string key, string group, string id = "0")
- {
- try
- {
- redis.XGroupCreate(key, group, id, true);
- return true;
- }
- catch
- {
- return false;
- }
- }
-
- ///
- /// 发布消息
- ///
- ///
- ///
- public static void Publish(string Topic, string Payload)
- {
- CSRedisCacheHelper.redis.PublishNoneMessageId(Topic, Payload);
- }
- }
-}
diff --git a/WxCheckApi/Controllers/CheckController.cs b/WxCheckApi/Controllers/CheckController.cs
deleted file mode 100644
index f76e5ed..0000000
--- a/WxCheckApi/Controllers/CheckController.cs
+++ /dev/null
@@ -1,869 +0,0 @@
-using Common;
-using Microsoft.AspNetCore.Authorization;
-using Microsoft.AspNetCore.Http;
-using Microsoft.AspNetCore.Mvc;
-using Microsoft.IdentityModel.Tokens;
-using MySql.Data.MySqlClient;
-using System;
-using System.Collections.Generic;
-using System.Data;
-using System.Linq;
-using System.Net.Http;
-using System.Text.Json;
-using System.Threading.Tasks;
-using System.Web;
-
-namespace WxCheckApi.Controllers
-{
- [Route("api/[controller]/[action]")]
- [ApiController]
- public class CheckController : ControllerBase
- {
- private readonly MySqlConnection _connection;
- private readonly HttpClient _httpClient;
- private readonly IConfiguration _configuration;
-
- public CheckController(MySqlConnection connection, IHttpClientFactory httpClientFactory, IConfiguration configuration)
- {
- _connection = connection;
- _httpClient = httpClientFactory.CreateClient();
- _configuration = configuration;
- }
-
- // 将经纬度转换为地址信息
- public async Task ConvertCoordinatesToAddress(string longitude,string latitude)
- {
- try
- {
- // 使用高德地图API进行逆地理编码
- string apiKey = _configuration["AmapApi:ApiKey"] ?? "4d5cb7818664ada68ae5f68783b8bd4c";
- string url = $"https://restapi.amap.com/v3/geocode/regeo?output=json&location={longitude},{latitude}&key={apiKey}&radius=1000&extensions=all";
-
- var response = await _httpClient.GetStringAsync(url);
- var jsonDoc = JsonDocument.Parse(response);
- var root = jsonDoc.RootElement;
-
-
-
- if (root.GetProperty("status").GetString() == "1" && root.TryGetProperty("regeocode", out var regeocodeElement) && regeocodeElement.ValueKind != JsonValueKind.Null)
- {
-
- if (regeocodeElement.TryGetProperty("formatted_address", out var formatted_address))
- {
- return formatted_address.ToString();
- }
- if (regeocodeElement.TryGetProperty("addressComponent", out var addressComponent))
- {
- string province = addressComponent.TryGetProperty("province", out var provinceElement) && provinceElement.ValueKind == JsonValueKind.String ? provinceElement.GetString() : "";
- string city = addressComponent.TryGetProperty("city", out var cityElement) && cityElement.ValueKind == JsonValueKind.String ? cityElement.GetString() : "";
- string district = addressComponent.TryGetProperty("district", out var districtElement) && districtElement.ValueKind == JsonValueKind.String ? districtElement.GetString() : "";
- string township = addressComponent.TryGetProperty("township", out var townshipElement) && townshipElement.ValueKind == JsonValueKind.String ? townshipElement.GetString() : "";
-
- // 获取街道和门牌号信息
- string street = "";
- string streetNumber = "";
- double distance = 0;
-
- // 方法1:从addressComponent获取街道信息
- if (addressComponent.TryGetProperty("streetNumber", out var streetNumberElement) && streetNumberElement.ValueKind == JsonValueKind.Object)
- {
- street = streetNumberElement.TryGetProperty("street", out var streetElement) && streetElement.ValueKind == JsonValueKind.String ? streetElement.GetString() : "";
- streetNumber = streetNumberElement.TryGetProperty("number", out var numberElement) && numberElement.ValueKind == JsonValueKind.String ? numberElement.GetString() : "";
-
- // 获取距离信息
- if (streetNumberElement.TryGetProperty("distance", out var distanceElement) && distanceElement.ValueKind == JsonValueKind.String)
- {
- double.TryParse(distanceElement.GetString(), out distance);
- }
- }
-
- // 方法2:如果方法1没有获取到街道信息,尝试从aoi信息中获取
- if (string.IsNullOrEmpty(street) && regeocodeElement.TryGetProperty("aois", out var aoisElement) && aoisElement.ValueKind == JsonValueKind.Array)
- {
- var aoisArray = aoisElement.EnumerateArray();
- foreach (var aoi in aoisArray)
- {
- if (aoi.TryGetProperty("name", out var aoiNameElement) && aoiNameElement.ValueKind == JsonValueKind.String)
- {
- street = aoiNameElement.GetString();
- break; // 取第一个AOI作为街道信息
- }
- }
- }
-
- // 方法3:如果前两种方法都没有获取到街道信息,尝试从pois信息中获取
- if (string.IsNullOrEmpty(street) && regeocodeElement.TryGetProperty("pois", out var poisElement) && poisElement.ValueKind == JsonValueKind.Array)
- {
- var poisArray = poisElement.EnumerateArray();
- foreach (var poi in poisArray)
- {
- if (poi.TryGetProperty("name", out var poiNameElement) && poiNameElement.ValueKind == JsonValueKind.String)
- {
- street = poiNameElement.GetString();
- break; // 取第一个POI作为街道信息
- }
- }
- }
-
- // 方法4:如果以上方法都没有获取到街道信息,尝试从formatted_address中解析
- if (string.IsNullOrEmpty(street) && regeocodeElement.TryGetProperty("formatted_address", out var formattedAddressElement) && formattedAddressElement.ValueKind == JsonValueKind.String)
- {
- string formattedAddress = formattedAddressElement.GetString();
- // 尝试从格式化地址中提取街道信息
- // 格式化地址通常格式为:省 市 区 街道 具体地址
-
- // 使用字符串数组作为分隔符
- string[] separators = { " ", "省", "市", "区", "县", "镇", "街道", "路", "巷", "号" };
- var addressParts = formattedAddress.Split(separators, StringSplitOptions.RemoveEmptyEntries);
-
- // 查找可能包含街道信息的部分
- for (int i = 0; i < addressParts.Length; i++)
- {
- var part = addressParts[i];
- // 如果部分包含"路"、"街"、"巷"等关键词,可能是街道信息
- if (part.Contains("路") || part.Contains("街") || part.Contains("巷") || part.Contains("道"))
- {
- street = part;
- // 如果下一个部分存在且不是区县名称,可能是门牌号
- if (i + 1 < addressParts.Length &&
- !addressParts[i + 1].Contains("区") &&
- !addressParts[i + 1].Contains("县"))
- {
- streetNumber = addressParts[i + 1];
- }
- break;
- }
- }
- }
-
- // 方法5:如果仍然没有获取到街道信息,尝试从nearestRoad信息中获取
- if (string.IsNullOrEmpty(street) && regeocodeElement.TryGetProperty("streetNumber", out var nearestStreetElement) &&
- nearestStreetElement.ValueKind == JsonValueKind.Object)
- {
- if (nearestStreetElement.TryGetProperty("street", out var nearestStreetNameElement) && nearestStreetNameElement.ValueKind == JsonValueKind.String)
- {
- street = nearestStreetNameElement.GetString();
- }
- }
-
- // 构建详细地址字符串
- string address = "";
- if (!string.IsNullOrEmpty(province))
- {
- address += province;
- }
- if (!string.IsNullOrEmpty(city) && city != province)
- {
- address += " " + city;
- }
- if (!string.IsNullOrEmpty(district))
- {
- address += " " + district;
- }
- if (!string.IsNullOrEmpty(township))
- {
- address += " " + township;
- }
- if (!string.IsNullOrEmpty(street))
- {
- address += " " + street;
- }
- if (!string.IsNullOrEmpty(streetNumber))
- {
- address += " " + streetNumber;
- }
-
- // 如果有距离信息,添加到地址后面
- if (distance > 0)
- {
- address += $" {distance:F1}米";
- }
- if (string.IsNullOrEmpty(address))
- {
- return "未获取到位置信息(高德返回值为空) " + latitude + "," + longitude;
- }
- return address.Trim();
- }
- }
-
- return latitude + "," + longitude; ; // 如果API调用失败,返回原始值
- }
- catch (Exception)
- {
- return latitude + "," + longitude; ; // 如果发生异常,返回原始值
- }
- }
-
- [HttpPost]
- public async Task CheckAddress([FromBody] CheckAddressRequest request)
- {
- try
- {
- if (_connection.State != ConnectionState.Open)
- {
- await _connection.OpenAsync();
- }
-
- // 从数据库查询经纬度信息
- string latitude = "";
- string longitude = "";
-
- using (MySqlCommand cmd = new MySqlCommand("SELECT Latitude, Longitude FROM xcx_conversation WHERE Guid = @Guid AND IsDeleted = 0", _connection))
- {
- cmd.Parameters.AddWithValue("@Guid", request.Guid);
-
- using (var reader = await cmd.ExecuteReaderAsync())
- {
- if (await reader.ReadAsync())
- {
- latitude = reader.IsDBNull(0) ? "" : reader.GetString(0);
- longitude = reader.IsDBNull(1) ? "" : reader.GetString(1);
- }
- else
- {
- return NotFound(new { success = false, message = "记录不存在或已被删除" });
- }
- }
- }
-
- // 转换经纬度为地址
- var address = await ConvertCoordinatesToAddress(longitude, latitude);
-
- // 更新数据库中的UserLocation字段
- using (MySqlCommand cmd = new MySqlCommand("UPDATE xcx_conversation SET UserLocation = @UserLocation WHERE Guid = @Guid AND IsDeleted = 0", _connection))
- {
- cmd.Parameters.AddWithValue("@Guid", request.Guid);
- cmd.Parameters.AddWithValue("@UserLocation", address);
-
- int rowsAffected = await cmd.ExecuteNonQueryAsync();
-
- if (rowsAffected == 0)
- {
- return NotFound(new { success = false, message = "记录不存在或已被删除" });
- }
- }
-
- return Ok(new { success = true, message = "地址更新成功", address = address });
- }
- catch (Exception ex)
- {
- return StatusCode(500, new { success = false, message = "更新失败", error = ex.Message });
- }
- finally
- {
- if (_connection.State == ConnectionState.Open)
- {
- await _connection.CloseAsync();
- }
- }
- }
- // 添加会话记录
- [HttpPost]
- public async Task AddConversation([FromBody] ConversationRequest request)
- {
- DateTime nowtime = DateTime.Now;
- try
- {
- if (_connection.State != ConnectionState.Open)
- {
- await _connection.OpenAsync();
- }
-
- // 解析经纬度并转换为地址
- string address = "";
- string latitude = "";
- string longitude = "";
-
- // 否则尝试从UserLocation字段解析
- if (!string.IsNullOrEmpty(request.UserLocation))
- {
- string[] parts = request.UserLocation.Split(',');
- if (parts.Length == 2)
- {
- if (double.TryParse(parts[0], out double lat) && double.TryParse(parts[1], out double lng))
- {
- longitude = lng.ToString();
- latitude = lat.ToString();
- address = "";// await ConvertCoordinatesToAddress(latitude, longitude);
- }
- }
- }
-
- // 生成GUID
- string conversationGuid = string.IsNullOrEmpty(request.Guid) ? Guid.NewGuid().ToString("N") : request.Guid;
- long conversationId = 0;
- using (MySqlCommand cmd = new MySqlCommand("INSERT INTO xcx_conversation (UserKey, ConversationContent, SendMethod, UserLocation, Latitude, Longitude, RecordTime, RecordTimeUTCStamp, IsDeleted, CreateTime, MessageType, Guid, SpeakingTime) VALUES (@UserKey, @ConversationContent, @SendMethod, @UserLocation, @Latitude, @Longitude, @RecordTime, @RecordTimeUTCStamp, @IsDeleted, @CreateTime, @MessageType, @Guid, @SpeakingTime); SELECT LAST_INSERT_ID();", _connection))
- {
- cmd.Parameters.AddWithValue("@UserKey", request.UserKey);
- cmd.Parameters.AddWithValue("@MessageType", request.MessageType);
- cmd.Parameters.AddWithValue("@ConversationContent", request.ConversationContent);
- cmd.Parameters.AddWithValue("@SendMethod", request.SendMethod);
- cmd.Parameters.AddWithValue("@UserLocation", address);
- cmd.Parameters.AddWithValue("@Latitude", latitude);
- cmd.Parameters.AddWithValue("@Longitude", longitude);
- cmd.Parameters.AddWithValue("@RecordTime", nowtime);
- cmd.Parameters.AddWithValue("@CreateTime", nowtime);
- cmd.Parameters.AddWithValue("@RecordTimeUTCStamp", DateTimeOffset.UtcNow.ToUnixTimeMilliseconds());
- cmd.Parameters.AddWithValue("@IsDeleted", 0);
- cmd.Parameters.AddWithValue("@Guid", conversationGuid);
- cmd.Parameters.AddWithValue("@SpeakingTime", request.SpeakingTime);
-
- object result = await cmd.ExecuteScalarAsync();
- conversationId = Convert.ToInt64(result);
- }
-
- // 查询刚插入的记录,并左连接用户表
- if (conversationId > 0)
- {
- string query = @"SELECT convs.Id, convs.Guid, convs.UserKey, convs.ConversationContent, convs.SendMethod,
- convs.UserLocation, convs.Latitude, convs.Longitude, convs.RecordTime,
- convs.RecordTimeUTCStamp, convs.IsDeleted, convs.CreateTime, convs.MessageType, convs.SpeakingTime,
- users.UserName, users.WeChatName, users.PhoneNumber, users.AvatarUrl
- FROM xcx_conversation AS convs
- LEFT JOIN xcx_users AS users ON convs.UserKey = users.UserKey
- WHERE convs.Guid = @Guid";
-
- using (MySqlCommand cmd = new MySqlCommand(query, _connection))
- {
- cmd.Parameters.AddWithValue("@Guid", conversationGuid);
- using (var reader = await cmd.ExecuteReaderAsync())
- {
- if (await reader.ReadAsync())
- {
- // 构建要发送到Redis的数据
- var messageData = new Dictionary
- {
- ["Id"] = reader.GetInt64(0).ToString(),
- ["Guid"] = reader.IsDBNull(1) ? "" : reader.GetString(1),
- ["UserKey"] = reader.GetString(2),
- ["ConversationContent"] = reader.GetString(3),
- ["SendMethod"] = reader.GetString(4),
- ["UserLocation"] = reader.IsDBNull(5) ? "" : reader.GetString(5),
- ["Latitude"] = reader.IsDBNull(6) ? "" : reader.GetString(6),
- ["Longitude"] = reader.IsDBNull(7) ? "" : reader.GetString(7),
- ["RecordTime"] = reader.GetDateTime(8).ToString("yyyy-MM-dd HH:mm:ss"),
- ["RecordTimeUTCStamp"] = reader.GetInt64(9).ToString(),
- ["IsDeleted"] = reader.GetBoolean(10).ToString(),
- ["CreateTime"] = reader.GetDateTime(11).ToString("yyyy-MM-dd HH:mm:ss"),
- ["MessageType"] = reader.GetInt32(12).ToString(),
- ["SpeakingTime"] = reader.IsDBNull(13) ? "" : reader.GetInt32(13).ToString(),
- ["UserName"] = reader.IsDBNull(14) ? "" : reader.GetString(14),
- ["WeChatName"] = reader.IsDBNull(15) ? "" : reader.GetString(15),
- ["PhoneNumber"] = reader.IsDBNull(16) ? "" : reader.GetString(16),
- ["AvatarUrl"] = reader.IsDBNull(17) ? "" : reader.GetString(17)
- };
-
- // 发送到Redis Stream
- try
- {
- // 确保Stream和Group存在
- CSRedisCacheHelper.XGroupCreate("xcx_msg", "xcx_group", "0");
-
- // 将Dictionary转换为params (string, string)[]格式
- var fieldValues = messageData.SelectMany(kvp => new (string, string)[] { (kvp.Key, kvp.Value) }).ToArray();
-
- // 添加消息到Stream
- string messageId = CSRedisCacheHelper.XAdd("xcx_msg", fieldValues);
-
- // 记录日志(可选)
- System.Diagnostics.Debug.WriteLine($"消息已发送到Redis Stream: {messageId}");
- }
- catch (Exception ex)
- {
- // 记录错误但不影响主流程
- System.Diagnostics.Debug.WriteLine($"发送到Redis Stream失败: {ex.Message}");
- }
- }
- }
- }
- }
-
- return Ok(new { success = true, message = "收到!", conversationGuid, receivedTime = nowtime.ToString("yyyy-MM-dd HH:mm:ss") });
- }
- catch (Exception ex)
- {
- return StatusCode(500, new { success = false, message = "发送失败", error = ex.Message });
- }
- finally
- {
- if (_connection.State == ConnectionState.Open)
- {
- await _connection.CloseAsync();
- }
- }
- }
-
- // 根据UserKey查询会话记录
- [HttpPost]
- public async Task GetConversations([FromBody] UserKeyRequest request)
- {
- try
- {
- if (_connection.State != ConnectionState.Open)
- {
- await _connection.OpenAsync();
- }
-
- List conversations = new List();
-
- // 构建查询SQL,根据MessageType参数决定是否添加过滤条件
- string query = "SELECT Id, Guid, UserKey, ConversationContent, SendMethod, UserLocation, Latitude, Longitude, RecordTime, RecordTimeUTCStamp, IsDeleted, CreateTime, MessageType, SpeakingTime FROM xcx_conversation WHERE UserKey = @UserKey AND IsDeleted = 0";
- if (request.MessageType == 1)
- {
- query += " AND MessageType = @MessageType";
- }
- query += " ORDER BY RecordTimeUTCStamp DESC";
-
- using (MySqlCommand cmd = new MySqlCommand(query, _connection))
- {
- cmd.Parameters.AddWithValue("@UserKey", request.UserKey);
-
- using (var reader = await cmd.ExecuteReaderAsync())
- {
- while (await reader.ReadAsync())
- {
- conversations.Add(new ConversationResponse
- {
- Id = reader.GetInt64(0),
- Guid = reader.IsDBNull(1) ? "" : reader.GetString(1),
- UserKey = reader.GetString(2),
- ConversationContent = reader.GetString(3),
- SendMethod = reader.GetString(4),
- UserLocation = reader.IsDBNull(5) ? "" : reader.GetString(5),
- Latitude = reader.IsDBNull(6) ? "" : reader.GetString(6),
- Longitude = reader.IsDBNull(7) ? "" : reader.GetString(7),
- RecordTime = reader.GetDateTime(8),
- RecordTimeUTCStamp = reader.GetInt64(9),
- IsDeleted = reader.GetBoolean(10),
- CreateTime = reader.GetDateTime(11),
- MessageType = reader.GetInt32(12),
- SpeakingTime = reader.IsDBNull(13) ? null : reader.GetInt32(13)
- });
- }
- }
- }
-
- return Ok(new { success = true, data = conversations.OrderBy(z => z.RecordTimeUTCStamp) });
- }
- catch (Exception ex)
- {
- return StatusCode(500, new { success = false, message = "查询失败", error = ex.Message });
- }
- finally
- {
- if (_connection.State == ConnectionState.Open)
- {
- await _connection.CloseAsync();
- }
- }
- }
-
- // 更新会话记录
- [HttpPost]
- public async Task UpdateConversation([FromBody] UpdateConversationRequest request)
- {
- try
- {
- if (_connection.State != ConnectionState.Open)
- {
- await _connection.OpenAsync();
- }
-
- DateTime nowtime = DateTime.Now;
- using (MySqlCommand cmd = new MySqlCommand("UPDATE xcx_conversation SET ConversationContent = @ConversationContent, SendMethod = @SendMethod, UserLocation = @UserLocation, MessageType = @MessageType, RecordTime = @RecordTime WHERE Guid = @Guid AND UserKey = @UserKey", _connection))
- {
- cmd.Parameters.AddWithValue("@Guid", request.Guid);
- cmd.Parameters.AddWithValue("@UserKey", request.UserKey);
- cmd.Parameters.AddWithValue("@ConversationContent", request.ConversationContent);
- cmd.Parameters.AddWithValue("@SendMethod", request.SendMethod);
- cmd.Parameters.AddWithValue("@UserLocation", request.UserLocation ?? "");
- cmd.Parameters.AddWithValue("@MessageType", request.MessageType);
- cmd.Parameters.AddWithValue("@RecordTime", nowtime);
-
- int rowsAffected = await cmd.ExecuteNonQueryAsync();
-
- if (rowsAffected == 0)
- {
- return NotFound(new { success = false, message = "记录不存在或无权限修改" });
- }
- }
-
- return Ok(new { success = true, message = "更新成功" , receivedTime = nowtime.ToString("yyyy-MM-dd HH:mm:ss") });
- }
- catch (Exception ex)
- {
- return StatusCode(500, new { success = false, message = "更新失败", error = ex.Message });
- }
- finally
- {
- if (_connection.State == ConnectionState.Open)
- {
- await _connection.CloseAsync();
- }
- }
- }
-
- // 软删除会话记录
- [HttpPost]
- public async Task DeleteConversation([FromBody] DeleteConversationRequest request)
- {
- try
- {
- if (_connection.State != ConnectionState.Open)
- {
- await _connection.OpenAsync();
- }
-
- using (MySqlCommand cmd = new MySqlCommand("UPDATE xcx_conversation SET IsDeleted = 1 WHERE Guid = @Guid AND UserKey = @UserKey AND IsDeleted = 0", _connection))
- {
- cmd.Parameters.AddWithValue("@Guid", request.Guid);
- cmd.Parameters.AddWithValue("@UserKey", request.UserKey);
-
- int rowsAffected = await cmd.ExecuteNonQueryAsync();
-
- if (rowsAffected == 0)
- {
- return NotFound(new { success = false, message = "记录不存在或已被删除" });
- }
- }
-
- return Ok(new { success = true, message = "删除成功" });
- }
- catch (Exception ex)
- {
- return StatusCode(500, new { success = false, message = "删除失败", error = ex.Message });
- }
- finally
- {
- if (_connection.State == ConnectionState.Open)
- {
- await _connection.CloseAsync();
- }
- }
- }
- // 根据GUID查询会话记录(不考虑IsDeleted状态)
- [HttpPost]
- public async Task GetConversationByGuid([FromBody] GetConversationByGuidRequest request)
- {
- try
- {
- if (_connection.State != ConnectionState.Open)
- {
- await _connection.OpenAsync();
- }
-
- // 查询记录,不考虑IsDeleted状态
- string query = @"SELECT Id, Guid, UserKey, ConversationContent, SendMethod, UserLocation,
- Latitude, Longitude, RecordTime, RecordTimeUTCStamp, IsDeleted, CreateTime, MessageType, SpeakingTime
- FROM xcx_conversation
- WHERE Guid = @Guid";
-
- using (MySqlCommand cmd = new MySqlCommand(query, _connection))
- {
- cmd.Parameters.AddWithValue("@Guid", request.Guid);
-
- using (var reader = await cmd.ExecuteReaderAsync())
- {
- if (await reader.ReadAsync())
- {
- var conversation = new ConversationResponse
- {
- Id = reader.GetInt64(0),
- Guid = reader.IsDBNull(1) ? "" : reader.GetString(1),
- UserKey = reader.GetString(2),
- ConversationContent = reader.GetString(3),
- SendMethod = reader.GetString(4),
- UserLocation = reader.IsDBNull(5) ? "" : reader.GetString(5),
- Latitude = reader.IsDBNull(6) ? "" : reader.GetString(6),
- Longitude = reader.IsDBNull(7) ? "" : reader.GetString(7),
- RecordTime = reader.GetDateTime(8),
- RecordTimeUTCStamp = reader.GetInt64(9),
- IsDeleted = reader.GetBoolean(10),
- CreateTime = reader.GetDateTime(11),
- MessageType = reader.GetInt32(12),
- SpeakingTime = reader.IsDBNull(13) ? null : reader.GetInt32(13)
- };
-
- return Ok(new { success = true, message = "查询成功", data = conversation });
- }
- else
- {
- return NotFound(new { success = false, message = "未找到该记录" });
- }
- }
- }
- }
- catch (Exception ex)
- {
- return StatusCode(500, new { success = false, message = "查询失败", error = ex.Message });
- }
- finally
- {
- if (_connection.State == ConnectionState.Open)
- {
- await _connection.CloseAsync();
- }
- }
- }
-
- // 分页查询会话记录
- [HttpPost]
- public async Task GetConversationsByPage([FromBody] PaginationRequest request)
- {
- try
- {
- if (_connection.State != ConnectionState.Open)
- {
- await _connection.OpenAsync();
- }
-
- // 验证并设置默认值
- if (request.Page < 1) request.Page = 1;
- if (request.PageSize < 1 || request.PageSize > 100) request.PageSize = 10;
-
- int offset = (request.Page - 1) * request.PageSize;
-
- List conversations = new List();
-
- // 构建分页查询SQL,根据MessageType参数决定是否添加过滤条件
- string query = @"SELECT Id, Guid, UserKey, ConversationContent, SendMethod, UserLocation, Latitude, Longitude, RecordTime, RecordTimeUTCStamp, IsDeleted, CreateTime, MessageType, SpeakingTime
- FROM xcx_conversation
- WHERE UserKey = @UserKey AND IsDeleted = 0";
- if (request.MessageType == 1)
- {
- query += " AND MessageType = @MessageType";
- }
- query += " ORDER BY RecordTimeUTCStamp DESC LIMIT @Offset, @Limit";
-
- using (MySqlCommand cmd = new MySqlCommand(query, _connection))
- {
- cmd.Parameters.AddWithValue("@UserKey", request.UserKey);
- if (request.MessageType == 1)
- {
- cmd.Parameters.AddWithValue("@MessageType", request.MessageType);
- }
- cmd.Parameters.AddWithValue("@Offset", offset);
- cmd.Parameters.AddWithValue("@Limit", request.PageSize);
-
- using (var reader = await cmd.ExecuteReaderAsync())
- {
- while (await reader.ReadAsync())
- {
- conversations.Add(new ConversationResponse
- {
- Id = reader.GetInt64(0),
- Guid = reader.IsDBNull(1) ? "" : reader.GetString(1),
- UserKey = reader.GetString(2),
- ConversationContent = reader.GetString(3),
- SendMethod = reader.GetString(4),
- UserLocation = reader.IsDBNull(5) ? "" : reader.GetString(5),
- Latitude = reader.IsDBNull(6) ? "" : reader.GetString(6),
- Longitude = reader.IsDBNull(7) ? "" : reader.GetString(7),
- RecordTime = reader.GetDateTime(8),
- RecordTimeUTCStamp = reader.GetInt64(9),
- IsDeleted = reader.GetBoolean(10),
- CreateTime = reader.GetDateTime(11),
- MessageType = reader.GetInt32(12),
- SpeakingTime = reader.IsDBNull(13) ? null : reader.GetInt32(13)
- });
- }
- }
- }
-
- // 查询总数,根据MessageType参数决定是否添加过滤条件
- int totalCount = 0;
- string countQuery = "SELECT COUNT(*) FROM xcx_conversation WHERE UserKey = @UserKey AND IsDeleted = 0";
- if (request.MessageType == 1)
- {
- countQuery += " AND MessageType = @MessageType";
- }
-
- using (MySqlCommand countCmd = new MySqlCommand(countQuery, _connection))
- {
- countCmd.Parameters.AddWithValue("@UserKey", request.UserKey);
- if (request.MessageType == 1)
- {
- countCmd.Parameters.AddWithValue("@MessageType", request.MessageType);
- }
- totalCount = Convert.ToInt32(await countCmd.ExecuteScalarAsync());
- }
-
- int totalPages = (int)Math.Ceiling((double)totalCount / request.PageSize);
-
- return Ok(new {
- success = true,
- data = new {
- conversations = conversations.OrderBy(z => z.RecordTimeUTCStamp),
- totalCount = totalCount,
- page = request.Page,
- pageSize = request.PageSize,
- totalPages = totalPages
- }
- });
- }
- catch (Exception ex)
- {
- return StatusCode(500, new { success = false, message = "查询失败", error = ex.Message });
- }
- finally
- {
- if (_connection.State == ConnectionState.Open)
- {
- await _connection.CloseAsync();
- }
- }
- }
-
- // 从Redis Stream读取消息
- [HttpPost]
- public async Task ReadMessageFromRedis([FromBody] RedisMessageRequest request)
- {
- try
- {
- // 确保Stream和Group存在
- CSRedisCacheHelper.XGroupCreate("xcx_msg", "xcx_group", "0");
-
- // 从Redis Stream读取消息
- string groupName = request.GroupName ?? "xcx_group";
- string consumerName = request.ConsumerName ?? "consumer_" + DateTime.Now.Ticks;
-
- string messages = CSRedisCacheHelper.XReadGroup("xcx_msg", groupName, consumerName, request.Count ?? 1);
-
- if (string.IsNullOrEmpty(messages))
- {
- return Ok(new { success = true, message = "没有新消息", data = new List() });
- }
-
- // 解析消息
- var messageList = new List