From d17a4272b42bc5c3e72286ed1de45b62850120c1 Mon Sep 17 00:00:00 2001 From: KrisYu Date: Fri, 26 Jul 2024 17:51:04 +0800 Subject: [PATCH] =?UTF-8?q?Version1.1.12=20=20BugFixes=20=20=201.=20?= =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E5=9B=BD=E9=99=85=E5=8C=96Bug;=E8=A1=A5?= =?UTF-8?q?=E5=85=85=E4=BA=86MessageId=E7=B1=BB=E5=9E=8B=EF=BC=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- sdk/api/api.go | 2 +- sdk/api/response/code.go | 6 +----- sdk/i18n/error.go | 14 ++++++++++++++ sdk/i18n/translator.go | 5 +++++ 4 files changed, 21 insertions(+), 6 deletions(-) diff --git a/sdk/api/api.go b/sdk/api/api.go index 04c51c1..6534736 100644 --- a/sdk/api/api.go +++ b/sdk/api/api.go @@ -133,7 +133,7 @@ func (e *Api) OK(ctx *gin.Context, data any) { func (e *Api) Error(ctx *gin.Context, businessStatus response.Status, errMsg ...i18n.MyError) { statusMsg := translate(ctx, businessStatus.Message) - var msg string + msg := statusMsg if len(errMsg) > 0 { errorMsg := translate(ctx, errMsg[0].Message) msg = strings.Join([]string{statusMsg, errorMsg}, ": ") diff --git a/sdk/api/response/code.go b/sdk/api/response/code.go index 5485549..959523a 100644 --- a/sdk/api/response/code.go +++ b/sdk/api/response/code.go @@ -8,9 +8,5 @@ type Status struct { } var ( - OK = Status{Code: 2000, Message: i18n.Message{ID: "response.ok", DefaultMessage: "OK"}} - RequestParamsError = Status{Code: 4000, Message: i18n.Message{ID: "response.badRequest", DefaultMessage: "Request Parameters Error"}} - Unauthorized = Status{Code: 4001, Message: i18n.Message{ID: "response.unauthorized", DefaultMessage: "Unauthorized Request"}} - InternalServerError = Status{Code: 5000, Message: i18n.Message{ID: "response.internalServerError", DefaultMessage: "Internal Server Error"}} - ServiceError = Status{Code: 5001, Message: i18n.Message{ID: "response.serviceError", DefaultMessage: "Service Error"}} + OK = Status{Code: 2000, Message: i18n.Message{ID: "response.ok", DefaultMessage: "OK"}} ) diff --git a/sdk/i18n/error.go b/sdk/i18n/error.go index a68c489..97549f5 100644 --- a/sdk/i18n/error.go +++ b/sdk/i18n/error.go @@ -15,3 +15,17 @@ func NewMyError(err error) MyError { Message: Message{DefaultMessage: err.Error()}, } } + +func NewMyErrorWithMessageID(mi MessageID, arg ...any) MyError { + myError := MyError{ + Err: nil, + Message: Message{ + ID: mi.ID, + DefaultMessage: mi.DefaultMessage, + }, + } + if len(arg) > 0 { + myError.Message.Args = arg[0] + } + return myError +} diff --git a/sdk/i18n/translator.go b/sdk/i18n/translator.go index e250091..c26ed99 100644 --- a/sdk/i18n/translator.go +++ b/sdk/i18n/translator.go @@ -29,3 +29,8 @@ type Message struct { DefaultMessage string `json:"defaultMessage"` // 默认消息,翻译失败时,使用 Args any `json:"args"` // 消息参数 } + +type MessageID struct { + ID string `json:"ID"` + DefaultMessage string `json:"defaultMessage"` +}