base_common.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. package controllers
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "github.com/beego/beego/v2/server/web"
  6. "hongze/hongze_mobile_admin/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:"errMsg" description:"给开发工程师看的错误信息"`
  19. }
  20. const (
  21. SUCCESS = 200 //成功
  22. ERROR = 400 //代表业务处理失败,前端同学需要做额外逻辑处理
  23. TOKEN_ERROR = 401 //代表token异常,用户需要重新静默授权,获取最新的token
  24. BIND_ERROR = 403 //403代表用户需要进行绑定操作,需要跳转到输入账号密码绑定页面
  25. SIGN_ERROR = 3 //签名异常
  26. WX_CODE_USED_ERROR = 4011 //待表微信授权code已使用
  27. )
  28. //返回数据
  29. func (c BaseCommon) Result() {
  30. var content []byte
  31. var err error
  32. content, err = json.Marshal(c.Response)
  33. ip := c.Ctx.Input.IP()
  34. requestBody, err := url.QueryUnescape(string(c.Ctx.Input.RequestBody))
  35. if err != nil {
  36. fmt.Println("base auth url.QueryUnescape Err:", err.Error())
  37. requestBody = string(c.Ctx.Input.RequestBody)
  38. }
  39. utils.ApiLog.Println("请求地址:", c.Ctx.Input.URI(), "Authorization:", c.Ctx.Input.Header("Authorization"), "RequestBody:", requestBody, "ResponseBody", string(content), "IP:", ip)
  40. c.Controller.Data["json"] = c.Response
  41. c.Controller.ServeJSON()
  42. c.StopRun()
  43. }
  44. //没有任何信息的返回
  45. func (c BaseCommon) Ok() {
  46. c.Response.Code = SUCCESS
  47. c.Response.Msg = "操作成功"
  48. c.Response.Data = map[string]interface{}{}
  49. c.Result()
  50. }
  51. func (c BaseCommon) OkWithMessage(message string) {
  52. c.Response.Code = SUCCESS
  53. c.Response.Msg = message
  54. c.Response.Data = map[string]interface{}{}
  55. c.Result()
  56. }
  57. func (c BaseCommon) OkWithData(data interface{}) {
  58. c.Response.Code = SUCCESS
  59. c.Response.Msg = "操作成功"
  60. c.Response.Data = data
  61. c.Result()
  62. }
  63. func (c BaseCommon) OkDetailed(data interface{}, message string) {
  64. c.Response.Code = SUCCESS
  65. c.Response.Msg = message
  66. c.Response.Data = data
  67. c.Result()
  68. }
  69. func (c BaseCommon) Fail() {
  70. c.Response.Code = ERROR
  71. c.Response.Msg = "操作失败"
  72. c.Response.Data = map[string]interface{}{}
  73. c.Result()
  74. }
  75. func (c BaseCommon) FailWithMessage(message, errMessage string) {
  76. c.Response.Code = ERROR
  77. c.Response.Msg = message
  78. c.Response.ErrMsg = errMessage
  79. c.Response.Data = map[string]interface{}{}
  80. c.Result()
  81. }
  82. func (c BaseCommon) FailWithCodeUsed(message, errMessage string) {
  83. c.Response.Code = WX_CODE_USED_ERROR
  84. c.Response.Msg = message
  85. c.Response.ErrMsg = errMessage
  86. c.Response.Data = map[string]interface{}{}
  87. c.Result()
  88. }
  89. //token异常
  90. func (c BaseCommon) TokenError(data interface{}, message string) {
  91. c.Response.Code = TOKEN_ERROR
  92. c.Response.Msg = message
  93. c.Response.Data = data
  94. c.Result()
  95. }
  96. //token异常
  97. func (c BaseCommon) TokenMsgError(message, errMessage string) {
  98. c.Response.Code = TOKEN_ERROR
  99. c.Response.Msg = message
  100. c.Response.ErrMsg = errMessage
  101. //c.Response.Data = data
  102. c.Result()
  103. }
  104. //账户绑定异常
  105. func (c BaseCommon) BindError(data interface{}, message string) {
  106. c.Response.Code = BIND_ERROR
  107. c.Response.Msg = message
  108. c.Response.Data = data
  109. c.Result()
  110. }
  111. //账户绑定异常
  112. func (c BaseCommon) BindMsgError(message, errMessage string) {
  113. c.Response.Code = BIND_ERROR
  114. c.Response.Msg = message
  115. c.Response.ErrMsg = errMessage
  116. //c.Response.Data = data
  117. c.Result()
  118. }
  119. //签名异常
  120. func (c BaseCommon) SignError(message string) {
  121. c.Response.Code = SIGN_ERROR
  122. c.Response.Msg = message
  123. c.Response.Data = map[string]interface{}{}
  124. c.Result()
  125. }
  126. func (c BaseCommon) FailWithDetailed(code int, data interface{}, message string) {
  127. c.Response.Code = code
  128. c.Response.Msg = message
  129. c.Response.Data = data
  130. c.Result()
  131. }