From b8edd41e69e4b2b9b6aa525698e27bb447c8ef05 Mon Sep 17 00:00:00 2001 From: laodaming <11058467+laudamine@user.noreply.gitee.com> Date: Wed, 13 Sep 2023 17:33:58 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E8=B4=AD=E7=89=A9=E8=BD=A6?= =?UTF-8?q?=E6=9C=8D=E5=8A=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/shopping-cart/etc/shopping-cart.yaml | 12 ++ .../shopping-cart/internal/config/config.go | 13 ++ .../internal/handler/addtocarthandler.go | 35 +++++ .../internal/handler/deletecarthandler.go | 35 +++++ .../internal/handler/getcartshandler.go | 35 +++++ .../modifycartpurchasequantityhandler.go | 35 +++++ .../shopping-cart/internal/handler/routes.go | 37 +++++ .../internal/logic/addtocartlogic.go | 43 ++++++ .../internal/logic/deletecartlogic.go | 43 ++++++ .../internal/logic/getcartslogic.go | 43 ++++++ .../logic/modifycartpurchasequantitylogic.go | 43 ++++++ .../internal/svc/servicecontext.go | 26 ++++ server/shopping-cart/internal/types/types.go | 137 ++++++++++++++++++ server/shopping-cart/shopping-cart.go | 36 +++++ 14 files changed, 573 insertions(+) create mode 100644 server/shopping-cart/etc/shopping-cart.yaml create mode 100644 server/shopping-cart/internal/config/config.go create mode 100644 server/shopping-cart/internal/handler/addtocarthandler.go create mode 100644 server/shopping-cart/internal/handler/deletecarthandler.go create mode 100644 server/shopping-cart/internal/handler/getcartshandler.go create mode 100644 server/shopping-cart/internal/handler/modifycartpurchasequantityhandler.go create mode 100644 server/shopping-cart/internal/handler/routes.go create mode 100644 server/shopping-cart/internal/logic/addtocartlogic.go create mode 100644 server/shopping-cart/internal/logic/deletecartlogic.go create mode 100644 server/shopping-cart/internal/logic/getcartslogic.go create mode 100644 server/shopping-cart/internal/logic/modifycartpurchasequantitylogic.go create mode 100644 server/shopping-cart/internal/svc/servicecontext.go create mode 100644 server/shopping-cart/internal/types/types.go create mode 100644 server/shopping-cart/shopping-cart.go diff --git a/server/shopping-cart/etc/shopping-cart.yaml b/server/shopping-cart/etc/shopping-cart.yaml new file mode 100644 index 00000000..2b0db3dd --- /dev/null +++ b/server/shopping-cart/etc/shopping-cart.yaml @@ -0,0 +1,12 @@ +Name: shopping-cart +Host: 0.0.0.0 +Port: 9918 +Timeout: 15000 #服务超时时间(毫秒) +SourceMysql: fsreaderwriter:XErSYmLELKMnf3Dh@tcp(fusen.cdmigcvz3rle.us-east-2.rds.amazonaws.com:3306)/fusen +SourceRabbitMq: amqp://rabbit001:rabbit001129@110.41.19.98:5672 +Log: + Stat: false +Auth: + AccessSecret: fusen2023 + AccessExpire: 2592000 + RefreshAfter: 1592000 diff --git a/server/shopping-cart/internal/config/config.go b/server/shopping-cart/internal/config/config.go new file mode 100644 index 00000000..57f6dab8 --- /dev/null +++ b/server/shopping-cart/internal/config/config.go @@ -0,0 +1,13 @@ +package config + +import ( + "fusenapi/server/shopping-cart/internal/types" + "github.com/zeromicro/go-zero/rest" +) + +type Config struct { + rest.RestConf + SourceMysql string + Auth types.Auth + SourceRabbitMq string +} diff --git a/server/shopping-cart/internal/handler/addtocarthandler.go b/server/shopping-cart/internal/handler/addtocarthandler.go new file mode 100644 index 00000000..6fe5e246 --- /dev/null +++ b/server/shopping-cart/internal/handler/addtocarthandler.go @@ -0,0 +1,35 @@ +package handler + +import ( + "net/http" + "reflect" + + "fusenapi/utils/basic" + + "fusenapi/server/shopping-cart/internal/logic" + "fusenapi/server/shopping-cart/internal/svc" + "fusenapi/server/shopping-cart/internal/types" +) + +func AddToCartHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + + var req types.AddToCartReq + userinfo, err := basic.RequestParse(w, r, svcCtx, &req) + if err != nil { + return + } + + // 创建一个业务逻辑层实例 + l := logic.NewAddToCartLogic(r.Context(), svcCtx) + + rl := reflect.ValueOf(l) + basic.BeforeLogic(w, r, rl) + + resp := l.AddToCart(&req, userinfo) + + if !basic.AfterLogic(w, r, rl, resp) { + basic.NormalAfterLogic(w, r, resp) + } + } +} diff --git a/server/shopping-cart/internal/handler/deletecarthandler.go b/server/shopping-cart/internal/handler/deletecarthandler.go new file mode 100644 index 00000000..fa7c4fd0 --- /dev/null +++ b/server/shopping-cart/internal/handler/deletecarthandler.go @@ -0,0 +1,35 @@ +package handler + +import ( + "net/http" + "reflect" + + "fusenapi/utils/basic" + + "fusenapi/server/shopping-cart/internal/logic" + "fusenapi/server/shopping-cart/internal/svc" + "fusenapi/server/shopping-cart/internal/types" +) + +func DeleteCartHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + + var req types.DeleteCartReq + userinfo, err := basic.RequestParse(w, r, svcCtx, &req) + if err != nil { + return + } + + // 创建一个业务逻辑层实例 + l := logic.NewDeleteCartLogic(r.Context(), svcCtx) + + rl := reflect.ValueOf(l) + basic.BeforeLogic(w, r, rl) + + resp := l.DeleteCart(&req, userinfo) + + if !basic.AfterLogic(w, r, rl, resp) { + basic.NormalAfterLogic(w, r, resp) + } + } +} diff --git a/server/shopping-cart/internal/handler/getcartshandler.go b/server/shopping-cart/internal/handler/getcartshandler.go new file mode 100644 index 00000000..8d0a4a0a --- /dev/null +++ b/server/shopping-cart/internal/handler/getcartshandler.go @@ -0,0 +1,35 @@ +package handler + +import ( + "net/http" + "reflect" + + "fusenapi/utils/basic" + + "fusenapi/server/shopping-cart/internal/logic" + "fusenapi/server/shopping-cart/internal/svc" + "fusenapi/server/shopping-cart/internal/types" +) + +func GetCartsHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + + var req types.GetCartsReq + userinfo, err := basic.RequestParse(w, r, svcCtx, &req) + if err != nil { + return + } + + // 创建一个业务逻辑层实例 + l := logic.NewGetCartsLogic(r.Context(), svcCtx) + + rl := reflect.ValueOf(l) + basic.BeforeLogic(w, r, rl) + + resp := l.GetCarts(&req, userinfo) + + if !basic.AfterLogic(w, r, rl, resp) { + basic.NormalAfterLogic(w, r, resp) + } + } +} diff --git a/server/shopping-cart/internal/handler/modifycartpurchasequantityhandler.go b/server/shopping-cart/internal/handler/modifycartpurchasequantityhandler.go new file mode 100644 index 00000000..b04427bf --- /dev/null +++ b/server/shopping-cart/internal/handler/modifycartpurchasequantityhandler.go @@ -0,0 +1,35 @@ +package handler + +import ( + "net/http" + "reflect" + + "fusenapi/utils/basic" + + "fusenapi/server/shopping-cart/internal/logic" + "fusenapi/server/shopping-cart/internal/svc" + "fusenapi/server/shopping-cart/internal/types" +) + +func ModifyCartPurchaseQuantityHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + + var req types.ModifyCartPurchaseQuantityReq + userinfo, err := basic.RequestParse(w, r, svcCtx, &req) + if err != nil { + return + } + + // 创建一个业务逻辑层实例 + l := logic.NewModifyCartPurchaseQuantityLogic(r.Context(), svcCtx) + + rl := reflect.ValueOf(l) + basic.BeforeLogic(w, r, rl) + + resp := l.ModifyCartPurchaseQuantity(&req, userinfo) + + if !basic.AfterLogic(w, r, rl, resp) { + basic.NormalAfterLogic(w, r, resp) + } + } +} diff --git a/server/shopping-cart/internal/handler/routes.go b/server/shopping-cart/internal/handler/routes.go new file mode 100644 index 00000000..08ade841 --- /dev/null +++ b/server/shopping-cart/internal/handler/routes.go @@ -0,0 +1,37 @@ +// Code generated by goctl. DO NOT EDIT. +package handler + +import ( + "net/http" + + "fusenapi/server/shopping-cart/internal/svc" + + "github.com/zeromicro/go-zero/rest" +) + +func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) { + server.AddRoutes( + []rest.Route{ + { + Method: http.MethodPost, + Path: "/api/shopping-cart/add_to_cart", + Handler: AddToCartHandler(serverCtx), + }, + { + Method: http.MethodPost, + Path: "/api/shopping-cart/delete_cart", + Handler: DeleteCartHandler(serverCtx), + }, + { + Method: http.MethodPost, + Path: "/api/shopping-cart/modify_cart_purchase_quantity", + Handler: ModifyCartPurchaseQuantityHandler(serverCtx), + }, + { + Method: http.MethodGet, + Path: "/api/shopping-cart/get_carts", + Handler: GetCartsHandler(serverCtx), + }, + }, + ) +} diff --git a/server/shopping-cart/internal/logic/addtocartlogic.go b/server/shopping-cart/internal/logic/addtocartlogic.go new file mode 100644 index 00000000..e360240d --- /dev/null +++ b/server/shopping-cart/internal/logic/addtocartlogic.go @@ -0,0 +1,43 @@ +package logic + +import ( + "fusenapi/utils/auth" + "fusenapi/utils/basic" + + "context" + + "fusenapi/server/shopping-cart/internal/svc" + "fusenapi/server/shopping-cart/internal/types" + + "github.com/zeromicro/go-zero/core/logx" +) + +type AddToCartLogic struct { + logx.Logger + ctx context.Context + svcCtx *svc.ServiceContext +} + +func NewAddToCartLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AddToCartLogic { + return &AddToCartLogic{ + Logger: logx.WithContext(ctx), + ctx: ctx, + svcCtx: svcCtx, + } +} + +// 处理进入前逻辑w,r +// func (l *AddToCartLogic) BeforeLogic(w http.ResponseWriter, r *http.Request) { +// } + +func (l *AddToCartLogic) AddToCart(req *types.AddToCartReq, userinfo *auth.UserInfo) (resp *basic.Response) { + // 返回值必须调用Set重新返回, resp可以空指针调用 resp.SetStatus(basic.CodeOK, data) + // userinfo 传入值时, 一定不为null + + return resp.SetStatus(basic.CodeOK) +} + +// 处理逻辑后 w,r 如:重定向, resp 必须重新处理 +// func (l *AddToCartLogic) AfterLogic(w http.ResponseWriter, r *http.Request, resp *basic.Response) { +// // httpx.OkJsonCtx(r.Context(), w, resp) +// } diff --git a/server/shopping-cart/internal/logic/deletecartlogic.go b/server/shopping-cart/internal/logic/deletecartlogic.go new file mode 100644 index 00000000..865a811f --- /dev/null +++ b/server/shopping-cart/internal/logic/deletecartlogic.go @@ -0,0 +1,43 @@ +package logic + +import ( + "fusenapi/utils/auth" + "fusenapi/utils/basic" + + "context" + + "fusenapi/server/shopping-cart/internal/svc" + "fusenapi/server/shopping-cart/internal/types" + + "github.com/zeromicro/go-zero/core/logx" +) + +type DeleteCartLogic struct { + logx.Logger + ctx context.Context + svcCtx *svc.ServiceContext +} + +func NewDeleteCartLogic(ctx context.Context, svcCtx *svc.ServiceContext) *DeleteCartLogic { + return &DeleteCartLogic{ + Logger: logx.WithContext(ctx), + ctx: ctx, + svcCtx: svcCtx, + } +} + +// 处理进入前逻辑w,r +// func (l *DeleteCartLogic) BeforeLogic(w http.ResponseWriter, r *http.Request) { +// } + +func (l *DeleteCartLogic) DeleteCart(req *types.DeleteCartReq, userinfo *auth.UserInfo) (resp *basic.Response) { + // 返回值必须调用Set重新返回, resp可以空指针调用 resp.SetStatus(basic.CodeOK, data) + // userinfo 传入值时, 一定不为null + + return resp.SetStatus(basic.CodeOK) +} + +// 处理逻辑后 w,r 如:重定向, resp 必须重新处理 +// func (l *DeleteCartLogic) AfterLogic(w http.ResponseWriter, r *http.Request, resp *basic.Response) { +// // httpx.OkJsonCtx(r.Context(), w, resp) +// } diff --git a/server/shopping-cart/internal/logic/getcartslogic.go b/server/shopping-cart/internal/logic/getcartslogic.go new file mode 100644 index 00000000..097f91b7 --- /dev/null +++ b/server/shopping-cart/internal/logic/getcartslogic.go @@ -0,0 +1,43 @@ +package logic + +import ( + "fusenapi/utils/auth" + "fusenapi/utils/basic" + + "context" + + "fusenapi/server/shopping-cart/internal/svc" + "fusenapi/server/shopping-cart/internal/types" + + "github.com/zeromicro/go-zero/core/logx" +) + +type GetCartsLogic struct { + logx.Logger + ctx context.Context + svcCtx *svc.ServiceContext +} + +func NewGetCartsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetCartsLogic { + return &GetCartsLogic{ + Logger: logx.WithContext(ctx), + ctx: ctx, + svcCtx: svcCtx, + } +} + +// 处理进入前逻辑w,r +// func (l *GetCartsLogic) BeforeLogic(w http.ResponseWriter, r *http.Request) { +// } + +func (l *GetCartsLogic) GetCarts(req *types.GetCartsReq, userinfo *auth.UserInfo) (resp *basic.Response) { + // 返回值必须调用Set重新返回, resp可以空指针调用 resp.SetStatus(basic.CodeOK, data) + // userinfo 传入值时, 一定不为null + + return resp.SetStatus(basic.CodeOK) +} + +// 处理逻辑后 w,r 如:重定向, resp 必须重新处理 +// func (l *GetCartsLogic) AfterLogic(w http.ResponseWriter, r *http.Request, resp *basic.Response) { +// // httpx.OkJsonCtx(r.Context(), w, resp) +// } diff --git a/server/shopping-cart/internal/logic/modifycartpurchasequantitylogic.go b/server/shopping-cart/internal/logic/modifycartpurchasequantitylogic.go new file mode 100644 index 00000000..9da2b886 --- /dev/null +++ b/server/shopping-cart/internal/logic/modifycartpurchasequantitylogic.go @@ -0,0 +1,43 @@ +package logic + +import ( + "fusenapi/utils/auth" + "fusenapi/utils/basic" + + "context" + + "fusenapi/server/shopping-cart/internal/svc" + "fusenapi/server/shopping-cart/internal/types" + + "github.com/zeromicro/go-zero/core/logx" +) + +type ModifyCartPurchaseQuantityLogic struct { + logx.Logger + ctx context.Context + svcCtx *svc.ServiceContext +} + +func NewModifyCartPurchaseQuantityLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ModifyCartPurchaseQuantityLogic { + return &ModifyCartPurchaseQuantityLogic{ + Logger: logx.WithContext(ctx), + ctx: ctx, + svcCtx: svcCtx, + } +} + +// 处理进入前逻辑w,r +// func (l *ModifyCartPurchaseQuantityLogic) BeforeLogic(w http.ResponseWriter, r *http.Request) { +// } + +func (l *ModifyCartPurchaseQuantityLogic) ModifyCartPurchaseQuantity(req *types.ModifyCartPurchaseQuantityReq, userinfo *auth.UserInfo) (resp *basic.Response) { + // 返回值必须调用Set重新返回, resp可以空指针调用 resp.SetStatus(basic.CodeOK, data) + // userinfo 传入值时, 一定不为null + + return resp.SetStatus(basic.CodeOK) +} + +// 处理逻辑后 w,r 如:重定向, resp 必须重新处理 +// func (l *ModifyCartPurchaseQuantityLogic) AfterLogic(w http.ResponseWriter, r *http.Request, resp *basic.Response) { +// // httpx.OkJsonCtx(r.Context(), w, resp) +// } diff --git a/server/shopping-cart/internal/svc/servicecontext.go b/server/shopping-cart/internal/svc/servicecontext.go new file mode 100644 index 00000000..2b0f1a9a --- /dev/null +++ b/server/shopping-cart/internal/svc/servicecontext.go @@ -0,0 +1,26 @@ +package svc + +import ( + "fusenapi/initalize" + "fusenapi/model/gmodel" + "fusenapi/server/shopping-cart/internal/config" + "gorm.io/gorm" +) + +type ServiceContext struct { + Config config.Config + MysqlConn *gorm.DB + AllModels *gmodel.AllModelsGen + RabbitMq *initalize.RabbitMqHandle +} + +func NewServiceContext(c config.Config) *ServiceContext { + conn := initalize.InitMysql(c.SourceMysql) + + return &ServiceContext{ + Config: c, + MysqlConn: conn, + AllModels: gmodel.NewAllModels(initalize.InitMysql(c.SourceMysql)), + RabbitMq: initalize.InitRabbitMq(c.SourceRabbitMq, nil), + } +} diff --git a/server/shopping-cart/internal/types/types.go b/server/shopping-cart/internal/types/types.go new file mode 100644 index 00000000..4dbebdbb --- /dev/null +++ b/server/shopping-cart/internal/types/types.go @@ -0,0 +1,137 @@ +// Code generated by goctl. DO NOT EDIT. +package types + +import ( + "fusenapi/utils/basic" +) + +type AddToCartReq struct { + ProductId int64 `json:"product_id"` //产品id + TemplateId int64 `json:"template_id"` //模板id + SizeId int64 `json:"size_id"` //尺寸id + FittingId int64 `json:"fitting_id"` //配件id + PurchaseQuantity int64 `json:"purchase_quantity"` //购买数量 + Logo string `json:"logo"` //logo地址 + CombineImage string `json:"combine_image"` //合图地址 + RenderImage string `json:"render_image"` //渲染结果图 +} + +type DeleteCartReq struct { + Id int64 `json:"id"` //购物车id +} + +type ModifyCartPurchaseQuantityReq struct { + Id int64 `json:"id"` //购物车id + Quantity int64 `json:"quantity"` //数量 +} + +type GetCartsReq struct { + Page int `form:"page"` //当前页 +} + +type GetCartsRsp struct { + Meta Meta `json:"meta"` //分页信息 + CartList []CartItem `json:"cart_list"` +} + +type CartItem struct { + ProductId int64 `json:"product_id"` //产品id + SizeInfo SizeInfo `json:"size_info"` //尺寸信息 + FittingInfo FittingInfo `json:"fitting_info"` //配件信息 + ItemPrice string `json:"item_price"` //单价 + TotalPrice string `json:"totalPrice"` //单价X数量=总价 + DiyInformation DiyInformation `json:"diy_information"` //diy信息 + StepNum []int64 `json:"step_num"` //阶梯数量 +} + +type SizeInfo struct { + SizeId int64 `json:"size_id"` //尺寸id + Capacity string `json:"capacity"` //尺寸名称 + Title SizeTitle `json:"title"` +} + +type FittingInfo struct { + FittingId int64 `json:"fitting_id"` //配件id + FittingName string `json:"fitting_name"` //配件名称 +} + +type SizeTitle struct { + Cm string `json:"cm"` + Inch string `json:"inch"` +} + +type DiyInformation struct { + Phone string `json:"phone"` + Address string `json:"address"` + Website string `json:"website"` + Qrcode string `json:"qrcode"` +} + +type Request struct { +} + +type Response struct { + Code int `json:"code"` + Message string `json:"msg"` + Data interface{} `json:"data"` +} + +type Auth struct { + AccessSecret string `json:"accessSecret"` + AccessExpire int64 `json:"accessExpire"` + RefreshAfter int64 `json:"refreshAfter"` +} + +type File struct { + Filename string `fsfile:"filename"` + Header map[string][]string `fsfile:"header"` + Size int64 `fsfile:"size"` + Data []byte `fsfile:"data"` +} + +type Meta struct { + TotalCount int64 `json:"totalCount"` + PageCount int64 `json:"pageCount"` + CurrentPage int `json:"currentPage"` + PerPage int `json:"perPage"` +} + +// 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 *basic.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 *basic.StatusResponse, msg string, data ...interface{}) *Response { + newResp := &Response{ + Code: sr.Code, + Message: msg, + } + if len(data) == 1 { + newResp.Data = data[0] + } + return newResp +} diff --git a/server/shopping-cart/shopping-cart.go b/server/shopping-cart/shopping-cart.go new file mode 100644 index 00000000..f7a8cb3f --- /dev/null +++ b/server/shopping-cart/shopping-cart.go @@ -0,0 +1,36 @@ +package main + +import ( + "flag" + "fmt" + "net/http" + "time" + + "fusenapi/utils/auth" + + "fusenapi/server/shopping-cart/internal/config" + "fusenapi/server/shopping-cart/internal/handler" + "fusenapi/server/shopping-cart/internal/svc" + + "github.com/zeromicro/go-zero/core/conf" + "github.com/zeromicro/go-zero/rest" +) + +var configFile = flag.String("f", "etc/shopping-cart.yaml", "the config file") + +func main() { + flag.Parse() + + var c config.Config + conf.MustLoad(*configFile, &c) + c.Timeout = int64(time.Second * 15) + server := rest.MustNewServer(c.RestConf, rest.WithCustomCors(auth.FsCors, func(w http.ResponseWriter) { + })) + defer server.Stop() + + ctx := svc.NewServiceContext(c) + handler.RegisterHandlers(server, ctx) + + fmt.Printf("Starting server at %s:%d...\n", c.Host, c.Port) + server.Start() +}