rescode.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. package common
  2. import (
  3. "fmt"
  4. "log/slog"
  5. )
  6. import (
  7. "github.com/gin-gonic/gin"
  8. "github.com/nicksnyder/go-i18n/v2/i18n"
  9. "github.com/spf13/cast"
  10. )
  11. const OKCode = "0x000000"
  12. const InvalidTokenCode = "0x010101"
  13. const InvalidRoleCode = "0x010102"
  14. type ResDesc string
  15. var (
  16. // 全局
  17. Success ResDesc = "ErrCode_Success"
  18. InvalidTokenError ResDesc = "ErrCode_InvalidTokenError"
  19. InvalidRoleError ResDesc = "ErrCode_InvalidRoleError"
  20. InvalidParamError ResDesc = "ErrCode_InvalidParamError"
  21. NotExistsError ResDesc = "ErrCode_NotExistsError"
  22. NotSupportError ResDesc = "ErrCode_NotSupportError"
  23. NoAuthError ResDesc = "ErrCode_NoAuthError"
  24. ExistsError ResDesc = "ErrCode_ExistsError"
  25. NotAvailableError ResDesc = "ErrCode_NotAvailableError"
  26. UnknownError ResDesc = "ErrCode_UnknownError"
  27. // 业务 错误
  28. InvalidIdError ResDesc = "InvalidIdError"
  29. FieldTypeError ResDesc = "FieldTypeError"
  30. )
  31. var (
  32. OK = &ResStatus{Code: "0x000000", Description: Success, Solution: ""}
  33. InvalidToken = &ResStatus{Code: "0x000101", Description: InvalidTokenError, Solution: ""}
  34. InvalidRole = &ResStatus{Code: "0x000102", Description: InvalidRoleError, Solution: ""}
  35. InvalidParam = &ResStatus{Code: "0x000103", Description: InvalidParamError, Solution: ""}
  36. NotExists = &ResStatus{Code: "0x000104", Description: NotExistsError, Solution: ""}
  37. NotSupport = &ResStatus{Code: "0x000105", Description: NotSupportError, Solution: ""}
  38. NoAuth = &ResStatus{Code: "0x000106", Description: NoAuthError, Solution: ""}
  39. Exists = &ResStatus{Code: "0x000107", Description: ExistsError, Solution: ""}
  40. NotAvailable = &ResStatus{Code: "0x000108", Description: NotAvailableError, Solution: ""}
  41. InvalidUsernameOrPasswd = &ResStatus{Code: "0x010101", Description: "ErrCode_InvalidUsernameOrPasswdError", Solution: ""}
  42. Unknown = &ResStatus{Code: "0x999999", Description: UnknownError, Solution: ""}
  43. )
  44. func helper(code string, msg ResDesc) *ResStatus {
  45. return &ResStatus{
  46. Code: code,
  47. Description: msg,
  48. Solution: "",
  49. }
  50. }
  51. type ResStatus struct {
  52. Code string `json:"code"`
  53. Description ResDesc `json:"description"`
  54. Params map[string]interface{} `json:"-"`
  55. Solution string `json:"solution"`
  56. }
  57. func (p *ResStatus) Error() string {
  58. return string(p.Description)
  59. }
  60. func (p *ResStatus) Desc(msg ResDesc) *ResStatus {
  61. return &ResStatus{p.Code, msg, nil, p.Solution}
  62. }
  63. func convertParamsToMap(params ...interface{}) map[string]interface{} {
  64. result := make(map[string]interface{})
  65. for i := 0; i < len(params); i += 2 {
  66. var value interface{}
  67. if i+1 < len(params) {
  68. value = params[i+1]
  69. } else {
  70. value = nil
  71. }
  72. result[cast.ToString(params[i])] = value
  73. }
  74. return result
  75. }
  76. func (p *ResStatus) SetParam(params ...interface{}) *ResStatus {
  77. return &ResStatus{p.Code, p.Description, convertParamsToMap(params), p.Solution}
  78. }
  79. func (p *ResStatus) Translator(localizer *i18n.Localizer) *ResStatus {
  80. params := []string{}
  81. if len(p.Params) > 0 {
  82. for _, param := range p.Params {
  83. params = append(params, fmt.Sprintf("%v", param))
  84. }
  85. }
  86. t, err := localizer.Localize(&i18n.LocalizeConfig{
  87. MessageID: string(p.Description),
  88. TemplateData: params,
  89. })
  90. if err != nil {
  91. slog.Error("translator fail\n", "desc", p.Description, "err", err)
  92. t = string(p.Description)
  93. }
  94. return &ResStatus{p.Code, ResDesc(t), p.Params, p.Solution}
  95. }
  96. func (p *ResStatus) H(data ...interface{}) gin.H {
  97. if len(data) > 0 {
  98. return gin.H{"status": helper(p.Code, p.Description), "data": data[0]}
  99. }
  100. return gin.H{"status": helper(p.Code, p.Description), "data": gin.H{}}
  101. }