Files
kefu/admin-web/apps/web-antd/src/views/console/users.vue
T
2026-08-27 14:04:28 +08:00

121 lines
4.0 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 type { RoleItem, UserItem } from '#/api/console';
import { computed, onMounted, reactive, ref } from 'vue';
import { useAccess } from '@vben/access';
import { Button, Card, Form, FormItem, Input, message, Modal, Select, Switch, Table, Tag } from 'ant-design-vue';
import { createUser, fetchRoles, fetchUsers, updateUser } from '#/api/console';
const { hasAccessByCodes } = useAccess();
const canWrite = computed(() => hasAccessByCodes(['user:write']));
const loading = ref(false);
const users = ref<UserItem[]>([]);
const roles = ref<RoleItem[]>([]);
const open = ref(false);
const form = reactive({ username: '', password: '', role: 'viewer' });
const roleOptions = computed(() =>
roles.value.map((item) => ({ value: item.code, label: `${item.name}(${item.code})` })),
);
async function load() {
loading.value = true;
try {
users.value = (await fetchUsers()).users;
roles.value = (await fetchRoles()).roles;
} finally {
loading.value = false;
}
}
function openCreate() {
Object.assign(form, { username: '', password: '', role: 'viewer' });
open.value = true;
}
async function submit() {
if (!form.username || !form.password) {
message.warning('用户名和密码不能为空');
return;
}
users.value = (await createUser({ ...form })).users;
open.value = false;
message.success('已创建,该用户首次登录必须先改密码');
}
async function changeRole(row: UserItem, role: string) {
users.value = (await updateUser(row.id, { role, active: row.active })).users;
message.success('角色已更新,立即生效');
}
async function toggleActive(row: UserItem, active: boolean) {
users.value = (await updateUser(row.id, { role: row.role, active })).users;
message.success(active ? '已启用' : '已停用');
}
const columns = [
{ title: '用户名', dataIndex: 'username', key: 'username' },
{ title: '角色', dataIndex: 'role', key: 'role', width: 220 },
{ title: '状态', dataIndex: 'active', key: 'active', width: 120 },
{ title: '创建时间', dataIndex: 'created_at', key: 'created_at', width: 180 },
];
onMounted(load);
</script>
<template>
<div class="p-5">
<Card title="用户管理">
<template #extra>
<Button v-if="canWrite" type="primary" @click="openCreate">新增用户</Button>
</template>
<Table
:columns="columns" :data-source="users" :loading="loading"
row-key="id" size="small" :pagination="false"
>
<template #bodyCell="{ column, record }">
<template v-if="column.key === 'username'">
{{ record.username }}
<Tag v-if="record.must_change_password" color="orange" class="ml-2">
待改密
</Tag>
</template>
<template v-else-if="column.key === 'role'">
<Select
:value="record.role" :options="roleOptions" :disabled="!canWrite"
size="small" class="w-full"
@change="(value) => changeRole(record as UserItem, String(value))"
/>
</template>
<template v-else-if="column.key === 'active'">
<Switch
:checked="record.active" :disabled="!canWrite" size="small"
@change="(value) => toggleActive(record as UserItem, Boolean(value))"
/>
<span class="ml-2 text-xs">{{ record.active ? '已启用' : '已停用' }}</span>
</template>
</template>
</Table>
</Card>
<Modal v-model:open="open" title="新增用户" @ok="submit">
<Form :model="form" layout="vertical" class="pt-2">
<FormItem label="用户名">
<Input v-model:value="form.username" />
</FormItem>
<FormItem label="初始密码(至少 10 位,需同时含字母和数字)">
<Input.Password v-model:value="form.password" />
</FormItem>
<FormItem label="角色">
<Select v-model:value="form.role" :options="roleOptions" />
</FormItem>
</Form>
</Modal>
</div>
</template>