From 8d7ee20a283be3f43912d4e6479e80cff4eca23c Mon Sep 17 00:00:00 2001 From: eson <9673575+githubcontent@user.noreply.gitee.com> Date: Wed, 18 Oct 2023 11:07:48 +0800 Subject: [PATCH] debug toekn --- .../internal/logic/debugauthdeletelogic.go | 2 +- .../logic/useremailconfirmationlogic.go | 10 ++-- .../internal/logic/usergoogleloginlogic.go | 4 +- .../internal/logic/userresetpasswordlogic.go | 4 +- .../internal/logic/userresettokenlogic.go | 2 +- .../internal/logic/updateprofilebaselogic.go | 4 +- utils/basic/basic.go | 57 ++++++++++--------- 7 files changed, 43 insertions(+), 40 deletions(-) diff --git a/server/auth/internal/logic/debugauthdeletelogic.go b/server/auth/internal/logic/debugauthdeletelogic.go index a841c396..cd48fa33 100644 --- a/server/auth/internal/logic/debugauthdeletelogic.go +++ b/server/auth/internal/logic/debugauthdeletelogic.go @@ -36,7 +36,7 @@ func (l *DebugAuthDeleteLogic) DebugAuthDelete(req *types.RequestAuthDelete, use err := l.svcCtx.AllModels.FsUser.DebugAuthDelete(l.ctx, req.Email) if err != nil { - return resp.SetStatus(basic.CodeDbSqlErr, err.Error()) + return resp.SetStatus(basic.CodeDbSqlErr, err) } return resp.SetStatus(basic.CodeOK) diff --git a/server/auth/internal/logic/useremailconfirmationlogic.go b/server/auth/internal/logic/useremailconfirmationlogic.go index 38de3d7c..8b5868c1 100644 --- a/server/auth/internal/logic/useremailconfirmationlogic.go +++ b/server/auth/internal/logic/useremailconfirmationlogic.go @@ -88,7 +88,7 @@ func (l *UserEmailConfirmationLogic) UserEmailConfirmation(req *types.RequestEma user, err := l.svcCtx.AllModels.FsUser.RegisterByGoogleOAuth(l.ctx, token) if err != nil { logx.Error(err, token.TraceId) - return resp.SetStatus(basic.CodeDbSqlErr, err.Error()) + return resp.SetStatus(basic.CodeDbSqlErr, err) } err = FinishRegister(l.svcCtx, user, token) @@ -117,7 +117,7 @@ func (l *UserEmailConfirmationLogic) UserEmailConfirmation(req *types.RequestEma rt, err := l.svcCtx.ResetTokenManger.Decrypt(req.Token) // ResetToken if err != nil { logx.Error(err) - return resp.SetStatus(basic.CodeOAuthResetTokenDecryptErr, err.Error()) + return resp.SetStatus(basic.CodeOAuthResetTokenDecryptErr, err) } if time.Since(rt.CreateAt) > 30*time.Minute { @@ -126,7 +126,7 @@ func (l *UserEmailConfirmationLogic) UserEmailConfirmation(req *types.RequestEma // TODO: 存储 if rt.OperateType != auth.OpTypeResetToken { - return resp.SetStatus(basic.CodeOAuthTypeErr, "error OperateType: rt.OperateType != auth.OpTypeResetToken") + return resp.SetStatus(basic.CodeOAuthTypeErr, fmt.Errorf("error OperateType: rt.OperateType != auth.OpTypeResetToken")) } err = l.svcCtx.AllModels.FsUser.Transaction(l.ctx, func(tx *gorm.DB) error { @@ -142,14 +142,14 @@ func (l *UserEmailConfirmationLogic) UserEmailConfirmation(req *types.RequestEma }) if err != nil { - return resp.SetStatus(basic.CodeDbSqlErr, err.Error()) + return resp.SetStatus(basic.CodeDbSqlErr, err) } event := wevent.NewWebsocketEventSuccess(wevent.UserResetToken, rt.TraceId) err = wevent.CommonNotify(l.svcCtx.Config.MainAddress, rt.Wid, event) if err != nil { logx.Error(err, rt.TraceId) - return resp.SetStatus(basic.CodeResetPasswordErr, err.Error()) + return resp.SetStatus(basic.CodeResetPasswordErr, err) } return resp.SetStatus(basic.CodeOK) diff --git a/server/auth/internal/logic/usergoogleloginlogic.go b/server/auth/internal/logic/usergoogleloginlogic.go index c9808571..11f7ad36 100644 --- a/server/auth/internal/logic/usergoogleloginlogic.go +++ b/server/auth/internal/logic/usergoogleloginlogic.go @@ -64,13 +64,13 @@ func (l *UserGoogleLoginLogic) UserGoogleLogin(req *types.RequestGoogleLogin, us token, err := googleOauthConfig.Exchange(l.ctx, req.Code) if err != nil { logx.Error(err) - return resp.SetStatus(basic.CodeApiErr, err.Error()) + return resp.SetStatus(basic.CodeApiErr, err) } r, err := requests.Get("https://www.googleapis.com/oauth2/v2/userinfo?access_token=" + token.AccessToken).Execute() if err != nil { logx.Error(err) - return resp.SetStatus(basic.CodeOAuthGoogleApiErr, err.Error()) + return resp.SetStatus(basic.CodeOAuthGoogleApiErr, err) } gresult := r.Json() diff --git a/server/auth/internal/logic/userresetpasswordlogic.go b/server/auth/internal/logic/userresetpasswordlogic.go index cc7a55a2..1560d55d 100644 --- a/server/auth/internal/logic/userresetpasswordlogic.go +++ b/server/auth/internal/logic/userresetpasswordlogic.go @@ -46,12 +46,12 @@ func (l *UserResetPasswordLogic) UserResetPassword(req *types.RequestUserResetPa rt, err := l.svcCtx.ResetTokenManger.Decrypt(req.ResetToken) // ResetToken if err != nil { logx.Error(err) - return resp.SetStatus(basic.CodeOAuthResetTokenDecryptErr, err.Error()) + return resp.SetStatus(basic.CodeOAuthResetTokenDecryptErr, err) } // TODO: 存储 if rt.OperateType != auth.OpTypeResetToken { - return resp.SetStatus(basic.CodeOAuthTypeErr, "error OperateType: rt.OperateType != auth.OpTypeResetToken") + return resp.SetStatus(basic.CodeOAuthTypeErr, fmt.Errorf("error OperateType: rt.OperateType != auth.OpTypeResetToken")) } if time.Since(rt.CreateAt) > 30*time.Minute { diff --git a/server/auth/internal/logic/userresettokenlogic.go b/server/auth/internal/logic/userresettokenlogic.go index 61ab8fdb..7981c2ab 100644 --- a/server/auth/internal/logic/userresettokenlogic.go +++ b/server/auth/internal/logic/userresettokenlogic.go @@ -56,7 +56,7 @@ func (l *UserResetTokenLogic) UserResetToken(req *types.RequestUserResetToken, u resetToken, err := l.svcCtx.ResetTokenManger.Encrypt(token) if err != nil { logx.Error(err) - return resp.SetStatus(basic.CodeOAuthResetTokenEncryptErr, err.Error()) + return resp.SetStatus(basic.CodeOAuthResetTokenEncryptErr, err) } userName := *user.FirstName + " " + *user.LastName diff --git a/server/info/internal/logic/updateprofilebaselogic.go b/server/info/internal/logic/updateprofilebaselogic.go index 57d2913b..71db9d6e 100644 --- a/server/info/internal/logic/updateprofilebaselogic.go +++ b/server/info/internal/logic/updateprofilebaselogic.go @@ -41,8 +41,8 @@ func (l *UpdateProfileBaseLogic) UpdateProfileBase(req *types.ProfileRequest, us err := l.svcCtx.AllModels.FsUserInfo.MergeMetadata(userinfo.UserId, req) if err != nil { - logx.Error(err) // 日志记录错误 - return resp.SetStatus(basic.CodeDbSqlErr, err.Error()) // 返回数据库创建错误 + logx.Error(err) // 日志记录错误 + return resp.SetStatus(basic.CodeDbSqlErr, err) // 返回数据库创建错误 } return resp.SetStatus(basic.CodeOK) diff --git a/utils/basic/basic.go b/utils/basic/basic.go index b6bc4be2..40b108da 100644 --- a/utils/basic/basic.go +++ b/utils/basic/basic.go @@ -131,40 +131,26 @@ func (resp *Response) Set(Code int, Message string) *Response { } } -// Set 设置整个Response -func (resp *Response) SetWithData(Code int, Message string, Data interface{}) *Response { - return &Response{ - Code: Code, - Message: Message, - Data: Data, - } -} +// SetStatus 设置默认StatusResponse(内部自定义) 默认msg, 可以带data, data只使用一个参数, 第二个data就是debug参数 +func (resp *Response) SetStatus(sr *StatusResponse, dataAndDebug ...interface{}) *Response { -// SetStatus 设置默认StatusResponse(内部自定义) 默认msg, 可以带data, data只使用一个参数 -func (resp *Response) SetStatus(sr *StatusResponse, data ...interface{}) *Response { newResp := &Response{ Code: sr.Code, Message: sr.Message, } - switch len(data) { + switch len(dataAndDebug) { case 0: // 0 直接返回 case 1: - if err, ok := data[0].(error); ok { + if err, ok := dataAndDebug[0].(error); ok { newResp.Message = err.Error() } else { - newResp.Data = data[0] + newResp.Data = dataAndDebug[0] } case 2: - var dataIndex = 0 - if err, ok := data[dataIndex].(error); ok { - newResp.Message = err.Error() - dataIndex++ - } else { - panic("data只接收1或2个 data 或 error.Error() 和 data ") - } - newResp.Data = data[1] + newResp.Data = dataAndDebug[0] + newResp.Debug = dataAndDebug[1] default: panic("data只接收1或2个 data, error.Error() 和 data ") } @@ -172,18 +158,32 @@ func (resp *Response) SetStatus(sr *StatusResponse, data ...interface{}) *Respon return newResp } +// SetStatus 设置默认StatusResponse(内部自定义) 默认msg, 可以带data, data只使用一个参数, 第二个data就是debug参数 +func (resp *Response) SetWithDebug(sr *StatusResponse, debug interface{}) *Response { + + resp = &Response{ + Code: sr.Code, + Message: sr.Message, + Debug: debug, + } + + return resp +} + // SetStatusWithMessage 设置默认StatusResponse(内部自定义) 非默认msg, 可以带data, data只使用一个参数 -func (resp *Response) SetStatusWithMessage(sr *StatusResponse, msg string, data ...interface{}) *Response { +func (resp *Response) SetStatusWithMessage(sr *StatusResponse, msg string, dataAndDebug ...interface{}) *Response { newResp := &Response{ Code: sr.Code, Message: msg, } - switch len(data) { + switch len(dataAndDebug) { case 0: // 0 直接返回 case 1: - newResp.Data = data[0] + newResp.Data = dataAndDebug[0] + case 2: + newResp.Debug = dataAndDebug[1] default: panic("data只接收一个 data") } @@ -191,17 +191,20 @@ func (resp *Response) SetStatusWithMessage(sr *StatusResponse, msg string, data return newResp } -func (resp *Response) SetStatusAddMessage(sr *StatusResponse, msg string, data ...interface{}) *Response { +func (resp *Response) SetStatusAddMessage(sr *StatusResponse, msg string, dataAndDebug ...interface{}) *Response { newResp := &Response{ Code: sr.Code, Message: sr.Message + ":" + msg, } - switch len(data) { + switch len(dataAndDebug) { case 0: // 0 直接返回 case 1: - newResp.Data = data[0] + newResp.Data = dataAndDebug[0] + case 2: + newResp.Data = dataAndDebug[0] + newResp.Debug = dataAndDebug[1] default: panic("data只接收一个 data") }