广告涉及的mq都放这里
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.

1 maand geleden
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. package e
  2. import (
  3. "net/http"
  4. "github.com/gin-gonic/gin"
  5. "applet/app/utils/logx"
  6. )
  7. // GetMsg get error information based on Code
  8. // 因为这里code是自己控制的, 因此没考虑报错信息
  9. func GetMsg(code int) (int, string) {
  10. if msg, ok := MsgFlags[code]; ok {
  11. return code / 1000, msg
  12. }
  13. if http.StatusText(code) == "" {
  14. code = 200
  15. }
  16. return code, MsgFlags[ERR_BAD_REQUEST]
  17. }
  18. // 成功输出, fields 是额外字段, 与code, msg同级
  19. func OutSuc(c *gin.Context, data interface{}, fields map[string]interface{}) {
  20. res := gin.H{
  21. "code": 1,
  22. "msg": "ok",
  23. "data": data,
  24. }
  25. if fields != nil {
  26. for k, v := range fields {
  27. res[k] = v
  28. }
  29. }
  30. c.AbortWithStatusJSON(200, res)
  31. }
  32. func OutSucPure(c *gin.Context, data interface{}, fields map[string]interface{}) {
  33. res := gin.H{
  34. "code": 1,
  35. "msg": "ok",
  36. "data": data,
  37. }
  38. if fields != nil {
  39. for k, v := range fields {
  40. res[k] = v
  41. }
  42. }
  43. c.Abort()
  44. c.PureJSON(200, res)
  45. }
  46. // 错误输出
  47. func OutErr(c *gin.Context, code int, err ...interface{}) {
  48. statusCode, msg := GetMsg(code)
  49. if len(err) > 0 && err[0] != nil {
  50. e := err[0]
  51. switch v := e.(type) {
  52. case E:
  53. statusCode = v.Code / 1000
  54. msg = v.Error()
  55. logx.Error(v.msg + ": " + v.st) // 记录堆栈信息
  56. case error:
  57. logx.Error(v)
  58. break
  59. case string:
  60. msg = v
  61. case int:
  62. if _, ok := MsgFlags[v]; ok {
  63. msg = MsgFlags[v]
  64. }
  65. }
  66. }
  67. c.AbortWithStatusJSON(statusCode, gin.H{
  68. "code": code,
  69. "msg": msg,
  70. "data": []struct{}{},
  71. })
  72. }
  73. // 重定向
  74. func OutRedirect(c *gin.Context, code int, loc string) {
  75. if code < 301 || code > 308 {
  76. code = 303
  77. }
  78. c.Redirect(code, loc)
  79. c.Abort()
  80. }