base_common.go 2.6 KB

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