2025-08-10 02:11:58 +08:00
|
|
|
|
package user
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
|
"fmt"
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
|
|
"toutoukan/init/redisInit"
|
2025-09-15 11:09:22 +08:00
|
|
|
|
"toutoukan/utill/jwt"
|
2025-08-10 02:11:58 +08:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
// 登出功能(从Redis删除令牌)
|
|
|
|
|
|
func LogoutHandler(c *gin.Context) {
|
2025-09-15 11:09:22 +08:00
|
|
|
|
tokenString := jwt.ExtractTokenFromHeader(c)
|
2025-08-10 02:11:58 +08:00
|
|
|
|
if tokenString == "" {
|
|
|
|
|
|
c.JSON(400, gin.H{"error": "令牌不存在", "code": "10038"})
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
redisKey := fmt.Sprintf("jwt:%s", tokenString)
|
|
|
|
|
|
err := redisInit.RedisClient.Del(redisInit.Ctx, redisKey).Err()
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
c.JSON(500, gin.H{"error": "登出失败", "code": "10039"})
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
c.JSON(200, gin.H{"code": "20002", "message": "登出成功"})
|
|
|
|
|
|
}
|