middle.go 1.5 KB
package middleware

import (
	"crypto/sha256"
	"encoding/hex"
	"fmt"
	"oppmg/common/log"
	"oppmg/protocol"
	"strings"

	"github.com/astaxie/beego/context"
)

//LogRequestData Before Router
var LogRequestData = func(ctx *context.Context) {
	log.Info("====>Recv Request:%s", ctx.Input.URI())
	if ctx.Input.RequestBody != nil {
		log.Info("====>Recv data from client:\nHeadData: %s \nBodyData: %s", ctx.Request.Header, string(ctx.Input.RequestBody))
	} else {
		log.Info("====>Recv data from client:\nHeadData: %s ", ctx.Request.Header)
	}
}

//CheckSign Before Router
var CheckSign = func(ctx *context.Context) {
	var (
		headTimeStamp   string
		headUuid        string
		headAccessToken string
		headSign        string
		signHex         string
	)
	headTimeStamp = ctx.Input.Header(protocol.HeaderTimestamp)
	headUuid = ctx.Input.Header(protocol.HeaderUUID)
	headSign = ctx.Input.Header(protocol.HeaderSign)
	setsign := fmt.Sprintf("v!(MmM%v%v%vMmM)i^", headTimeStamp, headUuid, headAccessToken)
	sha256 := sha256.New()
	sha256.Write([]byte(setsign))
	signHex = hex.EncodeToString(sha256.Sum(nil))
	if strings.Compare(signHex, headSign) != 0 {
		msg := protocol.BadRequestParam("113")
		ctx.Output.JSON(msg, false, false)
		return
	}
	return
}

//AuthToken Before Router
var AuthToken = func(ctx *context.Context) {
	log.Debug("执行中间件AuthToken")

	// ctx.Output.Body([]byte("{}"))
	return
}

//AppSwitch  使用外部变量停止处理新的请求
var AppSwitch = func(ctx *context.Context) {

	return
}