base_common.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. )
  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(), "Authorization:", c.Ctx.Input.Header("Authorization"), "RequestBody:", requestBody, "ResponseBody", string(content), "IP:", ip)
  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, errMessage string) {
  75. c.Response.Code = ERROR
  76. c.Response.Msg = message
  77. c.Response.ErrMsg = errMessage
  78. c.Response.Data = map[string]interface{}{}
  79. c.Result()
  80. }
  81. //token异常
  82. func (c BaseCommon) TokenError(data interface{}, message string) {
  83. c.Response.Code = TOKEN_ERROR
  84. c.Response.Msg = message
  85. c.Response.Data = data
  86. c.Result()
  87. }
  88. //token异常
  89. func (c BaseCommon) TokenMsgError(message, errMessage string) {
  90. c.Response.Code = TOKEN_ERROR
  91. c.Response.Msg = message
  92. c.Response.ErrMsg = errMessage
  93. //c.Response.Data = data
  94. c.Result()
  95. }
  96. //账户绑定异常
  97. func (c BaseCommon) BindError(data interface{}, message string) {
  98. c.Response.Code = BIND_ERROR
  99. c.Response.Msg = message
  100. c.Response.Data = data
  101. c.Result()
  102. }
  103. //账户绑定异常
  104. func (c BaseCommon) BindMsgError(message, errMessage string) {
  105. c.Response.Code = BIND_ERROR
  106. c.Response.Msg = message
  107. c.Response.ErrMsg = errMessage
  108. //c.Response.Data = data
  109. c.Result()
  110. }
  111. //签名异常
  112. func (c BaseCommon) SignError(message string) {
  113. c.Response.Code = SIGN_ERROR
  114. c.Response.Msg = message
  115. c.Response.Data = map[string]interface{}{}
  116. c.Result()
  117. }
  118. func (c BaseCommon) FailWithDetailed(code int, data interface{}, message string) {
  119. c.Response.Code = code
  120. c.Response.Msg = message
  121. c.Response.Data = data
  122. c.Result()
  123. }