refactor(router): 优化消息存储逻辑,修复Redis键格式错误并移除过期时间

This commit is contained in:
liyp 2024-10-20 16:29:19 +08:00
parent 787e3775be
commit 79a41bc5ad

View file

@ -30,6 +30,8 @@ func Router(data map[string]interface{}) {
sender := data["sender"].(map[string]interface{})
role := sender["role"].(string)
mid := data["message_id"].(float64)
// 将消息存入redis
redisClient := tools.GetRedisClient()
if redisClient != nil {
var group_message GroupMessage
@ -47,9 +49,9 @@ func Router(data map[string]interface{}) {
return
}
key := fmt.Sprintf("group_message:%f:%f", gid, uid)
exists, err := tools.CheckKeyExists(fmt.Sprintf("group_message:%f:%f", gid, uid))
exists, err := tools.CheckKeyExists(fmt.Sprintf("group_message:%d:%d", int64(gid), int64(uid)))
if err != nil || !exists {
redisClient.RPush(context.Background(), key, jsonString, 7*24*time.Hour)
redisClient.RPush(context.Background(), key, jsonString, 0)
// tools.SetValue(key, string(jsonString), 10*time.Second)
} else {
@ -77,24 +79,29 @@ func Router(data map[string]interface{}) {
}
// fmt.Println("raw_msg:", string(raw_msg))
if len(raw_msg) > 1 && string(raw_msg[0]) == prefix {
// 去除'!'
raw_msg = raw_msg[1:]
parms := strings.Fields(raw_msg)
if len(raw_msg) > 1 {
if string(raw_msg[0]) == prefix {
// 去除'!'
raw_msg = raw_msg[1:]
parms := strings.Fields(raw_msg)
worker := workers.NewWorker(parms, fmt.Sprintf("%d", int(uid)), fmt.Sprintf("%d", int(gid)), role, fmt.Sprintf("%d", int(mid)), raw_msg)
worker := workers.NewWorker(parms, fmt.Sprintf("%d", int(uid)), fmt.Sprintf("%d", int(gid)), role, fmt.Sprintf("%d", int(mid)), raw_msg)
// fmt.Println("router:", parms[0])
// fmt.Println("CheckPermission:", worker.CheckPermission())
// ans := NewStdAns(parms[0], fmt.Sprintf("%d", int(uid)), fmt.Sprintf("%d", int(gid)), role, fmt.Sprintf("%d", int(mid)), raw_msg)
message := worker.CheckPermission()
if message == "ok" {
// fmt.Println("router:", parms[0])
// fmt.Println("CheckPermission:", worker.CheckPermission())
// ans := NewStdAns(parms[0], fmt.Sprintf("%d", int(uid)), fmt.Sprintf("%d", int(gid)), role, fmt.Sprintf("%d", int(mid)), raw_msg)
message := worker.CheckPermission()
if message == "ok" {
message = worker.GetMsg()
worker.SendMsg(message)
message = worker.GetMsg()
worker.SendMsg(message)
} else {
println("权限校验失败")
}
} else {
println("权限校验失败")
// 实现触发器功能
}
}