From 8c7884bcc3e4acd0f62ca782586f7e13167b559a Mon Sep 17 00:00:00 2001 From: eson <9673575+githubcontent@user.noreply.gitee.com> Date: Thu, 20 Jul 2023 15:21:03 +0800 Subject: [PATCH] =?UTF-8?q?=E8=A7=A3=E5=86=B3=E9=83=A8=E5=88=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- goctl_template/api/handler.tpl | 62 ++---------- goctl_template_backend/api/handler.tpl | 61 ++---------- .../handler/usergoogleloginhandler.go | 67 ++----------- .../handler/usergoogleloginhandler_old.go1 | 78 +++++++++++++++ .../internal/logic/usergettypelogic.go | 6 +- .../internal/logic/usergoogleloginlogic.go | 12 +++ utils/basic/basic.go | 4 +- utils/basic/request_parse.go | 94 +++++++++++++++++++ 8 files changed, 210 insertions(+), 174 deletions(-) create mode 100644 server/home-user-auth/internal/handler/usergoogleloginhandler_old.go1 create mode 100644 utils/basic/request_parse.go diff --git a/goctl_template/api/handler.tpl b/goctl_template/api/handler.tpl index 3b228256..cf9cbfbc 100644 --- a/goctl_template/api/handler.tpl +++ b/goctl_template/api/handler.tpl @@ -2,12 +2,7 @@ package {{.PkgName}} import ( "net/http" - "errors" - - "github.com/zeromicro/go-zero/core/logx" - "github.com/zeromicro/go-zero/rest/httpx" - - "fusenapi/utils/auth" + "fusenapi/utils/basic" {{.ImportPackages}} @@ -16,61 +11,16 @@ import ( func {{.HandlerName}}(svcCtx *svc.ServiceContext) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { - var ( - // 定义错误变量 - err error - // 定义用户信息变量 - userinfo *auth.UserInfo - ) - // 解析JWT token,并对空用户进行判断 - claims, err := svcCtx.ParseJwtToken(r) - // 如果解析JWT token出错,则返回未授权的JSON响应并记录错误消息 - if err != nil { - httpx.OkJsonCtx(r.Context(), w, &basic.Response{ - Code: 401, // 返回401状态码,表示未授权 - Message: "unauthorized", // 返回未授权信息 - }) - logx.Info("unauthorized:", err.Error()) // 记录错误日志 - return - } - - if claims != nil { - // 从token中获取对应的用户信息 - userinfo, err = auth.GetUserInfoFormMapClaims(claims) - // 如果获取用户信息出错,则返回未授权的JSON响应并记录错误消息 - if err != nil { - httpx.OkJsonCtx(r.Context(), w, &basic.Response{ - Code: 401, - Message: "unauthorized", - }) - logx.Info("unauthorized:", err.Error()) - return - } - } else { - // 如果claims为nil,则认为用户身份为白板用户 - userinfo = &auth.UserInfo{UserId: 0, GuestId: 0} - } - {{if .HasRequest}}var req types.{{.RequestType}} - // 如果端点有请求结构体,则使用httpx.Parse方法从HTTP请求体中解析请求数据 - if err := httpx.Parse(r, &req); err != nil { - httpx.OkJsonCtx(r.Context(), w, &basic.Response{ - Code: 510, - Message: "parameter error", - }) - logx.Info(err) + userinfo, err := basic.RequestParse(w, r, svcCtx, &req) + if err != nil { return } + // 创建一个业务逻辑层实例 {{end}}l := {{.LogicName}}.New{{.LogicType}}(r.Context(), svcCtx) {{if .HasResp}}resp{{end}} := l.{{.Call}}({{if .HasRequest}}&req, {{end}}userinfo) - // 如果响应不为nil,则使用httpx.OkJsonCtx方法返回JSON响应; - if resp != nil { - {{if .HasResp}}httpx.OkJsonCtx(r.Context(), w, resp){{else}}httpx.Ok(w){{end}} - } else { - err := errors.New("server logic is error, resp must not be nil") - httpx.ErrorCtx(r.Context(), w, err) - logx.Error(err) - } + + basic.AfterLogic(w, r, resp) } } diff --git a/goctl_template_backend/api/handler.tpl b/goctl_template_backend/api/handler.tpl index 7ca8f282..e6f0d377 100644 --- a/goctl_template_backend/api/handler.tpl +++ b/goctl_template_backend/api/handler.tpl @@ -2,12 +2,7 @@ package {{.PkgName}} import ( "net/http" - "errors" - - "github.com/zeromicro/go-zero/core/logx" - "github.com/zeromicro/go-zero/rest/httpx" - - "fusenapi/utils/auth" + "fusenapi/utils/basic" {{.ImportPackages}} @@ -15,60 +10,16 @@ import ( func {{.HandlerName}}(svcCtx *svc.ServiceContext) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { - - var ( - // 定义错误变量 - err error - // 定义用户信息变量 - userinfo *auth.BackendUserInfo - ) - // 解析JWT token,并对空用户进行判断 - claims, err := svcCtx.ParseJwtToken(r) - // 如果解析JWT token出错,则返回未授权的JSON响应并记录错误消息 - if err != nil || claims == nil { - httpx.OkJsonCtx(r.Context(), w, &basic.Response{ - Code: 401, // 返回401状态码,表示未授权 - Message: "unauthorized", // 返回未授权信息 - }) - logx.Info("unauthorized:", err.Error()) // 记录错误日志 - return - } - - - - // 从token中获取对应的用户信息 - userinfo, err = auth.GetBackendUserInfoFormMapClaims(claims) - // 如果获取用户信息出错,则返回未授权的JSON响应并记录错误消息 - if err != nil { - httpx.OkJsonCtx(r.Context(), w, &basic.Response{ - Code: 401, - Message: "unauthorized", - }) - logx.Info("unauthorized:", err.Error()) - return - } - - {{if .HasRequest}}var req types.{{.RequestType}} - // 如果端点有请求结构体,则使用httpx.Parse方法从HTTP请求体中解析请求数据 - if err := httpx.Parse(r, &req); err != nil { - httpx.OkJsonCtx(r.Context(), w, &basic.Response{ - Code: 510, - Message: "parameter error", - }) - logx.Info(err) + userinfo, err := basic.RequestParse(w, r, svcCtx, &req) + if err != nil { return } + // 创建一个业务逻辑层实例 {{end}}l := {{.LogicName}}.New{{.LogicType}}(r.Context(), svcCtx) {{if .HasResp}}resp{{end}} := l.{{.Call}}({{if .HasRequest}}&req, {{end}}userinfo) - // 如果响应不为nil,则使用httpx.OkJsonCtx方法返回JSON响应; - if resp != nil { - {{if .HasResp}}httpx.OkJsonCtx(r.Context(), w, resp){{else}}httpx.Ok(w){{end}} - } else { - err := errors.New("server logic is error, resp must not be nil") - httpx.ErrorCtx(r.Context(), w, err) - logx.Error(err) - } + + basic.AfterLogic(w, r, resp) } } diff --git a/server/home-user-auth/internal/handler/usergoogleloginhandler.go b/server/home-user-auth/internal/handler/usergoogleloginhandler.go index 125bd876..33c0e06d 100644 --- a/server/home-user-auth/internal/handler/usergoogleloginhandler.go +++ b/server/home-user-auth/internal/handler/usergoogleloginhandler.go @@ -1,13 +1,9 @@ package handler import ( - "errors" "net/http" + "reflect" - "github.com/zeromicro/go-zero/core/logx" - "github.com/zeromicro/go-zero/rest/httpx" - - "fusenapi/utils/auth" "fusenapi/utils/basic" "fusenapi/server/home-user-auth/internal/logic" @@ -18,67 +14,22 @@ import ( func UserGoogleLoginHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { - var ( - // 定义错误变量 - err error - // 定义用户信息变量 - userinfo *auth.UserInfo - ) - // 解析JWT token,并对空用户进行判断 - claims, err := svcCtx.ParseJwtToken(r) - // 如果解析JWT token出错,则返回未授权的JSON响应并记录错误消息 - if err != nil { - httpx.OkJsonCtx(r.Context(), w, &basic.Response{ - Code: 401, // 返回401状态码,表示未授权 - Message: "unauthorized", // 返回未授权信息 - }) - logx.Info("unauthorized:", err.Error()) // 记录错误日志 - return - } - - if claims != nil { - // 从token中获取对应的用户信息 - userinfo, err = auth.GetUserInfoFormMapClaims(claims) - // 如果获取用户信息出错,则返回未授权的JSON响应并记录错误消息 - if err != nil { - httpx.OkJsonCtx(r.Context(), w, &basic.Response{ - Code: 401, - Message: "unauthorized", - }) - logx.Info("unauthorized:", err.Error()) - return - } - } else { - // 如果claims为nil,则认为用户身份为白板用户 - userinfo = &auth.UserInfo{UserId: 0, GuestId: 0} - } - var req types.RequestGoogleLogin - // 如果端点有请求结构体,则使用httpx.Parse方法从HTTP请求体中解析请求数据 - if err := httpx.Parse(r, &req); err != nil { - httpx.OkJsonCtx(r.Context(), w, &basic.Response{ - Code: 510, - Message: "parameter error", - }) - logx.Info(err) + userinfo, err := basic.RequestParse(w, r, svcCtx, &req) + if err != nil { return } // 创建一个业务逻辑层实例 l := logic.NewUserGoogleLoginLogic(r.Context(), svcCtx) + + rl := reflect.ValueOf(l) + basic.BeforeLogic(w, r, rl) + resp := l.UserGoogleLogin(&req, userinfo) - // 如果响应不为nil,则使用httpx.OkJsonCtx方法返回JSON响应; - if resp != nil { - if resp.IsRewriteHandler() { - resp.RewriteHandler(w, r) - } else { - httpx.OkJsonCtx(r.Context(), w, resp) - } - } else { - err := errors.New("server logic is error, resp must not be nil") - httpx.ErrorCtx(r.Context(), w, err) - logx.Error(err) + if !basic.AfterLogic(w, r, resp, rl) { + basic.NormalAfterLogic(w, r, resp) } } } diff --git a/server/home-user-auth/internal/handler/usergoogleloginhandler_old.go1 b/server/home-user-auth/internal/handler/usergoogleloginhandler_old.go1 new file mode 100644 index 00000000..934eebeb --- /dev/null +++ b/server/home-user-auth/internal/handler/usergoogleloginhandler_old.go1 @@ -0,0 +1,78 @@ +package handler + +import ( + "errors" + "net/http" + + "github.com/zeromicro/go-zero/core/logx" + "github.com/zeromicro/go-zero/rest/httpx" + + "fusenapi/utils/auth" + "fusenapi/utils/basic" + + "fusenapi/server/home-user-auth/internal/logic" + "fusenapi/server/home-user-auth/internal/svc" + "fusenapi/server/home-user-auth/internal/types" +) + +func UserGoogleLoginHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + + var ( + // 定义错误变量 + err error + // 定义用户信息变量 + userinfo *auth.UserInfo + ) + // 解析JWT token,并对空用户进行判断 + claims, err := svcCtx.ParseJwtToken(r) + // 如果解析JWT token出错,则返回未授权的JSON响应并记录错误消息 + if err != nil { + httpx.OkJsonCtx(r.Context(), w, &basic.Response{ + Code: 401, // 返回401状态码,表示未授权 + Message: "unauthorized", // 返回未授权信息 + }) + logx.Info("unauthorized:", err.Error()) // 记录错误日志 + return + } + + if claims != nil { + // 从token中获取对应的用户信息 + userinfo, err = auth.GetUserInfoFormMapClaims(claims) + // 如果获取用户信息出错,则返回未授权的JSON响应并记录错误消息 + if err != nil { + httpx.OkJsonCtx(r.Context(), w, &basic.Response{ + Code: 401, + Message: "unauthorized", + }) + logx.Info("unauthorized:", err.Error()) + return + } + } else { + // 如果claims为nil,则认为用户身份为白板用户 + userinfo = &auth.UserInfo{UserId: 0, GuestId: 0} + } + + var req types.RequestGoogleLogin + // 如果端点有请求结构体,则使用httpx.Parse方法从HTTP请求体中解析请求数据 + if err := httpx.Parse(r, &req); err != nil { + httpx.OkJsonCtx(r.Context(), w, &basic.Response{ + Code: 510, + Message: "parameter error", + }) + logx.Info(err) + return + } + // 创建一个业务逻辑层实例 + l := logic.NewUserGoogleLoginLogic(r.Context(), svcCtx) + resp := l.UserGoogleLogin(&req, userinfo) + // 如果响应不为nil,则使用httpx.OkJsonCtx方法返回JSON响应; + if resp != nil { + httpx.OkJsonCtx(r.Context(), w, resp) + } else { + err := errors.New("server logic is error, resp must not be nil") + httpx.ErrorCtx(r.Context(), w, err) + logx.Error(err) + } + } +} diff --git a/server/home-user-auth/internal/logic/usergettypelogic.go b/server/home-user-auth/internal/logic/usergettypelogic.go index 08ada2f2..850849db 100644 --- a/server/home-user-auth/internal/logic/usergettypelogic.go +++ b/server/home-user-auth/internal/logic/usergettypelogic.go @@ -27,9 +27,9 @@ func NewUserGetTypeLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UserG func (l *UserGetTypeLogic) UserGetType(req *types.Request, userinfo *auth.UserInfo) (resp *basic.Response) { - if userinfo.GetIdType() != auth.IDTYPE_User { - return resp.SetStatus(basic.CodeUnAuth) - } + // if userinfo.GetIdType() != auth.IDTYPE_User { + // return resp.SetStatus(basic.CodeUnAuth) + // } // 返回值必须调用Set重新返回, resp可以空指针调用 resp.SetStatus(basic.CodeOK, data) data, err := l.svcCtx.AllModels.FsCanteenType.FindAllGetType(l.ctx) diff --git a/server/home-user-auth/internal/logic/usergoogleloginlogic.go b/server/home-user-auth/internal/logic/usergoogleloginlogic.go index ba8efec7..12b9b56e 100644 --- a/server/home-user-auth/internal/logic/usergoogleloginlogic.go +++ b/server/home-user-auth/internal/logic/usergoogleloginlogic.go @@ -35,6 +35,16 @@ func NewUserGoogleLoginLogic(ctx context.Context, svcCtx *svc.ServiceContext) *U } } +func (l *UserGoogleLoginLogic) BeforeLogic(w http.ResponseWriter, r *http.Request) { + log.Println(r, w) +} + +func (l *UserGoogleLoginLogic) AfterLogic(w http.ResponseWriter, r *http.Request, resp *basic.Response) { + log.Println(resp.Message) + http.Redirect(w, r, "http://localhost:9900/?token="+resp.Message, http.StatusMovedPermanently) + // log.Println(r, w) +} + func (l *UserGoogleLoginLogic) UserGoogleLogin(req *types.RequestGoogleLogin, userinfo *auth.UserInfo) (resp *basic.Response) { // 返回值必须调用Set重新返回, resp可以空指针调用 resp.SetStatus(basic.CodeOK, data) // userinfo 传入值时, 一定不为null @@ -77,11 +87,13 @@ func (l *UserGoogleLoginLogic) UserGoogleLogin(req *types.RequestGoogleLogin, us log.Println(r.Json()) googleId := r.Json().Get("id").Int() + return resp.Set(304, "21321321") user, err := l.svcCtx.AllModels.FsUser.FindUserByGoogleId(context.TODO(), googleId) log.Println(user) if err != nil { if err != gorm.ErrRecordNotFound { logx.Error(err) + return resp.SetStatus(basic.CodeDbSqlErr) } diff --git a/utils/basic/basic.go b/utils/basic/basic.go index a33bd006..7ec3d02f 100644 --- a/utils/basic/basic.go +++ b/utils/basic/basic.go @@ -242,11 +242,11 @@ func (resp *Response) SetRewriteHandler(do http.HandlerFunc) *Response { } // RewriteHandler -func (resp *Response) RewriteHandler(w http.ResponseWriter, r *http.Request) { +func (resp *Response) rewriteHandler(w http.ResponseWriter, r *http.Request) { resp.rewriteHandlerFunc(w, r) } // Set 设置Response的Code和Message值 -func (resp *Response) IsRewriteHandler() bool { +func (resp *Response) isRewriteHandler() bool { return resp.rewriteHandlerFunc != nil } diff --git a/utils/basic/request_parse.go b/utils/basic/request_parse.go new file mode 100644 index 00000000..4533e64a --- /dev/null +++ b/utils/basic/request_parse.go @@ -0,0 +1,94 @@ +package basic + +import ( + "errors" + "fusenapi/utils/auth" + "net/http" + "reflect" + + "github.com/golang-jwt/jwt" + "github.com/zeromicro/go-zero/core/logx" + "github.com/zeromicro/go-zero/rest/httpx" +) + +type IJWTParse interface { + ParseJwtToken(r *http.Request) (jwt.MapClaims, error) +} + +func BeforeLogic(w http.ResponseWriter, r *http.Request, l reflect.Value) (isNext bool) { + + m := l.MethodByName("BeforeLogic") + if m.IsValid() { + result := m.Call([]reflect.Value{reflect.ValueOf(w), reflect.ValueOf(r)}) + if len(result) != 0 { + return false + } + } + return true +} + +func AfterLogic(w http.ResponseWriter, r *http.Request, resp *Response, l reflect.Value) bool { + + m := l.MethodByName("AfterLogic") + if m.IsValid() { + m.Call([]reflect.Value{reflect.ValueOf(w), reflect.ValueOf(r), reflect.ValueOf(resp)}) + return true + } + return false +} + +func NormalAfterLogic(w http.ResponseWriter, r *http.Request, resp *Response) { + // 如果响应不为nil,则使用httpx.OkJsonCtx方法返回JSON响应; + if resp != nil { + httpx.OkJsonCtx(r.Context(), w, resp) + } else { + err := errors.New("server logic is error, resp must not be nil") + httpx.ErrorCtx(r.Context(), w, err) + logx.Error(err) + } +} + +func RequestParse(w http.ResponseWriter, r *http.Request, svcCtx IJWTParse, LogicRequest any) (userinfo *auth.UserInfo, err error) { + + // 解析JWT token,并对空用户进行判断 + claims, err := svcCtx.ParseJwtToken(r) + // 如果解析JWT token出错,则返回未授权的JSON响应并记录错误消息 + if err != nil { + httpx.OkJsonCtx(r.Context(), w, &Response{ + Code: 401, // 返回401状态码,表示未授权 + Message: "unauthorized", // 返回未授权信息 + }) + logx.Info("unauthorized:", err.Error()) // 记录错误日志 + return + } + + if claims != nil { + // 从token中获取对应的用户信息 + userinfo, err = auth.GetUserInfoFormMapClaims(claims) + // 如果获取用户信息出错,则返回未授权的JSON响应并记录错误消息 + if err != nil { + httpx.OkJsonCtx(r.Context(), w, &Response{ + Code: 401, + Message: "unauthorized", + }) + logx.Info("unauthorized:", err.Error()) + return + } + } else { + // 如果claims为nil,则认为用户身份为白板用户 + userinfo = &auth.UserInfo{UserId: 0, GuestId: 0} + } + + // var req types.RequestGoogleLogin + // 如果端点有请求结构体,则使用httpx.Parse方法从HTTP请求体中解析请求数据 + if err = httpx.Parse(r, LogicRequest); err != nil { + httpx.OkJsonCtx(r.Context(), w, &Response{ + Code: 510, + Message: "parameter error", + }) + logx.Info(err) + return + } + + return userinfo, err +}