121 lines
3.2 KiB
Go
121 lines
3.2 KiB
Go
package db
|
||
|
||
import (
|
||
"time"
|
||
"xiawan/wx/clientsdk/baseinfo"
|
||
|
||
"github.com/gogf/gf/os/gcache"
|
||
)
|
||
|
||
var cache *gcache.Cache
|
||
|
||
// 添加一个持久缓存,用于保存最后的登录状态
|
||
var lastLoginStatusCache *gcache.Cache
|
||
|
||
// 添加短连接状态缓存
|
||
var shortLinkStatusCache *gcache.Cache
|
||
|
||
// ShortLinkStatus 短连接状态结构
|
||
type ShortLinkStatus struct {
|
||
IsEnabled bool
|
||
}
|
||
|
||
func init() {
|
||
cache = gcache.New()
|
||
lastLoginStatusCache = gcache.New()
|
||
shortLinkStatusCache = gcache.New()
|
||
}
|
||
|
||
// AddCheckStatusCache 添加扫码状态
|
||
func AddCheckStatusCache(queryKey string, val *baseinfo.CheckLoginQrCodeResult) {
|
||
_ = PublishSyncMsgCheckLogin(queryKey, val)
|
||
|
||
isContains := cache.Contains(queryKey)
|
||
if !isContains {
|
||
cache.Set(queryKey, val, time.Second*200)
|
||
// 当扫码状态为2(扫码成功)时,持久化保存状态
|
||
if val.GetState() == 2 {
|
||
SaveLastLoginStatus(queryKey, val)
|
||
}
|
||
return
|
||
} else {
|
||
// 不要先删除再添加,这可能导致竞态条件
|
||
// 先设置新的,再删除旧的(如果需要)
|
||
tempVal := cache.Get(queryKey)
|
||
cache.Set(queryKey, val, time.Second*200)
|
||
|
||
// 当扫码状态为2(扫码成功)时,持久化保存状态
|
||
if val.GetState() == 2 {
|
||
SaveLastLoginStatus(queryKey, val)
|
||
}
|
||
|
||
// 如果旧值和新值相同,不需要进一步处理
|
||
if tempVal == val {
|
||
return
|
||
}
|
||
}
|
||
|
||
if val.Ret == -3003 {
|
||
cache.Set(queryKey, val, time.Second*1000)
|
||
return
|
||
}
|
||
//扫码时间到
|
||
if val.GetEffectiveTime() == 0 {
|
||
//checkStatusCache.Remove(queryKey)
|
||
return
|
||
}
|
||
cache.Set(queryKey, val, time.Second*time.Duration(val.GetEffectiveTime()))
|
||
}
|
||
|
||
// GetCheckStatusCache 获取扫码状态
|
||
func GetCheckStatusCache(queryKey string) *baseinfo.CheckLoginQrCodeResult {
|
||
isContains := cache.Contains(queryKey)
|
||
if !isContains {
|
||
return nil
|
||
}
|
||
return cache.Get(queryKey).(*baseinfo.CheckLoginQrCodeResult)
|
||
}
|
||
|
||
// ClearCheckStatusCache 清除指定queryKey的状态缓存
|
||
func ClearCheckStatusCache(queryKey string) {
|
||
cache.Remove(queryKey)
|
||
}
|
||
|
||
// 获取
|
||
func GetCaChe() *gcache.Cache {
|
||
return cache
|
||
}
|
||
|
||
// SaveLastLoginStatus 持久化保存最后的登录状态
|
||
func SaveLastLoginStatus(queryKey string, val *baseinfo.CheckLoginQrCodeResult) {
|
||
// 使用更长的过期时间,确保在整个登录过程中状态可用
|
||
lastLoginStatusCache.Set(queryKey, val, time.Hour*24)
|
||
}
|
||
|
||
// GetLastLoginStatus 获取最后保存的登录状态
|
||
func GetLastLoginStatus(queryKey string) *baseinfo.CheckLoginQrCodeResult {
|
||
isContains := lastLoginStatusCache.Contains(queryKey)
|
||
if !isContains {
|
||
return nil
|
||
}
|
||
return lastLoginStatusCache.Get(queryKey).(*baseinfo.CheckLoginQrCodeResult)
|
||
}
|
||
|
||
// SaveShortLinkStatus 保存短链接状态
|
||
func SaveShortLinkStatus(queryKey string, isEnabled bool) {
|
||
status := &ShortLinkStatus{
|
||
IsEnabled: isEnabled,
|
||
}
|
||
// 使用长期缓存,确保在服务重启后仍能恢复短连接状态
|
||
shortLinkStatusCache.Set(queryKey, status, time.Hour*24*30) // 保存30天
|
||
}
|
||
|
||
// GetShortLinkStatus 获取短链接状态
|
||
func GetShortLinkStatus(queryKey string) *ShortLinkStatus {
|
||
isContains := shortLinkStatusCache.Contains(queryKey)
|
||
if !isContains {
|
||
return nil
|
||
}
|
||
return shortLinkStatusCache.Get(queryKey).(*ShortLinkStatus)
|
||
}
|