package controllers

import (
	"github.com/astaxie/beego/validation"
	"github.com/gin-gonic/gin"
	"github.com/tiptok/gocomm/pkg/mygin"
	"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/infrastructure/utils"
	"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/protocol"
	"strconv"
)

type BaseController struct {
	mygin.BaseController
}

func (this *BaseController) Resp(c *gin.Context, httpCode int, rsp *protocol.ResponseMessage) {
	c.Set("outputData", rsp)
	c.JSON(httpCode, rsp)
	//c.Abort()
}

func (controller *BaseController) Valid(obj interface{}) (result bool, msg *protocol.ResponseMessage) {
	/*校验*/
	var err error
	valid := validation.Validation{}
	result, err = valid.Valid(obj)
	if err != nil {
	}
	if !result {
		msg = protocol.BadRequestParam(2)
		return
	}

	return
}

//获取请求头信息
func (this *BaseController) GetRequestHeader(c *gin.Context) *protocol.RequestHeader {
	h := &protocol.RequestHeader{}

	h.AccessToken = c.Query("x-mmm-accesstoken")
	h.AppProject = c.Query("x-mmm-appproject")
	h.DeviceType, _ = strconv.Atoi(c.Query("x-mmm-devicetype"))
	h.Sign = c.Query("x-mmm-sign")
	h.Uuid = c.Query("x-mmm-uuid")
	h.TimeStamp = c.Query("x-mmm-timestamp")
	h.Version = c.Query("x-mmm-version")
	h.UserId, _ = strconv.ParseInt(c.Query("x-mmm-id"), 10, 64)

	if len(h.AccessToken) > 0 {
		if claim, err := utils.ParseJWTToken(h.AccessToken); err == nil {
			h.UserId = claim.UserId
		}
	}
	return h
}