fix
This commit is contained in:
@@ -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 CartOrderDetailHandler(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.CartOrderDetailReq
|
||||
// 如果端点有请求结构体,则使用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.NewCartOrderDetailLogic(r.Context(), svcCtx)
|
||||
resp := l.CartOrderDetail(&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)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -32,6 +32,11 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) {
|
||||
Path: "/cart/list",
|
||||
Handler: CartListHandler(serverCtx),
|
||||
},
|
||||
{
|
||||
Method: http.MethodGet,
|
||||
Path: "/cart/order-detail",
|
||||
Handler: CartOrderDetailHandler(serverCtx),
|
||||
},
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
@@ -32,14 +32,14 @@ func NewCartAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CartAddLo
|
||||
// 添加入购物车
|
||||
func (l *CartAddLogic) CartAdd(req *types.CartAddReq, userinfo *auth.UserInfo) (resp *basic.Response) {
|
||||
if req.BuyNum == 0 {
|
||||
return resp.SetStatusWithMessage(basic.CodeApiErr, "param buy_num can`t be 0")
|
||||
return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "param buy_num can`t be 0")
|
||||
}
|
||||
if req.IsCheck != 0 && req.IsCheck != 1 {
|
||||
return resp.SetStatusWithMessage(basic.CodeApiErr, "param is_check should be 0 or 1")
|
||||
return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "param is_check should be 0 or 1")
|
||||
}
|
||||
req.DesignId = strings.Trim(req.DesignId, " ")
|
||||
if req.DesignId == "" {
|
||||
return resp.SetStatusWithMessage(basic.CodeApiErr, "param design_id can`t be empty")
|
||||
return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "param design_id can`t be empty")
|
||||
}
|
||||
//查询是否有此设计
|
||||
productDesignModel := gmodel.NewFsProductDesignModel(l.svcCtx.MysqlConn)
|
||||
|
||||
@@ -29,7 +29,7 @@ func NewCartDeleteLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CartDe
|
||||
|
||||
func (l *CartDeleteLogic) CartDelete(req *types.CartDeleteReq, userinfo *auth.UserInfo) (resp *basic.Response) {
|
||||
if req.Id <= 0 {
|
||||
return resp.SetStatusWithMessage(basic.CodeApiErr, "invalid param id")
|
||||
return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "invalid param id")
|
||||
}
|
||||
cartModel := gmodel.NewFsCartModel(l.svcCtx.MysqlConn)
|
||||
status := int64(0)
|
||||
|
||||
@@ -0,0 +1,175 @@
|
||||
package logic
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"fusenapi/model/gmodel"
|
||||
"fusenapi/utils/auth"
|
||||
"fusenapi/utils/basic"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"context"
|
||||
|
||||
"fusenapi/server/shopping-cart-confirmation/internal/svc"
|
||||
"fusenapi/server/shopping-cart-confirmation/internal/types"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type CartOrderDetailLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
func NewCartOrderDetailLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CartOrderDetailLogic {
|
||||
return &CartOrderDetailLogic{
|
||||
Logger: logx.WithContext(ctx),
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
}
|
||||
}
|
||||
|
||||
func (l *CartOrderDetailLogic) CartOrderDetail(req *types.CartOrderDetailReq, 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")
|
||||
}
|
||||
//获取订单详细数据
|
||||
orderDetailModel := gmodel.NewFsOrderDetailModel(l.svcCtx.MysqlConn)
|
||||
orderDetailList, err := orderDetailModel.GetOrderDetailsByOrderId(l.ctx, orderInfo.Id)
|
||||
if err != nil {
|
||||
logx.Error(err)
|
||||
return resp.SetStatusWithMessage(basic.CodeServiceErr, "failed to get order details")
|
||||
}
|
||||
if len(orderDetailList) == 0 {
|
||||
return resp.SetStatusWithMessage(basic.CodeServiceErr, "order details is empty")
|
||||
}
|
||||
orderDetailTemplateIds := make([]int64, 0, len(orderDetailList))
|
||||
productIds := make([]int64, 0, len(orderDetailList))
|
||||
for _, v := range orderDetailList {
|
||||
orderDetailTemplateIds = append(orderDetailTemplateIds, *v.OrderDetailTemplateId)
|
||||
productIds = append(productIds, *v.ProductId)
|
||||
}
|
||||
//获取订单详情对应模板信息
|
||||
orderDetailTemplateModel := gmodel.NewFsOrderDetailTemplateModel(l.svcCtx.MysqlConn)
|
||||
orderDetailTemplateList, err := orderDetailTemplateModel.GetListByIds(l.ctx, orderDetailTemplateIds)
|
||||
if err != nil {
|
||||
logx.Error(err)
|
||||
return resp.SetStatusWithMessage(basic.CodeServiceErr, "failed to get order details templates")
|
||||
}
|
||||
sizeIds := make([]int64, 0, len(orderDetailTemplateList))
|
||||
mapDetailTemplate := make(map[int64]int)
|
||||
for k, v := range orderDetailTemplateList {
|
||||
sizeIds = append(sizeIds, *v.SizeId)
|
||||
mapDetailTemplate[v.Id] = k
|
||||
}
|
||||
//获取尺寸信息
|
||||
productSizeModel := gmodel.NewFsProductSizeModel(l.svcCtx.MysqlConn)
|
||||
productSizeList, err := productSizeModel.GetAllByIds(l.ctx, sizeIds, "")
|
||||
if err != nil {
|
||||
logx.Error(err)
|
||||
return resp.SetStatusWithMessage(basic.CodeServiceErr, "failed to get product size list")
|
||||
}
|
||||
mapProductSize := make(map[int64]int)
|
||||
for k, v := range productSizeList {
|
||||
mapProductSize[v.Id] = k
|
||||
}
|
||||
//获取产品信息
|
||||
productModel := gmodel.NewFsProductModel(l.svcCtx.MysqlConn)
|
||||
productList, err := productModel.GetProductListByIds(l.ctx, productIds, "")
|
||||
if err != nil {
|
||||
logx.Error(err)
|
||||
return resp.SetStatusWithMessage(basic.CodeServiceErr, "failed to get order products")
|
||||
}
|
||||
mapProduct := make(map[int64]int)
|
||||
for k, v := range productList {
|
||||
mapProduct[v.Id] = k
|
||||
}
|
||||
//获取用户地址信息
|
||||
addressModel := gmodel.NewFsAddressModel(l.svcCtx.MysqlConn)
|
||||
addressList, err := addressModel.GetUserAllAddress(l.ctx, userinfo.UserId)
|
||||
if err != nil {
|
||||
logx.Error(err)
|
||||
return resp.SetStatusWithMessage(basic.CodeServiceErr, "failed to get address")
|
||||
}
|
||||
//处理订单数据
|
||||
addressItems := make([]types.CartAddr, 0, len(addressList))
|
||||
for _, v := range addressList {
|
||||
addressItems = append(addressItems, types.CartAddr{
|
||||
Id: v.Id,
|
||||
Name: *v.Name,
|
||||
FirstName: *v.FirstName,
|
||||
LastName: *v.LastName,
|
||||
Mobile: *v.Mobile,
|
||||
Street: *v.Street,
|
||||
Suite: *v.Suite,
|
||||
City: *v.City,
|
||||
State: *v.State,
|
||||
ZipCode: *v.ZipCode,
|
||||
IsDefault: *v.IsDefault,
|
||||
})
|
||||
}
|
||||
items := make([]types.CartDetailItem, 0, len(orderDetailList))
|
||||
totalAmount := int64(0) //订单总金额
|
||||
for _, v := range orderDetailList {
|
||||
thisTotal := (*v.BuyNum) * (*v.Amount)
|
||||
size := ""
|
||||
if detailTemplateIndex, ok := mapDetailTemplate[*v.OrderDetailTemplateId]; ok {
|
||||
detailTemplate := orderDetailTemplateList[detailTemplateIndex]
|
||||
if sizeIndex, ok := mapProductSize[*detailTemplate.SizeId]; ok {
|
||||
size = *productSizeList[sizeIndex].Capacity
|
||||
}
|
||||
}
|
||||
name := ""
|
||||
if productIndex, ok := mapProduct[*v.ProductId]; ok {
|
||||
name = *productList[productIndex].Title
|
||||
}
|
||||
items = append(items, types.CartDetailItem{
|
||||
Cover: *v.Cover,
|
||||
Pcs: *v.BuyNum,
|
||||
Amount: fmt.Sprintf("$ %.2f", float64(thisTotal)/100),
|
||||
Option: *v.OptionalTitle,
|
||||
Size: size,
|
||||
Name: name,
|
||||
})
|
||||
totalAmount += thisTotal
|
||||
}
|
||||
//首付50%
|
||||
total := totalAmount / 2
|
||||
//尾款
|
||||
remaining := totalAmount - total
|
||||
payStep := int64(0) //未支付
|
||||
if *orderInfo.PayedAmount == *orderInfo.TotalAmount/2 {
|
||||
payStep = 1 //已支付首款
|
||||
}
|
||||
if *orderInfo.PayedAmount == *orderInfo.TotalAmount {
|
||||
payStep = 2 //已支付尾款
|
||||
}
|
||||
payTime := ""
|
||||
if *orderInfo.Ptime != 0 {
|
||||
payTime = time.Unix(*orderInfo.Ptime, 0).Format("2006-01-02 15:04:05")
|
||||
}
|
||||
return resp.SetStatusWithMessage(basic.CodeOK, "success", types.CartOrderDetailRsp{
|
||||
DeliveryMethod: 1,
|
||||
AddressId: 0,
|
||||
PayTime: payTime,
|
||||
PayMethod: 1,
|
||||
PayStep: payStep,
|
||||
Subtotal: fmt.Sprintf("$%.2f", float64(totalAmount)/100),
|
||||
Total: fmt.Sprintf("$%.2f", float64(total)/100),
|
||||
Remaining: fmt.Sprintf("$%.2f", float64(remaining)/100),
|
||||
AddrList: addressItems,
|
||||
Items: items,
|
||||
})
|
||||
}
|
||||
@@ -57,6 +57,46 @@ type CartSizeItem struct {
|
||||
Inch string `json:"inch"`
|
||||
}
|
||||
|
||||
type CartOrderDetailReq struct {
|
||||
Sn string `form:"sn"`
|
||||
}
|
||||
|
||||
type CartOrderDetailRsp struct {
|
||||
DeliveryMethod int64 `json:"delivery_method"`
|
||||
AddressId int64 `json:"address_id"`
|
||||
PayTime string `json:"pay_time"`
|
||||
PayMethod int64 `json:"pay_method"`
|
||||
PayStep int64 `json:"pay_step"`
|
||||
Subtotal string `json:"subtotal"`
|
||||
Total string `json:"total"`
|
||||
Remaining string `json:"remaining"`
|
||||
AddrList []CartAddr `json:"addr_list"`
|
||||
Items []CartDetailItem `json:"items"`
|
||||
}
|
||||
|
||||
type CartDetailItem struct {
|
||||
Cover string `json:"cover"`
|
||||
Pcs int64 `json:"pcs"`
|
||||
Amount string `json:"amount"`
|
||||
Option string `json:"option"`
|
||||
Size string `json:"size"`
|
||||
Name string `json:"name"`
|
||||
}
|
||||
|
||||
type CartAddr struct {
|
||||
Id int64 `json:"id"`
|
||||
Name string `json:"name"`
|
||||
FirstName string `json:"first_name"`
|
||||
LastName string `json:"last_name"`
|
||||
Mobile string `json:"mobile"`
|
||||
Street string `json:"street"`
|
||||
Suite string `json:"suite"`
|
||||
City string `json:"city"`
|
||||
State string `json:"state"`
|
||||
ZipCode string `json:"zip_code"`
|
||||
IsDefault int64 `json:"is_default"`
|
||||
}
|
||||
|
||||
type Response struct {
|
||||
Code int `json:"code"`
|
||||
Message string `json:"msg"`
|
||||
|
||||
Reference in New Issue
Block a user