@pisell/pisellos 0.0.195 → 0.0.199

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.
@@ -292,6 +292,8 @@ export interface PaymentModuleAPI {
292
292
  getPartiallyPaidOrdersAsync: () => Promise<PaymentOrder[]>;
293
293
  /** 获取未同步到服务器的订单 */
294
294
  getUnsyncedOrdersAsync: () => Promise<PaymentOrder[]>;
295
+ /** 检测未同步的订单并重新加入任务队列 */
296
+ recheckAndRequeueUnsyncedOrders: () => Promise<number>;
295
297
  /** 手动执行支付同步队列 */
296
298
  runPaymentSyncQueue: () => Promise<void>;
297
299
  }
@@ -317,7 +317,7 @@ export var RulesModule = /*#__PURE__*/function (_BaseModule) {
317
317
 
318
318
  // 然后再处理应用哪些优惠券,此时只考虑filteredDiscountList中的优惠券
319
319
  sortedProductList.forEach(function (originProduct, i) {
320
- var _product$discount_lis4, _product$discount_lis5, _product$discount_lis6, _product$discount_lis7, _product$discount_lis8, _product$discount_lis9;
320
+ var _product$discount_lis4, _product$discount_lis5, _product$discount_lis7, _product$discount_lis8, _product$discount_lis9, _product$discount_lis10;
321
321
  var product = _this3.hooks.getProduct(originProduct);
322
322
  if (product !== null && product !== void 0 && product.booking_id && (_product$discount_lis4 = product.discount_list) !== null && _product$discount_lis4 !== void 0 && _product$discount_lis4.length && product !== null && product !== void 0 && (_product$discount_lis5 = product.discount_list) !== null && _product$discount_lis5 !== void 0 && _product$discount_lis5.every(function (discount) {
323
323
  return discount.id && ['good_pass', 'discount_card', 'product_discount_card'].includes(discount.tag || discount.type);
@@ -328,11 +328,15 @@ export var RulesModule = /*#__PURE__*/function (_BaseModule) {
328
328
 
329
329
  // 找到适用于此商品的所有优惠券,仅考虑isSelected不为false的优惠券
330
330
  var applicableDiscounts = sortedDiscountList.filter(function (discount) {
331
+ var _product$discount_lis6;
331
332
  // 如果商品价格为 0,其实不需要使用任何优惠券,直接 return true
332
333
  // 商品券时主商品价格为0不可用
333
334
  if ((Number(product.price) === 0 || !product.price) && (discount.tag || discount.type) === 'good_pass') return false;
334
335
  // 折扣卡时总价为0时不可用
335
- if ((Number(product.total) === 0 || !product.total) && (discount.tag || discount.type) !== 'good_pass') return false;
336
+ if ((Number(product.total) === 0 || !product.total) && !((_product$discount_lis6 = product.discount_list) !== null && _product$discount_lis6 !== void 0 && _product$discount_lis6.find(function (n) {
337
+ var _n$discount;
338
+ return ((_n$discount = n.discount) === null || _n$discount === void 0 ? void 0 : _n$discount.resource_id) === discount.id;
339
+ })) && (discount.tag || discount.type) !== 'good_pass') return false;
336
340
  // 如果优惠券已被使用,则跳过
337
341
  var targetUsedDiscounts = usedDiscounts.get(discount.id);
338
342
  if (targetUsedDiscounts && (discount.tag || discount.type) === 'good_pass') return false;
@@ -356,12 +360,12 @@ export var RulesModule = /*#__PURE__*/function (_BaseModule) {
356
360
  var selectedDiscount = selectedDiscountCard || applicableDiscounts[0];
357
361
 
358
362
  // 如果是手动折扣,则不适用优惠券
359
- var isManualDiscount = typeof product.isManualDiscount === 'boolean' ? product.isManualDiscount : product.total != product.origin_total && (!((_product$discount_lis6 = product.discount_list) !== null && _product$discount_lis6 !== void 0 && _product$discount_lis6.length) || (product === null || product === void 0 || (_product$discount_lis7 = product.discount_list) === null || _product$discount_lis7 === void 0 || (_product$discount_lis8 = _product$discount_lis7.every) === null || _product$discount_lis8 === void 0 ? void 0 : _product$discount_lis8.call(_product$discount_lis7, function (item) {
363
+ var isManualDiscount = typeof product.isManualDiscount === 'boolean' ? product.isManualDiscount : product.total != product.origin_total && (!((_product$discount_lis7 = product.discount_list) !== null && _product$discount_lis7 !== void 0 && _product$discount_lis7.length) || (product === null || product === void 0 || (_product$discount_lis8 = product.discount_list) === null || _product$discount_lis8 === void 0 || (_product$discount_lis9 = _product$discount_lis8.every) === null || _product$discount_lis9 === void 0 ? void 0 : _product$discount_lis9.call(_product$discount_lis8, function (item) {
360
364
  return item.type === 'product';
361
365
  })));
362
366
 
363
367
  // 勾选时覆盖手动折扣
364
- if (options !== null && options !== void 0 && options.discountId && (_product$discount_lis9 = product.discount_list) !== null && _product$discount_lis9 !== void 0 && _product$discount_lis9.some(function (item) {
368
+ if (options !== null && options !== void 0 && options.discountId && (_product$discount_lis10 = product.discount_list) !== null && _product$discount_lis10 !== void 0 && _product$discount_lis10.some(function (item) {
365
369
  var _item$discount;
366
370
  return ((_item$discount = item.discount) === null || _item$discount === void 0 ? void 0 : _item$discount.resource_id) === options.discountId;
367
371
  })) {
@@ -423,7 +427,7 @@ export var RulesModule = /*#__PURE__*/function (_BaseModule) {
423
427
  }));
424
428
  }
425
429
  for (var _i = 0; _i < splitCount; _i++) {
426
- var _originProduct$_produ, _product$discount_lis10;
430
+ var _originProduct$_produ, _product$discount_lis11;
427
431
  // 如果用过折扣卡,也就不存在拆分的情况了,这里直接使用上面计算出来的折扣卡
428
432
  var _selectedDiscount = selectedDiscountCard || applicableDiscounts[_i];
429
433
  // 标记优惠券为已使用
@@ -439,7 +443,7 @@ export var RulesModule = /*#__PURE__*/function (_BaseModule) {
439
443
  productOriginTotal = product.total;
440
444
  }
441
445
  // 如果当前 product 有 discount_list,则必须从 origin_total 拿
442
- if ((_product$discount_lis10 = product.discount_list) !== null && _product$discount_lis10 !== void 0 && _product$discount_lis10.length && product.origin_total) {
446
+ if ((_product$discount_lis11 = product.discount_list) !== null && _product$discount_lis11 !== void 0 && _product$discount_lis11.length && product.origin_total) {
443
447
  productOriginTotal = product.origin_total;
444
448
  }
445
449
 
@@ -181,9 +181,47 @@ export declare class PaymentModule extends BaseModule implements Module, Payment
181
181
  * 注意:只清理完全支付的订单,部分支付的订单保留以便继续支付
182
182
  */
183
183
  cleanupFinishedOrders(): Promise<void>;
184
+ /**
185
+ * 检测未同步的订单并重新加入任务队列
186
+ *
187
+ * 此方法专门用于处理因网络问题导致任务队列执行失败但队列被清空的情况。
188
+ * 它会检查本地 IndexDB 中所有未同步的订单(isSynced: false),
189
+ * 并将这些订单重新添加到任务队列中。
190
+ *
191
+ * 使用场景:
192
+ * - 网络恢复后重新检查未同步的订单
193
+ * - 应用启动时检查并重新排队未完成的支付
194
+ * - 用户手动重试前的预检查
195
+ *
196
+ * @returns Promise<number> 返回重新加入队列的订单数量
197
+ */
198
+ recheckAndRequeueUnsyncedOrders(): Promise<number>;
184
199
  /**
185
200
  * 手动执行支付同步队列
186
201
  * 外部可以调用此方法来触发支付同步任务的执行
202
+ *
203
+ * 注意:此方法只执行现有队列中的任务,不会检查未同步的订单。
204
+ * 如果需要检查未同步订单,请先调用 recheckAndRequeueUnsyncedOrders()
205
+ *
206
+ * 使用示例:
207
+ * ```typescript
208
+ * // 1. 先检查并重新排队未同步订单,然后执行队列
209
+ * const requeuedCount = await paymentModule.recheckAndRequeueUnsyncedOrders();
210
+ * if (requeuedCount > 0) {
211
+ * await paymentModule.runPaymentSyncQueue();
212
+ * }
213
+ *
214
+ * // 2. 或者直接执行现有队列(不检查未同步订单)
215
+ * await paymentModule.runPaymentSyncQueue();
216
+ * ```
187
217
  */
188
218
  runPaymentSyncQueue(): Promise<void>;
219
+ /**
220
+ * 重新将订单添加到同步队列
221
+ * 用于处理因网络问题导致任务队列清空但订单未同步的情况
222
+ *
223
+ * @param order 要重新添加的订单
224
+ * @returns Promise<boolean> 是否成功添加到队列
225
+ */
226
+ private reAddOrderToSyncQueue;
189
227
  }
@@ -145,7 +145,7 @@ var PaymentModule = class extends import_BaseModule.BaseModule {
145
145
  registerNetworkHandlers() {
146
146
  const network = this.app.plugins.get("network");
147
147
  this.logInfo("Registering network status listener");
148
- network == null ? void 0 : network.addListener("networkStatusChange", (status) => {
148
+ network == null ? void 0 : network.addListener("networkStatusChange", async (status) => {
149
149
  console.log("网络状态:", status.connected);
150
150
  this.logInfo("Network status changed", {
151
151
  connected: status.connected,
@@ -153,7 +153,10 @@ var PaymentModule = class extends import_BaseModule.BaseModule {
153
153
  });
154
154
  if (status.connected) {
155
155
  this.logInfo("Network reconnected - triggering payment sync queue");
156
- this.runPaymentSyncQueue();
156
+ const requeuedCount = await this.recheckAndRequeueUnsyncedOrders();
157
+ if (requeuedCount > 0) {
158
+ await this.runPaymentSyncQueue();
159
+ }
157
160
  }
158
161
  });
159
162
  }
@@ -979,12 +982,86 @@ var PaymentModule = class extends import_BaseModule.BaseModule {
979
982
  console.error("[PaymentModule] 清理已完成订单失败", error);
980
983
  }
981
984
  }
985
+ /**
986
+ * 检测未同步的订单并重新加入任务队列
987
+ *
988
+ * 此方法专门用于处理因网络问题导致任务队列执行失败但队列被清空的情况。
989
+ * 它会检查本地 IndexDB 中所有未同步的订单(isSynced: false),
990
+ * 并将这些订单重新添加到任务队列中。
991
+ *
992
+ * 使用场景:
993
+ * - 网络恢复后重新检查未同步的订单
994
+ * - 应用启动时检查并重新排队未完成的支付
995
+ * - 用户手动重试前的预检查
996
+ *
997
+ * @returns Promise<number> 返回重新加入队列的订单数量
998
+ */
999
+ async recheckAndRequeueUnsyncedOrders() {
1000
+ this.logInfo("Starting recheckAndRequeueUnsyncedOrders");
1001
+ try {
1002
+ if (!this.app.tasksManager) {
1003
+ console.warn(
1004
+ "[PaymentModule] TasksManager 未初始化,无法重新排队未同步订单"
1005
+ );
1006
+ this.logWarning(
1007
+ "TasksManager not initialized, cannot requeue unsynced orders"
1008
+ );
1009
+ return 0;
1010
+ }
1011
+ const unsyncedOrders = await this.getUnsyncedOrdersAsync();
1012
+ if (unsyncedOrders.length === 0) {
1013
+ console.log("[PaymentModule] 没有发现未同步的订单");
1014
+ this.logInfo("No unsynced orders found");
1015
+ return 0;
1016
+ }
1017
+ console.log(
1018
+ `[PaymentModule] 发现 ${unsyncedOrders.length} 个未同步的订单,开始重新加入任务队列`
1019
+ );
1020
+ this.logInfo("Found unsynced orders, adding to sync queue", {
1021
+ unsyncedCount: unsyncedOrders.length,
1022
+ orderUuids: unsyncedOrders.map((o) => o.uuid)
1023
+ });
1024
+ let requeuedCount = 0;
1025
+ for (const order of unsyncedOrders) {
1026
+ const success = await this.reAddOrderToSyncQueue(order);
1027
+ if (success) {
1028
+ requeuedCount++;
1029
+ }
1030
+ }
1031
+ console.log(
1032
+ `[PaymentModule] 成功重新加入 ${requeuedCount} 个订单到任务队列`
1033
+ );
1034
+ this.logInfo("Successfully requeued unsynced orders", {
1035
+ requeuedCount,
1036
+ totalFound: unsyncedOrders.length
1037
+ });
1038
+ return requeuedCount;
1039
+ } catch (error) {
1040
+ console.error("[PaymentModule] 重新排队未同步订单失败", error);
1041
+ this.logError("recheckAndRequeueUnsyncedOrders failed", error);
1042
+ return 0;
1043
+ }
1044
+ }
982
1045
  /**
983
1046
  * 手动执行支付同步队列
984
1047
  * 外部可以调用此方法来触发支付同步任务的执行
1048
+ *
1049
+ * 注意:此方法只执行现有队列中的任务,不会检查未同步的订单。
1050
+ * 如果需要检查未同步订单,请先调用 recheckAndRequeueUnsyncedOrders()
1051
+ *
1052
+ * 使用示例:
1053
+ * ```typescript
1054
+ * // 1. 先检查并重新排队未同步订单,然后执行队列
1055
+ * const requeuedCount = await paymentModule.recheckAndRequeueUnsyncedOrders();
1056
+ * if (requeuedCount > 0) {
1057
+ * await paymentModule.runPaymentSyncQueue();
1058
+ * }
1059
+ *
1060
+ * // 2. 或者直接执行现有队列(不检查未同步订单)
1061
+ * await paymentModule.runPaymentSyncQueue();
1062
+ * ```
985
1063
  */
986
1064
  async runPaymentSyncQueue() {
987
- var _a;
988
1065
  this.logInfo("Starting runPaymentSyncQueue");
989
1066
  try {
990
1067
  if (!this.app.tasksManager) {
@@ -996,7 +1073,7 @@ var PaymentModule = class extends import_BaseModule.BaseModule {
996
1073
  module: "payment",
997
1074
  queueId: "payment_sync"
998
1075
  });
999
- if (!((_a = queue == null ? void 0 : queue.tasks) == null ? void 0 : _a.length)) {
1076
+ if (!(queue == null ? void 0 : queue.length)) {
1000
1077
  console.log("[PaymentModule] 支付同步队列无内容,不执行");
1001
1078
  return;
1002
1079
  }
@@ -1021,6 +1098,52 @@ var PaymentModule = class extends import_BaseModule.BaseModule {
1021
1098
  this.logError("runPaymentSyncQueue failed", error);
1022
1099
  }
1023
1100
  }
1101
+ /**
1102
+ * 重新将订单添加到同步队列
1103
+ * 用于处理因网络问题导致任务队列清空但订单未同步的情况
1104
+ *
1105
+ * @param order 要重新添加的订单
1106
+ * @returns Promise<boolean> 是否成功添加到队列
1107
+ */
1108
+ async reAddOrderToSyncQueue(order) {
1109
+ try {
1110
+ const paymentData = {
1111
+ payments: order.payment.map((payment) => payment),
1112
+ payment_status: order.payment_status === import_types.PaymentStatus.Finished ? "paid" : "partially_paid"
1113
+ };
1114
+ const existingQueue = this.app.tasksManager.getTaskQueue({
1115
+ module: "payment",
1116
+ queueId: "payment_sync"
1117
+ });
1118
+ const taskId = `payment_${order.uuid}`;
1119
+ const taskExists = existingQueue == null ? void 0 : existingQueue.some((task) => task.id === taskId);
1120
+ if (taskExists) {
1121
+ console.log(
1122
+ `[PaymentModule] 任务 ${taskId} 已存在于队列中,跳过重复添加`
1123
+ );
1124
+ return false;
1125
+ }
1126
+ await this.addToSyncQueue(order, paymentData);
1127
+ console.log(`[PaymentModule] 订单 ${order.uuid} 已重新添加到同步队列`);
1128
+ this.logInfo("Order re-added to sync queue", {
1129
+ orderUuid: order.uuid,
1130
+ orderId: order.order_info.order_id,
1131
+ paymentStatus: order.payment_status,
1132
+ unsyncedPayments: order.payment.filter((p) => !p.isSynced).length
1133
+ });
1134
+ return true;
1135
+ } catch (error) {
1136
+ console.error(
1137
+ `[PaymentModule] 重新添加订单 ${order.uuid} 到同步队列失败`,
1138
+ error
1139
+ );
1140
+ this.logError("Failed to re-add order to sync queue", error, {
1141
+ orderUuid: order.uuid,
1142
+ orderId: order.order_info.order_id
1143
+ });
1144
+ return false;
1145
+ }
1146
+ }
1024
1147
  };
1025
1148
  // Annotate the CommonJS export names for ESM import in node:
1026
1149
  0 && (module.exports = {
@@ -292,6 +292,8 @@ export interface PaymentModuleAPI {
292
292
  getPartiallyPaidOrdersAsync: () => Promise<PaymentOrder[]>;
293
293
  /** 获取未同步到服务器的订单 */
294
294
  getUnsyncedOrdersAsync: () => Promise<PaymentOrder[]>;
295
+ /** 检测未同步的订单并重新加入任务队列 */
296
+ recheckAndRequeueUnsyncedOrders: () => Promise<number>;
295
297
  /** 手动执行支付同步队列 */
296
298
  runPaymentSyncQueue: () => Promise<void>;
297
299
  }
@@ -214,9 +214,13 @@ var RulesModule = class extends import_BaseModule.BaseModule {
214
214
  return;
215
215
  }
216
216
  const applicableDiscounts = sortedDiscountList.filter((discount) => {
217
+ var _a2;
217
218
  if ((Number(product.price) === 0 || !product.price) && (discount.tag || discount.type) === "good_pass")
218
219
  return false;
219
- if ((Number(product.total) === 0 || !product.total) && (discount.tag || discount.type) !== "good_pass")
220
+ if ((Number(product.total) === 0 || !product.total) && !((_a2 = product.discount_list) == null ? void 0 : _a2.find((n) => {
221
+ var _a3;
222
+ return ((_a3 = n.discount) == null ? void 0 : _a3.resource_id) === discount.id;
223
+ })) && (discount.tag || discount.type) !== "good_pass")
220
224
  return false;
221
225
  const targetUsedDiscounts = usedDiscounts.get(discount.id);
222
226
  if (targetUsedDiscounts && (discount.tag || discount.type) === "good_pass")
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "private": false,
3
3
  "name": "@pisell/pisellos",
4
- "version": "0.0.195",
4
+ "version": "0.0.199",
5
5
  "description": "一个可扩展的前端模块化SDK框架,支持插件系统",
6
6
  "main": "dist/index.js",
7
7
  "types": "dist/index.d.ts",