@pisell/pisellos
Version:
一个可扩展的前端模块化SDK框架,支持插件系统
424 lines (422 loc) • 15.7 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/solution/ShopDiscount/index.ts
var ShopDiscount_exports = {};
__export(ShopDiscount_exports, {
ShopDiscountImpl: () => ShopDiscountImpl,
default: () => ShopDiscount_default
});
module.exports = __toCommonJS(ShopDiscount_exports);
var import_BaseModule = require("../../modules/BaseModule");
var import_types = require("./types");
var import_Discount = require("../../modules/Discount");
var import_Rules = require("../../modules/Rules");
var import_decimal = __toESM(require("decimal.js"));
var ShopDiscountImpl = class extends import_BaseModule.BaseModule {
constructor(name, version) {
super(name, version);
this.defaultName = "shopDiscount";
this.defaultVersion = "1.0.0";
this.isSolution = true;
this.options = {};
this.store = {
customer: null,
productList: [],
discount: null,
rules: null
};
}
// =========== 生命周期方法 ===========
async initialize(core, options) {
var _a, _b;
this.core = core;
this.options = options;
this.store = { ...this.store, ...options.store || {} };
this.hooks = (_b = (_a = options.otherParams) == null ? void 0 : _a.rules) == null ? void 0 : _b.hooks;
console.log("[ShopDiscount] 初始化完成");
this.initializePlugins();
this.registerDependentModules();
this.registerEventListeners();
}
async destroy() {
var _a, _b;
this.store.productList = [];
this.store.customer = null;
(_a = this.store.discount) == null ? void 0 : _a.destroy();
(_b = this.store.rules) == null ? void 0 : _b.destroy();
this.core.effects.offByModuleDestroy(this.name);
await this.core.effects.emit(import_types.ShopDiscountHooks.onDestroy, {});
console.log("[ShopDiscount] 已销毁");
}
async clear() {
var _a, _b;
this.store.productList = [];
this.store.customer = null;
(_a = this.store.discount) == null ? void 0 : _a.clear();
(_b = this.store.rules) == null ? void 0 : _b.clear();
console.log("[ShopDiscount] clear");
}
// =========== 初始化辅助方法 ===========
initializePlugins() {
this.request = this.core.getPlugin("request");
this.window = this.core.getPlugin("window");
if (!this.request) {
throw new Error("ShopDiscount需要 request 插件支持");
}
if (!this.window) {
throw new Error("ShopDiscount需要 window 插件支持");
}
}
registerDependentModules() {
var _a, _b, _c, _d, _e, _f, _g;
let targetCacheData = {};
if ((_a = this.options.otherParams) == null ? void 0 : _a.cacheId) {
const sessionData = this.window.sessionStorage.getItem(this.name);
if (sessionData) {
const data = JSON.parse(sessionData);
targetCacheData = data[this.options.otherParams.cacheId];
}
}
const discount = new import_Discount.DiscountModule(`${this.name}_discount`);
this.core.registerModule(discount, {
initialState: targetCacheData == null ? void 0 : targetCacheData[discount.name],
hooks: (_c = (_b = this.options.otherParams) == null ? void 0 : _b.discount) == null ? void 0 : _c.hooks,
otherParams: {
fatherModule: this.name,
openCache: ((_d = this.options.otherParams) == null ? void 0 : _d.cacheId) ? true : false,
cacheId: (_e = this.options.otherParams) == null ? void 0 : _e.cacheId
}
});
this.store.discount = discount;
const rules = new import_Rules.RulesModule(`${this.name}_rules`);
this.core.registerModule(rules, {
hooks: (_g = (_f = this.options.otherParams) == null ? void 0 : _f.rules) == null ? void 0 : _g.hooks
});
this.store.rules = rules;
}
registerEventListeners() {
this.core.effects.on(
import_types.ShopDiscountHooks.onCustomerChange,
(customer) => {
var _a;
if (((_a = this.options.otherParams) == null ? void 0 : _a.cacheId) && this.getDiscountList().length) {
return;
}
this.loadPrepareConfig({
customerId: customer.id,
action: "create",
with_good_pass: 1,
with_discount_card: 1
});
}
);
}
// =========== 公共 API ===========
// 设置客户
async setCustomer(customer) {
var _a;
if (((_a = this.store.customer) == null ? void 0 : _a.id) !== customer.id) {
this.store.customer = customer;
await this.core.effects.emit(
import_types.ShopDiscountHooks.onCustomerChange,
customer
);
}
}
// 计算优惠券
calcDiscount(productList, options) {
this.store.productList = productList;
const rulesModule = this.core.getModule(`${this.name}_rules`);
if (!rulesModule) {
return { productList, discountList: this.getDiscountList() };
}
let { productList: newProductList, discountList: newDiscountList } = rulesModule.calcDiscount(
{
productList,
discountList: this.getDiscountList()
},
options
) || { productList, discountList: this.getDiscountList() };
if (newDiscountList) {
newDiscountList = this.setDiscountList(newDiscountList);
}
return {
productList: newProductList || productList,
discountList: newDiscountList || this.getDiscountList()
};
}
// 设置优惠券使用状态
setDiscountSelected({
discountId,
isSelected
}) {
const discountList = this.getDiscountList();
const newDiscountList = discountList.map((discount) => {
if (discount.id === discountId) {
return {
...discount,
isSelected,
// 手动设置不使用
isManualSelect: !isSelected
};
}
return { ...discount, isManualSelect: !(discount == null ? void 0 : discount.isSelected) };
});
this.setDiscountList(newDiscountList);
return this.calcDiscount(this.store.productList || [], {
discountId,
isSelected
});
}
setProductList(productList) {
this.store.productList = productList;
}
// 扫码输入code
async scanCode(code) {
var _a, _b;
try {
const resultDiscountList = await ((_a = this.store.discount) == null ? void 0 : _a.batchSearch(code)) || [];
const rulesModule = this.store.rules;
if (!rulesModule) {
return {
type: "clientCalc",
isAvailable: false,
productList: this.store.productList || [],
discountList: this.getDiscountList()
};
}
if (!resultDiscountList.length) {
return {
type: "server",
isAvailable: false,
productList: this.store.productList || [],
discountList: this.getDiscountList()
};
}
const withScanList = resultDiscountList.map((item) => {
return {
...item,
isScan: true
};
});
const {
productList: newProductList,
discountList: newDiscountList,
isAvailable
} = rulesModule.isDiscountListAvailable({
productList: this.store.productList || [],
oldDiscountList: this.getDiscountList(),
newDiscountList: withScanList
}) || {
isAvailable: false,
productList: this.store.productList || [],
discountList: this.getDiscountList()
};
if (isAvailable) {
this.setDiscountList(newDiscountList || []);
this.setProductList(newProductList || []);
if (this.isWalkIn() && resultDiscountList.length && ((_b = this.options.otherParams) == null ? void 0 : _b.platform) === "shop") {
await this.getCustomerWallet(
resultDiscountList[0].customer_id
);
}
}
return {
type: "clientCalc",
isAvailable: isAvailable || false,
productList: newProductList || this.store.productList || [],
discountList: newDiscountList || this.getDiscountList()
};
} catch (error) {
console.error("[ShopDiscount] 扫码出错:", error);
return {
type: "clientCalc",
isAvailable: false,
productList: this.store.productList || [],
discountList: this.getDiscountList()
};
}
}
calcDiscountApplicableProductTotalPrice(discount) {
var _a;
return (_a = this.store.discount) == null ? void 0 : _a.calcDiscountApplicableProductTotalPrice(
discount
);
}
// =========== 私有辅助方法 ===========
// 获取客户
getCustomer() {
return this.store.customer;
}
// 是否是walkIn
isWalkIn() {
var _a;
return (((_a = this.getCustomer()) == null ? void 0 : _a.id) || 1) === 1;
}
// 触发优惠列表变更事件
async emitDiscountListChange(discountList) {
await this.core.effects.emit(
import_types.ShopDiscountHooks.onDiscountListChange,
discountList
);
}
// 设置优惠券列表
setDiscountList(discountList) {
var _a;
const productList = this.store.productList || [];
const editModeDiscountList = [];
productList.forEach((item) => {
var _a2;
if (item.booking_id) {
const product = (_a2 = this.hooks) == null ? void 0 : _a2.getProduct(item);
(item.discount_list || []).forEach((discount) => {
var _a3, _b, _c;
if (discount.id && ["good_pass", "discount_card"].includes(discount.type)) {
const index = editModeDiscountList.findIndex((n) => {
var _a4;
return n.id === (((_a4 = discount.discount) == null ? void 0 : _a4.resource_id) || discount.id);
});
if (index !== -1) {
editModeDiscountList[index] = {
...editModeDiscountList[index],
amount: new import_decimal.default(discount.amount || 0).plus(new import_decimal.default(editModeDiscountList[index].amount || 0)).toNumber(),
savedAmount: new import_decimal.default(discount.amount || 0).times((product == null ? void 0 : product.num) || 1).plus(new import_decimal.default(editModeDiscountList[index].savedAmount || 0)).toNumber()
};
} else {
if (discount.type && !discount.tag) {
discount.tag = discount.type;
}
editModeDiscountList.push({
...discount,
isEditMode: true,
limited_relation_product_data: {},
savedAmount: discount.amount * ((product == null ? void 0 : product.num) || 1),
isAvailable: true,
id: ((_a3 = discount.discount) == null ? void 0 : _a3.resource_id) || discount.id,
format_title: ((_b = discount.discount) == null ? void 0 : _b.title) || discount.format_title,
isDisabled: true,
isSelected: true,
product_id: ((_c = discount.discount) == null ? void 0 : _c.product_id) || discount.product_id
});
}
}
});
}
});
const newDiscountList = [
...editModeDiscountList,
...discountList.filter((item) => !item.isDisabled)
];
const allUsedProductIds = newDiscountList.map((n) => {
var _a2;
return n.isSelected ? (_a2 = n.appliedProductDetails) == null ? void 0 : _a2.map((n2) => {
var _a3;
return (_a3 = n2.discount) == null ? void 0 : _a3.product_id;
}) : [];
}).flat();
newDiscountList.forEach((item) => {
var _a2;
const isProductFree = (id) => {
var _a3;
const targetProduct = productList.find((n) => n.id === id);
const product = (_a3 = this.hooks) == null ? void 0 : _a3.getProduct(targetProduct);
return Number(product == null ? void 0 : product.price) === 0;
};
const isAllProductUsedOrFree = (_a2 = item.applicableProductIds) == null ? void 0 : _a2.every((id) => {
return (allUsedProductIds == null ? void 0 : allUsedProductIds.includes(id)) || isProductFree(id);
});
if (!item.isSelected && isAllProductUsedOrFree) {
item.isDisabledForProductUsed = true;
} else {
item.isDisabledForProductUsed = false;
}
});
(_a = this.store.discount) == null ? void 0 : _a.setDiscountList(newDiscountList);
this.emitDiscountListChange(newDiscountList);
return newDiscountList;
}
// 获取优惠券列表
getDiscountList() {
var _a;
return ((_a = this.store.discount) == null ? void 0 : _a.getDiscountList()) || [];
}
// 获取客户钱包信息
async getCustomerWallet(id) {
try {
const result = await this.request.get(`/customer/info/${id}`, {
with_trashed: 1
});
if (result == null ? void 0 : result.data) {
const { ...customer } = result.data;
this.setCustomer(customer);
await this.core.effects.emit(
import_types.ShopDiscountHooks.onScanCustomerChange,
customer
);
}
} catch (error) {
console.error("[ShopDiscount] 获取客户钱包信息出错:", error);
}
}
// 加载准备配置
async loadPrepareConfig(params) {
var _a, _b, _c, _d;
try {
const customerId = params.customerId || ((_a = this.getCustomer()) == null ? void 0 : _a.id);
const goodPassList = await ((_b = this.store.discount) == null ? void 0 : _b.loadPrepareConfig({
customer_id: customerId,
action: "create",
with_good_pass: 1,
with_discount_card: 1
}));
const scanDiscount = (_c = this.getDiscountList()) == null ? void 0 : _c.filter(
(item) => item.isScan
);
const newDiscountList = [...scanDiscount, ...goodPassList || []];
this.setDiscountList(newDiscountList || []);
if ((_d = this.store.productList) == null ? void 0 : _d.length) {
const result = this.calcDiscount(this.store.productList);
await this.core.effects.emit(
import_types.ShopDiscountHooks.onLoadPrepareCalcResult,
result
);
}
await this.core.effects.emit(
import_types.ShopDiscountHooks.onLoadDiscountList,
newDiscountList
);
} catch (error) {
console.error("[ShopDiscount] 加载准备配置出错:", error);
}
}
};
var ShopDiscount_default = ShopDiscountImpl;
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
ShopDiscountImpl
});