解决部分

This commit is contained in:
eson 2023-07-20 15:21:03 +08:00
parent 3e1d4d2e4f
commit 8c7884bcc3
8 changed files with 210 additions and 174 deletions

View File

@ -2,12 +2,7 @@ package {{.PkgName}}
import ( import (
"net/http" "net/http"
"errors"
"github.com/zeromicro/go-zero/core/logx"
"github.com/zeromicro/go-zero/rest/httpx"
"fusenapi/utils/auth"
"fusenapi/utils/basic" "fusenapi/utils/basic"
{{.ImportPackages}} {{.ImportPackages}}
@ -16,61 +11,16 @@ import (
func {{.HandlerName}}(svcCtx *svc.ServiceContext) http.HandlerFunc { func {{.HandlerName}}(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) { 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}} {{if .HasRequest}}var req types.{{.RequestType}}
// 如果端点有请求结构体则使用httpx.Parse方法从HTTP请求体中解析请求数据 userinfo, err := basic.RequestParse(w, r, svcCtx, &req)
if err := httpx.Parse(r, &req); err != nil { if err != nil {
httpx.OkJsonCtx(r.Context(), w, &basic.Response{
Code: 510,
Message: "parameter error",
})
logx.Info(err)
return return
} }
// 创建一个业务逻辑层实例 // 创建一个业务逻辑层实例
{{end}}l := {{.LogicName}}.New{{.LogicType}}(r.Context(), svcCtx) {{end}}l := {{.LogicName}}.New{{.LogicType}}(r.Context(), svcCtx)
{{if .HasResp}}resp{{end}} := l.{{.Call}}({{if .HasRequest}}&req, {{end}}userinfo) {{if .HasResp}}resp{{end}} := l.{{.Call}}({{if .HasRequest}}&req, {{end}}userinfo)
// 如果响应不为nil则使用httpx.OkJsonCtx方法返回JSON响应;
if resp != nil { basic.AfterLogic(w, r, resp)
{{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)
}
} }
} }

View File

@ -2,12 +2,7 @@ package {{.PkgName}}
import ( import (
"net/http" "net/http"
"errors"
"github.com/zeromicro/go-zero/core/logx"
"github.com/zeromicro/go-zero/rest/httpx"
"fusenapi/utils/auth"
"fusenapi/utils/basic" "fusenapi/utils/basic"
{{.ImportPackages}} {{.ImportPackages}}
@ -15,60 +10,16 @@ import (
func {{.HandlerName}}(svcCtx *svc.ServiceContext) http.HandlerFunc { func {{.HandlerName}}(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) { 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}} {{if .HasRequest}}var req types.{{.RequestType}}
// 如果端点有请求结构体则使用httpx.Parse方法从HTTP请求体中解析请求数据 userinfo, err := basic.RequestParse(w, r, svcCtx, &req)
if err := httpx.Parse(r, &req); err != nil { if err != nil {
httpx.OkJsonCtx(r.Context(), w, &basic.Response{
Code: 510,
Message: "parameter error",
})
logx.Info(err)
return return
} }
// 创建一个业务逻辑层实例 // 创建一个业务逻辑层实例
{{end}}l := {{.LogicName}}.New{{.LogicType}}(r.Context(), svcCtx) {{end}}l := {{.LogicName}}.New{{.LogicType}}(r.Context(), svcCtx)
{{if .HasResp}}resp{{end}} := l.{{.Call}}({{if .HasRequest}}&req, {{end}}userinfo) {{if .HasResp}}resp{{end}} := l.{{.Call}}({{if .HasRequest}}&req, {{end}}userinfo)
// 如果响应不为nil则使用httpx.OkJsonCtx方法返回JSON响应;
if resp != nil { basic.AfterLogic(w, r, resp)
{{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)
}
} }
} }

View File

@ -1,13 +1,9 @@
package handler package handler
import ( import (
"errors"
"net/http" "net/http"
"reflect"
"github.com/zeromicro/go-zero/core/logx"
"github.com/zeromicro/go-zero/rest/httpx"
"fusenapi/utils/auth"
"fusenapi/utils/basic" "fusenapi/utils/basic"
"fusenapi/server/home-user-auth/internal/logic" "fusenapi/server/home-user-auth/internal/logic"
@ -18,67 +14,22 @@ import (
func UserGoogleLoginHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { func UserGoogleLoginHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) { 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 var req types.RequestGoogleLogin
// 如果端点有请求结构体则使用httpx.Parse方法从HTTP请求体中解析请求数据 userinfo, err := basic.RequestParse(w, r, svcCtx, &req)
if err := httpx.Parse(r, &req); err != nil { if err != nil {
httpx.OkJsonCtx(r.Context(), w, &basic.Response{
Code: 510,
Message: "parameter error",
})
logx.Info(err)
return return
} }
// 创建一个业务逻辑层实例 // 创建一个业务逻辑层实例
l := logic.NewUserGoogleLoginLogic(r.Context(), svcCtx) l := logic.NewUserGoogleLoginLogic(r.Context(), svcCtx)
rl := reflect.ValueOf(l)
basic.BeforeLogic(w, r, rl)
resp := l.UserGoogleLogin(&req, userinfo) resp := l.UserGoogleLogin(&req, userinfo)
// 如果响应不为nil则使用httpx.OkJsonCtx方法返回JSON响应; if !basic.AfterLogic(w, r, resp, rl) {
if resp != nil { basic.NormalAfterLogic(w, r, resp)
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)
} }
} }
} }

View File

@ -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)
}
}
}

View File

@ -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) { func (l *UserGetTypeLogic) UserGetType(req *types.Request, userinfo *auth.UserInfo) (resp *basic.Response) {
if userinfo.GetIdType() != auth.IDTYPE_User { // if userinfo.GetIdType() != auth.IDTYPE_User {
return resp.SetStatus(basic.CodeUnAuth) // return resp.SetStatus(basic.CodeUnAuth)
} // }
// 返回值必须调用Set重新返回, resp可以空指针调用 resp.SetStatus(basic.CodeOK, data) // 返回值必须调用Set重新返回, resp可以空指针调用 resp.SetStatus(basic.CodeOK, data)
data, err := l.svcCtx.AllModels.FsCanteenType.FindAllGetType(l.ctx) data, err := l.svcCtx.AllModels.FsCanteenType.FindAllGetType(l.ctx)

View File

@ -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) { func (l *UserGoogleLoginLogic) UserGoogleLogin(req *types.RequestGoogleLogin, userinfo *auth.UserInfo) (resp *basic.Response) {
// 返回值必须调用Set重新返回, resp可以空指针调用 resp.SetStatus(basic.CodeOK, data) // 返回值必须调用Set重新返回, resp可以空指针调用 resp.SetStatus(basic.CodeOK, data)
// userinfo 传入值时, 一定不为null // userinfo 传入值时, 一定不为null
@ -77,11 +87,13 @@ func (l *UserGoogleLoginLogic) UserGoogleLogin(req *types.RequestGoogleLogin, us
log.Println(r.Json()) log.Println(r.Json())
googleId := r.Json().Get("id").Int() googleId := r.Json().Get("id").Int()
return resp.Set(304, "21321321")
user, err := l.svcCtx.AllModels.FsUser.FindUserByGoogleId(context.TODO(), googleId) user, err := l.svcCtx.AllModels.FsUser.FindUserByGoogleId(context.TODO(), googleId)
log.Println(user) log.Println(user)
if err != nil { if err != nil {
if err != gorm.ErrRecordNotFound { if err != gorm.ErrRecordNotFound {
logx.Error(err) logx.Error(err)
return resp.SetStatus(basic.CodeDbSqlErr) return resp.SetStatus(basic.CodeDbSqlErr)
} }

View File

@ -242,11 +242,11 @@ func (resp *Response) SetRewriteHandler(do http.HandlerFunc) *Response {
} }
// RewriteHandler // 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) resp.rewriteHandlerFunc(w, r)
} }
// Set 设置Response的Code和Message值 // Set 设置Response的Code和Message值
func (resp *Response) IsRewriteHandler() bool { func (resp *Response) isRewriteHandler() bool {
return resp.rewriteHandlerFunc != nil return resp.rewriteHandlerFunc != nil
} }

View File

@ -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
}