base_controller.go 2.9 KB
package controllers

import (
	"github.com/linmadan/egglib-go/core/application"
	"github.com/linmadan/egglib-go/utils/json"
	"github.com/linmadan/egglib-go/web/beego"
	"github.com/linmadan/egglib-go/web/beego/utils"
	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/domain"
	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/log"
	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/port/beego/middleware"
)

type BaseController struct {
	beego.BaseController
}

func (controller *BaseController) ReturnPageListData(count int64, data interface{}, err error, pageNumber int) {
	dataMap := map[string]interface{}{
		"grid": map[string]interface{}{
			"total": count,
			//"pageNumber": pageNumber,
			"list": data,
		},
	}
	controller.Response(dataMap, err)
}

func (controller *BaseController) ReturnListData(count int64, data interface{}, err error) {
	dataMap := map[string]interface{}{
		"total": count,
		//"pageNumber": pageNumber,
		"list": data,
	}
	controller.Response(dataMap, err)
}

func (controller *BaseController) Response(data interface{}, err error) {
	var response utils.JsonResponse
	if err != nil {
		if _, ok := err.(*application.ServiceError); !ok {
			controller.Data["json"] = map[string]interface{}{
				"code": 1,
				"msg":  err.Error(),
			}
			log.Logger.Error(err.Error())
			controller.ServeJSON()
			return
		}
	}
	if err != nil {
		response = utils.ResponseError(controller.Ctx, err)
	} else {
		response = utils.ResponseData(controller.Ctx, data)
	}
	controller.Data["json"] = response
	controller.ServeJSON()
}

func (controller *BaseController) GetUserId() int64 {
	return 1
}

func (controller *BaseController) GetCompanyId() int64 {
	return 1
}

func (controller *BaseController) GetUserBaseId() int64 {
	return 1
}

func (controller *BaseController) GetOrgId() int64 {
	return 1
}

func (controller *BaseController) GetOperator() domain.Operator {
	token := controller.Ctx.Input.Header("X-Mmm-Accesstoken")
	loginToken := &domain.LoginToken{}
	err := loginToken.ParseToken(token)
	if err != nil {
		log.Logger.Error(err.Error())
		*loginToken, _ = middleware.FormCtxLoginToken(controller.Ctx)
	}
	if tmpToken, ok := middleware.FormCtxLoginToken(controller.Ctx); ok {
		log.Logger.Debug(json.MarshalToString(tmpToken))
	}
	op := domain.Operator{
		UserId:     loginToken.UserId,
		CompanyId:  loginToken.CompanyId,
		OrgId:      loginToken.OrgId,
		UserBaseId: loginToken.UserBaseId,
		Phone:      loginToken.Account,
		Token:      token,
		OrgIds:     loginToken.OrgIds,
	}

	// TODO:打印测试日志
	log.Logger.Debug("operator " + json.MarshalToString(op))
	return op
}

func (controller *BaseController) GetDeviceType() string {
	return controller.Ctx.Input.Header("x-mmm-devicetype")
}

func (controller *BaseController) BodyKeys() map[string]interface{} {
	var bodyKV map[string]interface{}
	controller.Unmarshal(&bodyKV)
	return bodyKV
}

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