UNPKG

@pisell/pisellos

Version:

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

191 lines (189 loc) 6.62 kB
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/modules/Date/index.ts var Date_exports = {}; __export(Date_exports, { DateModule: () => DateModule }); module.exports = __toCommonJS(Date_exports); var import_BaseModule = require("../BaseModule"); var import_utils = require("./utils"); var import_cloneDeep = __toESM(require("lodash-es/cloneDeep")); var DateModule = class extends import_BaseModule.BaseModule { constructor(name, version) { super(name, version); this.defaultName = "date"; this.defaultVersion = "1.0.0"; this.openCache = false; } async initialize(core, options) { var _a, _b; this.core = core; this.store = options.store; this.request = this.core.getPlugin("request"); if (options.initialState) { this.store.dateRange = options.initialState.dateRange; this.store.dateList = options.initialState.dateList; } if (!this.request) { throw new Error("DateModule 需要 request 插件支持"); } if ((_a = options.otherParams) == null ? void 0 : _a.cacheId) { this.openCache = options.otherParams.openCache; this.cacheId = options.otherParams.cacheId; } if ((_b = options.otherParams) == null ? void 0 : _b.fatherModule) { this.fatherModule = options.otherParams.fatherModule; } } setDateRange(dateRange) { this.store.dateRange = dateRange; } getDateRange() { return this.store.dateRange; } async getResourceDates(params) { var _a; const currentDateList = this.getDateList() || []; const currentDateListMap = new Map( currentDateList.map((item) => [item.date, item]) ); const resourceIds = ((_a = params.query) == null ? void 0 : _a.resource_ids) || []; const hasResource = resourceIds.every((id) => { var _a2; const currentItem = currentDateListMap.get(id.toString()); return (_a2 = currentItem == null ? void 0 : currentItem.resource) == null ? void 0 : _a2.some((n) => n.id === id); }); if (hasResource) { return currentDateList; } const dates = await this.getResourceAvailableTimeList(params); this.setDateList(dates); return dates; } getDateList() { return this.store.dateList; } setDateList(dateList) { const currentDateList = (0, import_cloneDeep.default)(this.store.dateList) || []; dateList.forEach((item) => { var _a; const currentItemIndex = currentDateList.findIndex( (n) => n.date === item.date ); if (currentItemIndex !== -1) { const currentItem = currentDateList[currentItemIndex]; const newResource = (_a = item.resource) == null ? void 0 : _a.filter( (n) => { var _a2; return !((_a2 = currentItem.resource) == null ? void 0 : _a2.some((m) => m.id === n.id)); } ); currentItem.resource = [ ...currentItem.resource || [], ...newResource || [] ]; currentDateList[currentItemIndex] = currentItem; } else { currentDateList.push(item); } }); this.store.dateList = currentDateList; } async fetchResourceDates(params) { const { url, query } = params; const fetchUrl = url || "/schedule/resource/list"; const { start_date, end_date, resource_ids } = query || {}; try { const res = await this.request.get( fetchUrl, { start_date, end_date, resource_ids, front_end_cache_id: this.cacheId }, { useCache: true } ); return res; } catch (error) { console.error(error); } } async getResourceAvailableTimeList(params) { var _a; const { query, rules, type } = params; const { start_date, end_date, resource_ids } = query || {}; if (!start_date || !end_date) { return []; } let dates = (0, import_utils.generateMonthDates)(start_date, end_date, type); if (!(resource_ids == null ? void 0 : resource_ids.length)) { return (0, import_utils.disableAllDates)(dates); } try { const res = await this.fetchResourceDates(params); if (!((_a = res == null ? void 0 : res.data) == null ? void 0 : _a.length)) { return (0, import_utils.disableAllDates)(dates); } dates = (0, import_utils.handleAvailableDateByResource)(res.data, dates); if (rules == null ? void 0 : rules.length) { dates = (0, import_utils.handleAvailableDatesByRules)(dates, rules); } return dates; } catch (error) { console.log("getAvailableTimeList_error", error); return (0, import_utils.disableAllDates)(dates); } } clearDateRange() { this.store.dateRange = []; } storeChange() { if (this.openCache) { this.checkSaveCache({ cacheId: this.cacheId, fatherModule: this.fatherModule, store: this.store, cacheKey: ["dateRange", "dateList"] }); } } getResourcesListByDate(date) { var _a; const dateList = this.store.dateList; const resourcesList = (_a = dateList.find((item) => item.date === date)) == null ? void 0 : _a.resource; return resourcesList; } }; // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { DateModule });