first-commit
This commit is contained in:
52
services/wechat/qr-cache.go
Normal file
52
services/wechat/qr-cache.go
Normal file
@@ -0,0 +1,52 @@
|
||||
package wechat
|
||||
|
||||
import (
|
||||
"code.gitea.io/gitea/modules/cache"
|
||||
"code.gitea.io/gitea/modules/json"
|
||||
)
|
||||
|
||||
const (
|
||||
// KeyPrefix 缓存键前缀:微信公众号二维码 Ticket
|
||||
KeyPrefix = "wechat:official-account:qr:ticket:"
|
||||
)
|
||||
|
||||
// SetWechatQrTicketWithTTL 设置 WeChat QR Ticket 与 VO JSON 的映射关系,并设置过期时间
|
||||
func SetWechatQrTicketWithTTL(ticket, valueJSON string, ttl int) (ok bool) {
|
||||
keyStr := KeyPrefix + ticket
|
||||
|
||||
err := cache.GetCache().Put(keyStr, valueJSON, int64(ttl))
|
||||
|
||||
return err == nil
|
||||
}
|
||||
|
||||
// 获取 WeChat QR Ticket 扫码状态(只读取状态、不删除)
|
||||
func GetWechatQrStatusByTicket(ticket string) (*WechatTempQRStatus, error) {
|
||||
// 1. 将 ticket 与二维码缓存键拼接,作为 cache key 查询缓存
|
||||
keyStr := KeyPrefix + ticket
|
||||
wechatQrCacher := cache.GetCache()
|
||||
qrStatusJson, ok := wechatQrCacher.Get(keyStr)
|
||||
if !ok {
|
||||
return &WechatTempQRStatus{
|
||||
IsScanned: false,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// 2. 对缓存查询结果进行 JSON 反序列化为 VO 对象
|
||||
var qrStatus WechatTempQRStatus
|
||||
err := json.Unmarshal([]byte(qrStatusJson), &qrStatus)
|
||||
if err != nil {
|
||||
return nil, ErrorWechatTempQRStatus{
|
||||
Action: "Convert QR Status",
|
||||
Message: err.Error(),
|
||||
}
|
||||
}
|
||||
return &qrStatus, nil
|
||||
}
|
||||
|
||||
// DeleteWechatQrByTicket 通过 ticket 删除微信公众号临时二维码缓存记录
|
||||
func DeleteWechatQrByTicket(ticket string) error {
|
||||
|
||||
keyStr := KeyPrefix + ticket
|
||||
|
||||
return cache.GetCache().Delete(keyStr)
|
||||
}
|
Reference in New Issue
Block a user