初始化小程序端

This commit is contained in:
2025-12-11 11:06:11 +08:00
parent a2c0d17210
commit f9988ae675
531 changed files with 235994 additions and 114 deletions

View File

@@ -208,9 +208,9 @@ namespace WxCheckApi.Controllers
string latitude = "";
string longitude = "";
using (MySqlCommand cmd = new MySqlCommand("SELECT Latitude, Longitude FROM xcx_conversation WHERE Id = @Id AND IsDeleted = 0", _connection))
using (MySqlCommand cmd = new MySqlCommand("SELECT Latitude, Longitude FROM xcx_conversation WHERE Guid = @Guid AND IsDeleted = 0", _connection))
{
cmd.Parameters.AddWithValue("@Id", request.Id);
cmd.Parameters.AddWithValue("@Guid", request.Guid);
using (var reader = await cmd.ExecuteReaderAsync())
{
@@ -230,9 +230,9 @@ namespace WxCheckApi.Controllers
var address = await ConvertCoordinatesToAddress(longitude, latitude);
// 更新数据库中的UserLocation字段
using (MySqlCommand cmd = new MySqlCommand("UPDATE xcx_conversation SET UserLocation = @UserLocation WHERE Id = @Id AND IsDeleted = 0", _connection))
using (MySqlCommand cmd = new MySqlCommand("UPDATE xcx_conversation SET UserLocation = @UserLocation WHERE Guid = @Guid AND IsDeleted = 0", _connection))
{
cmd.Parameters.AddWithValue("@Id", request.Id);
cmd.Parameters.AddWithValue("@Guid", request.Guid);
cmd.Parameters.AddWithValue("@UserLocation", address);
int rowsAffected = await cmd.ExecuteNonQueryAsync();
@@ -261,6 +261,7 @@ namespace WxCheckApi.Controllers
[HttpPost]
public async Task<IActionResult> AddConversation([FromBody] ConversationRequest request)
{
DateTime nowtime = DateTime.Now;
try
{
if (_connection.State != ConnectionState.Open)
@@ -288,8 +289,10 @@ namespace WxCheckApi.Controllers
}
}
// 生成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) VALUES (@UserKey, @ConversationContent, @SendMethod, @UserLocation, @Latitude, @Longitude, @RecordTime, @RecordTimeUTCStamp, @IsDeleted, NOW(), @MessageType); SELECT LAST_INSERT_ID();", _connection))
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);
@@ -298,9 +301,12 @@ namespace WxCheckApi.Controllers
cmd.Parameters.AddWithValue("@UserLocation", address);
cmd.Parameters.AddWithValue("@Latitude", latitude);
cmd.Parameters.AddWithValue("@Longitude", longitude);
cmd.Parameters.AddWithValue("@RecordTime", DateTime.Now);
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);
@@ -309,18 +315,17 @@ namespace WxCheckApi.Controllers
// 查询刚插入的记录,并左连接用户表
if (conversationId > 0)
{
string query = @"SELECT convs.Id, convs.UserKey, convs.ConversationContent, convs.SendMethod,
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.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.Id = @Id";
WHERE convs.Guid = @Guid";
using (MySqlCommand cmd = new MySqlCommand(query, _connection))
{
cmd.Parameters.AddWithValue("@Id", conversationId);
cmd.Parameters.AddWithValue("@Guid", conversationGuid);
using (var reader = await cmd.ExecuteReaderAsync())
{
if (await reader.ReadAsync())
@@ -329,21 +334,23 @@ namespace WxCheckApi.Controllers
var messageData = new Dictionary<string, string>
{
["Id"] = reader.GetInt64(0).ToString(),
["UserKey"] = reader.GetString(1),
["ConversationContent"] = reader.GetString(2),
["SendMethod"] = reader.GetString(3),
["UserLocation"] = reader.IsDBNull(4) ? "" : reader.GetString(4),
["Latitude"] = reader.IsDBNull(5) ? "" : reader.GetString(5),
["Longitude"] = reader.IsDBNull(6) ? "" : reader.GetString(6),
["RecordTime"] = reader.GetDateTime(7).ToString("yyyy-MM-dd HH:mm:ss"),
["RecordTimeUTCStamp"] = reader.GetInt64(8).ToString(),
["IsDeleted"] = reader.GetBoolean(9).ToString(),
["CreateTime"] = reader.GetDateTime(10).ToString("yyyy-MM-dd HH:mm:ss"),
["MessageType"] = reader.GetInt32(11).ToString(),
["UserName"] = reader.IsDBNull(12) ? "" : reader.GetString(12),
["WeChatName"] = reader.IsDBNull(13) ? "" : reader.GetString(13),
["PhoneNumber"] = reader.IsDBNull(14) ? "" : reader.GetString(14),
["AvatarUrl"] = reader.IsDBNull(15) ? "" : reader.GetString(15)
["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
@@ -371,7 +378,7 @@ namespace WxCheckApi.Controllers
}
}
return Ok(new { success = true, message = "收到!", conversationId = conversationId });
return Ok(new { success = true, message = "收到!", conversationGuid, receivedTime = nowtime.ToString("yyyy-MM-dd HH:mm:ss") });
}
catch (Exception ex)
{
@@ -400,7 +407,7 @@ namespace WxCheckApi.Controllers
List<ConversationResponse> conversations = new List<ConversationResponse>();
// 构建查询SQL根据MessageType参数决定是否添加过滤条件
string query = "SELECT Id, UserKey, ConversationContent, SendMethod, UserLocation, Latitude, Longitude, RecordTime, RecordTimeUTCStamp, IsDeleted, CreateTime, MessageType FROM xcx_conversation WHERE UserKey = @UserKey AND IsDeleted = 0";
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";
@@ -418,17 +425,19 @@ namespace WxCheckApi.Controllers
conversations.Add(new ConversationResponse
{
Id = reader.GetInt64(0),
UserKey = reader.GetString(1),
ConversationContent = reader.GetString(2),
SendMethod = reader.GetString(3),
UserLocation = reader.IsDBNull(4) ? "" : reader.GetString(4),
Latitude = reader.IsDBNull(5) ? "" : reader.GetString(5),
Longitude = reader.IsDBNull(6) ? "" : reader.GetString(6),
RecordTime = reader.GetDateTime(7),
RecordTimeUTCStamp = reader.GetInt64(8),
IsDeleted = reader.GetBoolean(9),
CreateTime = reader.GetDateTime(10),
MessageType = reader.GetInt32(11)
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)
});
}
}
@@ -460,14 +469,16 @@ namespace WxCheckApi.Controllers
await _connection.OpenAsync();
}
using (MySqlCommand cmd = new MySqlCommand("UPDATE xcx_conversation SET ConversationContent = @ConversationContent, SendMethod = @SendMethod, UserLocation = @UserLocation, MessageType = @MessageType WHERE Id = @Id AND UserKey = @UserKey", _connection))
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("@Id", request.Id);
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();
@@ -477,7 +488,7 @@ namespace WxCheckApi.Controllers
}
}
return Ok(new { success = true, message = "更新成功" });
return Ok(new { success = true, message = "更新成功" , receivedTime = nowtime.ToString("yyyy-MM-dd HH:mm:ss") });
}
catch (Exception ex)
{
@@ -503,9 +514,9 @@ namespace WxCheckApi.Controllers
await _connection.OpenAsync();
}
using (MySqlCommand cmd = new MySqlCommand("UPDATE xcx_conversation SET IsDeleted = 1 WHERE Id = @Id AND UserKey = @UserKey AND IsDeleted = 0", _connection))
using (MySqlCommand cmd = new MySqlCommand("UPDATE xcx_conversation SET IsDeleted = 1 WHERE Guid = @Guid AND UserKey = @UserKey AND IsDeleted = 0", _connection))
{
cmd.Parameters.AddWithValue("@Id", request.Id);
cmd.Parameters.AddWithValue("@Guid", request.Guid);
cmd.Parameters.AddWithValue("@UserKey", request.UserKey);
int rowsAffected = await cmd.ExecuteNonQueryAsync();
@@ -530,6 +541,70 @@ namespace WxCheckApi.Controllers
}
}
}
// 根据GUID查询会话记录不考虑IsDeleted状态
[HttpPost]
public async Task<IActionResult> 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]
@@ -551,7 +626,7 @@ namespace WxCheckApi.Controllers
List<ConversationResponse> conversations = new List<ConversationResponse>();
// 构建分页查询SQL根据MessageType参数决定是否添加过滤条件
string query = @"SELECT Id, UserKey, ConversationContent, SendMethod, UserLocation, Latitude, Longitude, RecordTime, RecordTimeUTCStamp, IsDeleted, CreateTime, 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)
@@ -577,17 +652,19 @@ namespace WxCheckApi.Controllers
conversations.Add(new ConversationResponse
{
Id = reader.GetInt64(0),
UserKey = reader.GetString(1),
ConversationContent = reader.GetString(2),
SendMethod = reader.GetString(3),
UserLocation = reader.IsDBNull(4) ? "" : reader.GetString(4),
Latitude = reader.IsDBNull(5) ? "" : reader.GetString(5),
Longitude = reader.IsDBNull(6) ? "" : reader.GetString(6),
RecordTime = reader.GetDateTime(7),
RecordTimeUTCStamp = reader.GetInt64(8),
IsDeleted = reader.GetBoolean(9),
CreateTime = reader.GetDateTime(10),
MessageType = reader.GetInt32(11)
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)
});
}
}
@@ -718,6 +795,8 @@ namespace WxCheckApi.Controllers
public double Latitude { get; set; }
public double Longitude { get; set; }
public int MessageType { get; set; } = 1; // 1:公有2:私有
public string? Guid { get; set; } // 会话唯一标识
public int? SpeakingTime { get; set; } // 对话时长
}
public class UserKeyRequest
@@ -736,27 +815,37 @@ namespace WxCheckApi.Controllers
public class UpdateConversationRequest
{
public long Id { get; set; }
public long? Id { get; set; }
public string Guid { get; set; } // 会话唯一标识
public string UserKey { get; set; }
public string ConversationContent { get; set; }
public string SendMethod { get; set; }
public string UserLocation { get; set; }
public int MessageType { get; set; } = 0;
public int? SpeakingTime { get; set; }
}
public class DeleteConversationRequest
{
public long Id { get; set; }
public long? Id { get; set; }
public string Guid { get; set; } // 会话唯一标识
public string UserKey { get; set; }
}
public class CheckAddressRequest
{
public long Id { get; set; }
public long? Id { get; set; }
public string Guid { get; set; } // 会话唯一标识
}
public class GetConversationByGuidRequest
{
public long? Id { get; set; }
public string Guid { get; set; } // 会话唯一标识
}
public class ConversationResponse
{
public long Id { get; set; }
public string Guid { get; set; } // 会话唯一标识
public string UserKey { get; set; }
public string ConversationContent { get; set; }
public string SendMethod { get; set; }
@@ -768,6 +857,7 @@ namespace WxCheckApi.Controllers
public bool IsDeleted { get; set; }
public DateTime CreateTime { get; set; }
public int MessageType { get; set; } // 1:公有消息2:私有消息
public int? SpeakingTime { get; set; }
}
public class RedisMessageRequest