feat: 提示统一为可关闭的 toast

This commit is contained in:
2026-10-30 12:14:26 +08:00
parent 811dc448cb
commit ccff78ef9c
8 changed files with 76 additions and 65 deletions
+7 -6
View File
@@ -1,15 +1,16 @@
<script setup lang="ts">
import { onMounted, ref } from "vue";
import { NButton, NModal, NSelect, useDialog, useMessage } from "naive-ui";
import { NButton, NModal, NSelect, useDialog } from "naive-ui";
import Shell from "../../shell/Shell.vue";
import LoginView from "../auth/LoginView.vue";
import { api } from "../../shared/http";
import { useToast } from "../../shared/toast";
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 dialog = useDialog();
const message = useMessage();
const toast = useToast();
async function load() {
const result = await api<{ accounts: Account[] }>("/api/admin/pool");
@@ -19,20 +20,20 @@ async function load() {
async function setRole(id: number, role: string) {
try {
await api(`/api/admin/pool/${id}/role`, { method: "PUT", body: JSON.stringify({ role }) });
message.success("已保存");
toast.success("已保存");
await load();
} catch (error) {
message.error(error instanceof Error ? error.message : "保存失败");
toast.error(error instanceof Error ? error.message : "保存失败");
}
}
async function remove(id: number) {
try {
await api(`/api/admin/pool/${id}`, { method: "DELETE" });
message.success("已删除");
toast.success("已删除");
await load();
} catch (error) {
message.error(error instanceof Error ? error.message : "删除失败");
toast.error(error instanceof Error ? error.message : "删除失败");
}
}
+6 -5
View File
@@ -1,10 +1,11 @@
<script setup lang="ts">
import { onMounted, ref } from "vue";
import { NButton, NInputNumber, NRadio, NRadioGroup, useMessage } from "naive-ui";
import { NButton, NInputNumber, NRadio, NRadioGroup } from "naive-ui";
import Shell from "../../shell/Shell.vue";
import { api } from "../../shared/http";
import { useToast } from "../../shared/toast";
const message = useMessage();
const toast = useToast();
const mode = ref("local");
const seconds = ref(300);
const concurrency = ref(4);
@@ -19,7 +20,7 @@ async function load() {
concurrency.value = result.workerConcurrency;
loaded.value = true;
} catch (error) {
message.error(error instanceof Error ? error.message : "设置没有获取到");
toast.error(error instanceof Error ? error.message : "设置没有获取到");
}
}
@@ -31,9 +32,9 @@ async function save() {
method: "PUT",
body: JSON.stringify({ loginMode: mode.value, credentialRefreshSeconds: seconds.value, workerConcurrency: concurrency.value }),
});
message.success("已保存");
toast.success("已保存");
} catch (error) {
message.error(error instanceof Error ? error.message : "保存失败");
toast.error(error instanceof Error ? error.message : "保存失败");
} finally {
saving.value = false;
}
+7 -10
View File
@@ -1,8 +1,9 @@
<script setup lang="ts">
import { onMounted, ref } from "vue";
import { NAlert, NButton, NInput, NInputNumber, NModal, NSelect, NSwitch, useDialog, useMessage } from "naive-ui";
import { NButton, NInput, NInputNumber, NModal, NSelect, NSwitch, useDialog } from "naive-ui";
import Shell from "../../shell/Shell.vue";
import { api } from "../../shared/http";
import { useToast } from "../../shared/toast";
interface Source {
id: number;
@@ -27,9 +28,8 @@ const username = ref("");
const password = ref("");
const path = ref("");
const url = ref("");
const errorText = ref("");
const dialog = useDialog();
const message = useMessage();
const toast = useToast();
function reset(source?: Source) {
editing.value = source || null;
@@ -44,7 +44,6 @@ function reset(source?: Source) {
password.value = "";
path.value = String(source?.config.path || "");
url.value = String(source?.config.url || "");
errorText.value = "";
show.value = true;
}
@@ -54,7 +53,6 @@ async function load() {
}
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 };
@@ -66,10 +64,10 @@ async function save() {
await api("/api/admin/sources", { method: "POST", body: JSON.stringify(body) });
}
show.value = false;
message.success("已保存");
toast.success("已保存");
await load();
} catch (error) {
errorText.value = error instanceof Error ? error.message : "保存失败";
toast.error(error instanceof Error ? error.message : "保存失败");
return false;
}
}
@@ -77,10 +75,10 @@ async function save() {
async function remove(id: number) {
try {
await api(`/api/admin/sources/${id}`, { method: "DELETE" });
message.success("已删除");
toast.success("已删除");
await load();
} catch (error) {
message.error(error instanceof Error ? error.message : "删除失败");
toast.error(error instanceof Error ? error.message : "删除失败");
}
}
@@ -148,7 +146,6 @@ onMounted(load);
<template v-else>
<div class="form-row"><label>地址</label><n-input v-model:value="url" /></div>
</template>
<n-alert v-if="errorText" type="error" style="margin-bottom: 12px">{{ errorText }}</n-alert>
</n-modal>
</Shell>
</template>
+7 -9
View File
@@ -1,18 +1,18 @@
<script setup lang="ts">
import { onMounted, ref } from "vue";
import { NAlert, NButton, NInput, NModal, NSelect, useDialog, useMessage } from "naive-ui";
import { NButton, NInput, NModal, NSelect, useDialog } from "naive-ui";
import Shell from "../../shell/Shell.vue";
import { api } from "../../shared/http";
import { useToast } from "../../shared/toast";
interface User { id: number; username: string; role: string }
const dialog = useDialog();
const message = useMessage();
const toast = useToast();
const users = ref<User[]>([]);
const show = ref(false);
const username = ref("");
const password = ref("");
const role = ref("user");
const errorText = ref("");
async function load() {
const result = await api<{ users: User[] }>("/api/admin/users");
@@ -20,16 +20,15 @@ async function load() {
}
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 = "";
message.success("已添加");
toast.success("已添加");
await load();
} catch (error) {
errorText.value = error instanceof Error ? error.message : "添加失败";
toast.error(error instanceof Error ? error.message : "添加失败");
return false;
}
}
@@ -37,10 +36,10 @@ async function create() {
async function remove(id: number) {
try {
await api(`/api/admin/users/${id}`, { method: "DELETE" });
message.success("已删除");
toast.success("已删除");
await load();
} catch (error) {
message.error(error instanceof Error ? error.message : "删除失败");
toast.error(error instanceof Error ? error.message : "删除失败");
}
}
@@ -89,7 +88,6 @@ onMounted(load);
<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-select v-model:value="role" :options="[{ label: '用户', value: 'user' }, { label: '管理员', value: 'admin' }]" /></div>
<n-alert v-if="errorText" type="error" style="margin-bottom: 12px">{{ errorText }}</n-alert>
</n-modal>
</Shell>
</template>
+10 -20
View File
@@ -1,14 +1,16 @@
<script setup lang="ts">
import { computed, onMounted, onUnmounted, ref } from "vue";
import { useRoute, useRouter } from "vue-router";
import { NAlert, NButton, NInput, NModal, NSpin } from "naive-ui";
import { NButton, NInput, NModal, NSpin } from "naive-ui";
import { api, saveSession } from "../../shared/http";
import { useToast } from "../../shared/toast";
import ThemeButton from "../../shell/ThemeButton.vue";
const props = defineProps<{ purpose?: "login" | "pool" }>();
const emit = defineEmits<{ done: [] }>();
const route = useRoute();
const router = useRouter();
const toast = useToast();
const mode = ref("local");
const username = ref("");
const password = ref("");
@@ -27,14 +29,15 @@ function markCaptchaBroken(event: Event) {
const sessionId = ref("");
const smsCode = ref("");
const smsRequired = ref(false);
const smsHint = ref("");
const smsSending = ref(false);
const countdown = ref(0);
const errorText = ref("");
const busy = ref(false);
let countdownTimer = 0;
onMounted(async () => {
if (props.purpose !== "pool" && route.query.notice === "already") {
toast.info("这个数据库已经安装过,请使用原来的管理员账号登录。");
}
if (props.purpose === "pool") {
mode.value = "nqi";
await startNqi();
@@ -63,7 +66,6 @@ function submitLogin() {
async function beginLogin() {
busy.value = true;
errorText.value = "";
try {
const result = await api<{ status: string; token?: string; user?: { role: string; name: string }; sessionId?: string; captcha?: string; message?: string }>("/api/auth/login", {
method: "POST",
@@ -82,13 +84,12 @@ async function beginLogin() {
captcha.value = "";
captchaUrl.value = result.captcha || "";
captchaBroken.value = !captchaUrl.value;
errorText.value = result.message || "请重新输入验证码";
toast.error(result.message || "请重新输入验证码");
return;
}
if (result.status === "sms_required") {
smsRequired.value = true;
smsCode.value = "";
smsHint.value = "";
return;
}
if (result.status === "ok" && result.token && result.user) {
@@ -103,13 +104,12 @@ async function beginLogin() {
}
function fail(error: unknown) {
errorText.value = error instanceof Error ? error.message : "登录失败";
toast.error(error instanceof Error ? error.message : "登录失败");
}
async function startNqi() {
captchaLoading.value = true;
captchaBroken.value = false;
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",
@@ -175,23 +175,19 @@ async function backFromSms() {
smsRequired.value = false;
stopCountdown();
smsCode.value = "";
smsHint.value = "";
errorText.value = "";
await refreshCaptcha();
}
async function sendSms() {
if (!sessionId.value || countdown.value > 0 || smsSending.value) return;
smsSending.value = true;
errorText.value = "";
smsHint.value = "";
startCountdown();
try {
const result = await api<{ message?: string }>("/api/auth/nqi/sms", {
method: "POST",
body: JSON.stringify({ sessionId: sessionId.value, username: username.value, password: password.value, captcha: captcha.value }),
});
smsHint.value = result.message || "短信发送已受理";
toast.info(result.message || "短信发送已受理");
} catch (error) {
stopCountdown();
fail(error);
@@ -202,7 +198,6 @@ async function sendSms() {
async function finishNqi() {
busy.value = true;
errorText.value = "";
try {
const result = await api<{ status: string; token?: string; user?: { role: string; name: string }; message?: string; captcha?: string }>("/api/auth/nqi/finish", {
method: "POST",
@@ -221,13 +216,12 @@ async function finishNqi() {
captcha.value = "";
captchaUrl.value = result.captcha || "";
captchaBroken.value = !captchaUrl.value;
errorText.value = result.message || "请重新输入验证码";
toast.error(result.message || "请重新输入验证码");
return;
}
if (result.status === "sms_required") {
smsRequired.value = true;
smsCode.value = "";
smsHint.value = "";
return;
}
if (props.purpose === "pool") {
@@ -252,8 +246,6 @@ async function finishNqi() {
<section :class="purpose === 'pool' ? 'lv-embed' : 'lv-side'">
<div v-if="purpose !== 'pool'" class="lv-brand"><div class="lv-mark" aria-hidden="true">D</div><h1>DecomEval</h1></div>
<form class="lv-fields" @submit.prevent="submitLogin">
<n-alert v-if="route.query.notice === 'already'" type="info">这个数据库已经安装过,请使用原来的管理员账号登录。</n-alert>
<n-alert v-if="errorText" type="error">{{ errorText }}</n-alert>
<label>账号</label>
<n-input v-model:value="username" placeholder="账号" @keyup.enter="submitLogin" />
<label>密码</label>
@@ -272,9 +264,7 @@ async function finishNqi() {
</form>
</section>
<n-modal v-model:show="smsRequired" :mask-closable="false" preset="card" title="短信认证" style="width: 420px">
<n-alert v-if="errorText" type="error" style="margin-bottom: 12px">{{ errorText }}</n-alert>
<n-input v-model:value="smsCode" placeholder="输入短信验证码" @keyup.enter="finishNqi" />
<p v-if="smsHint" class="admin-status">{{ smsHint }}</p>
<div class="sms-actions">
<n-button @click="backFromSms">返回</n-button>
<n-button :disabled="smsSending || countdown > 0" @click="sendSms">{{ countdown > 0 ? `重新发送(${countdown})` : "发送验证码" }}</n-button>
+5 -6
View File
@@ -1,12 +1,14 @@
<script setup lang="ts">
import { onMounted, ref } from "vue";
import { useRouter } from "vue-router";
import { NAlert, NButton, NInput } from "naive-ui";
import { NButton, NInput } from "naive-ui";
import { api } from "../../shared/http";
import { markInstalled } from "../../shared/installState";
import { useToast } from "../../shared/toast";
import ThemeButton from "../../shell/ThemeButton.vue";
const router = useRouter();
const toast = useToast();
const host = ref("127.0.0.1");
const port = ref("5432");
const database = ref("decom");
@@ -14,7 +16,6 @@ const username = ref("postgres");
const password = ref("nixevol@242520");
const adminUsername = ref("admin");
const adminPassword = ref("nixevol@242520");
const errorText = ref("");
const busy = ref(false);
onMounted(async () => {
@@ -34,11 +35,10 @@ function allowPort(value: string) {
async function submit() {
const portNumber = Number(port.value);
if (!port.value || portNumber < 1 || portNumber > 65535) {
errorText.value = "端口需要在 1 到 65535 之间";
toast.error("端口需要在 1 到 65535 之间");
return;
}
busy.value = true;
errorText.value = "";
try {
const result = await api<{ already?: boolean }>("/api/install", {
method: "POST",
@@ -55,7 +55,7 @@ async function submit() {
markInstalled();
router.push(result.already ? { path: "/login", query: { notice: "already" } } : "/login");
} catch (error) {
errorText.value = error instanceof Error ? error.message : "安装失败";
toast.error(error instanceof Error ? error.message : "安装失败");
} finally {
busy.value = false;
}
@@ -70,7 +70,6 @@ async function submit() {
<div class="brand-mark" aria-hidden="true">D</div>
<h1>安装 DecomEval</h1>
</div>
<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="host" />
+7 -9
View File
@@ -1,18 +1,18 @@
<script setup lang="ts">
import { onMounted, ref } from "vue";
import { NAlert, NButton, NInput, NModal, NSelect, useMessage } from "naive-ui";
import { NButton, NInput, NModal, NSelect } from "naive-ui";
import Shell from "../../shell/Shell.vue";
import { api, token } from "../../shared/http";
import { useToast } from "../../shared/toast";
interface Task { id: number; status: string; format: string; cellKey: string; error: string }
const tasks = ref<Task[]>([]);
const message = useMessage();
const toast = useToast();
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" }];
function statusLabel(status: string) {
@@ -28,17 +28,16 @@ async function load() {
}
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;
message.success("已提交");
toast.success("已提交");
await load();
} catch (error) {
errorText.value = error instanceof Error ? error.message : "提交失败";
toast.error(error instanceof Error ? error.message : "提交失败");
}
}
@@ -46,7 +45,7 @@ async function download(row: Task) {
try {
const response = await fetch(`/api/tasks/${row.id}/file`, { headers: { Authorization: `Bearer ${token()}` } });
if (!response.ok) {
message.error("结果还不能下载");
toast.error("结果还不能下载");
return;
}
const blob = await response.blob();
@@ -57,7 +56,7 @@ async function download(row: Task) {
link.click();
URL.revokeObjectURL(url);
} catch {
message.error("下载失败");
toast.error("下载失败");
}
}
@@ -101,7 +100,6 @@ onMounted(load);
<div class="form-row"><label>小区</label><n-input v-model:value="cellKey" placeholder="留空则导出全部" /></div>
<div class="form-row"><label>开始</label><n-input v-model:value="start" placeholder="YYYY-MM-DD" /></div>
<div class="form-row"><label>结束</label><n-input v-model:value="end" placeholder="YYYY-MM-DD" /></div>
<n-alert v-if="errorText" type="error" style="margin-bottom: 12px">{{ errorText }}</n-alert>
</n-modal>
</Shell>
</template>
+27
View File
@@ -0,0 +1,27 @@
import { useMessage } from "naive-ui";
const MIN_MS = 2500;
const MAX_MS = 8000;
const BASE_CHARS = 6;
const EXTRA_MS = 300;
export function toastDuration(text: string): number {
const extra = Math.max(0, Array.from(text).length - BASE_CHARS);
return Math.min(MAX_MS, MIN_MS + extra * EXTRA_MS);
}
export function useToast() {
const message = useMessage();
function show(type: "success" | "error" | "info" | "warning", text: string) {
return message[type](text, { duration: toastDuration(text), closable: true });
}
return {
show,
success: (text: string) => show("success", text),
error: (text: string) => show("error", text),
info: (text: string) => show("info", text),
warning: (text: string) => show("warning", text),
};
}