This commit is contained in:
laodaming 2023-06-14 17:59:48 +08:00
parent f7da17778e
commit a5c3463fa9
7 changed files with 175 additions and 1 deletions

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -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"`

View File

@ -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);
}
//添加入购物车
@ -110,3 +113,11 @@ type CartAddr {
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"`
}