base.go 1.11 KB
package controllers

import (
	"apigame/service/constd"
	"apigame/sign"
	"encoding/json"
	"github.com/astaxie/beego"
)

var debugMode = true

type BaseController struct {
	beego.Controller
}

// RetData 返回封装
func (c *BaseController) RetData(resp map[string]any) {
	c.Data["json"] = resp
	c.ServeJSON()
}

// RetRspData 返回封装
func (c *BaseController) RetRspData(rspData any) {
	resp := make(map[string]any)
	resp["data"] = rspData
	c.RetData(resp)
}

// RetCode 返回错误封装
func (c *BaseController) RetCode(code string) {
	resp := make(map[string]interface{})
	resp = constd.CodeMsg(resp, code)
	c.RetData(resp)
}

// GetPostData 获取 PostData
func (c *BaseController) GetPostData(postData any) bool {

	req := c.Ctx.Input.RequestBody
	if debugMode {
		err := json.Unmarshal(req, &postData)
		if err != nil {
			c.RetCode(constd.RECODE_PARAMERROR)
			return false
		}
		return true
	}

	// 接收参数,验证签名登录
	code, err := sign.Check(req, &postData, true, true)
	if err != nil {
		resp := make(map[string]interface{})
		resp = constd.CodeMsg(resp, code)
		c.RetData(resp)
		return false
	}
	return true
}