refactor(frontend): 前端重构 V3
This commit is contained in:
1 parent
8d8b40812d
commit
ec97ea8787
80 files changed
+6887
-3733
No files matched your search
@@ -0,0 +1,154 @@
|
||||
/**
|
||||
* features/admin/modules/schedules/utils/schedule-cron.utils.ts
|
||||
* Cron 表达式与设计器互转及下次执行时间预览纯函数。
|
||||
* 依赖:schedule-admin.types。
|
||||
* 逻辑:由 useSchedulesAdmin 的 computed 与编辑流程调用。
|
||||
*/
|
||||
import type { ScheduleCronDesigner } from '../types/schedule-admin.types';
|
||||
import { createDefaultCronDesigner } from '../types/schedule-admin.types';
|
||||
|
||||
export { createDefaultCronDesigner } from '../types/schedule-admin.types';
|
||||
|
||||
/**
|
||||
* 根据 Cron 设计器状态生成五段 cron 表达式。
|
||||
* 核心逻辑:按 mode clamp 数值后拼接 `分 时 日 月 周`。
|
||||
* @param cfg - 设计器当前配置
|
||||
* @returns cron 表达式字符串
|
||||
*/
|
||||
export function buildCronExprFromDesigner(cfg: ScheduleCronDesigner): string {
|
||||
if (cfg.mode === 'every_minutes') {
|
||||
const every = Math.max(1, Math.min(59, Number(cfg.every_minutes || 30)));
|
||||
return `*/${every} * * * *`;
|
||||
}
|
||||
if (cfg.mode === 'hourly') {
|
||||
const minute = Math.max(0, Math.min(59, Number(cfg.hourly_minute || 0)));
|
||||
return `${minute} * * * *`;
|
||||
}
|
||||
if (cfg.mode === 'daily') {
|
||||
const minute = Math.max(0, Math.min(59, Number(cfg.daily_minute || 0)));
|
||||
const hour = Math.max(0, Math.min(23, Number(cfg.daily_hour || 0)));
|
||||
return `${minute} ${hour} * * *`;
|
||||
}
|
||||
const minute = Math.max(0, Math.min(59, Number(cfg.weekly_minute || 0)));
|
||||
const hour = Math.max(0, Math.min(23, Number(cfg.weekly_hour || 0)));
|
||||
const day = Number(cfg.weekly_day);
|
||||
return `${minute} ${hour} * * ${Number.isNaN(day) ? 1 : day}`;
|
||||
}
|
||||
|
||||
/**
|
||||
* 解析 cron 表达式为设计器状态(不修改入参,返回新对象)。
|
||||
* 核心逻辑:匹配 every_minutes(星斜杠 N)、hourly、daily、weekly 四种已知模式,否则回退默认。
|
||||
* @param expr - 后端存储的 cron 表达式
|
||||
* @returns 新的 ScheduleCronDesigner
|
||||
*/
|
||||
export function parseCronExprToDesigner(expr: string | null | undefined): ScheduleCronDesigner {
|
||||
const text = String(expr || '').trim();
|
||||
const parts = text.split(/\s+/);
|
||||
if (parts.length !== 5) {
|
||||
return createDefaultCronDesigner();
|
||||
}
|
||||
const m = parts[0] ?? '';
|
||||
const h = parts[1] ?? '';
|
||||
const dow = parts[4] ?? '';
|
||||
if (/^\*\/\d+$/.test(m) && h === '*' && dow === '*') {
|
||||
return {
|
||||
...createDefaultCronDesigner(),
|
||||
mode: 'every_minutes',
|
||||
every_minutes: Number(m.replace('*/', '')) || 30,
|
||||
};
|
||||
}
|
||||
if (/^\d+$/.test(m) && h === '*' && dow === '*') {
|
||||
return {
|
||||
...createDefaultCronDesigner(),
|
||||
mode: 'hourly',
|
||||
hourly_minute: Number(m) || 0,
|
||||
};
|
||||
}
|
||||
if (/^\d+$/.test(m) && /^\d+$/.test(h) && dow === '*') {
|
||||
return {
|
||||
...createDefaultCronDesigner(),
|
||||
mode: 'daily',
|
||||
daily_hour: Number(h) || 0,
|
||||
daily_minute: Number(m) || 0,
|
||||
};
|
||||
}
|
||||
if (/^\d+$/.test(m) && /^\d+$/.test(h) && /^\d+$/.test(dow)) {
|
||||
return {
|
||||
...createDefaultCronDesigner(),
|
||||
mode: 'weekly',
|
||||
weekly_hour: Number(h) || 0,
|
||||
weekly_minute: Number(m) || 0,
|
||||
weekly_day: Number(dow),
|
||||
};
|
||||
}
|
||||
return createDefaultCronDesigner();
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据 cron 表达式估算下一次本地执行时间(仅支持设计器可表达的模式)。
|
||||
* 核心逻辑:解析五段式 cron,按 every_minutes / hourly / daily / weekly 分支推算 Date。
|
||||
* @param expr - cron 表达式
|
||||
* @returns 下次执行时间,无法解析时 null
|
||||
*/
|
||||
export function computeNextRunPreview(expr: string): Date | null {
|
||||
if (!expr) return null;
|
||||
try {
|
||||
const parts = expr.trim().split(/\s+/);
|
||||
if (parts.length !== 5) return null;
|
||||
const now = new Date();
|
||||
const minuteRaw = parts[0]!;
|
||||
const hourRaw = parts[1]!;
|
||||
const dayOfMonth = parts[2]!;
|
||||
const month = parts[3]!;
|
||||
const dayOfWeek = parts[4]!;
|
||||
if (month !== '*') return null;
|
||||
if (dayOfMonth !== '*' && dayOfWeek !== '*') return null;
|
||||
const minute = minuteRaw.startsWith('*/') ? undefined : parseInt(minuteRaw, 10);
|
||||
const hour = hourRaw === '*' ? undefined : parseInt(hourRaw, 10);
|
||||
if (minuteRaw.startsWith('*/')) {
|
||||
const every = parseInt(minuteRaw.replace('*/', ''), 10);
|
||||
const next = new Date(now);
|
||||
next.setSeconds(0, 0);
|
||||
const mod = next.getMinutes() % every;
|
||||
next.setMinutes(next.getMinutes() + (every - mod));
|
||||
if (next <= now) next.setMinutes(next.getMinutes() + every);
|
||||
return next;
|
||||
}
|
||||
if (minute !== undefined && hour === undefined) {
|
||||
const next = new Date(now);
|
||||
next.setMinutes(minute, 0, 0);
|
||||
if (next <= now) next.setHours(next.getHours() + 1);
|
||||
return next;
|
||||
}
|
||||
if (minute !== undefined && hour !== undefined) {
|
||||
const next = new Date(now);
|
||||
next.setHours(hour, minute, 0, 0);
|
||||
if (next <= now) {
|
||||
if (dayOfWeek !== '*') {
|
||||
const dow = parseInt(dayOfWeek, 10);
|
||||
const currentDow = next.getDay();
|
||||
let diff = dow - currentDow;
|
||||
if (diff <= 0) diff += 7;
|
||||
next.setDate(next.getDate() + diff);
|
||||
} else {
|
||||
next.setDate(next.getDate() + 1);
|
||||
}
|
||||
} else if (dayOfWeek !== '*') {
|
||||
const dow = parseInt(dayOfWeek, 10);
|
||||
const target = new Date(next);
|
||||
const currentDow = target.getDay();
|
||||
let diff = dow - currentDow;
|
||||
if (diff < 0) diff += 7;
|
||||
if (diff > 0) target.setDate(target.getDate() + diff);
|
||||
if (target <= now) {
|
||||
target.setDate(target.getDate() + 7);
|
||||
}
|
||||
return target;
|
||||
}
|
||||
return next;
|
||||
}
|
||||
return null;
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
/**
|
||||
* features/admin/modules/schedules/utils/schedule-form-payload.utils.ts
|
||||
* 创建/编辑定时任务时 config 与请求 payload 组装。
|
||||
*/
|
||||
import type { ScheduleSqlEditForm } from '../types/schedule-admin.types';
|
||||
|
||||
export function buildScheduleConfigPayload(
|
||||
scheduleSqlEditForm: ScheduleSqlEditForm,
|
||||
hasAgent: boolean,
|
||||
headerLabels: Record<string, string>,
|
||||
apiHeaders: Record<string, string>,
|
||||
) {
|
||||
const aliasOrder = scheduleSqlEditForm.alias_order_str.split(',').map((s) => s.trim()).filter(Boolean);
|
||||
const detailAliasOrder = scheduleSqlEditForm.detail_alias_order_str.split(',').map((s) => s.trim()).filter(Boolean);
|
||||
|
||||
return {
|
||||
base_fetch_sql: scheduleSqlEditForm.base_fetch_sql,
|
||||
fetch_where_conditions: scheduleSqlEditForm.fetch_where_conditions,
|
||||
fetch_order_by: scheduleSqlEditForm.fetch_order_by,
|
||||
detail_sql: scheduleSqlEditForm.detail_sql,
|
||||
writeback_sql: scheduleSqlEditForm.writeback_sql,
|
||||
row_id_key: scheduleSqlEditForm.row_id_key || 'id',
|
||||
log_prefix: scheduleSqlEditForm.log_prefix,
|
||||
alias_order: aliasOrder,
|
||||
detail_alias_order: detailAliasOrder,
|
||||
header_labels: headerLabels,
|
||||
data_source: scheduleSqlEditForm.ds_host.trim() ? {
|
||||
db_type: scheduleSqlEditForm.ds_db_type,
|
||||
host: scheduleSqlEditForm.ds_host,
|
||||
port: scheduleSqlEditForm.ds_port,
|
||||
database: scheduleSqlEditForm.ds_database,
|
||||
username: scheduleSqlEditForm.ds_username,
|
||||
password: scheduleSqlEditForm.ds_password,
|
||||
driver: scheduleSqlEditForm.ds_driver,
|
||||
encrypt: scheduleSqlEditForm.ds_encrypt,
|
||||
trust_cert: scheduleSqlEditForm.ds_trust_cert,
|
||||
} : undefined,
|
||||
external_api: scheduleSqlEditForm.api_url.trim() ? {
|
||||
url: scheduleSqlEditForm.api_url,
|
||||
method: scheduleSqlEditForm.api_method,
|
||||
headers: apiHeaders,
|
||||
body_template: scheduleSqlEditForm.api_body_template,
|
||||
} : undefined,
|
||||
dify_system_prompt: hasAgent ? scheduleSqlEditForm.dify_system_prompt : '',
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
/**
|
||||
* features/admin/modules/schedules/utils/schedule-format.utils.ts
|
||||
* 定时任务 API 日期解析与展示格式化纯函数。
|
||||
* 依赖:无。
|
||||
* 逻辑:由 useSchedulesAdmin 及视图组件通过 admin.formatDateTime 间接使用。
|
||||
*/
|
||||
|
||||
/**
|
||||
* 将 API 返回的 ISO 字符串解析为 Date(无时区后缀则按 UTC 处理)。
|
||||
* @param iso - ISO 8601 或无时区后缀的时间字符串
|
||||
* @returns Date 实例
|
||||
*/
|
||||
export function parseApiUtcDate(iso: string): Date {
|
||||
const normalized = /(?:Z|[+-]\d{2}:\d{2})$/i.test(iso) ? iso : `${iso}Z`;
|
||||
return new Date(normalized);
|
||||
}
|
||||
|
||||
/**
|
||||
* 格式化为本地 `YYYY-MM-DD HH:mm:ss`;空值或非法日期返回 `-` 或原字符串。
|
||||
* @param iso - 可选 ISO 时间字符串
|
||||
* @returns 展示用日期时间文本
|
||||
*/
|
||||
export function formatDateTime(iso?: string | null): string {
|
||||
if (!iso) return '-';
|
||||
const d = parseApiUtcDate(iso);
|
||||
if (isNaN(d.getTime())) return iso;
|
||||
const pad = (n: number) => String(n).padStart(2, '0');
|
||||
return `${d.getFullYear()}-${pad(d.getMonth() + 1)}-${pad(d.getDate())} ${pad(d.getHours())}:${pad(d.getMinutes())}:${pad(d.getSeconds())}`;
|
||||
}
|
||||
Reference in new issue
Block a user