2024-06-17 14:18:39 +08:00
|
|
|
package middlewares
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
"github.com/qit-team/snow-core/redis"
|
|
|
|
"qteam/app/constants/common"
|
|
|
|
"qteam/app/constants/errorcode"
|
|
|
|
"qteam/app/http/controllers"
|
|
|
|
"qteam/app/http/requestmapping"
|
|
|
|
"qteam/app/utils"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
func Auth() gin.HandlerFunc {
|
|
|
|
return func(c *gin.Context) {
|
|
|
|
c.ClientIP()
|
|
|
|
var tokens = strings.SplitN(c.GetHeader("Authorization"), " ", 2)
|
2024-07-11 17:44:41 +08:00
|
|
|
utils.Log(c, "tokens:", tokens)
|
2024-06-17 14:18:39 +08:00
|
|
|
if len(tokens) != 2 || tokens[0] != "Bearer" {
|
|
|
|
controllers.HandCodeRes(c, nil, errorcode.NotLogin)
|
|
|
|
c.Abort()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// 验证token
|
|
|
|
token, claims, err := utils.ParseToken(tokens[1])
|
|
|
|
if err != nil || !token.Valid {
|
|
|
|
controllers.HandCodeRes(c, nil, errorcode.NotAuth)
|
|
|
|
c.Abort()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if err == nil {
|
|
|
|
c.Set("userId", claims.Id)
|
2024-06-25 13:51:22 +08:00
|
|
|
c.Set("custNo", claims.CustNo)
|
2024-06-17 14:18:39 +08:00
|
|
|
c.Next()
|
|
|
|
return
|
|
|
|
} else {
|
|
|
|
controllers.HandCodeRes(c, nil, errorcode.NotAuth)
|
|
|
|
c.Abort()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func Cors() gin.HandlerFunc {
|
|
|
|
return func(c *gin.Context) {
|
2024-08-06 10:06:51 +08:00
|
|
|
c.Writer.Header().Set("Access-Control-Allow-Origin", "milk.h5.cdlsxd.cn")
|
2024-06-17 14:18:39 +08:00
|
|
|
c.Writer.Header().Set("Access-Control-Allow-Credentials", "true")
|
|
|
|
c.Writer.Header().Set("Access-Control-Allow-Headers", "Content-Type, Content-Length, platform,Accept-Encoding, X-CSRF-Token, Authorization, accept, origin, Cache-Control,token, X-Requested-With")
|
|
|
|
c.Writer.Header().Set("Access-Control-Allow-Methods", "POST, OPTIONS, GET, PUT, DELETE")
|
|
|
|
|
|
|
|
if c.Request.Method == "OPTIONS" {
|
|
|
|
c.AbortWithStatus(204)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
c.Next()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func AdminAuth() gin.HandlerFunc {
|
|
|
|
return func(c *gin.Context) {
|
|
|
|
var token = c.GetHeader("token")
|
|
|
|
//将token放入redis
|
|
|
|
var playerId, err = redis.GetRedis().Get(context.Background(), utils.GetRealKey(common.TOKEN_Admin+token)).Result()
|
|
|
|
if rs, errRedis := redis.GetRedis().SIsMember(context.Background(), "disabled_uids", playerId).Result(); errRedis == nil && rs {
|
|
|
|
err = errors.New(errorcode.GetMsg(errorcode.NotFound, ""))
|
|
|
|
redis.GetRedis().SRem(context.Background(), "disabled_uids", playerId)
|
|
|
|
}
|
|
|
|
if err == nil {
|
|
|
|
c.Set("playerId", playerId)
|
|
|
|
c.Next()
|
|
|
|
return
|
|
|
|
} else {
|
|
|
|
controllers.HandCodeRes(c, nil, errorcode.Forbidden)
|
|
|
|
c.Abort()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func ValidateRequest() gin.HandlerFunc {
|
|
|
|
return func(c *gin.Context) {
|
|
|
|
var path = c.FullPath()
|
|
|
|
var handler func() interface{}
|
|
|
|
if strings.Index(path, "admin") >= 0 {
|
|
|
|
handler = requestmapping.BackendRequestMap[path]
|
|
|
|
} else {
|
|
|
|
handler = requestmapping.FrontRequestMap[path]
|
|
|
|
}
|
|
|
|
if handler == nil {
|
|
|
|
utils.Log(c, "path", path)
|
|
|
|
controllers.HandCodeRes(c, nil, errorcode.NotFound)
|
|
|
|
} else {
|
|
|
|
v := handler()
|
|
|
|
msg, err := controllers.GenRequest(c, v)
|
|
|
|
if err != nil {
|
|
|
|
utils.Log(c, "path", path)
|
|
|
|
controllers.Error(c, errorcode.ParamError, msg...)
|
|
|
|
} else {
|
|
|
|
c.Set("request", v)
|
|
|
|
c.Next()
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|