63 lines
1.3 KiB
Go
63 lines
1.3 KiB
Go
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
|
||
}
|