新增退出登陆,新增jwt中int64类型错误问题
This commit is contained in:
@@ -10,6 +10,8 @@ import (
|
||||
"hospital-admin-api/global"
|
||||
"hospital-admin-api/utils"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
type User struct{}
|
||||
@@ -226,3 +228,39 @@ func (r *User) PutUserPassword(c *gin.Context) {
|
||||
}
|
||||
responses.Ok(c)
|
||||
}
|
||||
|
||||
// LoginOut 退出登陆
|
||||
func (b *User) LoginOut(c *gin.Context) {
|
||||
userId := c.GetInt64("UserId")
|
||||
if userId == 0 {
|
||||
responses.FailWithMessage("用户错误", c)
|
||||
return
|
||||
}
|
||||
|
||||
// 获取用户信息
|
||||
adminUserDao := dao.AdminUserDao{}
|
||||
adminUser, err := adminUserDao.GetAdminUserFirstById(userId)
|
||||
if err != nil || adminUser == nil {
|
||||
responses.FailWithMessage("用户数据错误", c)
|
||||
return
|
||||
}
|
||||
|
||||
// token加入黑名单
|
||||
authorization := c.Request.Header.Get("Authorization")
|
||||
if authorization == "" || !strings.HasPrefix(authorization, "Bearer ") {
|
||||
responses.FailWithMessage("退出登陆失败", c)
|
||||
return
|
||||
}
|
||||
|
||||
// 去除Bearer
|
||||
authorization = authorization[7:] // 截取字符
|
||||
|
||||
// 增加缓存
|
||||
_, err = global.Redis.Set(c, "jwt_black_"+authorization, time.Now().Unix(), 60*time.Second).Result()
|
||||
if err != nil {
|
||||
responses.FailWithMessage("退出登陆失败", c)
|
||||
return
|
||||
}
|
||||
|
||||
responses.Ok(c)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user