|
|
package api |
|
|
|
|
|
import ( |
|
|
"augment2api/config" |
|
|
"math/rand" |
|
|
"net/http" |
|
|
|
|
|
"github.com/gin-gonic/gin" |
|
|
) |
|
|
|
|
|
|
|
|
type TokenInfo struct { |
|
|
Token string `json:"token"` |
|
|
TenantURL string `json:"tenant_url"` |
|
|
} |
|
|
|
|
|
|
|
|
func GetRedisTokenHandler(c *gin.Context) { |
|
|
|
|
|
keys, err := config.RedisKeys("token:*") |
|
|
if err != nil { |
|
|
c.JSON(http.StatusOK, gin.H{ |
|
|
"status": "error", |
|
|
"error": "获取token列表失败: " + err.Error(), |
|
|
}) |
|
|
return |
|
|
} |
|
|
|
|
|
|
|
|
if len(keys) == 0 { |
|
|
c.JSON(http.StatusOK, gin.H{ |
|
|
"status": "success", |
|
|
"tokens": []TokenInfo{}, |
|
|
}) |
|
|
return |
|
|
} |
|
|
|
|
|
|
|
|
var tokenList []TokenInfo |
|
|
for _, key := range keys { |
|
|
|
|
|
token := key[6:] |
|
|
|
|
|
|
|
|
tenantURL, err := config.RedisHGet(key, "tenant_url") |
|
|
if err != nil { |
|
|
continue |
|
|
} |
|
|
|
|
|
tokenList = append(tokenList, TokenInfo{ |
|
|
Token: token, |
|
|
TenantURL: tenantURL, |
|
|
}) |
|
|
} |
|
|
|
|
|
c.JSON(http.StatusOK, gin.H{ |
|
|
"status": "success", |
|
|
"tokens": tokenList, |
|
|
}) |
|
|
} |
|
|
|
|
|
|
|
|
func SaveTokenToRedis(token, tenantURL string) error { |
|
|
|
|
|
tokenKey := "token:" + token |
|
|
|
|
|
|
|
|
return config.RedisHSet(tokenKey, "tenant_url", tenantURL) |
|
|
} |
|
|
|
|
|
|
|
|
func GetRandomToken() (string, string) { |
|
|
|
|
|
keys, err := config.RedisKeys("token:*") |
|
|
if err != nil || len(keys) == 0 { |
|
|
return "", "" |
|
|
} |
|
|
|
|
|
|
|
|
randomIndex := rand.Intn(len(keys)) |
|
|
randomKey := keys[randomIndex] |
|
|
|
|
|
|
|
|
token := randomKey[6:] |
|
|
|
|
|
|
|
|
tenantURL, err := config.RedisHGet(randomKey, "tenant_url") |
|
|
if err != nil { |
|
|
return "", "" |
|
|
} |
|
|
|
|
|
return token, tenantURL |
|
|
} |
|
|
|
|
|
|
|
|
func DeleteTokenHandler(c *gin.Context) { |
|
|
token := c.Param("token") |
|
|
if token == "" { |
|
|
c.JSON(http.StatusBadRequest, gin.H{ |
|
|
"status": "error", |
|
|
"error": "未指定token", |
|
|
}) |
|
|
return |
|
|
} |
|
|
|
|
|
tokenKey := "token:" + token |
|
|
|
|
|
|
|
|
exists, err := config.RedisExists(tokenKey) |
|
|
if err != nil { |
|
|
c.JSON(http.StatusInternalServerError, gin.H{ |
|
|
"status": "error", |
|
|
"error": "检查token失败: " + err.Error(), |
|
|
}) |
|
|
return |
|
|
} |
|
|
|
|
|
if !exists { |
|
|
c.JSON(http.StatusNotFound, gin.H{ |
|
|
"status": "error", |
|
|
"error": "token不存在", |
|
|
}) |
|
|
return |
|
|
} |
|
|
|
|
|
|
|
|
if err := config.RedisDel(tokenKey); err != nil { |
|
|
c.JSON(http.StatusInternalServerError, gin.H{ |
|
|
"status": "error", |
|
|
"error": "删除token失败: " + err.Error(), |
|
|
}) |
|
|
return |
|
|
} |
|
|
|
|
|
c.JSON(http.StatusOK, gin.H{ |
|
|
"status": "success", |
|
|
}) |
|
|
} |
|
|
|
|
|
|
|
|
func UseTokenHandler(c *gin.Context) { |
|
|
token := c.Param("token") |
|
|
if token == "" { |
|
|
c.JSON(http.StatusBadRequest, gin.H{ |
|
|
"status": "error", |
|
|
"error": "未指定token", |
|
|
}) |
|
|
return |
|
|
} |
|
|
|
|
|
tokenKey := "token:" + token |
|
|
|
|
|
|
|
|
exists, err := config.RedisExists(tokenKey) |
|
|
if err != nil { |
|
|
c.JSON(http.StatusInternalServerError, gin.H{ |
|
|
"status": "error", |
|
|
"error": "检查token失败: " + err.Error(), |
|
|
}) |
|
|
return |
|
|
} |
|
|
|
|
|
if !exists { |
|
|
c.JSON(http.StatusNotFound, gin.H{ |
|
|
"status": "error", |
|
|
"error": "token不存在", |
|
|
}) |
|
|
return |
|
|
} |
|
|
|
|
|
|
|
|
if err := config.RedisSet("current_token", token, 0); err != nil { |
|
|
c.JSON(http.StatusInternalServerError, gin.H{ |
|
|
"status": "error", |
|
|
"error": "设置当前token失败: " + err.Error(), |
|
|
}) |
|
|
return |
|
|
} |
|
|
|
|
|
c.JSON(http.StatusOK, gin.H{ |
|
|
"status": "success", |
|
|
}) |
|
|
} |
|
|
|