UNPKG

@pisell/pisellos

Version:

一个可扩展的前端模块化SDK框架,支持插件系统

178 lines (176 loc) 6.01 kB
var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; 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 __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // src/server/modules/quotation/index.ts var quotation_exports = {}; __export(quotation_exports, { QuotationModule: () => QuotationModule }); module.exports = __toCommonJS(quotation_exports); var import_lodash_es = require("lodash-es"); var import_BaseModule = require("../../../modules/BaseModule"); var import_types = require("./types"); var INDEXDB_STORE_NAME = "quotation"; var QuotationModule = class extends import_BaseModule.BaseModule { // IndexDBManager 实例 constructor(name, version) { super(name, version); this.defaultName = "quotation"; this.defaultVersion = "1.0.0"; } async initialize(core, options) { var _a; this.core = core; this.store = options == null ? void 0 : options.store; if (Array.isArray((_a = options == null ? void 0 : options.initialState) == null ? void 0 : _a.quotationList)) { this.store.quotationList = options == null ? void 0 : options.initialState.quotationList; this.core.effects.emit(import_types.QuotationHooks.onQuotationChanged, this.store.quotationList); } else { this.store.quotationList = []; } this.request = core.getPlugin("request"); if (!this.request) { throw new Error("QuotationModule 需要 request 插件支持"); } const appPlugin = core.getPlugin("app"); if (appPlugin) { const app = appPlugin.getApp(); this.dbManager = app.dbManager; if (this.dbManager) { console.log("[Quotation] IndexDB Manager 已初始化"); } else { console.warn("[Quotation] IndexDB Manager 未找到"); } } } /** * 加载报价单列表(从服务器) * TODO: 接口地址待定 */ async loadQuotationList() { var _a; try { const response = await this.request.get( `/quotation`, { num: 999, skip: 1, status: "published" } ); const quotationList = ((_a = response.data) == null ? void 0 : _a.list) || []; await this.saveQuotationToIndexDB(quotationList); await this.setQuotationList(quotationList); return quotationList; } catch (error) { console.error("[Quotation] 加载报价单数据失败:", error); return []; } } /** * 设置报价单列表 */ async setQuotationList(quotationList) { this.store.quotationList = quotationList; await this.core.effects.emit(import_types.QuotationHooks.onQuotationLoaded, quotationList); } /** * 获取报价单列表 */ getQuotationList() { return this.store.quotationList; } /** * 清空缓存 */ async clear() { this.store.quotationList = []; if (this.dbManager) { try { await this.dbManager.clear(INDEXDB_STORE_NAME); console.log("[Quotation] IndexDB 缓存已清空"); } catch (error) { console.error("[Quotation] 清空 IndexDB 缓存失败:", error); } } console.log("[Quotation] 缓存已清空"); } /** * 从 IndexDB 加载报价单数据 * @private */ async loadQuotationFromIndexDB() { if (!this.dbManager) { return []; } try { const quotationList = await this.dbManager.getAll(INDEXDB_STORE_NAME); return quotationList || []; } catch (error) { console.error("[Quotation] 从 IndexDB 读取数据失败:", error); return []; } } /** * 保存报价单数据到 IndexDB * @private */ async saveQuotationToIndexDB(quotationList) { if (!this.dbManager) { return; } try { await this.dbManager.clear(INDEXDB_STORE_NAME); const savePromises = quotationList.map( (quotation) => this.dbManager.add(INDEXDB_STORE_NAME, quotation) ); await Promise.all(savePromises); console.log(`[Quotation] 已将 ${quotationList.length} 条报价单数据保存到 IndexDB`); } catch (error) { console.error("[Quotation] 保存数据到 IndexDB 失败:", error); } } /** * 预加载模块数据(统一接口) * 在模块注册后自动调用 */ async preload() { console.log("[Quotation] 开始预加载数据..."); try { const cachedData = await this.loadQuotationFromIndexDB(); if (cachedData && cachedData.length > 0) { console.log(`[Quotation] 从 IndexDB 加载了 ${cachedData.length} 条报价单数据`); this.store.quotationList = (0, import_lodash_es.cloneDeep)(cachedData); this.core.effects.emit(import_types.QuotationHooks.onQuotationChanged, this.store.quotationList); return; } console.log("[Quotation] IndexDB 中没有缓存数据,从服务器加载..."); } catch (error) { console.warn("[Quotation] 从 IndexDB 加载数据失败:", error); } const quotationList = await this.loadQuotationList(); if (quotationList && quotationList.length > 0) { await this.saveQuotationToIndexDB(quotationList); this.store.quotationList = (0, import_lodash_es.cloneDeep)(quotationList); this.core.effects.emit(import_types.QuotationHooks.onQuotationChanged, this.store.quotationList); } } }; // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { QuotationModule });