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

100 lines
3.5 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, 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 toast = useToast();
async function load() {
const result = await api<{ accounts: Account[] }>("/api/admin/pool");
accounts.value = result.accounts;
}
async function setRole(id: number, role: string) {
try {
await api(`/api/admin/pool/${id}/role`, { method: "PUT", body: JSON.stringify({ role }) });
toast.success("已保存");
await load();
} catch (error) {
toast.error(error instanceof Error ? error.message : "保存失败");
}
}
async function remove(id: number) {
try {
await api(`/api/admin/pool/${id}`, { method: "DELETE" });
toast.success("已删除");
await load();
} catch (error) {
toast.error(error instanceof Error ? error.message : "删除失败");
}
}
function confirmRemove(account: Account) {
dialog.warning({
title: "删除账号",
content: `从账号池删除 ${account.loginId}?`,
positiveText: "删除",
negativeText: "取消",
positiveButtonProps: { type: "error" },
onPositiveClick: () => remove(account.id),
});
}
onMounted(load);
</script>
<template>
<Shell>
<section class="page admin-page">
<header class="page-head">
<h2>账号池</h2>
<n-button type="primary" @click="show = true">添加账号</n-button>
</header>
<div class="admin-scroll">
<div v-if="accounts.length" class="surface table-card">
<table class="admin-table">
<thead>
<tr>
<th>账号</th>
<th>姓名</th>
<th>地市</th>
<th>凭证</th>
<th>登录</th>
<th class="col-pick">角色</th>
<th class="col-actions">操作</th>
</tr>
</thead>
<tbody>
<tr v-for="account in accounts" :key="account.id">
<td>{{ account.loginId }}</td>
<td>{{ account.userName }}</td>
<td>{{ account.cityName }}</td>
<td :class="account.credentialValid ? 'status-ok' : 'status-bad'">{{ account.credentialValid ? "有效" : "失效" }}</td>
<td :class="account.sessionOpen ? 'status-run' : 'status-wait'">{{ account.sessionOpen ? "未退出" : "已退出" }}</td>
<td class="col-pick">
<n-select :value="account.role" size="small" :options="[{ label: '用户', value: 'user' }, { label: '管理员', value: 'admin' }]" @update:value="(value) => setRole(account.id, value)" />
</td>
<td class="col-actions"><n-button size="small" quaternary @click="confirmRemove(account)">删除</n-button></td>
</tr>
</tbody>
</table>
</div>
<div v-else class="surface empty-card">
<p class="admin-status">没有账号</p>
</div>
</div>
</section>
<n-modal v-model:show="show" class="app-dialog" preset="card" title="添加账号">
<LoginView purpose="pool" @done="show = false; load()" />
</n-modal>
</Shell>
</template>