controller.go 1.6 KB
package controllers

import (
	"github.com/beego/beego/v2/server/web/context"
	"github.com/linmadan/egglib-go/web/beego"
	"github.com/linmadan/egglib-go/web/beego/utils"
	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/constant"
	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/domain"
	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/log"
	"strconv"
)

func ResponseGrid(c beego.BaseController, data interface{}, err error) {
	var response utils.JsonResponse
	if err != nil {
		response = utils.ResponseError(c.Ctx, err)
	} else {
		response = ResponseGridData(c.Ctx, data)
	}
	c.Data["json"] = response
	c.ServeJSON()
}

func ResponseGridData(ctx *context.Context, data interface{}) utils.JsonResponse {
	jsonResponse := utils.JsonResponse{}
	jsonResponse["code"] = 0
	jsonResponse["msg"] = "ok"
	jsonResponse["data"] = map[string]interface{}{"grid": data}
	ctx.Input.SetData("outputData", jsonResponse)
	return jsonResponse
}

func Must(err error) {
	if err != nil {
		log.Logger.Error(err.Error())
	}
}

// ParseOperateInfo  从头部解析操作对象信息
func ParseOperateInfo(c beego.BaseController) *domain.OperateInfo {
	opt := &domain.OperateInfo{}
	opt.UserId = header(c, constant.HeaderUserId)
	opt.CompanyId = header(c, constant.HeaderCompanyId)
	opt.OrgId = header(c, constant.HeaderOrgId)
	return opt
}

func header(c beego.BaseController, key string) int64 {
	if len(c.Ctx.Input.Header(key)) == 0 {
		return 0
	}
	res, err := strconv.ParseInt(c.Ctx.Input.Header(key), 10, 64)
	if err != nil {
		log.Logger.Error(err.Error())
		return 0
	}
	return res
}