feat: 采集按分组选表和固定时刻运行,号池可检查凭证,管理页不再退出

This commit is contained in:
2026-11-09 17:55:31 +08:00
parent 152cf92df9
commit 78ad5cea6b
19 changed files with 554 additions and 247 deletions
+197 -142
View File
@@ -1,47 +1,53 @@
<script setup lang="ts">
import { computed, onMounted, ref } from "vue";
import { NButton, NInput, NInputNumber, NModal, NSelect, NSwitch, useDialog } from "naive-ui";
import { computed, h, onMounted, ref } from "vue";
import { NButton, NIcon, NInput, NInputNumber, NModal, NSelect, NSwitch, NTimePicker, NTooltip, useDialog } from "naive-ui";
import type { SelectRenderTag } from "naive-ui";
import { CreateOutline, TrashOutline } from "@vicons/ionicons5";
import Shell from "../../shell/Shell.vue";
import { api } from "../../shared/http";
import { useToast } from "../../shared/toast";
interface Source {
id: number;
name: string;
kind: string;
periodDays: number;
enabled: boolean;
lastError: string;
config: Record<string, unknown>;
}
interface QueryTable {
code: number;
category: string;
tablename: string;
name: string;
}
interface Schedule {
mode: "weekly" | "monthly";
weekday?: number;
monthDay?: number;
time: string;
}
interface Source {
id: number;
name: string;
kind: string;
enabled: boolean;
lastError: string;
config: { tables?: QueryTable[]; timeMode?: string; timeCount?: number; city?: string; schedule?: Schedule };
}
interface Group {
code: number;
name: string;
tables: { tablename: string; name: string }[];
}
const sources = ref<Source[]>([]);
const show = ref(false);
const details = ref(false);
const editing = ref<Source | null>(null);
const name = ref("");
const kind = ref("ftp");
const periodDays = ref(1);
const enabled = ref(true);
const host = ref("");
const port = ref(22);
const protocol = ref("sftp");
const username = ref("");
const password = ref("");
const path = ref("");
const categories = ref<{ code: number; name: string }[]>([]);
const categoryCode = ref<number | null>(null);
const categoryTables = ref<{ tablename: string; name: string }[]>([]);
const groups = ref<Group[]>([]);
const chosen = ref<QueryTable[]>([]);
const timeMode = ref("prev_month");
const timeCount = ref(1);
const city = ref("");
const scheduleMode = ref<"weekly" | "monthly">("weekly");
const weekday = ref(1);
const monthDay = ref(1);
const clock = ref("08:00");
const catalogLoading = ref(false);
const tablesLoading = ref(false);
const dialog = useDialog();
const toast = useToast();
const timeOptions = [
@@ -52,90 +58,50 @@ const timeOptions = [
{ label: "前几月", value: "prev_months" },
{ label: "前多少天", value: "prev_days" },
];
const weekdays = ["一", "二", "三", "四", "五", "六", "日"].map((label, index) => ({ label: `周${label}`, value: index + 1 }));
const monthDays = Array.from({ length: 31 }, (_, index) => ({ label: `${index + 1} 日`, value: index + 1 }));
const needsCount = computed(() => ["prev_weeks", "prev_months", "prev_days"].includes(timeMode.value));
const countLabel = computed(() => (timeMode.value === "prev_weeks" ? "周数" : timeMode.value === "prev_months" ? "月数" : "天数"));
const categoryOptions = computed(() => categories.value.map((item) => ({ label: item.name, value: item.code })));
const tableOptions = computed(() => categoryTables.value.map((item) => ({ label: item.name, value: item.tablename })));
const pickedNames = computed(() => chosen.value.filter((item) => item.code === categoryCode.value).map((item) => item.tablename));
const tableOptions = computed(() => groups.value.map((group) => ({
type: "group" as const,
label: group.name,
key: String(group.code),
children: group.tables.map((table) => ({ label: table.name, value: table.tablename })),
})));
const picked = computed(() => chosen.value.map((item) => item.tablename));
const pickedText = computed(() => chosen.value.length ? `已选 ${chosen.value.length}:${chosen.value.map((item) => item.name).join("、")}` : "");
const renderTag: SelectRenderTag = () => h("span", { class: "picker-inline" }, pickedText.value || "请选择表");
function scheduleText(source: Source) {
const schedule = source.config.schedule;
if (!schedule) return "未设置";
const clockText = schedule.time || "";
if (schedule.mode === "monthly") return `每月 ${schedule.monthDay} 日 ${clockText}`;
const label = weekdays.find((item) => item.value === schedule.weekday)?.label || "";
return `每${label} ${clockText}`;
}
function reset(source?: Source) {
editing.value = source || null;
name.value = source?.name || "";
kind.value = source?.kind || "ftp";
periodDays.value = source?.periodDays || 1;
enabled.value = source?.enabled ?? true;
host.value = String(source?.config.host || "");
port.value = Number(source?.config.port || 22);
protocol.value = String(source?.config.protocol || "sftp");
username.value = String(source?.config.username || "");
password.value = "";
path.value = String(source?.config.path || "");
const stored = Array.isArray(source?.config.tables) ? source.config.tables as QueryTable[] : [];
chosen.value = stored.filter((item) => item && item.tablename).map((item) => ({
const stored = Array.isArray(source?.config.tables) ? source.config.tables : [];
chosen.value = stored.filter((item) => item?.tablename).map((item) => ({
code: Number(item.code) || 0,
category: String(item.category || ""),
tablename: String(item.tablename),
name: String(item.name || item.tablename),
}));
timeMode.value = String(source?.config.timeMode || "prev_month");
timeMode.value = source?.config.timeMode || "prev_month";
timeCount.value = Number(source?.config.timeCount || 1);
city.value = String(source?.config.city || "");
categoryCode.value = chosen.value[0]?.code || categories.value[0]?.code || null;
categoryTables.value = [];
city.value = source?.config.city || "";
const schedule = source?.config.schedule;
scheduleMode.value = schedule?.mode === "monthly" ? "monthly" : "weekly";
weekday.value = Number(schedule?.weekday || 1);
monthDay.value = Number(schedule?.monthDay || 1);
clock.value = schedule?.time || "08:00";
show.value = true;
if (kind.value === "nqi") {
void loadCategories();
}
}
async function changeKind(value: string) {
kind.value = value;
if (value === "nqi") await loadCategories();
}
async function loadCategories() {
catalogLoading.value = true;
try {
const result = await api<{ categories: { code: number; name: string }[] }>("/api/admin/nqi/catalog");
categories.value = result.categories;
if (categoryCode.value == null && categories.value.length) categoryCode.value = categories.value[0].code;
if (categoryCode.value != null) await loadTables(categoryCode.value);
} catch (error) {
toast.error(error instanceof Error ? error.message : "查询分类没有获取到");
} finally {
catalogLoading.value = false;
}
}
async function loadTables(code: number) {
categoryCode.value = code;
tablesLoading.value = true;
try {
const result = await api<{ tables: { tablename: string; name: string }[] }>(`/api/admin/nqi/catalog?code=${code}`);
categoryTables.value = result.tables;
} catch (error) {
categoryTables.value = [];
toast.error(error instanceof Error ? error.message : "数据表没有获取到");
} finally {
tablesLoading.value = false;
}
}
function setPicked(names: string[]) {
const code = categoryCode.value;
const category = categories.value.find((item) => item.code === code)?.name || "";
const current = categoryTables.value
.filter((item) => names.includes(item.tablename))
.map((item) => ({ code: code || 0, category, tablename: item.tablename, name: item.name }));
chosen.value = [...chosen.value.filter((item) => item.code !== code), ...current];
}
function selectCategory() {
setPicked(categoryTables.value.map((item) => item.tablename));
}
function removeTable(tablename: string) {
chosen.value = chosen.value.filter((item) => item.tablename !== tablename);
if (!groups.value.length) void loadCatalog();
}
async function load() {
@@ -143,15 +109,56 @@ async function load() {
sources.value = result.sources;
}
async function loadCatalog() {
catalogLoading.value = true;
try {
const result = await api<{ groups: Group[] }>("/api/admin/nqi/catalog?tree=true");
groups.value = result.groups;
} catch (error) {
toast.error(error instanceof Error ? error.message : "数据表没有获取到");
} finally {
catalogLoading.value = false;
}
}
function lookup(tablename: string) {
for (const group of groups.value) {
const table = group.tables.find((item) => item.tablename === tablename);
if (table) return { code: group.code, category: group.name, tablename, name: table.name };
}
return chosen.value.find((item) => item.tablename === tablename) || null;
}
function setPicked(names: string[]) {
chosen.value = names.map(lookup).filter((item): item is QueryTable => Boolean(item));
}
function removeTable(tablename: string) {
chosen.value = chosen.value.filter((item) => item.tablename !== tablename);
}
async function save() {
if (kind.value === "nqi" && !chosen.value.length) {
if (!name.value.trim()) {
toast.error("请填写名称");
return false;
}
if (!chosen.value.length) {
toast.error("请选择要采集的表");
return false;
}
const config = kind.value === "nqi"
? { tables: chosen.value, timeMode: timeMode.value, timeCount: timeCount.value, city: city.value }
: { protocol: protocol.value, host: host.value, port: port.value, username: username.value, password: password.value, path: path.value };
const body = { name: name.value, kind: kind.value, periodDays: periodDays.value, enabled: enabled.value, config };
if (!clock.value) {
toast.error("请填写采集时间");
return false;
}
const schedule = scheduleMode.value === "weekly"
? { mode: "weekly", weekday: weekday.value, time: clock.value }
: { mode: "monthly", monthDay: monthDay.value, time: clock.value };
const body = {
name: name.value,
kind: "nqi",
enabled: enabled.value,
config: { tables: chosen.value, timeMode: timeMode.value, timeCount: timeCount.value, city: city.value, schedule },
};
try {
if (editing.value) {
await api(`/api/admin/sources/${editing.value.id}`, { method: "PUT", body: JSON.stringify(body) });
@@ -204,8 +211,7 @@ onMounted(load);
<thead>
<tr>
<th>名称</th>
<th>来源</th>
<th>周期(天)</th>
<th>周期</th>
<th>状态</th>
<th class="col-actions">操作</th>
</tr>
@@ -213,16 +219,29 @@ onMounted(load);
<tbody>
<tr v-for="source in sources" :key="source.id">
<td>{{ source.name }}</td>
<td>{{ source.kind === "nqi" ? "NQI" : "FTP/SFTP" }}</td>
<td>{{ source.periodDays }}</td>
<td>{{ scheduleText(source) }}</td>
<td>
<span v-if="source.lastError" class="status-bad">{{ source.lastError }}</span>
<span v-else :class="source.enabled ? 'status-ok' : 'status-wait'">{{ source.enabled ? "启用" : "停用" }}</span>
</td>
<td class="col-actions">
<div class="row-actions">
<n-button size="small" quaternary @click="reset(source)">编辑</n-button>
<n-button size="small" quaternary @click="confirmRemove(source)">删除</n-button>
<n-tooltip>
<template #trigger>
<n-button size="small" quaternary circle aria-label="编辑" @click="reset(source)">
<template #icon><n-icon :component="CreateOutline" /></template>
</n-button>
</template>
编辑
</n-tooltip>
<n-tooltip>
<template #trigger>
<n-button size="small" quaternary circle aria-label="删除" @click="confirmRemove(source)">
<template #icon><n-icon :component="TrashOutline" /></template>
</n-button>
</template>
删除
</n-tooltip>
</div>
</td>
</tr>
@@ -234,56 +253,92 @@ onMounted(load);
</div>
</div>
</section>
<n-modal v-model:show="show" class="app-dialog app-dialog-wide" preset="dialog" :show-icon="false" title="采集" positive-text="保存" negative-text="取消" @positive-click="save">
<div class="form-row"><label>来源</label><n-select :value="kind" :options="[{ label: 'FTP/SFTP', value: 'ftp' }, { label: 'NQI', value: 'nqi' }]" @update:value="changeKind" /></div>
<div class="form-row"><label>名称</label><n-input v-model:value="name" /></div>
<p class="form-section">连接</p>
<template v-if="kind === 'ftp'">
<div class="form-row"><label>协议</label><n-select v-model:value="protocol" :options="[{ label: 'SFTP', value: 'sftp' }, { label: 'FTP', value: 'ftp' }]" /></div>
<div class="form-row"><label>主机</label><n-input v-model:value="host" /></div>
<div class="form-row"><label>端口</label><n-input-number v-model:value="port" :show-button="false" /></div>
<div class="form-row"><label>账号</label><n-input v-model:value="username" /></div>
<div class="form-row"><label>密码</label><n-input v-model:value="password" type="password" /></div>
<div class="form-row"><label>路径</label><n-input v-model:value="path" /></div>
</template>
<template v-else>
<div class="form-row"><label>查询分类</label><n-select :value="categoryCode" :options="categoryOptions" :loading="catalogLoading" @update:value="loadTables" /></div>
<n-modal v-model:show="show" class="app-dialog app-dialog-collect" preset="card" :show-icon="false" title="采集">
<div class="collect-form">
<div class="form-row"><label>名称</label><n-input v-model:value="name" /></div>
<div class="form-row">
<label>数据表</label>
<div>
<n-select multiple filterable :value="pickedNames" :options="tableOptions" :loading="tablesLoading" @update:value="setPicked" />
<n-button size="small" quaternary :disabled="!categoryTables.length" @click="selectCategory">分类全选</n-button>
</div>
</div>
<div v-if="chosen.length" class="form-row">
<label>已选</label>
<div class="picked-tables">
<button v-for="item in chosen" :key="item.tablename" type="button" @click="removeTable(item.tablename)">{{ item.name }}</button>
<div class="table-picker">
<n-select
class="table-select"
multiple
filterable
placeholder="请选择表"
:value="picked"
:options="tableOptions"
:loading="catalogLoading"
:render-tag="renderTag"
@update:value="setPicked"
/>
<div class="table-picker-meta">
<span>已选 {{ chosen.length }}</span>
<n-button text type="primary" :disabled="!chosen.length" @click="details = true">选择详情</n-button>
</div>
</div>
</div>
<div class="form-row"><label>数据时间</label><n-select v-model:value="timeMode" :options="timeOptions" /></div>
<div v-if="needsCount" class="form-row"><label>{{ countLabel }}</label><n-input-number v-model:value="timeCount" :min="1" :max="366" :show-button="false" /></div>
<div class="form-row"><label>城市</label><n-input v-model:value="city" placeholder="不填则不按城市过滤" /></div>
<div class="form-row">
<label>采集周期</label>
<n-select v-model:value="scheduleMode" :options="[{ label: '每周', value: 'weekly' }, { label: '每月', value: 'monthly' }]" />
</div>
<div class="form-row">
<label>{{ scheduleMode === 'weekly' ? '星期' : '日期' }}</label>
<div class="schedule-time">
<n-select v-if="scheduleMode === 'weekly'" v-model:value="weekday" :options="weekdays" />
<n-select v-else v-model:value="monthDay" :options="monthDays" />
<n-time-picker v-model:formatted-value="clock" format="HH:mm" value-format="HH:mm" />
</div>
</div>
<div class="form-row"><label>启用</label><n-switch v-model:value="enabled" /></div>
</div>
<template #footer>
<div class="dialog-actions">
<n-button @click="show = false">取消</n-button>
<n-button type="primary" @click="save">保存</n-button>
</div>
</template>
<p class="form-section">周期</p>
<div class="form-row"><label>周期(天)</label><n-input-number v-model:value="periodDays" :min="1" :max="31" :show-button="false" /></div>
<div class="form-row"><label>启用</label><n-switch v-model:value="enabled" /></div>
</n-modal>
<n-modal v-model:show="details" class="app-dialog" preset="card" title="已选表格">
<div v-if="chosen.length" class="picked-list">
<div v-for="item in chosen" :key="item.tablename" class="picked-row">
<div>
<strong>{{ item.name }}</strong>
<span>{{ item.category }}</span>
</div>
<n-tooltip>
<template #trigger>
<n-button size="small" quaternary circle aria-label="移除" @click="removeTable(item.tablename)">
<template #icon><n-icon :component="TrashOutline" /></template>
</n-button>
</template>
移除
</n-tooltip>
</div>
</div>
<p v-else class="admin-status">还没有选择表格</p>
</n-modal>
</Shell>
</template>
<style scoped>
.picked-tables {
display: flex;
flex-wrap: wrap;
gap: 6px;
}
.picked-tables button {
border: 1px solid var(--line);
border-radius: 999px;
background: transparent;
color: var(--ink);
padding: 2px 8px;
cursor: pointer;
.collect-form { display: grid; gap: 12px; }
.table-picker { display: grid; gap: 6px; min-width: 0; }
.table-picker-meta { display: flex; align-items: center; justify-content: space-between; color: var(--muted); font-size: 12px; }
.table-select :deep(.n-base-selection-tags) { flex-wrap: nowrap; overflow: hidden; }
.table-select :deep(.n-base-selection-tag-wrapper:not(:first-child)) { display: none; }
.table-select :deep(.picker-inline) {
display: block;
max-width: 280px;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
.schedule-time { display: grid; grid-template-columns: minmax(0, 1fr) 128px; gap: 8px; }
.dialog-actions { display: flex; justify-content: flex-end; gap: 8px; }
.picked-list { display: grid; gap: 8px; }
.picked-row { display: flex; align-items: center; justify-content: space-between; gap: 12px; }
.picked-row strong { display: block; }
.picked-row span { color: var(--muted); font-size: 12px; }
</style>