feat(schedule): 优化排程管理界面,新增弹窗控制与交互逻辑
This commit is contained in:
1 parent
7544361c05
commit
0ac165049d
10 files changed
+148
-80
No files matched your search
@@ -7,14 +7,17 @@
|
||||
<script setup lang="ts">
|
||||
import { onMounted, provide, reactive } from 'vue';
|
||||
import { useSchedulesAdmin } from './composables/useSchedulesAdmin';
|
||||
import { SCHEDULES_ADMIN_KEY, type SchedulesAdminContextProvided } from './composables/schedules.context';
|
||||
import { SCHEDULES_ADMIN_KEY, SCHEDULE_CREATE_MODAL_OPEN_KEY, type SchedulesAdminContextProvided } from './composables/schedules.context';
|
||||
import SchedulePageHeader from './components/SchedulePageHeader.vue';
|
||||
import ScheduleOverviewPanel from './components/ScheduleOverviewPanel.vue';
|
||||
import ScheduleWorkbenchPanel from './components/ScheduleWorkbenchPanel.vue';
|
||||
import SchedulesPageOverlays from './components/SchedulesPageOverlays.vue';
|
||||
|
||||
const admin = reactive(useSchedulesAdmin()) as SchedulesAdminContextProvided;
|
||||
const adminApi = useSchedulesAdmin();
|
||||
const admin = reactive(adminApi) as SchedulesAdminContextProvided;
|
||||
provide(SCHEDULES_ADMIN_KEY, admin);
|
||||
/** 弹窗 v-if 须绑定原始 ref,避免 reactive 属性赋值破坏 ref 链接 */
|
||||
provide(SCHEDULE_CREATE_MODAL_OPEN_KEY, adminApi.showScheduleCreateModal);
|
||||
onMounted(() => { void admin.loadAll(); });
|
||||
</script>
|
||||
|
||||
|
||||
+7
-4
@@ -4,20 +4,22 @@
|
||||
* 定时任务创建/编辑弹窗。
|
||||
*/
|
||||
import { ChevronDown, ChevronUp, Plus } from 'lucide-vue-next';
|
||||
import { useSchedulesAdminContext } from '../composables/schedules.context';
|
||||
import { useScheduleCreateModalOpen, useSchedulesAdminContext } from '../composables/schedules.context';
|
||||
import ScheduleCronDesigner from './ScheduleCronDesigner.vue';
|
||||
|
||||
const admin = useSchedulesAdminContext();
|
||||
const showCreateModal = useScheduleCreateModalOpen();
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<div v-if="admin.showScheduleCreateModal" class="admin-modal-overlay items-center">
|
||||
<Teleport to="body">
|
||||
<div v-if="showCreateModal" class="admin-modal-overlay items-center">
|
||||
<div class="admin-modal-panel max-w-3xl">
|
||||
<div class="flex items-center justify-between border-b border-subtle px-8 py-6">
|
||||
<h3 class="text-2xl font-bold tracking-tight text-primary">{{ admin.scheduleEditingId ? $t('admin.schedule.form.editTitle') : $t('admin.schedule.form.createTitle') }}</h3>
|
||||
<button
|
||||
class="rounded-full p-2 text-muted transition hover-surface-soft hover:text-secondary"
|
||||
@click="admin.showScheduleCreateModal = false"
|
||||
@click="admin.closeScheduleCreateModal"
|
||||
>
|
||||
✕
|
||||
</button>
|
||||
@@ -423,7 +425,7 @@ const admin = useSchedulesAdminContext();
|
||||
<button
|
||||
type="button"
|
||||
class="btn-secondary"
|
||||
@click="admin.showScheduleCreateModal = false"
|
||||
@click="admin.closeScheduleCreateModal"
|
||||
>
|
||||
{{ $t('admin.schedule.form.cancel') }}
|
||||
</button>
|
||||
@@ -439,4 +441,5 @@ const admin = useSchedulesAdminContext();
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</Teleport>
|
||||
</template>
|
||||
@@ -64,7 +64,14 @@ const admin = useSchedulesAdminContext();
|
||||
:key="s.id"
|
||||
class="flex h-full min-h-[260px] flex-col admin-panel p-4 transition hover:border-blue-200 hover:bg-surface-soft"
|
||||
>
|
||||
<button class="w-full text-left" @click="admin.openScheduleRunSubmenu(s.id)">
|
||||
<div
|
||||
class="w-full shrink-0 cursor-pointer rounded-lg text-left outline-none focus-visible:ring-2 focus-visible:ring-brand-400"
|
||||
role="button"
|
||||
tabindex="0"
|
||||
@click="admin.openScheduleRunSubmenu(s.id)"
|
||||
@keydown.enter.prevent="admin.openScheduleRunSubmenu(s.id)"
|
||||
@keydown.space.prevent="admin.openScheduleRunSubmenu(s.id)"
|
||||
>
|
||||
<div class="min-h-[96px]">
|
||||
<div class="flex items-start justify-between gap-3">
|
||||
<div class="min-w-0">
|
||||
@@ -79,8 +86,8 @@ const admin = useSchedulesAdminContext();
|
||||
<div class="mt-2 text-sm text-muted">Cron:{{ s.cron_expr }}</div>
|
||||
<div class="mt-1 text-sm text-muted">{{ $t('admin.schedule.owner') }}:{{ s.owner_username || s.owner_user_id }}</div>
|
||||
</div>
|
||||
</button>
|
||||
<div class="mt-auto pt-3">
|
||||
</div>
|
||||
<div class="relative z-10 mt-auto shrink-0 pt-3" @click.stop>
|
||||
<div class="mb-1 text-sm text-muted">
|
||||
{{ $t('admin.schedule.lastRun') }}:{{ admin.formatDateTime(s.last_run_at) }}
|
||||
</div>
|
||||
@@ -89,8 +96,9 @@ const admin = useSchedulesAdminContext();
|
||||
</div>
|
||||
<div class="flex flex-wrap items-center gap-2">
|
||||
<button
|
||||
type="button"
|
||||
class="inline-flex items-center gap-1 rounded-xl border border-default px-3 py-2 text-xs font-medium text-secondary transition-colors hover:bg-surface-soft"
|
||||
@click.stop="admin.openScheduleEditModal(s)"
|
||||
@click="admin.openScheduleEditModal(s)"
|
||||
>
|
||||
<Edit3 class="h-3.5 w-3.5" />
|
||||
{{ $t('admin.schedule.actions.edit') }}
|
||||
|
||||
@@ -46,12 +46,16 @@ const admin = useSchedulesAdminContext();
|
||||
<span class="text-xs text-muted">{{ $t('admin.schedule.switchByCard') }}</span>
|
||||
</div>
|
||||
<div class="space-y-3">
|
||||
<button
|
||||
<div
|
||||
v-for="s in admin.schedules"
|
||||
:key="`workbench-${s.id}`"
|
||||
class="w-full rounded-2xl border px-4 py-4 text-left transition-all"
|
||||
class="w-full cursor-pointer rounded-2xl border px-4 py-4 text-left transition-all"
|
||||
:class="admin.selectedScheduleId === s.id ? 'border-blue-300 bg-blue-50/60' : 'border-default bg-surface hover:border-blue-200 hover:bg-surface-soft'"
|
||||
role="button"
|
||||
tabindex="0"
|
||||
@click="admin.viewScheduleRuns(s.id)"
|
||||
@keydown.enter.prevent="admin.viewScheduleRuns(s.id)"
|
||||
@keydown.space.prevent="admin.viewScheduleRuns(s.id)"
|
||||
>
|
||||
<div class="flex items-start justify-between gap-3">
|
||||
<div class="min-w-0">
|
||||
@@ -59,6 +63,7 @@ const admin = useSchedulesAdminContext();
|
||||
<div class="mt-0.5 text-xs text-muted">{{ s.schedule_no }}</div>
|
||||
<div class="mt-2 flex flex-wrap items-center gap-2">
|
||||
<button
|
||||
type="button"
|
||||
class="inline-flex items-center justify-center rounded-xl border border-default p-1.5 text-xs font-medium text-secondary transition-colors hover:bg-surface-soft"
|
||||
:title="$t('admin.schedule.actions.edit')"
|
||||
@click.stop="admin.openScheduleEditModal(s)"
|
||||
@@ -110,7 +115,7 @@ const admin = useSchedulesAdminContext();
|
||||
{{ s.enabled ? $t('admin.schedule.enabled') : $t('admin.schedule.disabled') }}
|
||||
</span>
|
||||
</div>
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
|
||||
+93
-62
@@ -98,73 +98,99 @@ export function useScheduleAdminForm(
|
||||
cronDesigner.reset();
|
||||
};
|
||||
|
||||
const openScheduleCreateModal = async () => {
|
||||
resetScheduleCreateForm();
|
||||
scheduleConfigCollapsed.value = true;
|
||||
await Promise.all([lookups.loadAgents(), lookups.loadUsers(), lookups.loadKbGroups()]);
|
||||
showScheduleCreateModal.value = true;
|
||||
/**
|
||||
* 将后端返回的 SQL/API 配置写入编辑表单。
|
||||
* 核心逻辑:供 openScheduleEditModal 异步回填,避免阻塞弹窗打开。
|
||||
*/
|
||||
const applyScheduleTaskConfigResult = (
|
||||
result: Awaited<ReturnType<typeof getAdminScheduleTaskConfig>>,
|
||||
): void => {
|
||||
const cfg = result.config;
|
||||
scheduleCreateForm.value.data_source_type = (result.data_source_type || 'database') as 'database' | 'external_api';
|
||||
if (!cfg) return;
|
||||
scheduleSqlEditForm.value = {
|
||||
base_fetch_sql: cfg.base_fetch_sql || '',
|
||||
fetch_where_conditions: cfg.fetch_where_conditions || '',
|
||||
fetch_order_by: cfg.fetch_order_by || '',
|
||||
detail_sql: cfg.detail_sql || '',
|
||||
writeback_sql: cfg.writeback_sql || '',
|
||||
row_id_key: cfg.row_id_key || 'id',
|
||||
log_prefix: cfg.log_prefix || '',
|
||||
alias_order_str: (cfg.alias_order || []).join(','),
|
||||
detail_alias_order_str: (cfg.detail_alias_order || []).join(','),
|
||||
header_labels_str: cfg.header_labels ? JSON.stringify(cfg.header_labels, null, 2) : '{}',
|
||||
ds_db_type: cfg.data_source?.db_type || 'sqlserver',
|
||||
ds_host: cfg.data_source?.host || '',
|
||||
ds_port: cfg.data_source?.port || '',
|
||||
ds_database: cfg.data_source?.database || '',
|
||||
ds_username: cfg.data_source?.username || '',
|
||||
ds_password: cfg.data_source?.password || '',
|
||||
ds_driver: cfg.data_source?.driver || '',
|
||||
ds_encrypt: cfg.data_source?.encrypt || '',
|
||||
ds_trust_cert: cfg.data_source?.trust_cert || '',
|
||||
api_url: cfg.external_api?.url || '',
|
||||
api_method: cfg.external_api?.method || 'POST',
|
||||
api_headers_str: cfg.external_api?.headers ? JSON.stringify(cfg.external_api.headers, null, 2) : '{}',
|
||||
api_body_template: cfg.external_api?.body_template || '',
|
||||
dify_system_prompt: cfg.dify_system_prompt || '',
|
||||
};
|
||||
};
|
||||
|
||||
const openScheduleEditModal = async (task: ScheduleTask) => {
|
||||
/** 后台拉取下拉与 SQL 配置;弹窗已打开后静默回填。 */
|
||||
const loadScheduleEditModalData = async (taskId: number): Promise<void> => {
|
||||
await Promise.all([lookups.loadAgents(), lookups.loadUsers(), lookups.loadKbGroups()]);
|
||||
scheduleConfigCollapsed.value = true;
|
||||
scheduleNotifyUserQuery.value = '';
|
||||
scheduleNotifyDropdownOpen.value = false;
|
||||
scheduleOwnerDropdownOpen.value = false;
|
||||
scheduleEditingId.value = task.id;
|
||||
scheduleCreateForm.value = {
|
||||
name: task.name || '',
|
||||
dify_agent_id: task.dify_agent_id || null,
|
||||
owner_user_id: task.owner_user_id || null,
|
||||
wait_seconds: task.wait_seconds || 0,
|
||||
notify_enabled: !!task.notify_enabled,
|
||||
notify_user_ids_json: task.notify_user_ids_json || [],
|
||||
kb_sync_enabled: !!task.kb_sync_enabled,
|
||||
kb_group_id: task.kb_group_id || null,
|
||||
enabled: !!task.enabled,
|
||||
data_source_type: (task.data_source_type || 'database') as 'database' | 'external_api',
|
||||
};
|
||||
|
||||
try {
|
||||
const result = await getAdminScheduleTaskConfig(task.id);
|
||||
const cfg = result.config;
|
||||
scheduleCreateForm.value.data_source_type = (result.data_source_type || 'database') as 'database' | 'external_api';
|
||||
if (cfg) {
|
||||
scheduleSqlEditForm.value = {
|
||||
base_fetch_sql: cfg.base_fetch_sql || '',
|
||||
fetch_where_conditions: cfg.fetch_where_conditions || '',
|
||||
fetch_order_by: cfg.fetch_order_by || '',
|
||||
detail_sql: cfg.detail_sql || '',
|
||||
writeback_sql: cfg.writeback_sql || '',
|
||||
row_id_key: cfg.row_id_key || 'id',
|
||||
log_prefix: cfg.log_prefix || '',
|
||||
alias_order_str: (cfg.alias_order || []).join(','),
|
||||
detail_alias_order_str: (cfg.detail_alias_order || []).join(','),
|
||||
header_labels_str: cfg.header_labels ? JSON.stringify(cfg.header_labels, null, 2) : '{}',
|
||||
ds_db_type: cfg.data_source?.db_type || 'sqlserver',
|
||||
ds_host: cfg.data_source?.host || '',
|
||||
ds_port: cfg.data_source?.port || '',
|
||||
ds_database: cfg.data_source?.database || '',
|
||||
ds_username: cfg.data_source?.username || '',
|
||||
ds_password: cfg.data_source?.password || '',
|
||||
ds_driver: cfg.data_source?.driver || '',
|
||||
ds_encrypt: cfg.data_source?.encrypt || '',
|
||||
ds_trust_cert: cfg.data_source?.trust_cert || '',
|
||||
api_url: cfg.external_api?.url || '',
|
||||
api_method: cfg.external_api?.method || 'POST',
|
||||
api_headers_str: cfg.external_api?.headers ? JSON.stringify(cfg.external_api.headers, null, 2) : '{}',
|
||||
api_body_template: cfg.external_api?.body_template || '',
|
||||
dify_system_prompt: cfg.dify_system_prompt || '',
|
||||
};
|
||||
}
|
||||
} catch {
|
||||
scheduleSqlEditForm.value = createDefaultScheduleSqlEditForm();
|
||||
}
|
||||
|
||||
scheduleTestResult.value = null;
|
||||
cronDesigner.loadFromExpr(task.cron_expr);
|
||||
syncScheduleOwnerQuery();
|
||||
try {
|
||||
const result = await getAdminScheduleTaskConfig(taskId);
|
||||
if (scheduleEditingId.value !== taskId) return;
|
||||
applyScheduleTaskConfigResult(result);
|
||||
} catch {
|
||||
if (scheduleEditingId.value === taskId) {
|
||||
scheduleSqlEditForm.value = createDefaultScheduleSqlEditForm();
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
const openScheduleCreateModal = (): void => {
|
||||
resetScheduleCreateForm();
|
||||
scheduleConfigCollapsed.value = true;
|
||||
showScheduleCreateModal.value = true;
|
||||
void Promise.all([lookups.loadAgents(), lookups.loadUsers(), lookups.loadKbGroups()]);
|
||||
};
|
||||
|
||||
/**
|
||||
* 打开排程编辑弹窗。
|
||||
* 核心逻辑:先同步打开弹窗再异步拉取配置,避免 API 慢时按钮看似无响应。
|
||||
*/
|
||||
const openScheduleEditModal = (task: ScheduleTask): void => {
|
||||
try {
|
||||
scheduleConfigCollapsed.value = true;
|
||||
scheduleNotifyUserQuery.value = '';
|
||||
scheduleNotifyDropdownOpen.value = false;
|
||||
scheduleOwnerDropdownOpen.value = false;
|
||||
scheduleEditingId.value = task.id;
|
||||
scheduleCreateForm.value = {
|
||||
name: task.name || '',
|
||||
dify_agent_id: task.dify_agent_id || null,
|
||||
owner_user_id: task.owner_user_id || null,
|
||||
wait_seconds: task.wait_seconds || 0,
|
||||
notify_enabled: !!task.notify_enabled,
|
||||
notify_user_ids_json: task.notify_user_ids_json || [],
|
||||
kb_sync_enabled: !!task.kb_sync_enabled,
|
||||
kb_group_id: task.kb_group_id || null,
|
||||
enabled: !!task.enabled,
|
||||
data_source_type: (task.data_source_type || 'database') as 'database' | 'external_api',
|
||||
};
|
||||
scheduleSqlEditForm.value = createDefaultScheduleSqlEditForm();
|
||||
scheduleTestResult.value = null;
|
||||
cronDesigner.loadFromExpr(task.cron_expr);
|
||||
syncScheduleOwnerQuery();
|
||||
showScheduleCreateModal.value = true;
|
||||
void loadScheduleEditModalData(task.id);
|
||||
} catch (err: unknown) {
|
||||
const message = err instanceof Error ? err.message : t('common.errorOccurred');
|
||||
msg.error(message);
|
||||
}
|
||||
};
|
||||
|
||||
const scheduleCloseNotifyDropdown = (): void => {
|
||||
@@ -322,12 +348,17 @@ export function useScheduleAdminForm(
|
||||
}
|
||||
};
|
||||
|
||||
const closeScheduleCreateModal = (): void => {
|
||||
showScheduleCreateModal.value = false;
|
||||
};
|
||||
|
||||
return {
|
||||
cronDesigner,
|
||||
filteredNotifyUsers,
|
||||
filteredOwnerUsers,
|
||||
openScheduleCreateModal,
|
||||
openScheduleEditModal,
|
||||
closeScheduleCreateModal,
|
||||
scheduleCloseNotifyDropdown,
|
||||
selectScheduleOwner,
|
||||
handleScheduleOwnerInput,
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
* 依赖:useSchedulesAdmin。
|
||||
* 逻辑:SchedulesPage provide;子 panel / overlays inject 消费 admin 状态。
|
||||
*/
|
||||
import { inject, type InjectionKey, type Reactive, type UnwrapNestedRefs } from 'vue';
|
||||
import { inject, type InjectionKey, type Reactive, type Ref, type UnwrapNestedRefs } from 'vue';
|
||||
import type { useSchedulesAdmin } from './useSchedulesAdmin';
|
||||
|
||||
/** useSchedulesAdmin() 原始返回(含 Ref/ComputedRef) */
|
||||
@@ -15,6 +15,12 @@ export type SchedulesAdminContextProvided = Reactive<UnwrapNestedRefs<SchedulesA
|
||||
|
||||
export const SCHEDULES_ADMIN_KEY: InjectionKey<SchedulesAdminContextProvided> = Symbol('schedulesAdmin');
|
||||
|
||||
/**
|
||||
* 创建/编辑弹窗显隐 Ref(绕过 reactive 对 ref 属性的覆盖赋值问题)。
|
||||
* 逻辑:SchedulesPage 单独 provide 原始 ref;弹窗 v-if 必须绑定此 ref。
|
||||
*/
|
||||
export const SCHEDULE_CREATE_MODAL_OPEN_KEY: InjectionKey<Ref<boolean>> = Symbol('scheduleCreateModalOpen');
|
||||
|
||||
/**
|
||||
* 在 SchedulesPage 子树中获取定时任务管理上下文。
|
||||
* 必须在已 provide(SCHEDULES_ADMIN_KEY) 的组件树内调用。
|
||||
@@ -28,3 +34,15 @@ export function useSchedulesAdminContext(): SchedulesAdminContextProvided {
|
||||
}
|
||||
return ctx;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取排程创建/编辑弹窗显隐 Ref。
|
||||
* @throws 未 provide 时抛出 Error
|
||||
*/
|
||||
export function useScheduleCreateModalOpen(): Ref<boolean> {
|
||||
const open = inject(SCHEDULE_CREATE_MODAL_OPEN_KEY);
|
||||
if (!open) {
|
||||
throw new Error('[schedules] useScheduleCreateModalOpen() must be used inside SchedulesPage');
|
||||
}
|
||||
return open;
|
||||
}
|
||||
@@ -787,7 +787,7 @@
|
||||
"defaultNo": "Default (no)",
|
||||
"defaultYes": "Default (yes)",
|
||||
"envVarHint": "Values support {token} format and are resolved from environment variables at runtime (recommended for passwords).",
|
||||
"envVarToken": "${ENV_VAR_NAME}",
|
||||
"envVarToken": "$'{'ENV_VAR_NAME'}'",
|
||||
"baseFetchSql": "Base fetch SQL (base_fetch_sql) — SELECT + FROM + JOIN only, no WHERE",
|
||||
"fetchWhereConditions": "WHERE clause (fetch_where_conditions) — edit to test different filters",
|
||||
"fetchOrderBy": "Order by (fetch_order_by)",
|
||||
|
||||
@@ -672,7 +672,7 @@
|
||||
"defaultNo": "ค่าเริ่มต้น (no)",
|
||||
"defaultYes": "ค่าเริ่มต้น (yes)",
|
||||
"envVarHint": "รองรับรูปแบบ {token} และจะถูกแก้จากตัวแปรสภาพแวดล้อมขณะรัน (แนะนำสำหรับรหัสผ่าน)",
|
||||
"envVarToken": "${ชื่อตัวแปรสภาพแวดล้อม}",
|
||||
"envVarToken": "$'{'ชื่อตัวแปรสภาพแวดล้อม'}'",
|
||||
"baseFetchSql": "SQL ดึงข้อมูลพื้นฐาน (base_fetch_sql) — เฉพาะ SELECT + FROM + JOIN ไม่รวม WHERE",
|
||||
"fetchWhereConditions": "เงื่อนไข WHERE (fetch_where_conditions) — แก้ไขเพื่อทดสอบตัวกรอง",
|
||||
"fetchOrderBy": "เรียงลำดับ (fetch_order_by)",
|
||||
|
||||
@@ -767,7 +767,7 @@
|
||||
"defaultNo": "默认 (no)",
|
||||
"defaultYes": "默认 (yes)",
|
||||
"envVarHint": "值支持 {token} 格式,运行时自动从环境变量解析(推荐密码使用此方式)",
|
||||
"envVarToken": "${环境变量名}",
|
||||
"envVarToken": "$'{'环境变量名'}'",
|
||||
"baseFetchSql": "基础查询 SQL (base_fetch_sql) — SELECT + FROM + JOIN 部分,不含 WHERE",
|
||||
"fetchWhereConditions": "WHERE 条件 (fetch_where_conditions) — 可修改以测试不同筛选条件",
|
||||
"fetchOrderBy": "排序 (fetch_order_by)",
|
||||
|
||||
@@ -672,7 +672,7 @@
|
||||
"defaultNo": "預設 (no)",
|
||||
"defaultYes": "預設 (yes)",
|
||||
"envVarHint": "值支援 {token} 格式,執行時自動從環境變數解析(建議密碼使用此方式)",
|
||||
"envVarToken": "${環境變數名}",
|
||||
"envVarToken": "$'{'環境變數名'}'",
|
||||
"baseFetchSql": "基礎查詢 SQL (base_fetch_sql) — SELECT + FROM + JOIN 部分,不含 WHERE",
|
||||
"fetchWhereConditions": "WHERE 條件 (fetch_where_conditions) — 可修改以測試不同篩選條件",
|
||||
"fetchOrderBy": "排序 (fetch_order_by)",
|
||||
|
||||
Reference in new issue
Block a user