|
@@ -5,12 +5,12 @@ import (
|
|
|
"context"
|
|
"context"
|
|
|
"encoding/json"
|
|
"encoding/json"
|
|
|
"fmt"
|
|
"fmt"
|
|
|
- "log"
|
|
|
|
|
"os"
|
|
"os"
|
|
|
"strconv"
|
|
"strconv"
|
|
|
"time"
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/redis/go-redis/v9"
|
|
"github.com/redis/go-redis/v9"
|
|
|
|
|
+ "github.com/rs/zerolog/log"
|
|
|
)
|
|
)
|
|
|
|
|
|
|
|
// Redis客户端实例,全局变量
|
|
// Redis客户端实例,全局变量
|
|
@@ -76,10 +76,10 @@ func InitRedis(addr, password string, db int) error {
|
|
|
// 测试Redis连接
|
|
// 测试Redis连接
|
|
|
pong, err := redisClient.Ping(ctx).Result()
|
|
pong, err := redisClient.Ping(ctx).Result()
|
|
|
if err != nil {
|
|
if err != nil {
|
|
|
- log.Printf("Redis连接失败: %v", err)
|
|
|
|
|
|
|
+ log.Error().Err(err).Msg("Redis连接失败")
|
|
|
return err
|
|
return err
|
|
|
}
|
|
}
|
|
|
- log.Printf("Redis连接成功: %s", pong)
|
|
|
|
|
|
|
+ log.Info().Str("pong", pong).Msg("Redis连接成功")
|
|
|
return nil
|
|
return nil
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -87,7 +87,7 @@ func InitRedis(addr, password string, db int) error {
|
|
|
// 使用房间名作为Redis列表的键,存储消息历史记录
|
|
// 使用房间名作为Redis列表的键,存储消息历史记录
|
|
|
func StoreRawMessage(roomName string, rawMessage []byte) error {
|
|
func StoreRawMessage(roomName string, rawMessage []byte) error {
|
|
|
if redisClient == nil {
|
|
if redisClient == nil {
|
|
|
- log.Println("Redis客户端未初始化")
|
|
|
|
|
|
|
+ log.Warn().Msg("Redis客户端未初始化")
|
|
|
return nil
|
|
return nil
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -101,7 +101,7 @@ func StoreRawMessage(roomName string, rawMessage []byte) error {
|
|
|
// 将消息序列化为JSON
|
|
// 将消息序列化为JSON
|
|
|
msgBytes, err := json.Marshal(chatMsg)
|
|
msgBytes, err := json.Marshal(chatMsg)
|
|
|
if err != nil {
|
|
if err != nil {
|
|
|
- log.Printf("消息序列化失败: %v", err)
|
|
|
|
|
|
|
+ log.Error().Err(err).Msg("消息序列化失败")
|
|
|
return err
|
|
return err
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -110,14 +110,14 @@ func StoreRawMessage(roomName string, rawMessage []byte) error {
|
|
|
key := "room_messages:" + roomName
|
|
key := "room_messages:" + roomName
|
|
|
err = redisClient.RPush(ctx, key, msgBytes).Err()
|
|
err = redisClient.RPush(ctx, key, msgBytes).Err()
|
|
|
if err != nil {
|
|
if err != nil {
|
|
|
- log.Printf("消息存储到Redis失败: %v", err)
|
|
|
|
|
|
|
+ log.Error().Err(err).Str("key", key).Msg("消息存储到Redis失败")
|
|
|
return err
|
|
return err
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// 设置过期时间(可选)- 7天后自动删除
|
|
// 设置过期时间(可选)- 7天后自动删除
|
|
|
redisClient.Expire(ctx, key, 7*24*time.Hour)
|
|
redisClient.Expire(ctx, key, 7*24*time.Hour)
|
|
|
|
|
|
|
|
- log.Printf("消息已存储到Redis - 房间: %s\n\n", roomName)
|
|
|
|
|
|
|
+ log.Debug().Str("roomName", roomName).Msg("消息已存储到Redis")
|
|
|
return nil
|
|
return nil
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -125,7 +125,7 @@ func StoreRawMessage(roomName string, rawMessage []byte) error {
|
|
|
// 使用房间名作为Redis列表的键,存储消息历史记录
|
|
// 使用房间名作为Redis列表的键,存储消息历史记录
|
|
|
func StoreMessage(roomName, username, message string) error {
|
|
func StoreMessage(roomName, username, message string) error {
|
|
|
if redisClient == nil {
|
|
if redisClient == nil {
|
|
|
- log.Println("Redis客户端未初始化")
|
|
|
|
|
|
|
+ log.Warn().Msg("Redis客户端未初始化")
|
|
|
return nil
|
|
return nil
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -143,7 +143,7 @@ func StoreMessage(roomName, username, message string) error {
|
|
|
// 使用浮点数分数同时存储时间戳和未读消息数
|
|
// 使用浮点数分数同时存储时间戳和未读消息数
|
|
|
func UpdateRecentContacts(client *Client) error {
|
|
func UpdateRecentContacts(client *Client) error {
|
|
|
if redisClient == nil {
|
|
if redisClient == nil {
|
|
|
- log.Println("Redis客户端未初始化")
|
|
|
|
|
|
|
+ log.Warn().Msg("Redis客户端未初始化")
|
|
|
return nil
|
|
return nil
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -233,7 +233,7 @@ func UpdateRecentContacts(client *Client) error {
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- log.Printf("最近联系人列表已更新 - client name: %s", client.name)
|
|
|
|
|
|
|
+ log.Debug().Str("clientName", client.name).Msg("最近联系人列表已更新")
|
|
|
return nil
|
|
return nil
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -241,7 +241,7 @@ func UpdateRecentContacts(client *Client) error {
|
|
|
// 为接收者(对方)的联系人列表增加未读消息数
|
|
// 为接收者(对方)的联系人列表增加未读消息数
|
|
|
func IncrementUnreadCount(sender *Client, count int) error {
|
|
func IncrementUnreadCount(sender *Client, count int) error {
|
|
|
if redisClient == nil {
|
|
if redisClient == nil {
|
|
|
- log.Println("Redis客户端未初始化")
|
|
|
|
|
|
|
+ log.Warn().Msg("Redis客户端未初始化")
|
|
|
return nil
|
|
return nil
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -261,14 +261,14 @@ func IncrementUnreadCount(sender *Client, count int) error {
|
|
|
receiverID = sender.userInfo.ShopId
|
|
receiverID = sender.userInfo.ShopId
|
|
|
senderMember = strconv.Itoa(int(sender.userInfo.CustomId))
|
|
senderMember = strconv.Itoa(int(sender.userInfo.CustomId))
|
|
|
} else {
|
|
} else {
|
|
|
- log.Printf("未知的客户端类型: %s", sender.userInfo.Type)
|
|
|
|
|
|
|
+ log.Warn().Str("clientType", sender.userInfo.Type).Msg("未知的客户端类型")
|
|
|
return nil
|
|
return nil
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// 获取接收者的联系人列表键
|
|
// 获取接收者的联系人列表键
|
|
|
key := getContactKey(receiverType, receiverID)
|
|
key := getContactKey(receiverType, receiverID)
|
|
|
if key == "" {
|
|
if key == "" {
|
|
|
- log.Printf("无法生成联系人键 - 类型: %s, ID: %d", receiverType, receiverID)
|
|
|
|
|
|
|
+ log.Error().Str("receiverType", receiverType).Int32("receiverID", receiverID).Msg("无法生成联系人键")
|
|
|
return nil
|
|
return nil
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -296,8 +296,14 @@ func IncrementUnreadCount(sender *Client, count int) error {
|
|
|
// 设置过期时间
|
|
// 设置过期时间
|
|
|
redisClient.Expire(ctx, key, ContactTTL)
|
|
redisClient.Expire(ctx, key, ContactTTL)
|
|
|
|
|
|
|
|
- log.Printf("未读消息数已增加 - 接收者类型: %s, 接收者ID: %d, 发送者ID: %s, 未读数: %d, 新分数: %f",
|
|
|
|
|
- receiverType, receiverID, senderMember, newUnread, newScore)
|
|
|
|
|
|
|
+ // log.Printf("未读消息数已增加 - 接收者类型: %s, 接收者ID: %d, 发送者ID: %s, 未读数: %d, 新分数: %f", receiverType, receiverID, senderMember, newUnread, newScore)
|
|
|
|
|
+ log.Debug().
|
|
|
|
|
+ Str("receiverType", receiverType).
|
|
|
|
|
+ Int32("receiverID", receiverID).
|
|
|
|
|
+ Str("senderMember", senderMember).
|
|
|
|
|
+ Int("newUnread", newUnread).
|
|
|
|
|
+ Float64("newScore", newScore).
|
|
|
|
|
+ Msg("未读消息数已增加")
|
|
|
return nil
|
|
return nil
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -305,7 +311,7 @@ func IncrementUnreadCount(sender *Client, count int) error {
|
|
|
// 返回指定数量的最新消息
|
|
// 返回指定数量的最新消息
|
|
|
func GetRoomMessages(roomName string, count int64) ([]ChatMessage, error) {
|
|
func GetRoomMessages(roomName string, count int64) ([]ChatMessage, error) {
|
|
|
if redisClient == nil {
|
|
if redisClient == nil {
|
|
|
- log.Println("Redis客户端未初始化")
|
|
|
|
|
|
|
+ log.Warn().Msg("Redis客户端未初始化")
|
|
|
return nil, nil
|
|
return nil, nil
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -315,7 +321,8 @@ func GetRoomMessages(roomName string, count int64) ([]ChatMessage, error) {
|
|
|
// -count 表示从列表末尾开始的count个元素
|
|
// -count 表示从列表末尾开始的count个元素
|
|
|
result, err := redisClient.LRange(ctx, key, -count, -1).Result()
|
|
result, err := redisClient.LRange(ctx, key, -count, -1).Result()
|
|
|
if err != nil {
|
|
if err != nil {
|
|
|
- log.Printf("从Redis获取消息失败: %v", err)
|
|
|
|
|
|
|
+ //log.Printf("从Redis获取消息失败: %v", err)
|
|
|
|
|
+ log.Error().Err(err).Str("key", key).Msg("从Redis获取消息失败")
|
|
|
return nil, err
|
|
return nil, err
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -323,7 +330,8 @@ func GetRoomMessages(roomName string, count int64) ([]ChatMessage, error) {
|
|
|
for _, msgStr := range result {
|
|
for _, msgStr := range result {
|
|
|
var msg ChatMessage
|
|
var msg ChatMessage
|
|
|
if err := json.Unmarshal([]byte(msgStr), &msg); err != nil {
|
|
if err := json.Unmarshal([]byte(msgStr), &msg); err != nil {
|
|
|
- log.Printf("消息反序列化失败: %v", err)
|
|
|
|
|
|
|
+ // log.Printf("消息反序列化失败: %v", err)
|
|
|
|
|
+ log.Error().Err(err).Str("message", msgStr).Msg("消息反序列化失败")
|
|
|
continue
|
|
continue
|
|
|
}
|
|
}
|
|
|
messages = append(messages, msg)
|
|
messages = append(messages, msg)
|
|
@@ -384,7 +392,7 @@ func getContactKey(userType string, id int32) string {
|
|
|
// 返回按最后聊天时间排序的联系人列表(最新的在前)
|
|
// 返回按最后聊天时间排序的联系人列表(最新的在前)
|
|
|
func GetRecentContacts(userType string, userID int32, limit int64) ([]ContactInfo, error) {
|
|
func GetRecentContacts(userType string, userID int32, limit int64) ([]ContactInfo, error) {
|
|
|
if redisClient == nil {
|
|
if redisClient == nil {
|
|
|
- log.Println("Redis客户端未初始化")
|
|
|
|
|
|
|
+ log.Warn().Msg("Redis客户端未初始化")
|
|
|
return nil, nil
|
|
return nil, nil
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -396,7 +404,7 @@ func GetRecentContacts(userType string, userID int32, limit int64) ([]ContactInf
|
|
|
// 获取最近的联系人(按分数降序,最新的在前)
|
|
// 获取最近的联系人(按分数降序,最新的在前)
|
|
|
result, err := redisClient.ZRevRangeWithScores(ctx, key, 0, limit-1).Result()
|
|
result, err := redisClient.ZRevRangeWithScores(ctx, key, 0, limit-1).Result()
|
|
|
if err != nil {
|
|
if err != nil {
|
|
|
- log.Printf("获取最近联系人失败: %v", err)
|
|
|
|
|
|
|
+ log.Error().Err(err).Str("key", key).Msg("获取最近联系人失败")
|
|
|
return nil, err
|
|
return nil, err
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -404,7 +412,8 @@ func GetRecentContacts(userType string, userID int32, limit int64) ([]ContactInf
|
|
|
for _, item := range result {
|
|
for _, item := range result {
|
|
|
contactID, err := strconv.Atoi(item.Member.(string))
|
|
contactID, err := strconv.Atoi(item.Member.(string))
|
|
|
if err != nil {
|
|
if err != nil {
|
|
|
- log.Printf("解析联系人ID失败: %v", err)
|
|
|
|
|
|
|
+ // log.Printf("解析联系人ID失败: %v", err)
|
|
|
|
|
+ log.Error().Err(err).Any("member", item.Member).Msg("解析联系人ID失败")
|
|
|
continue
|
|
continue
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -431,7 +440,7 @@ func GetRecentContacts(userType string, userID int32, limit int64) ([]ContactInf
|
|
|
// GetTotalUnreadCount 获取用户的总未读消息数
|
|
// GetTotalUnreadCount 获取用户的总未读消息数
|
|
|
func GetTotalUnreadCount(userType string, userID int32) (int, error) {
|
|
func GetTotalUnreadCount(userType string, userID int32) (int, error) {
|
|
|
if redisClient == nil {
|
|
if redisClient == nil {
|
|
|
- log.Println("Redis客户端未初始化")
|
|
|
|
|
|
|
+ log.Warn().Msg("Redis客户端未初始化")
|
|
|
return 0, nil
|
|
return 0, nil
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -443,7 +452,7 @@ func GetTotalUnreadCount(userType string, userID int32) (int, error) {
|
|
|
// 获取所有联系人的分数
|
|
// 获取所有联系人的分数
|
|
|
result, err := redisClient.ZRangeWithScores(ctx, key, 0, -1).Result()
|
|
result, err := redisClient.ZRangeWithScores(ctx, key, 0, -1).Result()
|
|
|
if err != nil {
|
|
if err != nil {
|
|
|
- log.Printf("获取联系人分数失败: %v", err)
|
|
|
|
|
|
|
+ log.Error().Err(err).Str("key", key).Msg("获取联系人分数失败")
|
|
|
return 0, err
|
|
return 0, err
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -470,13 +479,13 @@ func ClearUnreadCount(client *Client) error {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
if redisClient == nil {
|
|
if redisClient == nil {
|
|
|
- log.Println("Redis客户端未初始化")
|
|
|
|
|
|
|
+ log.Warn().Msg("Redis客户端未初始化")
|
|
|
return fmt.Errorf("redis client is not initialized")
|
|
return fmt.Errorf("redis client is not initialized")
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
key := getContactKey(client.userInfo.Type, id)
|
|
key := getContactKey(client.userInfo.Type, id)
|
|
|
if key == "" {
|
|
if key == "" {
|
|
|
- log.Printf("无效的用户类型: %s", client.userInfo.Type)
|
|
|
|
|
|
|
+ log.Error().Str("userType", client.userInfo.Type).Msg("无效的用户类型")
|
|
|
return fmt.Errorf("invalid user type: %s", client.userInfo.Type)
|
|
return fmt.Errorf("invalid user type: %s", client.userInfo.Type)
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -486,13 +495,22 @@ func ClearUnreadCount(client *Client) error {
|
|
|
if err != nil {
|
|
if err != nil {
|
|
|
if err == redis.Nil {
|
|
if err == redis.Nil {
|
|
|
// 如果联系人不存在,这并非一个错误,只是无需清理。
|
|
// 如果联系人不存在,这并非一个错误,只是无需清理。
|
|
|
- log.Printf("Contact not found, no unread count to clear - UserType: %s, UserID: %d, ContactID: %d",
|
|
|
|
|
- client.userInfo.Type, id, contactID)
|
|
|
|
|
|
|
+ // log.Printf("Contact not found, no unread count to clear - UserType: %s, UserID: %d, ContactID: %d", client.userInfo.Type, id, contactID)
|
|
|
|
|
+ log.Debug().
|
|
|
|
|
+ Str("userType", client.userInfo.Type).
|
|
|
|
|
+ Int32("userID", id).
|
|
|
|
|
+ Int32("contactID", contactID).
|
|
|
|
|
+ Msg("Contact not found, no unread count to clear")
|
|
|
return nil
|
|
return nil
|
|
|
}
|
|
}
|
|
|
// 对于其他类型的错误(如连接问题),则返回错误。
|
|
// 对于其他类型的错误(如连接问题),则返回错误。
|
|
|
- log.Printf("failed to get score for contact - UserType: %s, UserID: %d, ContactID: %d, Error: %v",
|
|
|
|
|
- client.userInfo.Type, id, contactID, err)
|
|
|
|
|
|
|
+ // log.Printf("failed to get score for contact - UserType: %s, UserID: %d, ContactID: %d, Error: %v",client.userInfo.Type, id, contactID, err)
|
|
|
|
|
+ log.Error().
|
|
|
|
|
+ Err(err).
|
|
|
|
|
+ Str("userType", client.userInfo.Type).
|
|
|
|
|
+ Int32("userID", id).
|
|
|
|
|
+ Int32("contactID", contactID).
|
|
|
|
|
+ Msg("failed to get score for contact")
|
|
|
return err
|
|
return err
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -505,12 +523,20 @@ func ClearUnreadCount(client *Client) error {
|
|
|
Score: newScore,
|
|
Score: newScore,
|
|
|
Member: contactIDStr,
|
|
Member: contactIDStr,
|
|
|
}).Err(); err != nil {
|
|
}).Err(); err != nil {
|
|
|
- log.Printf("failed to clear unread count - UserType: %s, UserID: %d, ContactID: %d, Error: %v",
|
|
|
|
|
- client.userInfo.Type, id, contactID, err)
|
|
|
|
|
|
|
+ log.Error().
|
|
|
|
|
+ Err(err).
|
|
|
|
|
+ Str("userType", client.userInfo.Type).
|
|
|
|
|
+ Int32("userID", id).
|
|
|
|
|
+ Int32("contactID", contactID).
|
|
|
|
|
+ Msg("failed to clear unread count")
|
|
|
return err
|
|
return err
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- log.Printf("未读消息数已清除 - 用户类型: %s, 用户ID: %d, 联系人ID: %d",
|
|
|
|
|
- client.userInfo.Type, id, contactID)
|
|
|
|
|
|
|
+ // log.Printf("未读消息数已清除 - 用户类型: %s, 用户ID: %d, 联系人ID: %d", client.userInfo.Type, id, contactID)
|
|
|
|
|
+ log.Info().
|
|
|
|
|
+ Str("userType", client.userInfo.Type).
|
|
|
|
|
+ Int32("userID", id).
|
|
|
|
|
+ Int32("contactID", contactID).
|
|
|
|
|
+ Msg("未读消息数已清除")
|
|
|
return nil
|
|
return nil
|
|
|
}
|
|
}
|