附近小店
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

2 maanden geleden
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. package e
  2. import (
  3. "applet/app/utils"
  4. "encoding/json"
  5. "net/http"
  6. "github.com/gin-gonic/gin"
  7. "applet/app/utils/logx"
  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 && utils.CheckUri(c) > 0 { //加了签名校验只返回加密的字符串
  38. strs = utils.ResultAes(c, []byte(strs))
  39. }
  40. c.Writer.WriteString(strs)
  41. }
  42. func OutSucByLianlian(c *gin.Context, code string, msg string, data interface{}) {
  43. res := gin.H{
  44. "code": code,
  45. "msg": msg,
  46. "data": data,
  47. }
  48. jsonData, _ := json.Marshal(res)
  49. strs := string(jsonData)
  50. c.Writer.WriteString(strs)
  51. }
  52. func OutSucPure(c *gin.Context, data interface{}, fields map[string]interface{}) {
  53. res := gin.H{
  54. "code": 1,
  55. "msg": "ok",
  56. "data": data,
  57. }
  58. if fields != nil {
  59. for k, v := range fields {
  60. res[k] = v
  61. }
  62. }
  63. jsonData, _ := json.Marshal(res)
  64. strs := string(jsonData)
  65. if c.GetString("translate_open") != "" {
  66. strs = utils.ReadReverse(c, string(jsonData))
  67. }
  68. if utils.GetApiVersion(c) > 0 && utils.CheckUri(c) > 0 { //加了签名校验只返回加密的字符串
  69. strs = utils.ResultAes(c, []byte(strs))
  70. }
  71. c.Writer.WriteString(strs)
  72. }
  73. // 错误输出
  74. func OutErr(c *gin.Context, code int, err ...interface{}) {
  75. statusCode, msg := GetMsg(code)
  76. if len(err) > 0 && err[0] != nil {
  77. e := err[0]
  78. switch v := e.(type) {
  79. case E:
  80. statusCode = v.Code / 1000
  81. msg = v.Error()
  82. logx.Error(v.msg + ": " + v.st) // 记录堆栈信息
  83. case error:
  84. logx.Error(v)
  85. break
  86. case string:
  87. msg = v
  88. case int:
  89. if _, ok := MsgFlags[v]; ok {
  90. msg = MsgFlags[v]
  91. }
  92. }
  93. }
  94. if c.GetString("translate_open") != "" {
  95. msg = utils.ReadReverse(c, msg)
  96. }
  97. if utils.GetApiVersion(c) > 0 && utils.CheckUri(c) > 0 { //加了签名校验只返回加密的字符串
  98. jsonData, _ := json.Marshal(gin.H{
  99. "code": code,
  100. "msg": msg,
  101. "data": []struct{}{},
  102. })
  103. str := utils.ResultAes(c, jsonData)
  104. if code > 100000 {
  105. code = int(utils.FloatFormat(float64(code/1000), 0))
  106. }
  107. c.Status(500)
  108. c.Writer.WriteString(str)
  109. } else {
  110. c.AbortWithStatusJSON(statusCode, gin.H{
  111. "code": code,
  112. "msg": msg,
  113. "data": []struct{}{},
  114. })
  115. }
  116. }
  117. func OutErrSecond(c *gin.Context, code int, err ...interface{}) {
  118. statusCode, msg := GetMsg(code)
  119. if len(err) > 0 && err[0] != nil {
  120. e := err[0]
  121. switch v := e.(type) {
  122. case E:
  123. statusCode = v.Code / 1000
  124. msg = v.Error()
  125. logx.Error(v.msg + ": " + v.st) // 记录堆栈信息
  126. case error:
  127. logx.Error(v)
  128. break
  129. case string:
  130. msg = v
  131. case int:
  132. if _, ok := MsgFlags[v]; ok {
  133. msg = MsgFlags[v]
  134. }
  135. }
  136. }
  137. if c.GetString("translate_open") != "" {
  138. msg = utils.ReadReverse(c, msg)
  139. }
  140. if utils.GetApiVersion(c) > 0 && utils.CheckUri(c) > 0 { //加了签名校验只返回加密的字符串
  141. jsonData, _ := json.Marshal(gin.H{
  142. "code": code,
  143. "msg": msg,
  144. "data": "",
  145. })
  146. str := utils.ResultAes(c, jsonData)
  147. if code > 100000 {
  148. code = int(utils.FloatFormat(float64(code/1000), 0))
  149. }
  150. c.Status(500)
  151. c.Writer.WriteString(str)
  152. } else {
  153. c.AbortWithStatusJSON(statusCode, gin.H{
  154. "code": code,
  155. "msg": msg,
  156. "data": "",
  157. })
  158. }
  159. }
  160. // 重定向
  161. func OutRedirect(c *gin.Context, code int, loc string) {
  162. if code < 301 || code > 308 {
  163. code = 303
  164. }
  165. c.Redirect(code, loc)
  166. c.Abort()
  167. }
  168. func DouYouReturnMsg(c *gin.Context, statusCode, msg string) {
  169. res := gin.H{
  170. "status_code": statusCode,
  171. }
  172. if msg != "" {
  173. res["message"] = msg
  174. }
  175. jsonData, _ := json.Marshal(res)
  176. strs := string(jsonData)
  177. c.Writer.WriteString(strs)
  178. }
  179. func MeituanLmReturnMsg(c *gin.Context, statusCode, msg string) {
  180. res := gin.H{
  181. "errcode": statusCode,
  182. }
  183. if msg != "" {
  184. res["errmsg"] = msg
  185. }
  186. jsonData, _ := json.Marshal(res)
  187. strs := string(jsonData)
  188. c.Writer.WriteString(strs)
  189. }
  190. func TaskBoxReturnMsg(c *gin.Context, statusCode int, msg string) {
  191. res := gin.H{
  192. "code": statusCode,
  193. }
  194. if msg != "" {
  195. res["message"] = msg
  196. }
  197. jsonData, _ := json.Marshal(res)
  198. strs := string(jsonData)
  199. c.Writer.WriteString(strs)
  200. }