@pisell/pisellos
Version:
一个可扩展的前端模块化SDK框架,支持插件系统
303 lines (301 loc) • 11.9 kB
JavaScript
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
// If the importer is in node compatibility mode or this is not an ESM
// file that has been converted to a CommonJS file using a Babel-
// compatible transform (i.e. "__esModule" has not been set), then set
// "default" to the CommonJS "module.exports" for node compatibility.
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// src/modules/Order/index.ts
var Order_exports = {};
__export(Order_exports, {
OrderModule: () => OrderModule
});
module.exports = __toCommonJS(Order_exports);
var import_BaseModule = require("../BaseModule");
var import_utils = require("./utils");
var import_utils2 = require("../Product/utils");
var import_dayjs = __toESM(require("dayjs"));
var OrderModule = class extends import_BaseModule.BaseModule {
// LoggerManager 实例
constructor(name, version) {
super(name || "order", version);
this.defaultName = "order";
this.defaultVersion = "1.0.0";
}
async initialize(core, options) {
this.core = core;
this.store = options.store;
this.request = this.core.getPlugin("request");
const appPlugin = this.core.getPlugin("app");
if (!appPlugin) {
console.warn("Order 模块需要 app 插件支持");
}
const app = appPlugin == null ? void 0 : appPlugin.getApp();
if (app)
this.logger = app.logger;
this.logInfo("OrderModule initialized successfully");
}
/**
* 记录信息日志
*/
logInfo(title, metadata) {
if (this.logger) {
this.logger.addLog({
type: "info",
title: `[OrderModule] ${title}`,
metadata: metadata || {}
});
}
}
/**
* 记录警告日志
*/
logWarning(title, metadata) {
if (this.logger) {
this.logger.addLog({
type: "warning",
title: `[OrderModule] ${title}`,
metadata: metadata || {}
});
}
}
/**
* 记录错误日志
*/
logError(title, metadata) {
if (this.logger) {
this.logger.addLog({
type: "error",
title: `[OrderModule] ${title}`,
metadata: metadata || {}
});
}
}
createOrder(params) {
var _a;
const order = {
type: (params == null ? void 0 : params.type) || "appointment_booking",
// 要从外面拿,virtual
platform: (params == null ? void 0 : params.platform) === "pc" ? "PC" : "H5",
sales_channel: "my_pisel",
order_sales_channel: "online_store",
bookings: [],
shop_note: "",
schedule_date: "",
is_deposit: 0,
relation_products: [],
relation_forms: [],
business_code: params == null ? void 0 : params.business_code,
...(params == null ? void 0 : params.extraData) || {}
};
let is_deposit = 0;
if (params.cartItems.length > 0) {
params.cartItems.forEach((item) => {
if (!item._origin.duration) {
const { duration, durationType } = (0, import_utils.generateDuration)(item);
item._origin.duration = duration;
item._origin.sub_type = durationType;
}
const discountList = (0, import_utils.getAllDiscountList)(item);
item._origin.product.discount_list = discountList;
if ((0, import_utils2.isNormalProduct)(item._origin)) {
order.relation_products.push(item._origin.product);
const relationForms = item._origin.relation_forms || [];
order.relation_forms.push(...relationForms);
delete item._origin.relation_forms;
} else {
const relationForms = (0, import_utils.mergeRelationForms)(
item._origin.relation_forms || []
);
item._origin.relation_forms = relationForms;
order.bookings.push(item._origin);
}
if (item == null ? void 0 : item.deposit) {
is_deposit = 1;
}
});
if (order.type === "appointment_booking") {
const firstAppointmentCartItem = (_a = params.cartItems.filter(
(n) => !(0, import_utils2.isNormalProduct)(n._productOrigin)
)) == null ? void 0 : _a[0];
if (firstAppointmentCartItem) {
order.schedule_date = firstAppointmentCartItem.start_date + " " + firstAppointmentCartItem.start_time + ":00";
}
}
order.is_deposit = is_deposit;
}
return order;
}
checkBeforeSubmitOrder(params) {
var _a;
this.logInfo("checkBeforeSubmitOrder called", {
cartItemsCount: ((_a = params.cartItems) == null ? void 0 : _a.length) || 0,
type: params.type
});
const { cartItems, type } = params;
if (type === "holder") {
const hasNoHolderId = cartItems.some((item) => !item.holder_id);
if (hasNoHolderId) {
return false;
}
}
return true;
}
async submitOrder(order) {
var _a, _b, _c;
this.logInfo("submitOrder called", {
url: order.url,
orderType: order.query.type,
platform: order.query.platform,
cartItemsCount: ((_a = order.query.cartItems) == null ? void 0 : _a.length) || 0
});
const { url, query } = order;
const fetchUrl = url || "/order/appointment";
const params = this.createOrder(query);
this.logInfo("Calling backend order API", {
url: fetchUrl,
orderType: params.type,
platform: params.platform,
isDeposit: params.is_deposit,
bookingsCount: ((_b = params.bookings) == null ? void 0 : _b.length) || 0,
relationProductsCount: ((_c = params.relation_products) == null ? void 0 : _c.length) || 0,
scheduleDate: params.schedule_date
});
return this.request.post(fetchUrl, params);
}
/**
* Checkout 专用:创建订单到后端
*
* 专门为 Checkout 模块设计的订单创建方法,
* 直接调用 /order/checkout 接口创建订单
* 接收已经处理好的订单数据,无需再处理购物车
*
* @param params Checkout 订单参数(已处理的订单数据)
* @returns 后端返回的订单数据(包含订单ID等)
*/
async createOrderByCheckout(params) {
var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q, _r;
const onlineStorePaymentCodeList = ["WXPAY", "WECHAT", "ALIPAY", "APPLE_PAY", "CREDIT_CARD_3DS", "CREDIT_CARD_TOKEN", "GOOGLE_PAY", "GOOGLE_PAY_3DS", "CREDIT_CARD"];
params.payments = (_a = params.payments) == null ? void 0 : _a.filter((p) => !onlineStorePaymentCodeList.includes(p.code));
this.logInfo("createOrderByCheckout called", {
type: params.type,
platform: params.platform,
is_deposit: params.is_deposit,
customer_id: params.customer_id,
bookingsCount: ((_b = params.bookings) == null ? void 0 : _b.length) || 0,
relationProductsCount: ((_c = params.relation_products) == null ? void 0 : _c.length) || 0,
paymentsCount: ((_d = params.payments) == null ? void 0 : _d.length) || 0,
depositAmount: params.deposit_amount,
productTaxFee: params.product_tax_fee,
note: params.note,
scheduleDate: params.schedule_date,
hasOrderId: !!params.order_id,
orderIdIncluded: params.order_id,
paymentMethods: ((_e = params.payments) == null ? void 0 : _e.map((p) => p.code)) || []
});
console.log("[Order] createOrderByCheckout 开始创建订单:", {
type: params.type,
platform: params.platform,
is_deposit: params.is_deposit,
customer_id: params.customer_id,
bookingsCount: ((_f = params.bookings) == null ? void 0 : _f.length) || 0,
relationProductsCount: ((_g = params.relation_products) == null ? void 0 : _g.length) || 0,
paymentsCount: ((_h = params.payments) == null ? void 0 : _h.length) || 0
});
try {
const orderData = {
sales_channel: "my_pisel",
order_sales_channel: "online_store",
shop_note: "",
schedule_date: "",
is_deposit: 0,
bookings: [],
relation_products: [],
relation_forms: [],
payments: [],
is_full_overwrite_flag: 1,
...params
// 使用传入的参数覆盖默认值
};
if ((_i = orderData.payments) == null ? void 0 : _i.length) {
orderData.small_ticket_data_flag = 1;
}
if (!params.order_id && !orderData.schedule_date) {
orderData.schedule_date = (0, import_dayjs.default)().format("YYYY-MM-DD HH:mm:ss");
}
console.log("[Order] 调用后端接口创建订单:", {
url: "/order/checkout",
orderType: orderData.type,
platform: orderData.platform,
isDeposit: orderData.is_deposit,
customerId: orderData.customer_id,
bookingsCount: ((_j = orderData.bookings) == null ? void 0 : _j.length) || 0,
relationProductsCount: ((_k = orderData.relation_products) == null ? void 0 : _k.length) || 0,
paymentsCount: ((_l = orderData.payments) == null ? void 0 : _l.length) || 0,
paymentsMethods: ((_m = orderData.payments) == null ? void 0 : _m.map((p) => p.code)) || []
});
this.logInfo("Calling backend checkout API", {
url: "/order/checkout",
orderType: orderData.type,
platform: orderData.platform,
isDeposit: orderData.is_deposit,
customerId: orderData.customer_id,
depositAmount: orderData.deposit_amount,
bookingsCount: ((_n = orderData.bookings) == null ? void 0 : _n.length) || 0,
relationProductsCount: ((_o = orderData.relation_products) == null ? void 0 : _o.length) || 0,
paymentsCount: ((_p = orderData.payments) == null ? void 0 : _p.length) || 0,
paymentMethods: ((_q = orderData.payments) == null ? void 0 : _q.map((p) => ({
code: p.code,
amount: p.amount,
type: p.type,
hasVoucherId: !!p.voucher_id,
orderPaymentType: p.order_payment_type
}))) || [],
productTaxFee: orderData.product_tax_fee,
note: orderData.note,
scheduleDate: orderData.schedule_date,
hasOrderId: !!orderData.order_id,
smallTicketDataFlag: orderData.small_ticket_data_flag
});
const response = await this.request.post("/order/checkout", orderData);
this.logInfo("Order API called successfully", {
response
});
console.log("[Order] 订单创建成功,后端响应:", {
success: !!response,
hasOrderId: !!(((_r = response == null ? void 0 : response.data) == null ? void 0 : _r.order_id) || (response == null ? void 0 : response.order_id))
});
return response;
} catch (error) {
console.error("[Order] createOrderByCheckout 创建订单失败:", error);
this.logInfo("Order API called failed", {
error: error instanceof Error ? error.message : String(error)
});
throw error;
}
}
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
OrderModule
});