rescode.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. package common
  2. import (
  3. "fmt"
  4. )
  5. import (
  6. "github.com/gin-gonic/gin"
  7. ut "github.com/go-playground/universal-translator"
  8. )
  9. const OKCode = "0x000000"
  10. const InvalidTokenCode = "0x010101"
  11. const InvalidRoleCode = "0x010102"
  12. type ResDesc string
  13. var (
  14. // 全局
  15. Success ResDesc = "Success"
  16. InvalidTokenError ResDesc = "InvalidTokenError"
  17. InvalidRoleError ResDesc = "InvalidRoleError"
  18. InvalidParamError ResDesc = "InvalidParamError"
  19. NotExistsError ResDesc = "NotExistsError"
  20. NotSupportError ResDesc = "NotSupportError"
  21. NoAuthError ResDesc = "NoAuthError"
  22. ExistsError ResDesc = "ExistsError"
  23. NotAvailableError ResDesc = "NotAvailableError"
  24. UnknownError ResDesc = "UnknownError"
  25. // 业务 错误
  26. InvalidIdError ResDesc = "InvalidIdError"
  27. FieldTypeError ResDesc = "FieldTypeError"
  28. )
  29. var (
  30. OK = &ResStatus{Code: OKCode, Description: Success, Solution: ""}
  31. InvalidToken = &ResStatus{Code: InvalidTokenCode, Description: InvalidTokenError, Solution: "请先登录"}
  32. InvalidRole = &ResStatus{Code: InvalidRoleCode, Description: InvalidRoleError, Solution: ""}
  33. InvalidParam = &ResStatus{Code: "0x010103", Description: InvalidParamError, Solution: ""}
  34. NotExists = &ResStatus{Code: "0x010104", Description: NotExistsError, Solution: ""}
  35. NotSupport = &ResStatus{Code: "0x010105", Description: NotSupportError, Solution: ""}
  36. NoAuth = &ResStatus{Code: "0x010106", Description: NoAuthError, Solution: ""}
  37. Exists = &ResStatus{Code: "0x010107", Description: ExistsError, Solution: ""}
  38. NotAvailable = &ResStatus{Code: "0x010108", Description: NotAvailableError, Solution: ""}
  39. InvalidUsernameOrPasswd = &ResStatus{Code: "0x010201", Description: "InvalidUsernameOrPasswdError", Solution: ""}
  40. Unknown = &ResStatus{Code: "0x999999", Description: UnknownError, Solution: "服务异常,请联系管理员"}
  41. )
  42. func helper(code string, msg ResDesc) *ResStatus {
  43. return &ResStatus{
  44. Code: code,
  45. Description: msg,
  46. Solution: "",
  47. }
  48. }
  49. type ResStatus struct {
  50. Code string `json:"code"`
  51. Description ResDesc `json:"description"`
  52. Params []interface{} `json:"-"`
  53. Solution string `json:"solution"`
  54. }
  55. func (p *ResStatus) Error() string {
  56. return string(p.Description)
  57. }
  58. func (p *ResStatus) Desc(msg ResDesc, params ...interface{}) *ResStatus {
  59. return &ResStatus{p.Code, msg, params, p.Solution}
  60. }
  61. func (p *ResStatus) SetParam(params ...interface{}) *ResStatus {
  62. return &ResStatus{p.Code, p.Description, params, p.Solution}
  63. }
  64. func (p *ResStatus) Translator(trans ut.Translator) *ResStatus {
  65. params := []string{}
  66. if len(p.Params) > 0 {
  67. for _, param := range p.Params {
  68. params = append(params, fmt.Sprintf("%v", param))
  69. }
  70. }
  71. t, err := trans.T(string(p.Description), params...)
  72. if err != nil {
  73. fmt.Printf("translator %s err: %v\n", p.Description, err)
  74. t = string(p.Description)
  75. }
  76. return &ResStatus{p.Code, ResDesc(t), p.Params, p.Solution}
  77. }
  78. func (p *ResStatus) H(data ...interface{}) gin.H {
  79. if len(data) > 0 {
  80. return gin.H{"status": helper(p.Code, p.Description), "data": data[0]}
  81. }
  82. return gin.H{"status": helper(p.Code, p.Description), "data": gin.H{}}
  83. }