所有序列化, 都加入 BeforeLogic AfterLogic方法. 便于处理 w r的传递的参数

This commit is contained in:
eson
2023-07-21 15:20:18 +08:00
parent 373d5dca45
commit 2825f8ae54
100 changed files with 1499 additions and 454 deletions

View File

@@ -2,16 +2,34 @@ package handler
import (
"net/http"
"reflect"
"fusenapi/utils/basic"
"fusenapi/server/map-library/internal/logic"
"fusenapi/server/map-library/internal/svc"
"fusenapi/server/map-library/internal/types"
)
func GetMapLibraryListHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
l := logic.NewGetMapLibraryListLogic(r.Context(), svcCtx)
resp := l.GetMapLibraryList(userinfo)
var req types.Request
userinfo, err := basic.RequestParse(w, r, svcCtx, &req)
if err != nil {
return
}
// 创建一个业务逻辑层实例
l := logic.NewGetMapLibraryListLogic(r.Context(), svcCtx)
rl := reflect.ValueOf(l)
basic.BeforeLogic(w, r, rl)
resp := l.GetMapLibraryList(&req, userinfo)
if !basic.AfterLogic(w, r, rl) {
basic.NormalAfterLogic(w, r, resp)
}
}
}

View File

@@ -2,17 +2,34 @@ package handler
import (
"net/http"
"reflect"
"fusenapi/utils/basic"
"fusenapi/server/map-library/internal/logic"
"fusenapi/server/map-library/internal/svc"
"fusenapi/server/map-library/internal/types"
)
func SaveMapLibraryHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req types.Request
userinfo, err := basic.RequestParse(w, r, svcCtx, &req)
if err != nil {
return
}
// 创建一个业务逻辑层实例
l := logic.NewSaveMapLibraryLogic(r.Context(), svcCtx)
resp := l.SaveMapLibrary(userinfo, r)
rl := reflect.ValueOf(l)
basic.BeforeLogic(w, r, rl)
resp := l.SaveMapLibrary(&req, userinfo)
if !basic.AfterLogic(w, r, rl) {
basic.NormalAfterLogic(w, r, resp)
}
}
}

View File

@@ -10,6 +10,7 @@ import (
"time"
"context"
"github.com/zeromicro/go-zero/core/logx"
)
@@ -27,7 +28,7 @@ func NewGetMapLibraryListLogic(ctx context.Context, svcCtx *svc.ServiceContext)
}
}
func (l *GetMapLibraryListLogic) GetMapLibraryList(userinfo *auth.UserInfo) (resp *basic.Response) {
func (l *GetMapLibraryListLogic) GetMapLibraryList(req *types.Request, userinfo *auth.UserInfo) (resp *basic.Response) {
if userinfo.GetIdType() != auth.IDTYPE_User {
return resp.SetStatusWithMessage(basic.CodeServiceErr, "please login first")
}

View File

@@ -19,8 +19,9 @@ import (
type SaveMapLibraryLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
ctx context.Context
svcCtx *svc.ServiceContext
bodyData []byte
}
func NewSaveMapLibraryLogic(ctx context.Context, svcCtx *svc.ServiceContext) *SaveMapLibraryLogic {
@@ -31,14 +32,31 @@ func NewSaveMapLibraryLogic(ctx context.Context, svcCtx *svc.ServiceContext) *Sa
}
}
func (l *SaveMapLibraryLogic) SaveMapLibrary(userinfo *auth.UserInfo, r *http.Request) (resp *basic.Response) {
// 处理进入前逻辑w,r
func (l *SaveMapLibraryLogic) BeforeLogic(w http.ResponseWriter, r *http.Request) {
bodyData, err := ioutil.ReadAll(r.Body)
defer r.Body.Close()
if err != nil {
logx.Error(err)
return
}
l.bodyData = bodyData
}
func (l *SaveMapLibraryLogic) SaveMapLibrary(req *types.Request, userinfo *auth.UserInfo) (resp *basic.Response) {
if userinfo.GetIdType() != auth.IDTYPE_User {
return resp.SetStatusWithMessage(basic.CodeServiceErr, "please login first")
}
bodyData, err := ioutil.ReadAll(r.Body)
defer r.Body.Close()
if len(l.bodyData) == 0 {
return resp.SetStatus(basic.CodeApiErr, http.ErrBodyReadAfterClose.Error())
}
var err error
var postData []types.SaveMapLibraryData
if err = json.Unmarshal(bodyData, &postData); err != nil {
if err = json.Unmarshal(l.bodyData, &postData); err != nil {
logx.Error(err)
return resp.SetStatusWithMessage(basic.CodeSaveErr, "param err")
}