base.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. package resp
  2. import (
  3. "encoding/json"
  4. "eta_gn/eta_obs/global"
  5. "eta_gn/eta_obs/utils"
  6. "fmt"
  7. "github.com/gin-gonic/gin"
  8. "strings"
  9. )
  10. var (
  11. OK_CODE = 200 //业务成功
  12. FAIL_CODE = 400 //业务错误
  13. TOKEN_ERROR_CODE = 401 //toke异常
  14. NO_AUTH = 403 //没有权限
  15. SPECIFIC_FAIL_CODE = 4001 // 业务指定错误
  16. )
  17. type ResultData struct {
  18. Code int `json:"code" description:"状态码"`
  19. Msg string `json:"msg" description:"提示信息"`
  20. Data interface{} `json:"data" description:"返回数据"`
  21. ErrMsg string `json:"-" description:"错误信息,不用返回给前端,只是做日志记录"`
  22. }
  23. func result(code int, resultData ResultData, c *gin.Context) {
  24. jsonByte, _ := json.Marshal(resultData)
  25. token := c.Request.Header.Get("Authorization")
  26. if token == "" {
  27. token = c.DefaultQuery("authorization", "")
  28. if token == "" {
  29. token = c.DefaultQuery("Authorization", "")
  30. }
  31. }
  32. logSlice := make([]string, 0)
  33. logSlice = append(logSlice, fmt.Sprint("Url:", c.Request.RequestURI))
  34. logSlice = append(logSlice, fmt.Sprint("Token:", token))
  35. //logSlice = append(logSlice, fmt.Sprint("resultData:", string(jsonByte)))
  36. //记录错误日志
  37. if resultData.ErrMsg != "" {
  38. logSlice = append(logSlice, fmt.Sprint("ErrMsg:", resultData.ErrMsg))
  39. //global.LOG.Info(strings.Join(logSlice, ";"))
  40. }
  41. // 测试环境不加密
  42. if global.CONFIG.Serve.RunMode == "debug" {
  43. global.LOG.Info(strings.Join(logSlice, ";"))
  44. c.JSON(code, resultData)
  45. } else {
  46. global.LOG.Info(strings.Join(logSlice, ";"))
  47. encryptResult := utils.DesBase64Encrypt(jsonByte, global.CONFIG.Serve.DesKey)
  48. c.JSON(code, string(encryptResult))
  49. }
  50. c.Abort()
  51. }
  52. // OK 操作成功
  53. func Ok(msg string, c *gin.Context) {
  54. resultData := ResultData{
  55. Code: OK_CODE,
  56. Msg: msg,
  57. }
  58. result(200, resultData, c)
  59. }
  60. // OkData 成功返回数据
  61. func OkData(msg string, data interface{}, c *gin.Context) {
  62. resultData := ResultData{
  63. Code: OK_CODE,
  64. Msg: msg,
  65. Data: data,
  66. }
  67. result(200, resultData, c)
  68. }
  69. // Fail 操作失败
  70. func Fail(msg string, c *gin.Context) {
  71. resultData := ResultData{
  72. Code: FAIL_CODE,
  73. Msg: msg,
  74. }
  75. result(200, resultData, c)
  76. }
  77. // FailData 成功返回数据
  78. func FailData(msg string, data interface{}, c *gin.Context) {
  79. resultData := ResultData{
  80. Code: FAIL_CODE,
  81. Msg: msg,
  82. Data: data,
  83. }
  84. result(200, resultData, c)
  85. }
  86. // Custom 自定义状态码+操作成功
  87. func Custom(code int, msg string, c *gin.Context) {
  88. resultData := ResultData{
  89. Code: code,
  90. Msg: msg,
  91. }
  92. result(200, resultData, c)
  93. }
  94. // CustomData 自定义状态码+返回数据
  95. func CustomData(code int, msg string, data interface{}, c *gin.Context) {
  96. resultData := ResultData{
  97. Code: code,
  98. Msg: msg,
  99. Data: data,
  100. }
  101. result(200, resultData, c)
  102. }
  103. // TokenError token异常
  104. func TokenError(data interface{}, message, errMsg string, c *gin.Context) {
  105. resultData := ResultData{
  106. Code: TOKEN_ERROR_CODE,
  107. Msg: message,
  108. Data: data,
  109. ErrMsg: errMsg,
  110. }
  111. result(200, resultData, c)
  112. }
  113. // AuthError 没有权限
  114. func AuthError(data interface{}, message string, c *gin.Context) {
  115. resultData := ResultData{
  116. Code: NO_AUTH,
  117. Msg: message,
  118. Data: data,
  119. }
  120. result(200, resultData, c)
  121. }
  122. // SpecificFail 业务指定错误
  123. func SpecificFail(data interface{}, message string, c *gin.Context) {
  124. resultData := ResultData{
  125. Code: SPECIFIC_FAIL_CODE,
  126. Msg: message,
  127. Data: data,
  128. }
  129. result(200, resultData, c)
  130. }
  131. // FailMsg 操作失败
  132. func FailMsg(msg, errMsg string, c *gin.Context) {
  133. resultData := ResultData{
  134. Code: FAIL_CODE,
  135. Msg: msg,
  136. ErrMsg: errMsg,
  137. }
  138. result(200, resultData, c)
  139. }
  140. // Gn4AOperationResult 国能4A响应结果
  141. type Gn4AOperationResult struct {
  142. RequestId string `json:"requestID" description:"请求唯一标识"`
  143. ReturnFlag bool `json:"returnFlag" description:"处理结果标识: true-成功; false-失败"`
  144. ReturnCode string `json:"returnCode" description:"返回结果编号, returnFlag为true时, returnCode为0, 否则为错误编码"`
  145. ReturnMsg string `json:"returnMsg" description:"返回结果信息, 可自定义"`
  146. }
  147. // Gn4AResultData 4A返回结果
  148. func Gn4AResultData(flag bool, code, msg string, c *gin.Context) {
  149. resultData := Gn4AOperationResult{
  150. ReturnFlag: flag,
  151. ReturnCode: code,
  152. ReturnMsg: msg,
  153. }
  154. gnResult(200, resultData, c)
  155. }
  156. func gnResult(code int, resultData Gn4AOperationResult, c *gin.Context) {
  157. logSlice := make([]string, 0)
  158. logSlice = append(logSlice, fmt.Sprint("Url:", c.Request.RequestURI))
  159. // 记录错误日志
  160. if !resultData.ReturnFlag {
  161. logSlice = append(logSlice, fmt.Sprintf("ErrCode: %s, ErrMsg: %s", resultData.ReturnCode, resultData.ReturnMsg))
  162. }
  163. global.LOG.Info(strings.Join(logSlice, ";"))
  164. c.JSON(code, resultData)
  165. c.Abort()
  166. }