zwpanel/service/api/api_v1/middleware/LoginInterceptor.go
2023-11-08 21:53:07 +08:00

79 lines
2.0 KiB
Go
Raw 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 middleware
import (
"sun-panel/api/api_v1/common/apiReturn"
"sun-panel/global"
"sun-panel/models"
"github.com/gin-gonic/gin"
)
func LoginInterceptor(c *gin.Context) {
// 继续执行后续的操作,再回来
// c.Next()
// 获得token
cToken := c.GetHeader("token")
// 没有token信息视为未登录
if cToken == "" {
apiReturn.ErrorCode(c, 1000, global.Lang.Get("login.err_not_login"), nil)
c.Abort() // 终止执行后续的操作一般配合return使用
return
}
token := ""
{
var ok bool
token, ok = global.CUserToken.Get(cToken)
// 可能已经安全退出或者很久没有使用已过期
if !ok || token == "" {
apiReturn.ErrorCode(c, 1001, global.Lang.Get("login.err_not_login"), nil)
c.Abort() // 终止执行后续的操作一般配合return使用
return
}
}
// 直接返回缓存的用户信息
if userInfo, success := global.UserToken.Get(token); success {
c.Set("userInfo", userInfo)
return
}
global.Logger.Debug("准备查询数据库的用户资料", token)
mUser := models.User{}
// 去库中查询是否存在该用户;否则返回错误
if info, err := mUser.GetUserInfoByToken(token); err != nil || info.Token == "" || info.ID == 0 {
apiReturn.ErrorCode(c, 1001, global.Lang.Get("login.err_token_expire"), nil)
c.Abort()
return
} else {
// 通过 设置当前用户信息
global.UserToken.SetDefault(info.Token, info)
global.CUserToken.SetDefault(cToken, token)
c.Set("userInfo", info)
}
}
// 不验证缓存直接验证库省去没有缓存每次都要手动登录的问题
func LoginInterceptorDev(c *gin.Context) {
// 获得token
token := c.GetHeader("token")
mUser := models.User{}
// 去库中查询是否存在该用户;否则返回错误
if info, err := mUser.GetUserInfoByToken(token); err != nil || info.ID == 0 {
apiReturn.ErrorCode(c, 1001, global.Lang.Get("login.err_token_expire"), nil)
c.Abort()
return
} else {
// 通过
// 设置当前用户信息
c.Set("userInfo", info)
}
}