@pisell/pisellos
Version:
一个可扩展的前端模块化SDK框架,支持插件系统
448 lines (446 loc) • 13.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 __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default"));
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/BookingTicket/index.ts
var BookingTicket_exports = {};
__export(BookingTicket_exports, {
BookingTicket: () => BookingTicketImpl,
BookingTicketImpl: () => BookingTicketImpl
});
module.exports = __toCommonJS(BookingTicket_exports);
var import_BaseModule = require("../../modules/BaseModule");
var import_types = require("./types");
var import_scan = __toESM(require("./utils/scan"));
var import_handleScan = require("./utils/scan/handleScan");
var import_scanCache = __toESM(require("./utils/scan/scanCache"));
__reExport(BookingTicket_exports, require("./types"), module.exports);
var BookingTicketImpl = class extends import_BaseModule.BaseModule {
constructor() {
super(...arguments);
this.defaultName = "bookingTicket";
this.defaultVersion = "1.0.0";
this.isSolution = true;
this.otherParams = {};
}
async initialize(core, options) {
var _a, _b, _c;
this.core = core;
this.store = options.store || {};
this.otherParams = options.otherParams || {};
console.log("[BookingTicket] 初始化完成");
this.request = core.getPlugin("request");
this.window = core.getPlugin("window");
this.shopStore = core.getPlugin("shopStore");
if (!this.request) {
throw new Error("bookingTicket解决方案需要 request 插件支持");
}
if (!this.shopStore) {
throw new Error("bookingTicket解决方案需要 shopStore 插件支持");
}
let targetCacheData = {};
this.cacheId = (_a = this.otherParams) == null ? void 0 : _a.cacheId;
this.platform = (_b = this.otherParams) == null ? void 0 : _b.platform;
this.scan = new import_scan.default(this, "BOOKING_TICKET_SCAN");
if ((_c = this.otherParams) == null ? void 0 : _c.cacheId) {
const sessionData = this.window.sessionStorage.getItem(this.name);
if (sessionData) {
const data = JSON.parse(sessionData);
targetCacheData = data[this.otherParams.cacheId];
}
}
const moduleArr = [
"cart",
"products",
"accountList",
"customer",
"order"
];
moduleArr.forEach((step) => {
var _a2, _b2;
const targetModule = (0, import_types.createModule)(step, this.name);
if (targetModule) {
this.store[step] = targetModule;
this.core.registerModule(targetModule, {
initialState: targetCacheData == null ? void 0 : targetCacheData[targetModule.name],
otherParams: {
...this.otherParams,
fatherModule: this.name,
openCache: ((_a2 = this.otherParams) == null ? void 0 : _a2.cacheId) ? true : false,
cacheId: (_b2 = this.otherParams) == null ? void 0 : _b2.cacheId
}
});
} else {
throw new Error(`模块 ${step} 不存在`);
}
});
this.core.effects.emit(`${this.name}:onInited`, {});
}
/**
* 获取商品列表
* @param params 包含 schedule_date 的参数
* @returns 商品列表
*/
async loadProducts(params = {}, options) {
const { schedule_date, customer_id, menu_list_ids, schedule_datetime } = params;
try {
const result = await this.store.products.loadProducts({
with_count: ["bundleGroup", "optionGroup"],
with_schedule: 1,
...params,
cacheId: this.cacheId
}, options);
this.core.effects.emit(`${this.name}:onProductsLoaded`, result);
return result;
} catch (error) {
console.error("Failed to load products:", error);
throw error;
}
}
/**
* 取消商品查询订阅
* @param subscriberId 订阅时传入的 subscriberId
*/
unsubscribeProductQuery(subscriberId) {
var _a;
(_a = this.core.server) == null ? void 0 : _a.removeProductQuerySubscriber(subscriberId);
}
/**
* 初始化外设扫码结果监听
*/
initPeripheralsListener() {
this.scan.initPeripheralsListener();
}
/**
* 获取商品列表(不加载到模块中)
* @returns 商品列表
*/
async getProducts() {
return this.store.products.getProducts();
}
/**
* 获取日程时间段点
* @param params 参数
* @returns 日程时间段点
*/
async getScheduleTimePoints(params) {
const result = await this.request.post("/menu/schedule-time-points", {
menu_list_ids: params.menu_list_ids
}, {
osServer: true
});
return result.data || [];
}
/**
* 获取客户列表
* @param params 查询参数
* @returns 客户列表响应
*/
async getCustomerList(params = {}) {
try {
const result = await this.store.customer.getCustomerList(params);
return result;
} catch (error) {
console.error("Failed to get customer list:", error);
throw error;
}
}
/**
* 设置活跃客户
* @param customer 客户信息
*/
setActiveCustomer(customer) {
this.store.customer.setSelectedCustomer(customer);
this.core.effects.emit(`${this.name}:onCustomerSelected`, {
customer
});
}
/**
* 获取当前活跃客户
* @returns 当前活跃客户
*/
getActiveCustomer() {
return this.store.customer.getSelectedCustomer();
}
/**
* 根据ID设置选中的客户
* @param customerId 客户ID
*/
setSelectedCustomerById(customerId) {
const customer = this.store.customer.getCustomerById(customerId);
if (customer) {
this.store.customer.setSelectedCustomer(customer);
this.core.effects.emit(`${this.name}:onCustomerSelected`, {
customer
});
}
}
/**
* 获取所有客户
* @returns 客户列表
*/
getCustomers() {
return this.store.customer.getCustomers();
}
/**
* 根据ID获取客户
* @param customerId 客户ID
* @returns 客户信息
*/
getCustomerById(customerId) {
return this.store.customer.getCustomerById(customerId);
}
/**
* 清空客户列表
*/
clearCustomers() {
this.store.customer.clearCustomers();
}
/**
* 添加客户到列表第一位
* @param customer 要添加的客户信息
*/
addCustomerToFirst(customer) {
this.store.customer.addCustomerToFirst(customer);
}
/**
* 获取客户分页信息
* @returns 分页信息
*/
getCustomerPaginationInfo() {
return this.store.customer.getPaginationInfo();
}
/**
* 获取客户列表总数
* @returns 总数
*/
getCustomerTotal() {
return this.store.customer.getState().total;
}
/**
* 设置客户分页信息
* @param page 页码
* @param pageSize 每页数量
*/
setCustomerPaginationInfo(page, pageSize) {
this.store.customer.setPaginationInfo(page, pageSize);
}
/**
* 便捷方法:切换客户分页并自动获取数据
* @param page 页码
* @param pageSize 每页数量(可选)
* @returns 客户列表响应
*/
async changeCustomerPage(page, pageSize) {
try {
const result = await this.store.customer.changeCustomerPage(
page,
pageSize
);
return result;
} catch (error) {
console.error("Failed to change customer page:", error);
throw error;
}
}
/**
* 滚动加载更多客户数据 - 数据会追加到现有列表中
* @returns 客户列表响应
*/
async loadMoreCustomers() {
try {
const result = await this.store.customer.loadMoreCustomers();
this.core.effects.emit(`${this.name}:onCustomerListUpdate`, result);
return result;
} catch (error) {
console.error("Failed to load more customers:", error);
throw error;
}
}
/**
* 重置并重新开始滚动加载客户数据
* @param params 查询参数
* @returns 客户列表响应
*/
async resetAndLoadCustomers(params = {}) {
try {
const result = await this.store.customer.resetAndLoadCustomers(params);
this.core.effects.emit(`${this.name}:onCustomerListReset`, result);
return result;
} catch (error) {
console.error("Failed to reset and load customers:", error);
throw error;
}
}
/**
* 检查是否还有更多客户数据可以加载
* @returns 是否还有更多数据
*/
hasMoreCustomers() {
return this.store.customer.hasMoreCustomers();
}
/**
* 获取当前的客户搜索条件
* @returns 当前搜索条件
*/
getCurrentCustomerSearchParams() {
return this.store.customer.getCurrentSearchParams();
}
/**
* 获取客户列表状态(包含滚动加载相关状态)
* @returns 客户状态
*/
getCustomerState() {
return this.store.customer.getState();
}
/**
* 全局扫描监听
* @param callback 回调
*/
scanGlobalListener(callback) {
const localSearch = (v) => this.store.products.findProductsByCodeOrBarcode(v);
const scanCallback = (0, import_handleScan.handleGlobalScan)(this.request, localSearch);
const safeCallback = (d) => {
try {
callback(d);
} catch (error) {
console.error("scanGlobalListener传入的回调函数异常", error);
}
};
const listener = { key: "global", callback: safeCallback };
const removeListener = this.scan.addListener(listener, scanCallback);
return removeListener;
}
/**
* 客户扫描监听
* @param callback 回调
*/
scanCustomerListener(callback) {
const scanCallback = (0, import_handleScan.handleCustomerScan)();
const safeCallback = (d) => {
try {
callback(d);
} catch (error) {
console.error("scanCustomerListener传入的回调函数异常", error);
}
};
const listener = { key: "customer", callback: safeCallback };
const removeListener = this.scan.addListener(listener, scanCallback);
return removeListener;
}
/**
* @title 通用扫描监听
* @description 直接将扫描结果返回给调用方
* @param callback 回调
*/
scanUniversalListener(callback, key) {
const scanCallback = (0, import_handleScan.handleUniversalScan)();
const safeCallback = (d) => {
try {
callback(d);
} catch (error) {
console.error(
`scanUniversalListener传入的回调函数异常, key: ${key}`,
error
);
}
};
const listener = { key, callback: safeCallback };
const removeListener = this.scan.addListener(listener, scanCallback);
return removeListener;
}
/**
* 调用摄像头
* @param data 用户自定义数据
*/
activateCamera(data) {
var _a, _b, _c, _d;
(_d = (_c = (_b = (_a = this.window) == null ? void 0 : _a.interaction) == null ? void 0 : _b.utils) == null ? void 0 : _c.postMessageToApp) == null ? void 0 : _d.call(_c, {
module: "global",
key: "active_native_scanner",
data
});
}
/**
* 禁用所有扫描监听
*/
disableAllScanListeners() {
this.scan.disableAllListeners();
}
/**
* 启用所有扫描监听
*/
enableAllScanListeners() {
this.scan.enableAllListeners();
}
/**
* 清空所有扫描监听对应的任务执行队列
*/
clearAllScanListenersTaskQueue() {
this.scan.clearTaskQueue();
}
/**
* 设置其他参数
* @param params 参数
* @param options 选项
*/
async setOtherParams(params, { cover = false } = {}) {
if (cover) {
this.otherParams = params;
} else {
this.otherParams = { ...this.otherParams, ...params };
}
}
/**
* 获取其他参数
* @returns 其他参数
*/
async getOtherParams() {
return this.otherParams;
}
/**
* 销毁模块
*/
destroy() {
var _a, _b, _c, _d, _e;
(_a = this.store.cart) == null ? void 0 : _a.destroy();
(_b = this.store.products) == null ? void 0 : _b.destroy();
(_c = this.store.accountList) == null ? void 0 : _c.destroy();
(_d = this.store.customer) == null ? void 0 : _d.destroy();
(_e = this.store.order) == null ? void 0 : _e.destroy();
this.core.effects.offByModuleDestroy(this.name);
this.core.unregisterModule(this);
this.scan.removeAllListeners();
import_scanCache.default.clear();
}
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
BookingTicket,
BookingTicketImpl,
...require("./types")
});