check_token.go 846 字节
package middleware

import (
	"github.com/astaxie/beego/context"
	"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/infrastructure/utils"
	"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/protocol"
	"strings"
)

//检查access_token
func CheckJWTToken(ctx *context.Context) {
	var (
		msg *protocol.ResponseMessage
	)
	token := ctx.Input.Header("x-mmm-accesstoken")
	if strings.HasSuffix(ctx.Request.RequestURI, "login") ||
		strings.HasSuffix(ctx.Request.RequestURI, "accessToken") ||
		strings.HasSuffix(ctx.Request.RequestURI, "refreshToken") ||
		strings.HasSuffix(ctx.Request.RequestURI, "smsCode") {
		return
	}
	defer func() {
		if msg != nil {
			ctx.Output.JSON(msg, false, false)
		}
	}()
	if u, err := utils.ParseJWTToken(token); err != nil {
		msg = protocol.NewMesage(4141)
		return
	} else {
		ctx.Input.SetData("UserId", u.UserId)
	}
	return
}