first commit

This commit is contained in:
Yangtao
2025-11-18 17:48:20 +08:00
commit 6e56cab848
196 changed files with 65809 additions and 0 deletions

68
pkg/cache/user/user.go vendored Normal file
View File

@ -0,0 +1,68 @@
package cache_user
import (
"servicebase/pkg/cache"
"servicebase/pkg/tools"
"fmt"
"strings"
"time"
"github.com/anxpp/beego/logs"
"github.com/anxpp/common-utils/str"
)
const key = "user:models"
const keyToken = "user:token"
// 用户当前所在的房间ID + userid = chatroomid
const ACCESSTOKEN_KEY_PREV = "ACCESSTOKEN_"
const ACCESSTOKEN_EXPIRE_DURATION = time.Hour * 24 * 180
// 通过token获取userId
func IdByToken(token string) (userId string) {
if len(token) == 0 {
return
}
var e error
if userId, e = cache.CommonClient().HGet(cache.Ctx(), keyToken, token).Result(); e != nil || len(userId) == 0 {
userId, e = cache.CommonClient().Get(cache.Ctx(), ACCESSTOKEN_KEY_PREV+token).Result()
if e != nil {
logs.Error("GetUserIdByAccessToken redisError=%v token=%v", e.Error(), token)
return
} else if len(userId) > 0 {
// 迁移token
ttl, err := cache.CommonClient().TTL(cache.Ctx(), ACCESSTOKEN_KEY_PREV+token).Result()
if err != nil {
return
}
value := fmt.Sprintf("%s#%s", userId, str.TimeToString(time.Now().Add(ttl)))
if err := cache.CommonClient().HSet(cache.Ctx(), keyToken, token, value).Err(); err == nil {
cache.CommonClient().Del(cache.Ctx(), ACCESSTOKEN_KEY_PREV+token)
}
}
} else {
arr := strings.Split(userId, "#")
if len(arr) != 2 {
_ = IdTokenDel(token)
return ""
}
over := tools.StrToDateTime(arr[1])
if over.Before(time.Now()) {
_ = IdTokenDel(token)
return ""
}
userId = arr[0]
}
return
}
func IdTokenSet(token, userId string) (e error) {
value := fmt.Sprintf("%s#%s", userId, str.TimeToString(time.Now().Add(ACCESSTOKEN_EXPIRE_DURATION)))
return cache.CommonClient().HSet(cache.Ctx(), keyToken, token, value).Err()
}
func IdTokenDel(token string) (e error) {
_, e = cache.CommonClient().HDel(cache.Ctx(), keyToken, token).Result()
cache.CommonClient().Del(cache.Ctx(), ACCESSTOKEN_KEY_PREV+token)
return
}