base_common.go 3.7 KB

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