Files
DecomEval/web/src/features/admin/SourcesView.vue
T

136 lines
5.2 KiB
Vue
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<script setup lang="ts">
import { onMounted, ref } from "vue";
import { NButton, NInput, NInputNumber, NModal, NSelect, NSwitch } from "naive-ui";
import Shell from "../../shell/Shell.vue";
import { api } from "../../shared/http";
interface Source {
id: number;
name: string;
kind: string;
periodDays: number;
enabled: boolean;
lastError: string;
config: Record<string, string | number | boolean>;
}
const sources = ref<Source[]>([]);
const show = 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 url = ref("");
const errorText = ref("");
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 || "");
url.value = String(source?.config.url || "");
errorText.value = "";
show.value = true;
}
async function load() {
const result = await api<{ sources: Source[] }>("/api/admin/sources");
sources.value = result.sources;
}
async function save() {
errorText.value = "";
const config = kind.value === "nqi"
? { url: url.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 };
try {
if (editing.value) {
await api(`/api/admin/sources/${editing.value.id}`, { method: "PUT", body: JSON.stringify(body) });
} else {
await api("/api/admin/sources", { method: "POST", body: JSON.stringify(body) });
}
show.value = false;
await load();
} catch (error) {
errorText.value = error instanceof Error ? error.message : "保存失败";
return false;
}
}
async function remove(id: number) {
await api(`/api/admin/sources/${id}`, { method: "DELETE" });
await load();
}
onMounted(load);
</script>
<template>
<Shell>
<section class="page admin-page">
<header class="page-head">
<h2>采集</h2>
<n-button type="primary" @click="reset()">添加采集</n-button>
</header>
<div class="admin-scroll">
<table v-if="sources.length" class="admin-table">
<thead>
<tr>
<th>名称</th>
<th>来源</th>
<th>周期(天)</th>
<th>状态</th>
<th>操作</th>
</tr>
</thead>
<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>{{ source.lastError || (source.enabled ? "启用" : "停用") }}</td>
<td>
<n-button size="small" quaternary @click="reset(source)">编辑</n-button>
<n-button size="small" quaternary @click="remove(source.id)">删除</n-button>
</td>
</tr>
</tbody>
</table>
<p v-else class="admin-status">没有采集</p>
</div>
</section>
<n-modal v-model:show="show" preset="dialog" title="采集" positive-text="保存" negative-text="取消" @positive-click="save">
<div class="form-row"><label>名称</label><n-input v-model:value="name" /></div>
<div class="form-row"><label>来源</label><n-select v-model:value="kind" :options="[{ label: 'FTP/SFTP', value: 'ftp' }, { label: 'NQI', value: 'nqi' }]" /></div>
<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>
<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-input v-model:value="url" /></div>
</template>
<p v-if="errorText" style="color: var(--danger)">{{ errorText }}</p>
</n-modal>
</Shell>
</template>