208 lines
7.7 KiB
Vue
208 lines
7.7 KiB
Vue
<template>
|
|
<div class="page table-page">
|
|
<div class="page-toolbar">
|
|
<n-input v-model:value="query.keyword" :placeholder="t('common.label.keyword')" clearable style="width: 200px" @keyup.enter="onSearch" />
|
|
<n-button type="primary" @click="onSearch">
|
|
<template #icon><n-icon :component="SearchOutline" /></template>
|
|
{{ t('common.action.search') }}
|
|
</n-button>
|
|
<div class="spacer" />
|
|
<n-button type="primary" @click="openCreate">
|
|
<template #icon><n-icon :component="AddOutline" /></template>
|
|
{{ t('pages.user.create') }}
|
|
</n-button>
|
|
</div>
|
|
|
|
<n-data-table
|
|
:columns="columns"
|
|
:data="rows"
|
|
:loading="loading"
|
|
:pagination="false"
|
|
class="table-fill"
|
|
flex-height
|
|
remote
|
|
:row-key="(r) => r.id"
|
|
/>
|
|
<div class="table-pagination">
|
|
<n-pagination v-bind="pagination" :disabled="loading" />
|
|
</div>
|
|
|
|
<n-modal v-model:show="showModal" preset="card" :title="editing ? t('pages.user.edit') : t('pages.user.create')" style="width: 460px">
|
|
<n-form ref="formRef" :model="form" :rules="rules" label-placement="top">
|
|
<n-form-item path="username" :label="t('pages.user.username')">
|
|
<n-input v-model:value="form.username" :disabled="!!editing" />
|
|
</n-form-item>
|
|
<n-form-item v-if="!editing" path="password" :label="t('pages.login.password')">
|
|
<n-input v-model:value="form.password" type="password" show-password-on="click" />
|
|
</n-form-item>
|
|
<n-form-item :label="t('pages.user.realName')">
|
|
<n-input v-model:value="form.realName" />
|
|
</n-form-item>
|
|
<n-form-item :label="t('pages.user.role')">
|
|
<n-select v-model:value="form.role" :options="roleOptions" />
|
|
</n-form-item>
|
|
<n-form-item v-if="editing" :label="t('common.label.status')">
|
|
<n-select v-model:value="form.status" :options="statusOptions" />
|
|
</n-form-item>
|
|
</n-form>
|
|
<template #footer>
|
|
<div style="text-align: right">
|
|
<n-button class="mr8" @click="showModal = false">{{ t('common.action.cancel') }}</n-button>
|
|
<n-button type="primary" :loading="saving" @click="save">{{ t('common.action.save') }}</n-button>
|
|
</div>
|
|
</template>
|
|
</n-modal>
|
|
|
|
<n-modal v-model:show="pwdModal" preset="card" :title="t('pages.user.resetPassword')" style="width: 380px">
|
|
<n-input v-model:value="newPwd" type="password" show-password-on="click" :placeholder="t('pages.user.newPassword')" />
|
|
<template #footer>
|
|
<div style="text-align: right">
|
|
<n-button class="mr8" @click="pwdModal = false">{{ t('common.action.cancel') }}</n-button>
|
|
<n-button type="primary" @click="doReset">{{ t('common.action.confirm') }}</n-button>
|
|
</div>
|
|
</template>
|
|
</n-modal>
|
|
</div>
|
|
</template>
|
|
|
|
<script setup lang="ts">
|
|
import { h, onMounted, reactive, ref } from 'vue';
|
|
import { useI18n } from 'vue-i18n';
|
|
import { NButton, NIcon, NTag, type DataTableColumns, type FormInst, type FormRules } from 'naive-ui';
|
|
import { SearchOutline, AddOutline, CreateOutline, KeyOutline, TrashOutline } from '@vicons/ionicons5';
|
|
import { RoleType, UserStatus } from '@artveris/shared';
|
|
import { userApi } from '@/api';
|
|
import { message, dialog } from '@/utils/feedback';
|
|
import { fmt, statusType } from '@/utils/format';
|
|
import { useTablePage } from '@/composables/useTablePage';
|
|
|
|
const { t } = useI18n();
|
|
const rows = ref<any[]>([]);
|
|
const loading = ref(false);
|
|
const query = reactive({ keyword: '' });
|
|
const { pagination, page, pageSize, resetPage, setPageResult } = useTablePage(load);
|
|
const roleOptions = Object.values(RoleType).map((v) => ({ label: t(`common.role.${v}`), value: v }));
|
|
const statusOptions = Object.values(UserStatus).map((v) => ({ label: t(`common.status.${v}`), value: v }));
|
|
|
|
const showModal = ref(false);
|
|
const editing = ref<number | null>(null);
|
|
const saving = ref(false);
|
|
const formRef = ref<FormInst | null>(null);
|
|
const form = reactive<any>({ username: '', password: '', realName: '', role: RoleType.OPERATOR, status: UserStatus.ACTIVE });
|
|
const rules: FormRules = {
|
|
username: { required: true, message: () => t('pages.user.username'), trigger: 'blur' },
|
|
password: { required: true, message: () => t('pages.login.password'), trigger: 'blur' },
|
|
};
|
|
|
|
const pwdModal = ref(false);
|
|
const newPwd = ref('');
|
|
const pwdUserId = ref<number | null>(null);
|
|
|
|
const columns: DataTableColumns<any> = [
|
|
{ title: 'ID', key: 'id', width: 64 },
|
|
{ title: () => t('pages.user.username'), key: 'username' },
|
|
{ title: () => t('pages.user.realName'), key: 'realName', render: (r) => r.realName || '-' },
|
|
{ title: () => t('pages.user.role'), key: 'role', width: 120, render: (r) => t(`common.role.${r.role}`) },
|
|
{
|
|
title: () => t('common.label.status'),
|
|
key: 'status',
|
|
width: 90,
|
|
render: (r) => h(NTag, { size: 'small', type: statusType(r.status) }, { default: () => t(`common.status.${r.status}`) }),
|
|
},
|
|
{ title: () => t('pages.user.lastLoginAt'), key: 'lastLoginAt', width: 160, render: (r) => fmt(r.lastLoginAt) },
|
|
{
|
|
title: () => t('common.label.operation'),
|
|
key: 'ops',
|
|
width: 140,
|
|
render: (r) =>
|
|
h('div', { style: 'display:flex;gap:2px' }, [
|
|
h(NButton, { quaternary: true, circle: true, size: 'small', onClick: () => openEdit(r) }, { icon: () => h(NIcon, null, { default: () => h(CreateOutline) }) }),
|
|
h(NButton, { quaternary: true, circle: true, size: 'small', onClick: () => openReset(r) }, { icon: () => h(NIcon, null, { default: () => h(KeyOutline) }) }),
|
|
h(NButton, { quaternary: true, circle: true, size: 'small', type: 'error', onClick: () => remove(r) }, { icon: () => h(NIcon, null, { default: () => h(TrashOutline) }) }),
|
|
]),
|
|
},
|
|
];
|
|
|
|
async function load() {
|
|
loading.value = true;
|
|
try {
|
|
const res: any = await userApi.list({ keyword: query.keyword, page: page.value, pageSize: pageSize.value });
|
|
rows.value = res.list;
|
|
setPageResult(res);
|
|
} finally {
|
|
loading.value = false;
|
|
}
|
|
}
|
|
function onSearch() {
|
|
resetPage();
|
|
load();
|
|
}
|
|
|
|
function openCreate() {
|
|
editing.value = null;
|
|
Object.assign(form, { username: '', password: '', realName: '', role: RoleType.OPERATOR, status: UserStatus.ACTIVE });
|
|
showModal.value = true;
|
|
}
|
|
function openEdit(r: any) {
|
|
editing.value = r.id;
|
|
Object.assign(form, { username: r.username, realName: r.realName || '', role: r.role, status: r.status });
|
|
showModal.value = true;
|
|
}
|
|
async function save() {
|
|
try {
|
|
await formRef.value?.validate();
|
|
} catch {
|
|
return;
|
|
}
|
|
saving.value = true;
|
|
try {
|
|
if (editing.value) {
|
|
await userApi.update(editing.value, { realName: form.realName, role: form.role, status: form.status });
|
|
message.success(t('common.msg.updated'));
|
|
} else {
|
|
await userApi.create({ username: form.username, password: form.password, realName: form.realName, role: form.role });
|
|
message.success(t('common.msg.created'));
|
|
}
|
|
showModal.value = false;
|
|
load();
|
|
} finally {
|
|
saving.value = false;
|
|
}
|
|
}
|
|
function openReset(r: any) {
|
|
pwdUserId.value = r.id;
|
|
newPwd.value = '';
|
|
pwdModal.value = true;
|
|
}
|
|
async function doReset() {
|
|
if (!pwdUserId.value || newPwd.value.length < 6) {
|
|
message.error(t('pages.user.newPassword'));
|
|
return;
|
|
}
|
|
await userApi.resetPassword(pwdUserId.value, newPwd.value);
|
|
message.success(t('common.msg.updated'));
|
|
pwdModal.value = false;
|
|
}
|
|
function remove(r: any) {
|
|
dialog.warning({
|
|
title: t('common.action.delete'),
|
|
content: t('common.msg.confirmDelete'),
|
|
positiveText: t('common.action.confirm'),
|
|
negativeText: t('common.action.cancel'),
|
|
onPositiveClick: async () => {
|
|
await userApi.remove(r.id);
|
|
message.success(t('common.msg.deleted'));
|
|
load();
|
|
},
|
|
});
|
|
}
|
|
|
|
onMounted(load);
|
|
</script>
|
|
|
|
<style scoped>
|
|
.mr8 {
|
|
margin-right: 8px;
|
|
}
|
|
</style>
|