base_common.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. package controllers
  2. import (
  3. "github.com/astaxie/beego"
  4. "hongze/hongze_mobile_admin/utils"
  5. "rdluck_tools/log"
  6. )
  7. var apiLog *log.Log
  8. func init() {
  9. if utils.RunMode == "release" {
  10. logDir := `/data/rdlucklog/hongze_api`
  11. apiLog = log.Init("20060102.api", logDir)
  12. } else {
  13. apiLog = log.Init("20060102.api")
  14. }
  15. }
  16. //不需要授权的基类
  17. type BaseCommon struct {
  18. beego.Controller
  19. Response
  20. }
  21. type Response struct {
  22. Code int `json:"code"`
  23. Data interface{} `json:"data"`
  24. Msg string `json:"msg"`
  25. ErrMsg string `json:"errMsg";description:"给开发工程师看的错误信息"`
  26. }
  27. const (
  28. SUCCESS = 200 //成功
  29. ERROR = 400 //代表业务处理失败,前端同学需要做额外逻辑处理
  30. TOKEN_ERROR = 401 //代表token异常,用户需要重新静默授权,获取最新的token
  31. BIND_ERROR = 403 //403代表用户需要进行绑定操作,需要跳转到输入账号密码绑定页面
  32. SIGN_ERROR = 3 //签名异常
  33. )
  34. //返回数据
  35. func (this BaseCommon) Result() {
  36. this.Controller.Data["json"] = this.Response
  37. this.Controller.ServeJSON()
  38. this.StopRun()
  39. }
  40. //没有任何信息的返回
  41. func (this BaseCommon) Ok() {
  42. this.Response.Code = SUCCESS
  43. this.Response.Msg = "操作成功"
  44. this.Response.Data = map[string]interface{}{}
  45. this.Result()
  46. }
  47. func (this BaseCommon) OkWithMessage(message string) {
  48. this.Response.Code = SUCCESS
  49. this.Response.Msg = message
  50. this.Response.Data = map[string]interface{}{}
  51. this.Result()
  52. }
  53. func (this BaseCommon) OkWithData(data interface{}) {
  54. this.Response.Code = SUCCESS
  55. this.Response.Msg = "操作成功"
  56. this.Response.Data = data
  57. this.Result()
  58. }
  59. func (this BaseCommon) OkDetailed(data interface{}, message string) {
  60. this.Response.Code = SUCCESS
  61. this.Response.Msg = message
  62. this.Response.Data = data
  63. this.Result()
  64. }
  65. func (this BaseCommon) Fail() {
  66. this.Response.Code = ERROR
  67. this.Response.Msg = "操作失败"
  68. this.Response.Data = map[string]interface{}{}
  69. this.Result()
  70. }
  71. func (this BaseCommon) FailWithMessage(message, errMessage string) {
  72. this.Response.Code = ERROR
  73. this.Response.Msg = message
  74. this.Response.ErrMsg = errMessage
  75. this.Response.Data = map[string]interface{}{}
  76. this.Result()
  77. }
  78. //token异常
  79. func (this BaseCommon) TokenError(data interface{}, message string) {
  80. this.Response.Code = TOKEN_ERROR
  81. this.Response.Msg = message
  82. this.Response.Data = data
  83. this.Result()
  84. }
  85. //token异常
  86. func (this BaseCommon) TokenMsgError(message, errMessage string) {
  87. this.Response.Code = TOKEN_ERROR
  88. this.Response.Msg = message
  89. this.Response.ErrMsg = errMessage
  90. //this.Response.Data = data
  91. this.Result()
  92. }
  93. //账户绑定异常
  94. func (this BaseCommon) BindError(data interface{}, message string) {
  95. this.Response.Code = BIND_ERROR
  96. this.Response.Msg = message
  97. this.Response.Data = data
  98. this.Result()
  99. }
  100. //账户绑定异常
  101. func (this BaseCommon) BindMsgError(message, errMessage string) {
  102. this.Response.Code = BIND_ERROR
  103. this.Response.Msg = message
  104. this.Response.ErrMsg = errMessage
  105. //this.Response.Data = data
  106. this.Result()
  107. }
  108. //签名异常
  109. func (this BaseCommon) SignError(message string) {
  110. this.Response.Code = SIGN_ERROR
  111. this.Response.Msg = message
  112. this.Response.Data = map[string]interface{}{}
  113. this.Result()
  114. }
  115. func (this BaseCommon) FailWithDetailed(code int, data interface{}, message string) {
  116. this.Response.Code = code
  117. this.Response.Msg = message
  118. this.Response.Data = data
  119. this.Result()
  120. }