base_controller.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. package controllers
  2. import (
  3. "encoding/json"
  4. "errors"
  5. logger "eta/eta_mini_ht_api/common/component/log"
  6. "eta/eta_mini_ht_api/common/exception"
  7. "eta/eta_mini_ht_api/common/http"
  8. "github.com/beego/beego/v2/server/web"
  9. "net/url"
  10. )
  11. type WrapData struct {
  12. Msg string
  13. Data interface{}
  14. }
  15. type BaseResponse struct {
  16. Ret int `description:"返回状态码"`
  17. Msg string
  18. ErrMsg string
  19. ErrCode int
  20. Data interface{} `json:"data,omitempty"`
  21. Success bool `description:"true 执行成功,false 执行失败"`
  22. }
  23. type BaseController struct {
  24. web.Controller
  25. }
  26. func (b *BaseController) FailResponse(errInfo error, msg string) {
  27. var retData BaseResponse
  28. var etaError *exception.EtaError
  29. if !errors.As(errInfo, &etaError) {
  30. etaError = exception.New(exception.UnknownError)
  31. }
  32. retData = BaseResponse{
  33. Ret: 200,
  34. Msg: msg,
  35. ErrMsg: etaError.ErrorMsg,
  36. ErrCode: etaError.ErrorCode,
  37. Data: nil}
  38. b.Data["json"] = retData
  39. b.ServeJSON()
  40. }
  41. // JsonResult /*
  42. func (b *BaseController) JsonResult(status int, errCode int, errMsg string, msg string, success bool, data interface{}) {
  43. retData := BaseResponse{
  44. Ret: status,
  45. Msg: msg,
  46. ErrMsg: errMsg,
  47. ErrCode: errCode,
  48. Data: data,
  49. Success: success}
  50. b.Ctx.Output.SetStatus(status)
  51. b.Data["json"] = retData
  52. b.ServeJSON()
  53. }
  54. func (b *BaseController) GetPostParams(data interface{}) {
  55. err := json.Unmarshal(b.Ctx.Input.RequestBody, data)
  56. if err != nil {
  57. logger.Error("解析请求参数失败:%v", err)
  58. data = nil
  59. }
  60. }
  61. // Wrap ControllerWrap 是一个用于封装控制器方法的函数
  62. func Wrap(a *BaseController, fn func() (*WrapData, error)) {
  63. result, err := fn()
  64. if err != nil {
  65. logger.Error("%v", err)
  66. a.FailResponse(err, result.Msg)
  67. return
  68. }
  69. a.JsonResult(http.GetHttpStatusByAlias("ok"), http.ErrOK, "", result.Msg, http.Success, result.Data)
  70. }
  71. func (b *BaseController) InitWrapData(msg string) *WrapData {
  72. return &WrapData{Msg: msg}
  73. }
  74. func (b *BaseController) SuccessResult(msg string, data interface{}, wrapData *WrapData) {
  75. wrapData.Msg = msg
  76. wrapData.Data = data
  77. }
  78. func (b *BaseController) FailedResult(msg string, wrapData *WrapData) {
  79. wrapData.Msg = msg
  80. }
  81. func (b *BaseController) Prepare() {
  82. var requestBody string
  83. uri := b.Ctx.Input.URI()
  84. method := b.Ctx.Input.Method()
  85. if method == "GET" {
  86. requestBody = b.Ctx.Request.RequestURI
  87. } else {
  88. requestBody, _ = url.QueryUnescape(string(b.Ctx.Input.RequestBody))
  89. }
  90. ip := b.Ctx.Input.IP()
  91. b.Ctx.Input.URL()
  92. logger.Info("apiRequest:[uri:%s, requestBody:%s, ip:%s]", uri, requestBody, ip)
  93. }
  94. func (b *BaseController) Finish() {
  95. runMode := web.BConfig.RunMode
  96. if b.Data["json"] == nil {
  97. logger.Warn("apiRequest:[异常提醒:%v 接口:URI:%v;无返回值]", runMode, b.Ctx.Input.URI())
  98. return
  99. }
  100. baseRes := b.Data["json"].(BaseResponse)
  101. if !baseRes.Success {
  102. logger.Info("apiRequest:[异常提醒:%v接口:URI:%v;ErrMsg:&v;Msg:%v]", b.Ctx.Input.URI(), baseRes.ErrMsg, baseRes.Msg)
  103. } else {
  104. if baseRes.Data == nil {
  105. logger.Warn("apiRequest:[异常提醒:%v 接口:URI:%v;无返回值]", runMode, b.Ctx.Input.URI())
  106. return
  107. } else {
  108. logger.Info("apiRequest:[uri:%s, resData:%s, ip:%s]", b.Ctx.Input.URI(), baseRes.Data)
  109. }
  110. }
  111. }
  112. type RequestInfo struct {
  113. Uri string `json:"uri"`
  114. IpAddress string `json:"ip_address"`
  115. Method string `json:"method"`
  116. Params string `json:"params"`
  117. }