package mw

import (
	"applet/app/e"
	"applet/app/utils"
	"bytes"
	"errors"
	"fmt"
	"github.com/gin-gonic/gin"
	"io/ioutil"
)

// CheckSign is 中间件 用来检查签名
func CheckSign(c *gin.Context) {
	if utils.SignCheck(c) == false {
		e.OutErr(c, 400, errors.New("请求失败~~"))
		return
	}
	c.Next()
}

func CheckBody(c *gin.Context) {
	c.Set("api_version", "1")
	if utils.GetApiVersion(c) > 0 {
		body, _ := ioutil.ReadAll(c.Request.Body)
		fmt.Println("check_", c.GetString("mid"), string(body))
		if string(body) != "" {
			str := utils.ResultAesDecrypt(c, string(body))
			fmt.Println("check_de", c.GetString("mid"), str)
			if str != "" {
				c.Set("body_str", str)
				c.Request.Body = ioutil.NopCloser(bytes.NewBuffer([]byte(str)))
			}
		}
	}
	c.Next()
}
