123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100 |
- package common
- import (
- "fmt"
- )
- import (
- "github.com/gin-gonic/gin"
- ut "github.com/go-playground/universal-translator"
- )
- const OKCode = "0x000000"
- const InvalidTokenCode = "0x010101"
- const InvalidRoleCode = "0x010102"
- type ResDesc string
- var (
- // 全局
- Success ResDesc = "Success"
- InvalidTokenError ResDesc = "InvalidTokenError"
- InvalidRoleError ResDesc = "InvalidRoleError"
- InvalidParamError ResDesc = "InvalidParamError"
- NotExistsError ResDesc = "NotExistsError"
- NotSupportError ResDesc = "NotSupportError"
- NoAuthError ResDesc = "NoAuthError"
- ExistsError ResDesc = "ExistsError"
- NotAvailableError ResDesc = "NotAvailableError"
- UnknownError ResDesc = "UnknownError"
- // 业务 错误
- InvalidIdError ResDesc = "InvalidIdError"
- FieldTypeError ResDesc = "FieldTypeError"
- )
- var (
- OK = &ResStatus{Code: OKCode, Description: Success, Solution: ""}
- InvalidToken = &ResStatus{Code: InvalidTokenCode, Description: InvalidTokenError, Solution: "请先登录"}
- InvalidRole = &ResStatus{Code: InvalidRoleCode, Description: InvalidRoleError, Solution: ""}
- InvalidParam = &ResStatus{Code: "0x010103", Description: InvalidParamError, Solution: ""}
- NotExists = &ResStatus{Code: "0x010104", Description: NotExistsError, Solution: ""}
- NotSupport = &ResStatus{Code: "0x010105", Description: NotSupportError, Solution: ""}
- NoAuth = &ResStatus{Code: "0x010106", Description: NoAuthError, Solution: ""}
- Exists = &ResStatus{Code: "0x010107", Description: ExistsError, Solution: ""}
- NotAvailable = &ResStatus{Code: "0x010108", Description: NotAvailableError, Solution: ""}
- InvalidUsernameOrPasswd = &ResStatus{Code: "0x010201", Description: "InvalidUsernameOrPasswdError", Solution: ""}
- Unknown = &ResStatus{Code: "0x999999", Description: UnknownError, Solution: "服务异常,请联系管理员"}
- )
- func helper(code string, msg ResDesc) *ResStatus {
- return &ResStatus{
- Code: code,
- Description: msg,
- Solution: "",
- }
- }
- type ResStatus struct {
- Code string `json:"code"`
- Description ResDesc `json:"description"`
- Params []interface{} `json:"-"`
- Solution string `json:"solution"`
- }
- func (p *ResStatus) Error() string {
- return string(p.Description)
- }
- func (p *ResStatus) Desc(msg ResDesc, params ...interface{}) *ResStatus {
- return &ResStatus{p.Code, msg, params, p.Solution}
- }
- func (p *ResStatus) SetParam(params ...interface{}) *ResStatus {
- return &ResStatus{p.Code, p.Description, params, p.Solution}
- }
- func (p *ResStatus) Translator(trans ut.Translator) *ResStatus {
- params := []string{}
- if len(p.Params) > 0 {
- for _, param := range p.Params {
- params = append(params, fmt.Sprintf("%v", param))
- }
- }
- t, err := trans.T(string(p.Description), params...)
- if err != nil {
- fmt.Printf("translator %s err: %v\n", p.Description, err)
- t = string(p.Description)
- }
- return &ResStatus{p.Code, ResDesc(t), p.Params, p.Solution}
- }
- func (p *ResStatus) H(data ...interface{}) gin.H {
- if len(data) > 0 {
- return gin.H{"status": helper(p.Code, p.Description), "data": data[0]}
- }
- return gin.H{"status": helper(p.Code, p.Description), "data": gin.H{}}
- }
|