base_common.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. package controllers
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "github.com/beego/beego/v2/server/web"
  6. "hongze/hongze_open_api/services"
  7. "hongze/hongze_open_api/utils"
  8. "net/http"
  9. "net/url"
  10. )
  11. //不需要授权的基类
  12. type BaseCommon struct {
  13. web.Controller
  14. Response
  15. }
  16. type Response struct {
  17. Code int `json:"code"`
  18. Data interface{} `json:"data"`
  19. Msg string `json:"msg"`
  20. ErrMsg string `json:"err_msg"`
  21. }
  22. const (
  23. SUCCESS = 200 //成功
  24. ERROR = 400 //代表业务处理失败,前端同学需要做额外逻辑处理
  25. SIGN_ERROR = 401 //签名异常
  26. )
  27. //返回数据
  28. func (c BaseCommon) Result() {
  29. var content []byte
  30. var err error
  31. content, err = json.Marshal(c.Response)
  32. ip := c.Ctx.Input.IP()
  33. requestBody, err := url.QueryUnescape(string(c.Ctx.Input.RequestBody))
  34. if err != nil {
  35. fmt.Println("base auth url.QueryUnescape Err:", err.Error())
  36. requestBody = string(c.Ctx.Input.RequestBody)
  37. }
  38. utils.ApiLog.Println("请求地址:", c.Ctx.Input.URI(), "RequestBody:", requestBody, "ResponseBody", string(content), "IP:", ip)
  39. //不将errMsg暴露给用户
  40. c.Response.ErrMsg = c.Response.Msg
  41. //c.Controller.Data["json"] = c.Response
  42. //c.Controller.ServeJSON()
  43. // 将处理后的数据返回给前端
  44. content, err = json.Marshal(c.Response)
  45. if err != nil {
  46. http.Error(c.Ctx.Output.Context.ResponseWriter, err.Error(), http.StatusInternalServerError)
  47. return
  48. }
  49. //content = []byte(utils.StringsToJSON(string(content)))
  50. // 数据加密
  51. if services.CheckEncryption(c.Ctx.Request.URL.Path) {
  52. content = utils.DesBase64EncryptV2(content, utils.REPORT_KEY)
  53. }
  54. c.Ctx.Output.Header("Content-Type", "application/json; charset=utf-8")
  55. c.Ctx.Output.Body(content)
  56. c.StopRun()
  57. }
  58. //没有任何信息的返回
  59. func (c BaseCommon) Ok() {
  60. c.Response.Code = SUCCESS
  61. c.Response.Msg = "操作成功"
  62. c.Response.Data = map[string]interface{}{}
  63. c.Result()
  64. }
  65. func (c BaseCommon) OkWithMessage(message string) {
  66. c.Response.Code = SUCCESS
  67. c.Response.Msg = message
  68. c.Response.Data = map[string]interface{}{}
  69. c.Result()
  70. }
  71. func (c BaseCommon) OkWithData(data interface{}) {
  72. c.Response.Code = SUCCESS
  73. c.Response.Msg = "操作成功"
  74. c.Response.Data = data
  75. c.Result()
  76. }
  77. func (c BaseCommon) OkDetailed(data interface{}, message string) {
  78. c.Response.Code = SUCCESS
  79. c.Response.Msg = message
  80. c.Response.Data = data
  81. c.Result()
  82. }
  83. func (c BaseCommon) Fail() {
  84. c.Response.Code = ERROR
  85. c.Response.Msg = "操作失败"
  86. c.Response.Data = map[string]interface{}{}
  87. c.Result()
  88. }
  89. func (c BaseCommon) FailWithMessage(message string) {
  90. c.Response.Code = ERROR
  91. c.Response.Msg = message
  92. c.Response.Data = map[string]interface{}{}
  93. c.Result()
  94. }
  95. func (c BaseCommon) FailWithMessageErr(message, errMsg string) {
  96. c.Response.Code = ERROR
  97. c.Response.Msg = message
  98. c.Response.ErrMsg = errMsg
  99. c.Response.Data = map[string]interface{}{}
  100. c.Result()
  101. }
  102. func (c BaseCommon) FailWithDetailed(code int, data interface{}, message string) {
  103. c.Response.Code = code
  104. c.Response.Msg = message
  105. c.Response.Data = data
  106. c.Result()
  107. }
  108. // SignError 签名异常
  109. func (c BaseCommon) SignError(message string) {
  110. c.Response.Code = SIGN_ERROR
  111. c.Response.Msg = message
  112. c.Response.Data = map[string]interface{}{}
  113. c.Result()
  114. }