From a5c3463fa9ed4d6058d832c540215f4f54cfd752 Mon Sep 17 00:00:00 2001 From: laodaming <11058467+laudamine@user.noreply.gitee.com> Date: Wed, 14 Jun 2023 17:59:48 +0800 Subject: [PATCH] fix --- model/gmodel/fsaddressmodel.go | 9 ++- model/gmodel/fsordermodel.go | 3 + .../handler/changeordermethodhandler.go | 67 +++++++++++++++++ .../internal/handler/routes.go | 5 ++ .../internal/logic/changeordermethodlogic.go | 74 +++++++++++++++++++ .../internal/types/types.go | 7 ++ server_api/shopping-cart-confirmation.api | 11 +++ 7 files changed, 175 insertions(+), 1 deletion(-) create mode 100644 server/shopping-cart-confirmation/internal/handler/changeordermethodhandler.go create mode 100644 server/shopping-cart-confirmation/internal/logic/changeordermethodlogic.go diff --git a/model/gmodel/fsaddressmodel.go b/model/gmodel/fsaddressmodel.go index f6b14d0c..08c51241 100755 --- a/model/gmodel/fsaddressmodel.go +++ b/model/gmodel/fsaddressmodel.go @@ -2,6 +2,7 @@ package gmodel import ( "context" + "errors" "gorm.io/gorm" ) @@ -29,7 +30,13 @@ type FsAddressModel struct { func NewFsAddressModel(db *gorm.DB) *FsAddressModel { return &FsAddressModel{db} } - +func (a *FsAddressModel) GetOne(ctx context.Context, id int64, userId int64) (resp FsAddress, err error) { + err = a.db.WithContext(ctx).Model(&FsAddress{}).Where("`id` = ? and `user_id` = ? and `status` = ? ", id, userId, 1).First(&resp).Error + if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) { + return FsAddress{}, err + } + return resp, nil +} func (a *FsAddressModel) GetUserAllAddress(ctx context.Context, userId int64) (resp []FsAddress, err error) { err = a.db.WithContext(ctx).Model(&FsAddress{}).Where("`user_id` = ? and `status` = ?", userId, 1).Order("`id` DESC").Find(&resp).Error if err != nil { diff --git a/model/gmodel/fsordermodel.go b/model/gmodel/fsordermodel.go index 43f74cfd..bfd17060 100755 --- a/model/gmodel/fsordermodel.go +++ b/model/gmodel/fsordermodel.go @@ -60,3 +60,6 @@ func (o *FsOrderModel) FindOneBySn(ctx context.Context, userId int64, sn string) } return resp, nil } +func (o *FsOrderModel) Update(ctx context.Context, id int64, data FsOrder) error { + return o.db.WithContext(ctx).Model(&FsOrder{}).Where("`id` = ?", id).Updates(data).Error +} diff --git a/server/shopping-cart-confirmation/internal/handler/changeordermethodhandler.go b/server/shopping-cart-confirmation/internal/handler/changeordermethodhandler.go new file mode 100644 index 00000000..0516e923 --- /dev/null +++ b/server/shopping-cart-confirmation/internal/handler/changeordermethodhandler.go @@ -0,0 +1,67 @@ +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/shopping-cart-confirmation/internal/logic" + "fusenapi/server/shopping-cart-confirmation/internal/svc" + "fusenapi/server/shopping-cart-confirmation/internal/types" +) + +func ChangeOrderMethodHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + // 解析jwtToken + claims, err := svcCtx.ParseJwtToken(r) + // 如果解析出错,则返回未授权的JSON响应并记录错误消息 + if err != nil { + httpx.OkJsonCtx(r.Context(), w, &basic.Response{ + Code: 401, + Message: "unauthorized", + }) + logx.Info("unauthorized:", err.Error()) + return + } + + // 从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 + } + + var req types.ChangeOrderMethodReq + // 如果端点有请求结构体,则使用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.NewChangeOrderMethodLogic(r.Context(), svcCtx) + resp := l.ChangeOrderMethod(&req, userinfo) + // 如果响应不为nil,则使用httpx.OkJsonCtx方法返回JSON响应; + // 否则,发送500内部服务器错误的JSON响应并记录错误消息logx.Error。 + 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/shopping-cart-confirmation/internal/handler/routes.go b/server/shopping-cart-confirmation/internal/handler/routes.go index 376133e9..c69baeaf 100644 --- a/server/shopping-cart-confirmation/internal/handler/routes.go +++ b/server/shopping-cart-confirmation/internal/handler/routes.go @@ -37,6 +37,11 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) { Path: "/cart/order-detail", Handler: CartOrderDetailHandler(serverCtx), }, + { + Method: http.MethodPost, + Path: "/cart/chang-order-method", + Handler: ChangeOrderMethodHandler(serverCtx), + }, }, ) } diff --git a/server/shopping-cart-confirmation/internal/logic/changeordermethodlogic.go b/server/shopping-cart-confirmation/internal/logic/changeordermethodlogic.go new file mode 100644 index 00000000..ddf9301f --- /dev/null +++ b/server/shopping-cart-confirmation/internal/logic/changeordermethodlogic.go @@ -0,0 +1,74 @@ +package logic + +import ( + "encoding/json" + "fusenapi/model/gmodel" + "fusenapi/utils/auth" + "fusenapi/utils/basic" + "strings" + + "context" + + "fusenapi/server/shopping-cart-confirmation/internal/svc" + "fusenapi/server/shopping-cart-confirmation/internal/types" + + "github.com/zeromicro/go-zero/core/logx" +) + +type ChangeOrderMethodLogic struct { + logx.Logger + ctx context.Context + svcCtx *svc.ServiceContext +} + +func NewChangeOrderMethodLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ChangeOrderMethodLogic { + return &ChangeOrderMethodLogic{ + Logger: logx.WithContext(ctx), + ctx: ctx, + svcCtx: svcCtx, + } +} + +func (l *ChangeOrderMethodLogic) ChangeOrderMethod(req *types.ChangeOrderMethodReq, userinfo *auth.UserInfo) (resp *basic.Response) { + req.Sn = strings.Trim(req.Sn, " ") + if req.Sn == "" { + return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "param sn is required") + } + //查询订单信息 + orderModel := gmodel.NewFsOrderModel(l.svcCtx.MysqlConn) + orderInfo, err := orderModel.FindOneBySn(l.ctx, userinfo.UserId, req.Sn) + if err != nil { + logx.Error(err) + return resp.SetStatusWithMessage(basic.CodeServiceErr, "failed to get order info") + } + if orderInfo.Id == 0 { + return resp.SetStatusWithMessage(basic.CodeServiceErr, "the order is not exists") + } + if *orderInfo.PayedAmount > 0 { + return resp.SetStatusWithMessage(basic.CodeApiErr, "the order`s address cannot be changed for it is paid") + } + updData := gmodel.FsOrder{} + //地址 + if req.AddressId > 0 { + addressModel := gmodel.NewFsAddressModel(l.svcCtx.MysqlConn) + addr, err := addressModel.GetOne(l.ctx, req.AddressId, userinfo.UserId) + if err != nil { + logx.Error(err) + return resp.SetStatusWithMessage(basic.CodeServiceErr, "failed to get address info") + } + if addr.Id == 0 { + return resp.SetStatusWithMessage(basic.CodeServiceErr, "address is not exists") + } + infoJsonByte, _ := json.Marshal(addr) + strInfoJson := string(infoJsonByte) + updData.AddressInfo = &strInfoJson + } + updData.DeliveryMethod = &req.DeliveryMethod + updData.AddressId = &req.AddressId + updData.PayMethod = &req.PayMethod + if err = orderModel.Update(l.ctx, orderInfo.Id, updData); err != nil { + logx.Error(err) + return resp.SetStatusWithMessage(basic.CodeServiceErr, "failed to save data") + } + return resp.SetStatusWithMessage(basic.CodeOK, "success") +} diff --git a/server/shopping-cart-confirmation/internal/types/types.go b/server/shopping-cart-confirmation/internal/types/types.go index f7594cc7..84ad80b8 100644 --- a/server/shopping-cart-confirmation/internal/types/types.go +++ b/server/shopping-cart-confirmation/internal/types/types.go @@ -97,6 +97,13 @@ type CartAddr struct { IsDefault int64 `json:"is_default"` } +type ChangeOrderMethodReq struct { + Sn string `json:"sn"` + DeliveryMethod int64 `json:"delivery_method , options=1|2"` + AddressId int64 `json:"address_id"` + PayMethod int64 `json:"pay_method"` +} + type Response struct { Code int `json:"code"` Message string `json:"msg"` diff --git a/server_api/shopping-cart-confirmation.api b/server_api/shopping-cart-confirmation.api index 4aefe8a8..5dafc1d6 100644 --- a/server_api/shopping-cart-confirmation.api +++ b/server_api/shopping-cart-confirmation.api @@ -23,6 +23,9 @@ service shopping-cart-confirmation { //获取用户购物车订单详情 @handler CartOrderDetailHandler get /cart/order-detail (CartOrderDetailReq) returns (response); + //变更发货方式和地址 + @handler ChangeOrderMethodHandler + post /cart/chang-order-method (ChangeOrderMethodReq) returns (response); } //添加入购物车 @@ -109,4 +112,12 @@ type CartAddr { State string `json:"state"` ZipCode string `json:"zip_code"` IsDefault int64 `json:"is_default"` +} + +//变更发货方式和地址 +type ChangeOrderMethodReq { + Sn string `json:"sn"` + DeliveryMethod int64 `json:"delivery_method , options=1|2"` + AddressId int64 `json:"address_id"` + PayMethod int64 `json:"pay_method"` } \ No newline at end of file