base.go 4.6 KB

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