66 lines
1.8 KiB
Go
66 lines
1.8 KiB
Go
package logic
|
|
|
|
import (
|
|
"fusenapi/constants"
|
|
"fusenapi/service/repositories"
|
|
"fusenapi/utils/auth"
|
|
"fusenapi/utils/basic"
|
|
"time"
|
|
|
|
"context"
|
|
|
|
"fusenapi/server/order/internal/svc"
|
|
"fusenapi/server/order/internal/types"
|
|
|
|
"github.com/zeromicro/go-zero/core/logx"
|
|
)
|
|
|
|
type CreateOrderLogic struct {
|
|
logx.Logger
|
|
ctx context.Context
|
|
svcCtx *svc.ServiceContext
|
|
}
|
|
|
|
func NewCreateOrderLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CreateOrderLogic {
|
|
return &CreateOrderLogic{
|
|
Logger: logx.WithContext(ctx),
|
|
ctx: ctx,
|
|
svcCtx: svcCtx,
|
|
}
|
|
}
|
|
|
|
// 处理进入前逻辑w,r
|
|
// func (l *CreateOrderLogic) BeforeLogic(w http.ResponseWriter, r *http.Request) {
|
|
// }
|
|
|
|
func (l *CreateOrderLogic) CreateOrder(req *types.CreateOrderReq, userinfo *auth.UserInfo) (resp *basic.Response) {
|
|
// 返回值必须调用Set重新返回, resp可以空指针调用 resp.SetStatus(basic.CodeOK, data)
|
|
// userinfo 传入值时, 一定不为null
|
|
if !userinfo.IsUser() {
|
|
// 如果是,返回未授权的错误码
|
|
return resp.SetStatus(basic.CodeUnAuth)
|
|
}
|
|
tPlus60Days := time.Now().AddDate(0, 0, 60)
|
|
res, err := l.svcCtx.Repositories.NewOrder.Create(l.ctx, &repositories.CreateReq{
|
|
ExpectedDeliveryTime: tPlus60Days,
|
|
CurrentCurrency: string(constants.CURRENCYUSD),
|
|
OriginalCurrency: string(constants.CURRENCYUSD),
|
|
UserId: userinfo.UserId,
|
|
CartIds: req.CartIds,
|
|
DeliveryMethod: req.DeliveryMethod,
|
|
})
|
|
|
|
if err != nil {
|
|
return resp.SetStatus(&res.ErrorCode)
|
|
}
|
|
|
|
return resp.SetStatus(basic.CodeOK, map[string]interface{}{
|
|
"order_sn": res.OrderSn,
|
|
})
|
|
}
|
|
|
|
// 处理逻辑后 w,r 如:重定向, resp 必须重新处理
|
|
// func (l *CreateOrderLogic) AfterLogic(w http.ResponseWriter, r *http.Request, resp *basic.Response) {
|
|
// // httpx.OkJsonCtx(r.Context(), w, resp)
|
|
// }
|