admin_backend/store/token.go
2025-12-12 15:31:36 +08:00

63 lines
1.3 KiB
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package store
import (
"backend/util"
)
var TokenList = make(map[string]*Token)
type Token struct {
Token string `json:"token"`
Expires int64 `json:"expires"`
UserName string `json:"username"`
Role string `json:"role"` // 添加角色字段
}
func AddToken(token string, expires int64, username string, role string) {
TokenList[token] = &Token{
Token: token,
Expires: expires,
UserName: username,
Role: role, // 设置角色
}
}
func GetTokenInfo(token string) (*Token, bool) {
if tokenInfo, exists := TokenList[token]; exists {
if tokenInfo.Expires > util.Now() {
return tokenInfo, true
}
delete(TokenList, token) // 如果过期了删除token
}
return nil, false
}
func RemoveToken(token string) {
delete(TokenList, token)
}
func IsTokenValid(token string) bool {
if tokenInfo, exists := TokenList[token]; exists {
if tokenInfo.Expires > util.Now() {
return true
}
delete(TokenList, token) // 如果过期了删除token
}
return false
}
var CodeList = make(map[string]string)
func AddCode(phone, code string) {
CodeList[phone] = code
}
func GetCode(phone string) (string, bool) {
code, exists := CodeList[phone]
if exists {
delete(CodeList, phone) // 使用后删除
return code, true
}
return "", false
}