UNPKG

@pisell/pisellos

Version:

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

239 lines (237 loc) 8.51 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/Schedule/index.ts var Schedule_exports = {}; __export(Schedule_exports, { ScheduleModule: () => ScheduleModule }); module.exports = __toCommonJS(Schedule_exports); var import_lodash_es = require("lodash-es"); var import_BaseModule = require("../BaseModule"); var import_dayjs = __toESM(require("dayjs")); var import_isSameOrBefore = __toESM(require("dayjs/plugin/isSameOrBefore")); var import_isSameOrAfter = __toESM(require("dayjs/plugin/isSameOrAfter")); var import_utils = require("../Date/utils"); var import_utils2 = require("./utils"); var import_getDateIsInSchedule = require("./getDateIsInSchedule"); import_dayjs.default.extend(import_isSameOrBefore.default); import_dayjs.default.extend(import_isSameOrAfter.default); var ScheduleModule = class extends import_BaseModule.BaseModule { constructor(name, version) { super(name, version); this.defaultName = "schedule"; this.defaultVersion = "1.0.0"; this.store = {}; this.openCache = false; } async initialize(core, options) { var _a, _b; this.core = core; this.request = core.getPlugin("request"); if (!this.request) { throw new Error("ScheduleModule 需要 request 插件支持"); } this.store = options == null ? void 0 : options.store; if (options.initialState) { this.store.scheduleList = options.initialState.scheduleList; this.store.availabilityDateList = options.initialState.availabilityDateList; } else { this.store.scheduleList = []; this.store.availabilityDateList = []; } 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; } } /** * 加载当前店铺下所有 schedule * * @memberof ScheduleModule */ async loadAllSchedule() { var _a; const scheduleList = await this.request.get( `/schedule`, { num: 999 }, { useCache: true } ); this.setScheduleList(((_a = scheduleList.data) == null ? void 0 : _a.list) || []); } setScheduleList(list) { this.store.scheduleList = list; } async loadScheduleAvailableDate({ startDate, endDate, custom_page_id, channel }) { var _a, _b, _c; if ((0, import_dayjs.default)(startDate).isBefore((0, import_dayjs.default)(), "day")) { startDate = (0, import_dayjs.default)().format("YYYY-MM-DD"); } let dates = (0, import_utils.generateMonthDates)(startDate, endDate); dates = (0, import_utils.disableAllDates)(dates); if ((0, import_dayjs.default)(endDate).isBefore((0, import_dayjs.default)(), "day")) { return dates; } const res = await this.request.get( `/schedule/product/availability/v2`, { start_date: startDate, end_date: endDate, custom_page_id, channel } ); this.setAvailabilityScheduleDateList(res.data.date_list); this.setOtherProductsIds(res.data.other_product_ids || []); if (!((_a = res.data.date_list) == null ? void 0 : _a.length) && !((_b = res.data.other_product_ids) == null ? void 0 : _b.length) || (0, import_dayjs.default)(endDate).isBefore((0, import_dayjs.default)(), "day")) { return dates; } if ((_c = res.data.other_product_ids) == null ? void 0 : _c.length) { dates.forEach((n) => { n.status = "available"; }); } else { res.data.date_list.forEach((n) => { const index = dates.findIndex((m) => m.date === n.date); if (index !== -1) { dates[index].status = "available"; } }); } dates = (0, import_utils.disableDatesBeforeOneDay)(dates); return dates; } getScheduleListByIds(ids) { return this.store.scheduleList.filter((n) => ids.includes(n.id)); } setAvailabilityScheduleDateList(list) { this.store.availabilityDateList = list; } getAvailabilityScheduleDateList() { return this.store.availabilityDateList; } setOtherProductsIds(ids) { this.store.otherProductsIds = ids; } getOtherProductsIds() { return this.store.otherProductsIds; } storeChange() { if (this.openCache) { const store = (0, import_lodash_es.cloneDeep)(this.store); this.checkSaveCache({ cacheId: this.cacheId, fatherModule: this.fatherModule, store, cacheKey: ["scheduleList", "availabilityDateList", "otherProductsIds"] }); } } /** * 传入一个时间, 判断改时间是否在schedule 内 * @param param0 { date: string, schedule: any } date: 日期, schedule: schedule * @returns */ isInScheduleByDate({ date, schedule }) { return ScheduleModule.isInScheduleByDate({ date, schedule }); } /** * 传入一个时间, 判断改时间是否在schedule 内 * @param param0 { date: string, schedule: any } date: 日期, schedule: schedule * @returns */ static isInScheduleByDate({ date, schedule }) { var _a, _b, _c, _d; return (0, import_getDateIsInSchedule.getDateIsInSchedule)(date, [schedule]); if (schedule.start_time && schedule.end_time) { const isBeforeStartTime = (0, import_dayjs.default)(date).isBefore( (0, import_dayjs.default)(schedule.start_time) ); const isAfterEndTime = (0, import_dayjs.default)(date).isAfter((0, import_dayjs.default)(schedule.end_time)); if (isBeforeStartTime || isAfterEndTime) { return false; } } let _schedule = { ...schedule, // 开始时间向前推一天 start_time: (0, import_dayjs.default)(date).subtract(1, "day").format("YYYY-MM-DD HH:mm:ss"), end_time: (0, import_dayjs.default)(date).add(1, "day").format("YYYY-MM-DD HH:mm:ss") }; if (((_b = (_a = _schedule.repeat_rule) == null ? void 0 : _a.end) == null ? void 0 : _b.type) === "never") { _schedule = { ..._schedule, repeat_rule: { ..._schedule.repeat_rule, end: { type: "date", end_date: _schedule.end_time, occurrence: null } } }; } if (((_d = (_c = _schedule.repeat_rule) == null ? void 0 : _c.end) == null ? void 0 : _d.type) === "date") { _schedule = { ..._schedule, repeat_rule: { ..._schedule.repeat_rule, end: { ..._schedule.repeat_rule.end, end_date: _schedule.end_time } } }; } const dateRanges = (0, import_utils2.calcScheduleDateRange)({ ..._schedule }); for (const range of dateRanges) { const startTime = (0, import_dayjs.default)(range.start); const endTime = (0, import_dayjs.default)(range.end); if ((0, import_dayjs.default)(date).isSameOrAfter(startTime) && (0, import_dayjs.default)(date).isSameOrBefore(endTime)) { return true; } } } }; // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { ScheduleModule });