工具包
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

125 строки
2.7 KiB

  1. package e
  2. import (
  3. "code.fnuoos.com/go_rely_warehouse/zyos_go_tools.git/utils"
  4. "code.fnuoos.com/go_rely_warehouse/zyos_go_tools.git/utils/logx"
  5. "encoding/json"
  6. "net/http"
  7. "github.com/gin-gonic/gin"
  8. )
  9. // GetMsg get error information based on Code
  10. // 因为这里code是自己控制的, 因此没考虑报错信息
  11. func GetMsg(code int) (int, string) {
  12. if msg, ok := MsgFlags[code]; ok {
  13. return code / 1000, msg
  14. }
  15. if http.StatusText(code) == "" {
  16. code = 200
  17. }
  18. return code, MsgFlags[ERR_BAD_REQUEST]
  19. }
  20. // 成功输出, fields 是额外字段, 与code, msg同级
  21. func OutSuc(c *gin.Context, data interface{}, fields map[string]interface{}) {
  22. res := gin.H{
  23. "code": 1,
  24. "msg": "ok",
  25. "data": data,
  26. }
  27. if fields != nil {
  28. for k, v := range fields {
  29. res[k] = v
  30. }
  31. }
  32. jsonData, _ := json.Marshal(res)
  33. strs := string(jsonData)
  34. if c.GetString("translate_open") != "" {
  35. strs = utils.ReadReverse(c, string(jsonData), "")
  36. }
  37. if utils.GetApiVersion(c) > 0 { //加了签名校验只返回加密的字符串
  38. strs = utils.ResultAes(c, []byte(strs))
  39. }
  40. c.Writer.WriteString(strs)
  41. }
  42. func OutSucPure(c *gin.Context, data interface{}, fields map[string]interface{}) {
  43. res := gin.H{
  44. "code": 1,
  45. "msg": "ok",
  46. "data": data,
  47. }
  48. if fields != nil {
  49. for k, v := range fields {
  50. res[k] = v
  51. }
  52. }
  53. jsonData, _ := json.Marshal(res)
  54. strs := string(jsonData)
  55. if c.GetString("translate_open") != "" {
  56. strs = utils.ReadReverse(c, string(jsonData), "")
  57. }
  58. if utils.GetApiVersion(c) > 0 { //加了签名校验只返回加密的字符串
  59. strs = utils.ResultAes(c, []byte(strs))
  60. }
  61. c.Writer.WriteString(strs)
  62. }
  63. // 错误输出
  64. func OutErr(c *gin.Context, code int, err ...interface{}) {
  65. statusCode, msg := GetMsg(code)
  66. if len(err) > 0 && err[0] != nil {
  67. e := err[0]
  68. switch v := e.(type) {
  69. case E:
  70. statusCode = v.Code / 1000
  71. msg = v.Error()
  72. logx.Error(v.msg + ": " + v.st) // 记录堆栈信息
  73. case error:
  74. logx.Error(v)
  75. break
  76. case string:
  77. msg = v
  78. case int:
  79. if _, ok := MsgFlags[v]; ok {
  80. msg = MsgFlags[v]
  81. }
  82. }
  83. }
  84. if c.GetString("translate_open") != "" {
  85. msg = utils.ReadReverse(c, msg, "")
  86. }
  87. if utils.GetApiVersion(c) > 0 { //加了签名校验只返回加密的字符串
  88. jsonData, _ := json.Marshal(gin.H{
  89. "code": code,
  90. "msg": msg,
  91. "data": []struct{}{},
  92. })
  93. str := utils.ResultAes(c, jsonData)
  94. if code > 100000 {
  95. code = int(utils.FloatFormat(float64(code/1000), 0))
  96. }
  97. c.Status(500)
  98. c.Writer.WriteString(str)
  99. } else {
  100. c.AbortWithStatusJSON(statusCode, gin.H{
  101. "code": code,
  102. "msg": msg,
  103. "data": []struct{}{},
  104. })
  105. }
  106. }
  107. // 重定向
  108. func OutRedirect(c *gin.Context, code int, loc string) {
  109. if code < 301 || code > 308 {
  110. code = 303
  111. }
  112. c.Redirect(code, loc)
  113. c.Abort()
  114. }