check_token.go 1.4 KB
package middlewares

import (
	"github.com/beego/beego/v2/server/web/context"
)

func setUserId(userId int64, ctx *context.Context) {
	ctx.Input.SetData("_UserId", userId)
}

func GetUserId(ctx *context.Context) int64 {
	userId := ctx.Input.GetData("_UserId")
	return userId.(int64)
}

func setCompanyId(companyId int64, ctx *context.Context) {
	ctx.Input.SetData("_CompanyId", companyId)
}

func GetCompanyId(ctx *context.Context) int64 {
	companyId := ctx.Input.GetData("_CompanyId")
	return companyId.(int64)
}

func setCompanyType(companyId int, ctx *context.Context) {
	ctx.Input.SetData("_CompanyType", companyId)
}

func GetCompanyType(ctx *context.Context) int {
	companyId := ctx.Input.GetData("_CompanyType")
	return companyId.(int)
}

func invalidOrExpired(ctx *context.Context) {
	resp := map[string]interface{}{
		"code": 902,
		"msg":  "Authorization过期或无效,需要进行重新获取令牌",
	}
	_ = ctx.Output.JSON(resp, false, false)
}

func CheckToken() func(ctx *context.Context) {
	return func(ctx *context.Context) {
		tokenStr := ctx.Input.Header("x-mmm-accesstoken")
		if tokenStr == "" { //没有带token
			invalidOrExpired(ctx)
			return
		}

		//userServe := service.UserService{}
		//userTk, err := userServe.ValidLoginToken(tokenStr)
		//if err != nil {
		//	invalidOrExpired(ctx)
		//	return
		//}
		//setUserId(userTk.UserId, ctx)
		//setCompanyId(userTk.CompanyId, ctx)
		//setCompanyType(userTk.CompanyType, ctx)
	}
}