修改公共方法为basic入口

This commit is contained in:
eson 2023-06-12 17:28:49 +08:00
parent 15e423fb4e
commit 0f31e298d4
12 changed files with 79 additions and 33 deletions

View File

@ -8,7 +8,8 @@ import (
"github.com/zeromicro/go-zero/rest/httpx"
"fusenapi/utils/auth"
"fusenapi/utils/basic"
{{.ImportPackages}}
)
@ -18,7 +19,7 @@ func {{.HandlerName}}(svcCtx *svc.ServiceContext) http.HandlerFunc {
claims, err := svcCtx.ParseJwtToken(r)
// 如果解析出错则返回未授权的JSON响应并记录错误消息
if err != nil {
httpx.OkJsonCtx(r.Context(), w, &types.Response{
httpx.OkJsonCtx(r.Context(), w, &basic.Response{
Code: 401,
Message: "unauthorized",
})
@ -30,7 +31,7 @@ func {{.HandlerName}}(svcCtx *svc.ServiceContext) http.HandlerFunc {
userinfo, err := auth.GetUserInfoFormMapClaims(claims)
// 如果获取用户信息出错则返回未授权的JSON响应并记录错误消息
if err != nil {
httpx.OkJsonCtx(r.Context(), w, &types.Response{
httpx.OkJsonCtx(r.Context(), w, &basic.Response{
Code: 401,
Message: "unauthorized",
})
@ -41,7 +42,7 @@ func {{.HandlerName}}(svcCtx *svc.ServiceContext) http.HandlerFunc {
{{if .HasRequest}}var req types.{{.RequestType}}
// 如果端点有请求结构体则使用httpx.Parse方法从HTTP请求体中解析请求数据
if err := httpx.Parse(r, &req); err != nil {
httpx.OkJsonCtx(r.Context(), w, &types.Response{
httpx.OkJsonCtx(r.Context(), w, &basic.Response{
Code: 510,
Message: "parameter error",
})
@ -60,6 +61,5 @@ func {{.HandlerName}}(svcCtx *svc.ServiceContext) http.HandlerFunc {
httpx.ErrorCtx(r.Context(), w, err)
logx.Error(err)
}
return
}
}

View File

@ -21,7 +21,7 @@ func New{{.logic}}(ctx context.Context, svcCtx *svc.ServiceContext) *{{.logic}}
}
}
func (l *{{.logic}}) {{.function}}({{.request}}, userinfo *auth.UserInfo) (resp *types.Response) {
func (l *{{.logic}}) {{.function}}({{.request}}, userinfo *auth.UserInfo) (resp *basic.Response) {
// 返回值必须调用Set重新返回, resp可以空指针调用 resp.SetStatus(basic.CodeOK, data)
// userinfo 传入值时, 一定不为null

View File

@ -8,6 +8,7 @@ import (
"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"
@ -20,7 +21,7 @@ func UserAddressListHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
claims, err := svcCtx.ParseJwtToken(r)
// 如果解析出错则返回未授权的JSON响应并记录错误消息
if err != nil {
httpx.OkJsonCtx(r.Context(), w, &types.Response{
httpx.OkJsonCtx(r.Context(), w, &basic.Response{
Code: 401,
Message: "unauthorized",
})
@ -32,7 +33,7 @@ func UserAddressListHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
userinfo, err := auth.GetUserInfoFormMapClaims(claims)
// 如果获取用户信息出错则返回未授权的JSON响应并记录错误消息
if err != nil {
httpx.OkJsonCtx(r.Context(), w, &types.Response{
httpx.OkJsonCtx(r.Context(), w, &basic.Response{
Code: 401,
Message: "unauthorized",
})
@ -43,7 +44,7 @@ func UserAddressListHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
var req types.Request
// 如果端点有请求结构体则使用httpx.Parse方法从HTTP请求体中解析请求数据
if err := httpx.Parse(r, &req); err != nil {
httpx.OkJsonCtx(r.Context(), w, &types.Response{
httpx.OkJsonCtx(r.Context(), w, &basic.Response{
Code: 510,
Message: "parameter error",
})
@ -62,6 +63,5 @@ func UserAddressListHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
httpx.ErrorCtx(r.Context(), w, err)
logx.Error(err)
}
return
}
}

View File

@ -8,6 +8,7 @@ import (
"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"
@ -20,7 +21,7 @@ func UserBasicInfoHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
claims, err := svcCtx.ParseJwtToken(r)
// 如果解析出错则返回未授权的JSON响应并记录错误消息
if err != nil {
httpx.OkJsonCtx(r.Context(), w, &types.Response{
httpx.OkJsonCtx(r.Context(), w, &basic.Response{
Code: 401,
Message: "unauthorized",
})
@ -32,7 +33,7 @@ func UserBasicInfoHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
userinfo, err := auth.GetUserInfoFormMapClaims(claims)
// 如果获取用户信息出错则返回未授权的JSON响应并记录错误消息
if err != nil {
httpx.OkJsonCtx(r.Context(), w, &types.Response{
httpx.OkJsonCtx(r.Context(), w, &basic.Response{
Code: 401,
Message: "unauthorized",
})
@ -43,7 +44,7 @@ func UserBasicInfoHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
var req types.Request
// 如果端点有请求结构体则使用httpx.Parse方法从HTTP请求体中解析请求数据
if err := httpx.Parse(r, &req); err != nil {
httpx.OkJsonCtx(r.Context(), w, &types.Response{
httpx.OkJsonCtx(r.Context(), w, &basic.Response{
Code: 510,
Message: "parameter error",
})
@ -62,6 +63,5 @@ func UserBasicInfoHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
httpx.ErrorCtx(r.Context(), w, err)
logx.Error(err)
}
return
}
}

View File

@ -8,6 +8,7 @@ import (
"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"
@ -20,7 +21,7 @@ func UserFontsHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
claims, err := svcCtx.ParseJwtToken(r)
// 如果解析出错则返回未授权的JSON响应并记录错误消息
if err != nil {
httpx.OkJsonCtx(r.Context(), w, &types.Response{
httpx.OkJsonCtx(r.Context(), w, &basic.Response{
Code: 401,
Message: "unauthorized",
})
@ -32,7 +33,7 @@ func UserFontsHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
userinfo, err := auth.GetUserInfoFormMapClaims(claims)
// 如果获取用户信息出错则返回未授权的JSON响应并记录错误消息
if err != nil {
httpx.OkJsonCtx(r.Context(), w, &types.Response{
httpx.OkJsonCtx(r.Context(), w, &basic.Response{
Code: 401,
Message: "unauthorized",
})
@ -43,7 +44,7 @@ func UserFontsHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
var req types.Request
// 如果端点有请求结构体则使用httpx.Parse方法从HTTP请求体中解析请求数据
if err := httpx.Parse(r, &req); err != nil {
httpx.OkJsonCtx(r.Context(), w, &types.Response{
httpx.OkJsonCtx(r.Context(), w, &basic.Response{
Code: 510,
Message: "parameter error",
})
@ -62,6 +63,5 @@ func UserFontsHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
httpx.ErrorCtx(r.Context(), w, err)
logx.Error(err)
}
return
}
}

View File

@ -8,6 +8,7 @@ import (
"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"
@ -20,7 +21,7 @@ func UserGetTypeHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
claims, err := svcCtx.ParseJwtToken(r)
// 如果解析出错则返回未授权的JSON响应并记录错误消息
if err != nil {
httpx.OkJsonCtx(r.Context(), w, &types.Response{
httpx.OkJsonCtx(r.Context(), w, &basic.Response{
Code: 401,
Message: "unauthorized",
})
@ -32,7 +33,7 @@ func UserGetTypeHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
userinfo, err := auth.GetUserInfoFormMapClaims(claims)
// 如果获取用户信息出错则返回未授权的JSON响应并记录错误消息
if err != nil {
httpx.OkJsonCtx(r.Context(), w, &types.Response{
httpx.OkJsonCtx(r.Context(), w, &basic.Response{
Code: 401,
Message: "unauthorized",
})
@ -43,7 +44,7 @@ func UserGetTypeHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
var req types.Request
// 如果端点有请求结构体则使用httpx.Parse方法从HTTP请求体中解析请求数据
if err := httpx.Parse(r, &req); err != nil {
httpx.OkJsonCtx(r.Context(), w, &types.Response{
httpx.OkJsonCtx(r.Context(), w, &basic.Response{
Code: 510,
Message: "parameter error",
})
@ -62,6 +63,5 @@ func UserGetTypeHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
httpx.ErrorCtx(r.Context(), w, err)
logx.Error(err)
}
return
}
}

View File

@ -21,7 +21,7 @@ func UserLoginHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
var req types.RequestUserLogin
// 如果端点有请求结构体则使用httpx.Parse方法从HTTP请求体中解析请求数据
if err := httpx.Parse(r, &req); err != nil {
httpx.OkJsonCtx(r.Context(), w, &types.Response{
httpx.OkJsonCtx(r.Context(), w, &basic.Response{
Code: 510,
Message: "parameter error",
})

View File

@ -8,6 +8,7 @@ import (
"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"
@ -20,7 +21,7 @@ func UserSaveBasicInfoHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
claims, err := svcCtx.ParseJwtToken(r)
// 如果解析出错则返回未授权的JSON响应并记录错误消息
if err != nil {
httpx.OkJsonCtx(r.Context(), w, &types.Response{
httpx.OkJsonCtx(r.Context(), w, &basic.Response{
Code: 401,
Message: "unauthorized",
})
@ -32,7 +33,7 @@ func UserSaveBasicInfoHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
userinfo, err := auth.GetUserInfoFormMapClaims(claims)
// 如果获取用户信息出错则返回未授权的JSON响应并记录错误消息
if err != nil {
httpx.OkJsonCtx(r.Context(), w, &types.Response{
httpx.OkJsonCtx(r.Context(), w, &basic.Response{
Code: 401,
Message: "unauthorized",
})
@ -43,7 +44,7 @@ func UserSaveBasicInfoHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
var req types.RequestBasicInfoForm
// 如果端点有请求结构体则使用httpx.Parse方法从HTTP请求体中解析请求数据
if err := httpx.Parse(r, &req); err != nil {
httpx.OkJsonCtx(r.Context(), w, &types.Response{
httpx.OkJsonCtx(r.Context(), w, &basic.Response{
Code: 510,
Message: "parameter error",
})
@ -62,6 +63,5 @@ func UserSaveBasicInfoHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
httpx.ErrorCtx(r.Context(), w, err)
logx.Error(err)
}
return
}
}

View File

@ -8,6 +8,7 @@ import (
"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"
@ -20,7 +21,7 @@ func UserStatusConfigHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
claims, err := svcCtx.ParseJwtToken(r)
// 如果解析出错则返回未授权的JSON响应并记录错误消息
if err != nil {
httpx.OkJsonCtx(r.Context(), w, &types.Response{
httpx.OkJsonCtx(r.Context(), w, &basic.Response{
Code: 401,
Message: "unauthorized",
})
@ -32,7 +33,7 @@ func UserStatusConfigHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
userinfo, err := auth.GetUserInfoFormMapClaims(claims)
// 如果获取用户信息出错则返回未授权的JSON响应并记录错误消息
if err != nil {
httpx.OkJsonCtx(r.Context(), w, &types.Response{
httpx.OkJsonCtx(r.Context(), w, &basic.Response{
Code: 401,
Message: "unauthorized",
})
@ -43,7 +44,7 @@ func UserStatusConfigHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
var req types.Request
// 如果端点有请求结构体则使用httpx.Parse方法从HTTP请求体中解析请求数据
if err := httpx.Parse(r, &req); err != nil {
httpx.OkJsonCtx(r.Context(), w, &types.Response{
httpx.OkJsonCtx(r.Context(), w, &basic.Response{
Code: 510,
Message: "parameter error",
})
@ -62,6 +63,5 @@ func UserStatusConfigHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
httpx.ErrorCtx(r.Context(), w, err)
logx.Error(err)
}
return
}
}

View File

@ -26,7 +26,7 @@ func NewUserFontsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UserFon
}
}
func (l *UserFontsLogic) UserFonts(req *types.Request, userinfo *auth.UserInfo) (resp *types.Response) {
func (l *UserFontsLogic) UserFonts(req *types.Request, userinfo *auth.UserInfo) (resp *basic.Response) {
data, err := model.NewFsFontModel(l.svcCtx.MysqlConn).FindAllOrderSortByDesc(l.ctx)
if err != nil {
logx.Error(err)

View File

@ -27,7 +27,7 @@ func NewUserLoginLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UserLog
}
}
func (l *UserLoginLogic) UserLogin(req *types.RequestUserLogin) (resp *types.Response, jwtToken string) {
func (l *UserLoginLogic) UserLogin(req *types.RequestUserLogin) (resp *basic.Response, jwtToken string) {
// 创建一个 FsUserModel 对象 m 并实例化之,该对象用于操作 MySQL 数据库中的用户数据表。
m := model.NewFsUserModel(l.svcCtx.MysqlConn)

View File

@ -15,3 +15,49 @@ var (
CodeUpdateErr = &StatusResponse{5000, "update database error"} // update database logic 错误
)
type Response struct {
Code int `json:"code"`
Message string `json:"msg"`
Data interface{} `json:"data"`
}
// Set 设置Response的Code和Message值
func (resp *Response) Set(Code int, Message string) *Response {
return &Response{
Code: Code,
Message: Message,
}
}
// 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只使用一个参数
func (resp *Response) SetStatus(sr *StatusResponse, data ...interface{}) *Response {
newResp := &Response{
Code: sr.Code,
}
if len(data) == 1 {
newResp.Data = data[0]
}
return newResp
}
// SetStatusWithMessage 设置默认StatusResponse(内部自定义) 非默认msg, 可以带data, data只使用一个参数
func (resp *Response) SetStatusWithMessage(sr *StatusResponse, msg string, data ...interface{}) *Response {
newResp := &Response{
Code: sr.Code,
Message: msg,
}
if len(data) == 1 {
newResp.Data = data[0]
}
return newResp
}