@pisell/pisellos
Version:
一个可扩展的前端模块化SDK框架,支持插件系统
263 lines (261 loc) • 9.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/modules/Date/index.ts
var Date_exports = {};
__export(Date_exports, {
DateModule: () => DateModule
});
module.exports = __toCommonJS(Date_exports);
var import_dayjs = __toESM(require("dayjs"));
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;
if (params.useCache) {
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, _b;
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));
}
);
(_b = currentItem.resource) == null ? void 0 : _b.forEach((n) => {
var _a2;
const newResource2 = (_a2 = item.resource) == null ? void 0 : _a2.find((m) => m.id === n.id);
if (newResource2) {
n.times = newResource2.times;
}
});
currentItem.resource = [
...currentItem.resource || [],
...newResource || []
];
currentDateList[currentItemIndex] = currentItem;
} else {
currentDateList.push(item);
}
});
this.store.dateList = currentDateList;
}
async fetchResourceDates(params) {
const { url, query, useCache = true } = 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: useCache && this.cacheId
}, {
useCache
});
if ((res == null ? void 0 : res.data) && Array.isArray(res.data)) {
res.data = this.correctResourceTimeSlots(res.data);
}
return res;
} catch (error) {
console.error(error);
}
}
/**
* 将时间向上取整到下一个10分钟整数
*
* @param time dayjs 时间对象
* @returns 向上取整后的时间字符串 (YYYY-MM-DD HH:mm 格式)
*/
roundUpToNext10Minutes(time) {
const minutes = time.minute();
const remainder = minutes % 10;
if (remainder === 0) {
return time.format("YYYY-MM-DD HH:mm");
} else {
const minutesToAdd = 10 - remainder;
const roundedTime = time.add(minutesToAdd, "minute");
return roundedTime.format("YYYY-MM-DD HH:mm");
}
}
/**
* 校正资源时间段数据
*
* 如果时间段的 start_at 早于资源的 start_time,将其同步为 start_time 的下一个10分钟整数
* 如果修正后 end_at 也早于修正后的 start_time,则删除该时间段
*
* @param resourcesData 资源数据数组
* @returns 校正后的资源数据数组
*/
correctResourceTimeSlots(resourcesData) {
return resourcesData.map((resource) => {
if (!resource.times || !Array.isArray(resource.times) || !resource.start_time) {
return resource;
}
if (!resource.advanced || resource.advanced.unit === 0) {
return resource;
}
const resourceStartTime = (0, import_dayjs.default)(resource.start_time);
const correctedTimes = resource.times.map((timeSlot) => {
if (!timeSlot.start_at || !timeSlot.end_at) {
return timeSlot;
}
const startAt = (0, import_dayjs.default)(timeSlot.start_at);
const endAt = (0, import_dayjs.default)(timeSlot.end_at);
if (startAt.isBefore(resourceStartTime)) {
const roundedStartTime = this.roundUpToNext10Minutes(resourceStartTime);
const roundedStartTimeDayjs = (0, import_dayjs.default)(roundedStartTime);
console.log(`[DateModule] 修正时间段开始时间: ${timeSlot.start_at} -> ${roundedStartTime} (资源ID: ${resource.id}, 原始start_time: ${resource.start_time})`);
const correctedTimeSlot = {
...timeSlot,
start_at: roundedStartTime
};
if (endAt.isBefore(roundedStartTimeDayjs)) {
console.log(`[DateModule] 时间段无效,将被删除: ${timeSlot.start_at} - ${timeSlot.end_at} (资源ID: ${resource.id}, 修正后start_time: ${roundedStartTime})`);
return null;
}
return correctedTimeSlot;
}
return timeSlot;
}).filter((timeSlot) => timeSlot !== null);
return {
...resource,
times: correctedTimes
};
});
}
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
});