base_controller.go 3.9 KB

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