check_admin_token.go
1016 字节
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package middlewares
import (
"github.com/beego/beego/v2/server/web/context"
"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/constant"
"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/domain"
)
func GetUser(ctx *context.Context) *domain.UserAuth {
user := ctx.Input.GetData(domain.UserAuth{})
if user != nil {
return user.(*domain.UserAuth)
}
return nil
}
func CheckAdminToken() func(ctx *context.Context) {
return func(ctx *context.Context) {
adminToken := ctx.Input.Header("x-admin-token")
userAuth, err := (&domain.UserAuth{}).ParseAccessToken(adminToken)
if err != nil || userAuth.UserId <= 0 {
forbidden(ctx)
return
}
if userAuth.PlatformId != constant.PLATFORM_ADMIN_ID {
forbidden(ctx)
return
}
ctx.Input.SetData(domain.UserAuth{}, userAuth)
}
}
func forbidden(ctx *context.Context) {
resp := map[string]interface{}{
"code": 902,
"msg": "Authorization过期或无效,需要进行重新获取令牌",
}
_ = ctx.Output.JSON(resp, false, false)
}