feat: 实现已确认的登录、采集和任务导出

This commit is contained in:
2026-10-29 18:11:15 +08:00
parent af91488934
commit 7ed299bbf6
44 changed files with 4546 additions and 2 deletions
+59
View File
@@ -0,0 +1,59 @@
<script setup lang="ts">
import { onMounted, ref } from "vue";
import { NButton, NDataTable, NModal, NSelect } from "naive-ui";
import Shell from "../../shell/Shell.vue";
import LoginView from "../auth/LoginView.vue";
import { api } from "../../shared/http";
interface Account { id: number; loginId: string; userName: string; cityName: string; role: string; sessionOpen: boolean; credentialValid: boolean }
const accounts = ref<Account[]>([]);
const show = ref(false);
const columns = [
{ title: "账号", key: "loginId" },
{ title: "姓名", key: "userName" },
{ title: "地市", key: "cityName" },
{ title: "凭证", key: "validLabel" },
{ title: "登录", key: "openLabel" },
];
async function load() {
const result = await api<{ accounts: Account[] }>("/api/admin/pool");
accounts.value = result.accounts.map((item) => ({
...item,
validLabel: item.credentialValid ? "有效" : "失效",
openLabel: item.sessionOpen ? "未退出" : "已退出",
}));
}
async function setRole(id: number, role: string) {
await api(`/api/admin/pool/${id}/role`, { method: "PUT", body: JSON.stringify({ role }) });
await load();
}
async function remove(id: number) {
await api(`/api/admin/pool/${id}`, { method: "DELETE" });
await load();
}
onMounted(load);
</script>
<template>
<Shell>
<header class="topbar">
<h1>账号池</h1>
<n-button type="primary" @click="show = true">添加账号</n-button>
</header>
<div class="content">
<n-data-table :columns="columns" :data="accounts" :bordered="false" />
<div v-for="account in accounts" :key="account.id" class="row-actions" style="margin-top: 8px">
<span>{{ account.loginId }}</span>
<n-select :value="account.role" size="small" style="width: 120px" :options="[{ label: '用户', value: 'user' }, { label: '管理员', value: 'admin' }]" @update:value="(value) => setRole(account.id, value)" />
<n-button size="small" @click="remove(account.id)">删除</n-button>
</div>
</div>
<n-modal v-model:show="show" preset="card" title="添加账号" style="width: 520px">
<LoginView purpose="pool" @done="show = false; load()" />
</n-modal>
</Shell>
</template>
+52
View File
@@ -0,0 +1,52 @@
<script setup lang="ts">
import { onMounted, ref } from "vue";
import { NButton, NInputNumber, NRadio, NRadioGroup, NTooltip } from "naive-ui";
import Shell from "../../shell/Shell.vue";
import { api } from "../../shared/http";
const mode = ref("local");
const seconds = ref(300);
const message = ref("");
async function load() {
const result = await api<{ loginMode: string; credentialRefreshSeconds: number }>("/api/admin/settings");
mode.value = result.loginMode;
seconds.value = result.credentialRefreshSeconds;
}
async function save() {
message.value = "";
try {
await api("/api/admin/settings", {
method: "PUT",
body: JSON.stringify({ loginMode: mode.value, credentialRefreshSeconds: seconds.value }),
});
message.value = "已保存";
} catch (error) {
message.value = error instanceof Error ? error.message : "保存失败";
}
}
onMounted(load);
</script>
<template>
<Shell>
<header class="topbar">
<h1>登录方式 <n-tooltip><template #trigger><span class="hint">?</span></template>同时只启用一种登录方式</n-tooltip></h1>
<n-button type="primary" @click="save">保存</n-button>
</header>
<div class="content">
<div class="form-grid">
<label>方式</label>
<n-radio-group v-model:value="mode">
<n-radio value="local">自有账号</n-radio>
<n-radio value="nqi">大数据平台账号</n-radio>
</n-radio-group>
<label>刷新间隔</label>
<n-input-number v-model:value="seconds" :min="60" :max="86400" />
</div>
<p>{{ message }}</p>
</div>
</Shell>
</template>
+137
View File
@@ -0,0 +1,137 @@
<script setup lang="ts">
import { onMounted, ref } from "vue";
import { NButton, NDataTable, NInput, NInputNumber, NModal, NSelect, NSwitch, NTooltip } 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("");
const columns = [
{ title: "名称", key: "name" },
{ title: "来源", key: "kindLabel" },
{ title: "周期(天)", key: "periodDays" },
{ title: "状态", key: "stateLabel" },
];
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.map((item) => ({
...item,
kindLabel: item.kind === "nqi" ? "NQI" : "FTP/SFTP",
stateLabel: item.lastError || (item.enabled ? "启用" : "停用"),
}));
}
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>
<header class="topbar">
<h1>采集 <n-tooltip><template #trigger><span class="hint">?</span></template>周期最短一天,最长一个月</n-tooltip></h1>
<n-button type="primary" @click="reset()">添加采集</n-button>
</header>
<div class="content">
<n-data-table :columns="columns" :data="sources" :bordered="false" />
<div v-for="source in sources" :key="source.id" class="row-actions" style="margin-top: 8px">
<n-button size="small" @click="reset(source)">编辑</n-button>
<n-button size="small" @click="remove(source.id)">删除</n-button>
</div>
</div>
<n-modal v-model:show="show" preset="dialog" title="采集" positive-text="保存" negative-text="取消" @positive-click="save">
<div class="form-grid">
<label>名称</label>
<n-input v-model:value="name" />
<label>来源</label>
<n-select v-model:value="kind" :options="[{ label: 'FTP/SFTP', value: 'ftp' }, { label: 'NQI', value: 'nqi' }]" />
<label>周期(天)</label>
<n-input-number v-model:value="periodDays" :min="1" :max="31" />
<label>启用</label>
<n-switch v-model:value="enabled" />
<template v-if="kind === 'ftp'">
<label>协议</label>
<n-select v-model:value="protocol" :options="[{ label: 'SFTP', value: 'sftp' }, { label: 'FTP', value: 'ftp' }]" />
<label>主机</label>
<n-input v-model:value="host" />
<label>端口</label>
<n-input-number v-model:value="port" />
<label>账号</label>
<n-input v-model:value="username" />
<label>密码</label>
<n-input v-model:value="password" type="password" />
<label>路径</label>
<n-input v-model:value="path" />
</template>
<template v-else>
<label>地址</label>
<n-input v-model:value="url" />
</template>
</div>
<p v-if="errorText" style="color: var(--danger)">{{ errorText }}</p>
</n-modal>
</Shell>
</template>
+71
View File
@@ -0,0 +1,71 @@
<script setup lang="ts">
import { onMounted, ref } from "vue";
import { NButton, NDataTable, NInput, NModal, NSelect } from "naive-ui";
import Shell from "../../shell/Shell.vue";
import { api } from "../../shared/http";
interface User { id: number; username: string; role: string }
const users = ref<User[]>([]);
const show = ref(false);
const username = ref("");
const password = ref("");
const role = ref("user");
const errorText = ref("");
const columns = [
{ title: "账号", key: "username" },
{ title: "角色", key: "roleLabel" },
];
async function load() {
const result = await api<{ users: User[] }>("/api/admin/users");
users.value = result.users.map((item) => ({ ...item, roleLabel: item.role === "admin" ? "管理员" : "用户" }));
}
async function create() {
errorText.value = "";
try {
await api("/api/admin/users", { method: "POST", body: JSON.stringify({ username: username.value, password: password.value, role: role.value }) });
show.value = false;
username.value = "";
password.value = "";
await load();
} catch (error) {
errorText.value = error instanceof Error ? error.message : "添加失败";
return false;
}
}
async function remove(id: number) {
await api(`/api/admin/users/${id}`, { method: "DELETE" });
await load();
}
onMounted(load);
</script>
<template>
<Shell>
<header class="topbar">
<h1>用户</h1>
<n-button type="primary" @click="show = true">添加用户</n-button>
</header>
<div class="content">
<n-data-table :columns="columns" :data="users" :bordered="false" />
<div v-for="user in users" :key="user.id" class="row-actions" style="margin-top: 8px">
<span>{{ user.username }}</span>
<n-button size="small" @click="remove(user.id)">删除</n-button>
</div>
</div>
<n-modal v-model:show="show" preset="dialog" title="添加用户" positive-text="保存" negative-text="取消" @positive-click="create">
<div class="form-grid">
<label>账号</label>
<n-input v-model:value="username" />
<label>密码</label>
<n-input v-model:value="password" type="password" />
<label>角色</label>
<n-select v-model:value="role" :options="[{ label: '用户', value: 'user' }, { label: '管理员', value: 'admin' }]" />
</div>
<p v-if="errorText" style="color: var(--danger)">{{ errorText }}</p>
</n-modal>
</Shell>
</template>
+180
View File
@@ -0,0 +1,180 @@
<script setup lang="ts">
import { onMounted, ref } from "vue";
import { useRouter } from "vue-router";
import { NAlert, NButton, NInput } from "naive-ui";
import { api, saveSession } from "../../shared/http";
import { setThemeChoice, themeChoice, type ThemeChoice } from "../../shared/theme";
const props = defineProps<{ purpose?: "login" | "pool" }>();
const emit = defineEmits<{ done: [] }>();
const router = useRouter();
const mode = ref("local");
const username = ref("");
const password = ref("");
const captcha = ref("");
const captchaUrl = ref("");
const sessionId = ref("");
const smsCode = ref("");
const smsRequired = ref(false);
const errorText = ref("");
const busy = ref(false);
const themes = [
{ label: "跟随系统", value: "system" },
{ label: "深色", value: "dark" },
{ label: "浅色", value: "light" },
];
onMounted(async () => {
if (props.purpose === "pool") {
mode.value = "nqi";
return;
}
const result = await api<{ mode: string }>("/api/auth/mode");
mode.value = result.mode;
});
function fail(error: unknown) {
errorText.value = error instanceof Error ? error.message : "登录失败";
}
async function enterLocal() {
busy.value = true;
errorText.value = "";
try {
const result = await api<{ token: string; user: { role: string; name: string } }>("/api/auth/local", {
method: "POST",
body: JSON.stringify({ username: username.value, password: password.value }),
});
saveSession(result.token, result.user);
router.push(result.user.role === "admin" ? "/admin/settings" : "/tasks");
} catch (error) {
fail(error);
} finally {
busy.value = false;
}
}
async function startNqi() {
busy.value = true;
errorText.value = "";
try {
const result = await api<{ status: string; token?: string; user?: { role: string; name: string }; sessionId?: string; captcha?: string }>("/api/auth/nqi/start", {
method: "POST",
body: JSON.stringify({ username: username.value, purpose: props.purpose || "login" }),
});
if (result.status === "ok" && result.token && result.user) {
saveSession(result.token, result.user);
router.push(result.user.role === "admin" ? "/admin/settings" : "/tasks");
return;
}
sessionId.value = result.sessionId || "";
captchaUrl.value = result.captcha || "";
} catch (error) {
fail(error);
} finally {
busy.value = false;
}
}
async function refreshCaptcha() {
const result = await api<{ captcha: string }>("/api/auth/nqi/captcha", {
method: "POST",
body: JSON.stringify({ sessionId: sessionId.value }),
});
captchaUrl.value = result.captcha;
}
async function sendSms() {
busy.value = true;
errorText.value = "";
try {
await api("/api/auth/nqi/sms", {
method: "POST",
body: JSON.stringify({ sessionId: sessionId.value, username: username.value, password: password.value, captcha: captcha.value }),
});
smsRequired.value = true;
} catch (error) {
fail(error);
} finally {
busy.value = false;
}
}
async function finishNqi() {
busy.value = true;
errorText.value = "";
try {
const result = await api<{ status: string; token?: string; user?: { role: string; name: string } }>("/api/auth/nqi/finish", {
method: "POST",
body: JSON.stringify({
sessionId: sessionId.value,
username: username.value,
password: password.value,
captcha: captcha.value,
smsCode: smsCode.value,
}),
});
if (result.status === "sms_required") {
smsRequired.value = true;
return;
}
if (props.purpose === "pool") {
emit("done");
return;
}
if (result.token && result.user) {
saveSession(result.token, result.user);
router.push(result.user.role === "admin" ? "/admin/settings" : "/tasks");
}
} catch (error) {
fail(error);
} finally {
busy.value = false;
}
}
function chooseTheme(value: ThemeChoice) {
setThemeChoice(value);
}
</script>
<template>
<div :class="purpose === 'pool' ? '' : 'gate'">
<div class="gate-card">
<h1 style="margin: 0 0 16px; font-size: 20px; display: flex; justify-content: space-between; align-items: center;">
<span>{{ purpose === "pool" ? "添加账号" : "DecomEval" }}</span>
<select v-if="purpose !== 'pool'" :value="themeChoice" @change="chooseTheme(($event.target as HTMLSelectElement).value as ThemeChoice)">
<option value="system">跟随系统</option>
<option value="dark">深色</option>
<option value="light">浅色</option>
</select>
</h1>
<n-alert v-if="errorText" type="error" style="margin-bottom: 12px">{{ errorText }}</n-alert>
<div class="form-grid">
<label>账号</label>
<n-input v-model:value="username" />
<label>密码</label>
<n-input v-model:value="password" type="password" />
<template v-if="mode === 'nqi' && captchaUrl">
<label>验证码</label>
<div>
<n-input v-model:value="captcha" />
<img class="captcha" :src="captchaUrl" alt="验证码" @click="refreshCaptcha" />
</div>
<template v-if="smsRequired">
<label>短信验证码</label>
<n-input v-model:value="smsCode" />
</template>
</template>
</div>
<div style="margin-top: 16px; display: flex; justify-content: flex-end; gap: 8px;">
<n-button v-if="mode === 'local'" type="primary" :loading="busy" @click="enterLocal">登录</n-button>
<template v-else>
<n-button v-if="!captchaUrl" type="primary" :loading="busy" @click="startNqi">继续</n-button>
<n-button v-if="captchaUrl" :loading="busy" @click="sendSms">发送短信验证码</n-button>
<n-button v-if="captchaUrl" type="primary" :loading="busy" @click="finishNqi">登录</n-button>
</template>
</div>
</div>
</div>
</template>
+91
View File
@@ -0,0 +1,91 @@
<script setup lang="ts">
import { onMounted, ref } from "vue";
import { NButton, NDataTable, NInput, NModal, NSelect, NTooltip } from "naive-ui";
import Shell from "../../shell/Shell.vue";
import { api, token } from "../../shared/http";
interface Task { id: number; status: string; format: string; cellKey: string; error: string }
const tasks = ref<Task[]>([]);
const show = ref(false);
const format = ref("xlsx");
const cellKey = ref("");
const start = ref("");
const end = ref("");
const errorText = ref("");
const formats = [{ label: "xlsx", value: "xlsx" }, { label: "xls", value: "xls" }, { label: "csv", value: "csv" }];
const columns = [
{ title: "小区", key: "cellKey" },
{ title: "格式", key: "format" },
{ title: "状态", key: "statusLabel" },
{ title: "结果", key: "action" },
];
function statusLabel(status: string) {
if (status === "queued") return "排队";
if (status === "running") return "进行中";
if (status === "done") return "可下载";
return "失败";
}
async function load() {
const result = await api<{ tasks: Task[] }>("/api/tasks");
tasks.value = result.tasks.map((item) => ({ ...item, statusLabel: statusLabel(item.status) }));
}
async function submit() {
errorText.value = "";
try {
await api("/api/tasks", {
method: "POST",
body: JSON.stringify({ format: format.value, cellKey: cellKey.value, start: start.value, end: end.value }),
});
show.value = false;
await load();
} catch (error) {
errorText.value = error instanceof Error ? error.message : "提交失败";
}
}
async function download(row: Task) {
const response = await fetch(`/api/tasks/${row.id}/file`, { headers: { Authorization: `Bearer ${token()}` } });
const blob = await response.blob();
const url = URL.createObjectURL(blob);
const link = document.createElement("a");
link.href = url;
link.download = `结果.${row.format}`;
link.click();
URL.revokeObjectURL(url);
}
onMounted(load);
</script>
<template>
<Shell>
<header class="topbar">
<h1>任务 <n-tooltip><template #trigger><span class="hint">?</span></template>按已入库的数据生成结果文件</n-tooltip></h1>
<n-button type="primary" @click="show = true">提交任务</n-button>
</header>
<div class="content">
<n-data-table :columns="columns" :data="tasks" :bordered="false">
<template #empty>还没有任务</template>
</n-data-table>
<div v-for="row in tasks.filter((item) => item.status === 'done')" :key="row.id" class="row-actions" style="margin-top: 8px">
<n-button size="small" @click="download(row)">下载 {{ row.cellKey || "全部" }}.{{ row.format }}</n-button>
</div>
</div>
<n-modal v-model:show="show" preset="dialog" title="提交任务" positive-text="提交" negative-text="取消" @positive-click="submit">
<div class="form-grid">
<label>格式</label>
<n-select v-model:value="format" :options="formats" />
<label>小区</label>
<n-input v-model:value="cellKey" placeholder="留空则导出全部" />
<label>开始</label>
<n-input v-model:value="start" placeholder="YYYY-MM-DD" />
<label>结束</label>
<n-input v-model:value="end" placeholder="YYYY-MM-DD" />
</div>
<p v-if="errorText" style="color: var(--danger)">{{ errorText }}</p>
</n-modal>
</Shell>
</template>