fix:购物车下单
This commit is contained in:
parent
137919a4d7
commit
4f61702e14
|
@ -7,20 +7,6 @@ import (
|
|||
|
||||
// TODO: 使用model的属性做你想做的
|
||||
|
||||
type NewFsOrder struct {
|
||||
Id int64 `gorm:"primary_key;default:0;auto_increment;" json:"id"` // 订单ID
|
||||
UserId *int64 `gorm:"index;default:0;" json:"user_id"` // 用户ID
|
||||
DeliveryMethod *int64 `gorm:"index;default:0;" json:"delivery_method"` // 物流类型
|
||||
OrderSn *string `gorm:"index;default:'';" json:"order_sn"` //
|
||||
OrderSource *string `gorm:"default:'';" json:"order_source"` //
|
||||
Status *int64 `gorm:"index;default:0;" json:"status"` // 订单状态
|
||||
PayStatus *int64 `gorm:"default:0;" json:"pay_status"` // 支付状态
|
||||
Metadata *OrderDetail `gorm:"metadata,type:json" json:"metadata"` //
|
||||
Ctime *time.Time `gorm:"default:'0000-00-00 00:00:00';" json:"ctime"` //
|
||||
Utime *time.Time `gorm:"default:'0000-00-00 00:00:00';" json:"utime"` //
|
||||
IsDel *int64 `gorm:"default:0;" json:"is_del"` // 是否删除:0=否,1=是
|
||||
}
|
||||
|
||||
// 订单详情
|
||||
type OrderDetail struct {
|
||||
DeliveryAddress *OrderAddress `json:"delivery_address"` // 收货地址
|
||||
|
|
35
server/order/internal/handler/orderdetailhandler.go
Normal file
35
server/order/internal/handler/orderdetailhandler.go
Normal file
|
@ -0,0 +1,35 @@
|
|||
package handler
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"reflect"
|
||||
|
||||
"fusenapi/utils/basic"
|
||||
|
||||
"fusenapi/server/order/internal/logic"
|
||||
"fusenapi/server/order/internal/svc"
|
||||
"fusenapi/server/order/internal/types"
|
||||
)
|
||||
|
||||
func OrderDetailHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
var req types.OrderDetailReq
|
||||
userinfo, err := basic.RequestParse(w, r, svcCtx, &req)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
// 创建一个业务逻辑层实例
|
||||
l := logic.NewOrderDetailLogic(r.Context(), svcCtx)
|
||||
|
||||
rl := reflect.ValueOf(l)
|
||||
basic.BeforeLogic(w, r, rl)
|
||||
|
||||
resp := l.OrderDetail(&req, userinfo)
|
||||
|
||||
if !basic.AfterLogic(w, r, rl, resp) {
|
||||
basic.NormalAfterLogic(w, r, resp)
|
||||
}
|
||||
}
|
||||
}
|
|
@ -23,10 +23,15 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) {
|
|||
Handler: CreatePrePaymentDepositHandler(serverCtx),
|
||||
},
|
||||
{
|
||||
Method: http.MethodPost,
|
||||
Method: http.MethodGet,
|
||||
Path: "/api/order/list",
|
||||
Handler: OrderListHandler(serverCtx),
|
||||
},
|
||||
{
|
||||
Method: http.MethodGet,
|
||||
Path: "/api/order/detail",
|
||||
Handler: OrderDetailHandler(serverCtx),
|
||||
},
|
||||
},
|
||||
)
|
||||
}
|
||||
|
|
57
server/order/internal/logic/orderdetaillogic.go
Normal file
57
server/order/internal/logic/orderdetaillogic.go
Normal file
|
@ -0,0 +1,57 @@
|
|||
package logic
|
||||
|
||||
import (
|
||||
"fusenapi/service/repositories"
|
||||
"fusenapi/utils/auth"
|
||||
"fusenapi/utils/basic"
|
||||
|
||||
"context"
|
||||
|
||||
"fusenapi/server/order/internal/svc"
|
||||
"fusenapi/server/order/internal/types"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type OrderDetailLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
func NewOrderDetailLogic(ctx context.Context, svcCtx *svc.ServiceContext) *OrderDetailLogic {
|
||||
return &OrderDetailLogic{
|
||||
Logger: logx.WithContext(ctx),
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
}
|
||||
}
|
||||
|
||||
// 处理进入前逻辑w,r
|
||||
// func (l *OrderDetailLogic) BeforeLogic(w http.ResponseWriter, r *http.Request) {
|
||||
// }
|
||||
|
||||
func (l *OrderDetailLogic) OrderDetail(req *types.OrderDetailReq, userinfo *auth.UserInfo) (resp *basic.Response) {
|
||||
// 返回值必须调用Set重新返回, resp可以空指针调用 resp.SetStatus(basic.CodeOK, data)
|
||||
// userinfo 传入值时, 一定不为null
|
||||
if !userinfo.IsUser() {
|
||||
// 如果是,返回未授权的错误码
|
||||
return resp.SetStatus(basic.CodeUnAuth)
|
||||
}
|
||||
res, err := l.svcCtx.Repositories.NewOrder.Detail(l.ctx, &repositories.DetailReq{
|
||||
OrderSn: req.OrderSn,
|
||||
UserId: userinfo.UserId,
|
||||
})
|
||||
if err != nil {
|
||||
return resp.SetStatus(basic.CodeApiErr)
|
||||
}
|
||||
|
||||
return resp.SetStatus(basic.CodeOK, map[string]interface{}{
|
||||
"order_detail": res,
|
||||
})
|
||||
}
|
||||
|
||||
// 处理逻辑后 w,r 如:重定向, resp 必须重新处理
|
||||
// func (l *OrderDetailLogic) AfterLogic(w http.ResponseWriter, r *http.Request, resp *basic.Response) {
|
||||
// // httpx.OkJsonCtx(r.Context(), w, resp)
|
||||
// }
|
|
@ -5,6 +5,10 @@ import (
|
|||
"fusenapi/utils/basic"
|
||||
)
|
||||
|
||||
type OrderDetailReq struct {
|
||||
OrderSn string `form:"order_sn"`
|
||||
}
|
||||
|
||||
type CreateOrderReq struct {
|
||||
CartIds []int64 `json:"cart_ids"`
|
||||
DeliveryMethod int64 `json:"delivery_method,options=[1,2]"`
|
||||
|
|
|
@ -18,8 +18,15 @@ service order {
|
|||
post /api/order/create-prepayment-deposit(CreatePrePaymentDepositReq) returns (response);
|
||||
|
||||
@handler OrderListHandler
|
||||
post /api/order/list(OrderListReq) returns (response);
|
||||
get /api/order/list(OrderListReq) returns (response);
|
||||
|
||||
@handler OrderDetailHandler
|
||||
get /api/order/detail(OrderDetailReq) returns (response);
|
||||
|
||||
}
|
||||
|
||||
type OrderDetailReq {
|
||||
OrderSn string `form:"order_sn"`
|
||||
}
|
||||
|
||||
type CreateOrderReq {
|
||||
|
|
|
@ -4,6 +4,7 @@ import (
|
|||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"fusenapi/constants"
|
||||
"fusenapi/model/gmodel"
|
||||
"fusenapi/utils/basic"
|
||||
|
@ -31,6 +32,7 @@ type (
|
|||
// 预支付
|
||||
// 列表
|
||||
// 详情
|
||||
Detail(ctx context.Context, in *DetailReq) (res *DetailRes, err error)
|
||||
}
|
||||
|
||||
OrderAddress struct {
|
||||
|
@ -55,13 +57,46 @@ type (
|
|||
OrderSn string
|
||||
}
|
||||
/* 下单 */
|
||||
|
||||
/* 详情 */
|
||||
DetailReq struct {
|
||||
UserId int64 `json:"user_id"`
|
||||
OrderSn string `json:"order_sn"`
|
||||
}
|
||||
DetailRes struct {
|
||||
}
|
||||
/* 详情 */
|
||||
)
|
||||
|
||||
// 详情
|
||||
func (d *defaultOrder) Detail(ctx context.Context, in *DetailReq) (res *DetailRes, err error) {
|
||||
var order gmodel.FsOrder
|
||||
result := d.MysqlConn.Where("order_sn = ?", in.OrderSn).Where("user_id = ?", in.UserId).Take(&order)
|
||||
if result.Error != nil {
|
||||
return nil, result.Error
|
||||
}
|
||||
d.OrderDetailHandler(ctx, &order)
|
||||
return &DetailRes{}, nil
|
||||
}
|
||||
|
||||
func (d *defaultOrder) OrderDetailHandler(ctx context.Context, order *gmodel.FsOrder) (res *DetailRes, err error) {
|
||||
var orderDetail gmodel.OrderDetail
|
||||
|
||||
err = json.Unmarshal(*order.Metadata, &orderDetail)
|
||||
if err != nil {
|
||||
logx.Errorf("create handler unmarshal metadata failed, err: %v", err)
|
||||
return nil, err
|
||||
}
|
||||
fmt.Println(orderDetail)
|
||||
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
// 下单
|
||||
func (d *defaultOrder) Create(ctx context.Context, in *CreateReq) (res *CreateRes, err error) {
|
||||
var errorCode basic.StatusResponse
|
||||
// 订单编号
|
||||
var orderSn string = order.GenerateOrderNumber(int(in.DeliveryMethod), int(in.UserId))
|
||||
var orderSn string = order.GenerateOrderNumber()
|
||||
|
||||
err = d.MysqlConn.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
|
||||
// 查询购物车
|
||||
|
|
|
@ -79,9 +79,32 @@ func GetAmountInfo(req GetAmountInfoReq) gmodel.AmountInfo {
|
|||
}
|
||||
}
|
||||
|
||||
func GenerateOrderNumber(deliveryMethod int, userID int) string {
|
||||
type GetAmountCurrencyUSDReq struct {
|
||||
ExchangeRate int64 `json:"exchange_rate"` // 换算汇率
|
||||
CurrentAmount int64 `json:"current_amount"` // 当前金额
|
||||
OriginalAmount int64 `json:"original_amount"` // 原始金额
|
||||
CurrentCurrency string `json:"current_currency"` // 当前货币
|
||||
OriginalCurrency string `json:"original_currency"` // 原始货币
|
||||
}
|
||||
|
||||
type GetAmountCurrencyUSDRes struct {
|
||||
ExchangeRate string `json:"exchange_rate"` // 换算汇率
|
||||
CurrentAmount string `json:"current_amount"` // 当前金额
|
||||
OriginalAmount string `json:"original_amount"` // 原始金额
|
||||
CurrentCurrency string `json:"current_currency"` // 当前货币
|
||||
OriginalCurrency string `json:"original_currency"` // 原始货币
|
||||
}
|
||||
|
||||
// 处理金额(美元)
|
||||
func GetAmountCurrencyUSD(req *GetAmountCurrencyUSDReq) (res GetAmountCurrencyUSDRes) {
|
||||
return GetAmountCurrencyUSDRes{
|
||||
ExchangeRate: fmt.Sprintf("%.2f", float64(req.ExchangeRate)/1000),
|
||||
}
|
||||
}
|
||||
|
||||
func GenerateOrderNumber() string {
|
||||
t := time.Now()
|
||||
orderNumber := fmt.Sprintf("%d%02d%02d%d", t.Year(), t.Month(), t.Day(), t.Nanosecond())
|
||||
orderNumber := fmt.Sprintf("%d%02d%02d%08d", t.Year(), t.Month(), t.Day(), t.UnixNano()%100000000)
|
||||
fmt.Println(orderNumber)
|
||||
return orderNumber
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user