package controllers import ( "encoding/json" "errors" logger "eta/eta_mini_ht_api/common/component/log" "eta/eta_mini_ht_api/common/exception" "eta/eta_mini_ht_api/common/http" "github.com/beego/beego/v2/server/web" "net/url" ) type WrapData struct { Msg string Data interface{} } type BaseResponse struct { Ret int `description:"返回状态码"` Msg string ErrMsg string ErrCode int Data interface{} `json:"data,omitempty"` Success bool `description:"true 执行成功,false 执行失败"` } type BaseController struct { web.Controller } func (b *BaseController) FailResponse(errInfo error, msg string) { var retData BaseResponse var etaError *exception.EtaError if !errors.As(errInfo, &etaError) { etaError = exception.New(exception.UnknownError) } retData = BaseResponse{ Ret: 200, Msg: msg, ErrMsg: etaError.ErrorMsg, ErrCode: etaError.ErrorCode, Data: nil} b.Data["json"] = retData b.ServeJSON() } // JsonResult /* func (b *BaseController) JsonResult(status int, errCode int, errMsg string, msg string, success bool, data interface{}) { retData := BaseResponse{ Ret: status, Msg: msg, ErrMsg: errMsg, ErrCode: errCode, Data: data, Success: success} b.Ctx.Output.SetStatus(status) b.Data["json"] = retData b.ServeJSON() } func (b *BaseController) GetPostParams(data interface{}) { err := json.Unmarshal(b.Ctx.Input.RequestBody, data) if err != nil { logger.Error("解析请求参数失败:%v", err) data = nil } } // Wrap ControllerWrap 是一个用于封装控制器方法的函数 func Wrap(a *BaseController, fn func() (*WrapData, error)) { result, err := fn() if err != nil { logger.Error("%v", err) a.FailResponse(err, result.Msg) return } a.JsonResult(http.GetHttpStatusByAlias("ok"), http.ErrOK, "", result.Msg, http.Success, result.Data) } func (b *BaseController) InitWrapData(msg string) *WrapData { return &WrapData{Msg: msg} } func (b *BaseController) SuccessResult(msg string, data interface{}, wrapData *WrapData) { wrapData.Msg = msg wrapData.Data = data } func (b *BaseController) FailedResult(msg string, wrapData *WrapData) { wrapData.Msg = msg } func (b *BaseController) Prepare() { var requestBody string uri := b.Ctx.Input.URI() method := b.Ctx.Input.Method() if method == "GET" { requestBody = b.Ctx.Request.RequestURI } else { requestBody, _ = url.QueryUnescape(string(b.Ctx.Input.RequestBody)) } ip := b.Ctx.Input.IP() b.Ctx.Input.URL() logger.Info("apiRequest:[uri:%s, requestBody:%s, ip:%s]", uri, requestBody, ip) } func (b *BaseController) Finish() { runMode := web.BConfig.RunMode if b.Data["json"] == nil { logger.Warn("apiRequest:[异常提醒:%v 接口:URI:%v;无返回值]", runMode, b.Ctx.Input.URI()) return } baseRes := b.Data["json"].(BaseResponse) if !baseRes.Success { logger.Info("apiRequest:[异常提醒:%v接口:URI:%v;ErrMsg:&v;Msg:%v]", b.Ctx.Input.URI(), baseRes.ErrMsg, baseRes.Msg) } else { if baseRes.Data == nil { logger.Warn("apiRequest:[异常提醒:%v 接口:URI:%v;无返回值]", runMode, b.Ctx.Input.URI()) return } else { logger.Info("apiRequest:[uri:%s, resData:%s, ip:%s]", b.Ctx.Input.URI(), baseRes.Data) } } } type RequestInfo struct { Uri string `json:"uri"` IpAddress string `json:"ip_address"` Method string `json:"method"` Params string `json:"params"` }