@pisell/pisellos 0.0.252 → 0.0.253

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,4 +1,5 @@
1
- import { CheckoutInitParams, CheckoutError, CheckoutErrorType, CheckoutStatus, CheckoutStep } from '../types';
1
+ import { CheckoutInitParams, CheckoutError, CheckoutErrorType, CheckoutStatus, CheckoutStep, LocalOrderData, CartSummaryItem, ExtractedAmountInfo } from '../types';
2
+ import { CartItem } from '../../../modules/Cart/types';
2
3
  /**
3
4
  * 验证结账数据
4
5
  */
@@ -71,3 +72,46 @@ export declare const logger: {
71
72
  error: (message: string, ...args: any[]) => void;
72
73
  debug: (message: string, ...args: any[]) => void;
73
74
  };
75
+ /**
76
+ * 验证本地订单数据
77
+ */
78
+ export declare function validateLocalOrderData(orderData: LocalOrderData): {
79
+ valid: boolean;
80
+ errors: string[];
81
+ };
82
+ /**
83
+ * 生成本地订单ID
84
+ */
85
+ export declare function generateLocalOrderId(): string;
86
+ /**
87
+ * 格式化日期时间为 YYYY-MM-DD hh:mm:ss 格式
88
+ *
89
+ * @param date 要格式化的日期对象
90
+ * @returns 格式化后的日期时间字符串
91
+ */
92
+ export declare function formatDateTime(date: Date): string;
93
+ /**
94
+ * 从购物车小计数据中提取金额信息
95
+ */
96
+ export declare function extractAmountFromCartSummary(cartSummary: CartSummaryItem[]): ExtractedAmountInfo;
97
+ /**
98
+ * 计算购物车总金额
99
+ */
100
+ export declare function calculateTotalAmount(cartItems: CartItem[]): string;
101
+ /**
102
+ * 判断订单ID是否为本地生成的虚拟ID
103
+ *
104
+ * @param orderId 订单ID
105
+ * @returns true 表示是虚拟ID,false 表示是真实的后端ID
106
+ */
107
+ export declare function isVirtualOrderId(orderId: string): boolean;
108
+ /**
109
+ * 判断支付方式是否需要同步订单到后端
110
+ *
111
+ * 现金支付(CASHMANUAL)和自定义支付不需要同步,其他支付方式需要同步
112
+ *
113
+ * @param paymentCode 支付方式代码
114
+ * @param paymentType 支付方式类型
115
+ * @returns 是否需要同步订单
116
+ */
117
+ export declare function shouldSyncOrderForPayment(paymentCode: string, paymentType: string): boolean;
@@ -20,24 +20,32 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
20
20
  var utils_exports = {};
21
21
  __export(utils_exports, {
22
22
  calculateProgress: () => calculateProgress,
23
+ calculateTotalAmount: () => calculateTotalAmount,
23
24
  createCheckoutError: () => createCheckoutError,
24
25
  debounce: () => debounce,
25
26
  deepClone: () => deepClone,
27
+ extractAmountFromCartSummary: () => extractAmountFromCartSummary,
26
28
  formatAmount: () => formatAmount,
29
+ formatDateTime: () => formatDateTime,
30
+ generateLocalOrderId: () => generateLocalOrderId,
27
31
  generateOrderId: () => generateOrderId,
28
32
  getErrorMessage: () => getErrorMessage,
29
33
  isEmpty: () => isEmpty,
30
34
  isProduction: () => isProduction,
31
35
  isValidPaymentMethodCode: () => isValidPaymentMethodCode,
36
+ isVirtualOrderId: () => isVirtualOrderId,
32
37
  logger: () => logger,
33
38
  retry: () => retry,
34
39
  safeJsonParse: () => safeJsonParse,
40
+ shouldSyncOrderForPayment: () => shouldSyncOrderForPayment,
35
41
  throttle: () => throttle,
36
42
  validateAmount: () => validateAmount,
37
- validateCheckoutData: () => validateCheckoutData
43
+ validateCheckoutData: () => validateCheckoutData,
44
+ validateLocalOrderData: () => validateLocalOrderData
38
45
  });
39
46
  module.exports = __toCommonJS(utils_exports);
40
47
  var import_types = require("../types");
48
+ var import_types2 = require("../../../modules/Payment/types");
41
49
  function validateCheckoutData(params) {
42
50
  const errors = [];
43
51
  if (!params.cartItems || params.cartItems.length === 0) {
@@ -245,22 +253,151 @@ var logger = {
245
253
  }
246
254
  }
247
255
  };
256
+ function validateLocalOrderData(orderData) {
257
+ var _a;
258
+ const errors = [];
259
+ if (!orderData.type) {
260
+ errors.push("订单类型不能为空");
261
+ }
262
+ if (!orderData.platform) {
263
+ errors.push("平台信息不能为空");
264
+ }
265
+ if ((!orderData.bookings || orderData.bookings.length === 0) && !((_a = orderData == null ? void 0 : orderData.relation_products) == null ? void 0 : _a.length)) {
266
+ errors.push("预订信息不能为空");
267
+ }
268
+ if (orderData.bookings) {
269
+ orderData.bookings.forEach((booking, index) => {
270
+ if (!booking.product || !booking.product.product_id) {
271
+ errors.push(`预订项 ${index + 1} 缺少商品信息`);
272
+ }
273
+ if (!booking.start_date) {
274
+ errors.push(`预订项 ${index + 1} 缺少开始日期`);
275
+ }
276
+ if (!booking.start_time) {
277
+ errors.push(`预订项 ${index + 1} 缺少开始时间`);
278
+ }
279
+ });
280
+ }
281
+ return {
282
+ valid: errors.length === 0,
283
+ errors
284
+ };
285
+ }
286
+ function generateLocalOrderId() {
287
+ const timestamp = Date.now();
288
+ const random = Math.floor(Math.random() * 1e4).toString().padStart(4, "0");
289
+ return `local_order_${timestamp}_${random}`;
290
+ }
291
+ function formatDateTime(date) {
292
+ const year = date.getFullYear();
293
+ const month = String(date.getMonth() + 1).padStart(2, "0");
294
+ const day = String(date.getDate()).padStart(2, "0");
295
+ const hours = String(date.getHours()).padStart(2, "0");
296
+ const minutes = String(date.getMinutes()).padStart(2, "0");
297
+ const seconds = String(date.getSeconds()).padStart(2, "0");
298
+ return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
299
+ }
300
+ function extractAmountFromCartSummary(cartSummary) {
301
+ const result = {
302
+ totalAmount: "0.00",
303
+ subTotal: "0.00",
304
+ taxAmount: "0.00",
305
+ discountAmount: "0.00",
306
+ shopDiscountAmount: "0.00",
307
+ roundingAmount: "0.00"
308
+ };
309
+ cartSummary.forEach((item) => {
310
+ const value = Number(item.value).toFixed(2);
311
+ switch (item.key) {
312
+ case "expect_amount":
313
+ result.totalAmount = value;
314
+ break;
315
+ case "sub_total":
316
+ result.subTotal = value;
317
+ break;
318
+ case "tax":
319
+ result.taxAmount = value;
320
+ if (item.tax) {
321
+ result.taxDetails = item.tax;
322
+ }
323
+ break;
324
+ case "discount":
325
+ result.discountAmount = value;
326
+ break;
327
+ case "shop_discount":
328
+ result.shopDiscountAmount = value;
329
+ break;
330
+ case "custom_roundingAmount":
331
+ result.roundingAmount = value;
332
+ break;
333
+ default:
334
+ if (item.key.includes("deposit")) {
335
+ result.depositAmount = value;
336
+ }
337
+ break;
338
+ }
339
+ });
340
+ console.log("[Checkout] 从购物车小计提取金额信息:", {
341
+ totalAmount: result.totalAmount,
342
+ subTotal: result.subTotal,
343
+ taxAmount: result.taxAmount,
344
+ discountAmount: result.discountAmount,
345
+ shopDiscountAmount: result.shopDiscountAmount,
346
+ roundingAmount: result.roundingAmount
347
+ });
348
+ return result;
349
+ }
350
+ function calculateTotalAmount(cartItems) {
351
+ const total = cartItems.reduce((sum, item) => {
352
+ const price = parseFloat(String(item.price) || "0");
353
+ const quantity = item.quantity || 1;
354
+ return sum + price * quantity;
355
+ }, 0);
356
+ return total.toFixed(2);
357
+ }
358
+ function isVirtualOrderId(orderId) {
359
+ return orderId.startsWith("local_order_");
360
+ }
361
+ function shouldSyncOrderForPayment(paymentCode, paymentType) {
362
+ const codeUpper = (paymentCode == null ? void 0 : paymentCode.toUpperCase()) || "";
363
+ const typeUpper = (paymentType == null ? void 0 : paymentType.toUpperCase()) || "";
364
+ const cashIdentifiers = ["CASHMANUAL", "CASH", "MANUAL"];
365
+ if (cashIdentifiers.some(
366
+ (id) => codeUpper.includes(id) || typeUpper.includes(id)
367
+ )) {
368
+ return false;
369
+ }
370
+ if (paymentCode === import_types2.PaymentMethodType.Cash || paymentType === import_types2.PaymentMethodType.Cash) {
371
+ return false;
372
+ }
373
+ if (codeUpper.includes("CUSTOM") || typeUpper.includes("CUSTOM")) {
374
+ return false;
375
+ }
376
+ return true;
377
+ }
248
378
  // Annotate the CommonJS export names for ESM import in node:
249
379
  0 && (module.exports = {
250
380
  calculateProgress,
381
+ calculateTotalAmount,
251
382
  createCheckoutError,
252
383
  debounce,
253
384
  deepClone,
385
+ extractAmountFromCartSummary,
254
386
  formatAmount,
387
+ formatDateTime,
388
+ generateLocalOrderId,
255
389
  generateOrderId,
256
390
  getErrorMessage,
257
391
  isEmpty,
258
392
  isProduction,
259
393
  isValidPaymentMethodCode,
394
+ isVirtualOrderId,
260
395
  logger,
261
396
  retry,
262
397
  safeJsonParse,
398
+ shouldSyncOrderForPayment,
263
399
  throttle,
264
400
  validateAmount,
265
- validateCheckoutData
401
+ validateCheckoutData,
402
+ validateLocalOrderData
266
403
  });
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "private": false,
3
3
  "name": "@pisell/pisellos",
4
- "version": "0.0.252",
4
+ "version": "0.0.253",
5
5
  "description": "一个可扩展的前端模块化SDK框架,支持插件系统",
6
6
  "main": "dist/index.js",
7
7
  "types": "dist/index.d.ts",