check_admin_token.go 847 字节
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 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)
}