307 lines
10 KiB
Vue
307 lines
10 KiB
Vue
/**
|
|
* features/admin/modules/users/UsersPage.vue
|
|
* 用途:用户管理独立页面(服务端筛选分页、用户/部门列排序、批量操作)。
|
|
* 依赖:useUsersAdmin、useElementProvider、element-plus、用户相关 Modal、adminRoleLabel、getUserAvatarLabel。
|
|
* 逻辑:路由 /admin/users。列表骨架用 AdminListPage;筛选、表格、分页和三个业务弹窗仍由本页组装。
|
|
* 删除和改角色用 Element 确认框。列表经 listAdminUsersPage 按页拉取。
|
|
*/
|
|
<script setup lang="ts">
|
|
import { computed, onMounted } from 'vue';
|
|
import { useI18n } from 'vue-i18n';
|
|
import { Delete, Edit, Plus, User } from '@element-plus/icons-vue';
|
|
import {
|
|
ElButton,
|
|
ElConfigProvider,
|
|
ElInput,
|
|
ElLoading,
|
|
ElOption,
|
|
ElPagination,
|
|
ElSelect,
|
|
ElSpace,
|
|
ElTable,
|
|
ElTableColumn,
|
|
ElTag,
|
|
} from 'element-plus';
|
|
import 'element-plus/dist/index.css';
|
|
import 'element-plus/theme-chalk/dark/css-vars.css';
|
|
import '@/features/admin/styles/element-trial.css';
|
|
import { adminRoleLabel } from '@/features/admin/utils/roleLabel';
|
|
import { getUserAvatarLabel } from '@/shared/utils/user-avatar';
|
|
import type { AdminUserRow } from '@/features/admin/types/admin.types';
|
|
import { useElementProvider } from '@/features/admin/composables/useElementProvider';
|
|
import AdminListPage from '@/features/admin/components/AdminListPage.vue';
|
|
import BatchEditUserModal from './components/BatchEditUserModal.vue';
|
|
import BatchPermissionModal from './components/BatchPermissionModal.vue';
|
|
import UserFormModal from './components/UserFormModal.vue';
|
|
import { useUsersAdmin } from './composables/useUsersAdmin';
|
|
|
|
const vLoading = ElLoading.directive;
|
|
|
|
const { t } = useI18n();
|
|
const { elementLocale } = useElementProvider();
|
|
|
|
const {
|
|
agents,
|
|
departments,
|
|
roles,
|
|
rolesConfigured,
|
|
isLoading,
|
|
isListLoading,
|
|
isSaving,
|
|
userFilters,
|
|
sortKey,
|
|
sortDir,
|
|
selectedUserIds,
|
|
users,
|
|
usersTotal,
|
|
userPage,
|
|
userPageSize,
|
|
userPageSizeOptions,
|
|
showUserModal,
|
|
editingUser,
|
|
showBatchEditModal,
|
|
batchEditForm,
|
|
showBatchPermissionModal,
|
|
batchPermissionForm,
|
|
loadAll,
|
|
applyServerSort,
|
|
openCreateUserModal,
|
|
openEditUserModal,
|
|
saveUser,
|
|
batchDeleteUsers,
|
|
openBatchEditModal,
|
|
openBatchPermissionModal,
|
|
submitBatchEdit,
|
|
submitBatchPermission,
|
|
} = useUsersAdmin();
|
|
|
|
const pageSizes = computed(() => [...userPageSizeOptions]);
|
|
|
|
const defaultSort = computed(() => ({
|
|
prop: sortKey.value || 'username',
|
|
order: sortDir.value === 'desc' ? 'descending' as const : 'ascending' as const,
|
|
}));
|
|
|
|
/**
|
|
* 把 Element 表格的勾选结果写回已选用户 id。
|
|
* @param rows - 当前保留的选中行
|
|
*/
|
|
function onSelectionChange(rows: AdminUserRow[]): void {
|
|
selectedUserIds.value = rows.map((row) => row.id);
|
|
}
|
|
|
|
/**
|
|
* 部门筛选。清空时写回空字符串,列表接口仍按「全部部门」查询。
|
|
* @param value - 选中的部门名;清空为 undefined
|
|
*/
|
|
function onDepartmentChange(value: string | undefined): void {
|
|
userFilters.value.department = value ?? '';
|
|
}
|
|
|
|
/**
|
|
* 把 Element 表头排序交给服务端查询。
|
|
* 核心逻辑:取消排序时保持当前方向;有效排序走 applyServerSort 并回到第一页。
|
|
* @param payload.prop - 列字段
|
|
* @param payload.order - ascending、descending 或空
|
|
*/
|
|
function onSortChange(payload: { prop: string | null; order: string | null }): void {
|
|
const field = payload.prop;
|
|
if ((field === 'username' || field === 'department') && payload.order) {
|
|
applyServerSort(field, payload.order === 'descending' ? 'desc' : 'asc');
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 打开编辑弹窗。表格插槽行类型较宽,这里收成用户行。
|
|
* @param row - 当前表格行
|
|
*/
|
|
function editUser(row: AdminUserRow): void {
|
|
openEditUserModal(row);
|
|
}
|
|
|
|
onMounted(() => {
|
|
void loadAll();
|
|
});
|
|
</script>
|
|
|
|
<template>
|
|
<ElConfigProvider :locale="elementLocale">
|
|
<AdminListPage :loading="isLoading">
|
|
<template #title>
|
|
<h2 class="section-title">{{ t('admin.user.title') }}</h2>
|
|
<p class="section-desc mt-1">{{ t('admin.user.subtitle') }}</p>
|
|
</template>
|
|
<template #actions>
|
|
<ElSpace wrap>
|
|
<ElButton
|
|
v-if="selectedUserIds.length > 0"
|
|
type="danger"
|
|
:icon="Delete"
|
|
@click="batchDeleteUsers"
|
|
>
|
|
{{ t('admin.user.batchDelete') }} ({{ selectedUserIds.length }})
|
|
</ElButton>
|
|
<ElButton v-if="selectedUserIds.length > 0" :icon="Edit" @click="openBatchEditModal">
|
|
{{ t('admin.user.batchEdit') }} ({{ selectedUserIds.length }})
|
|
</ElButton>
|
|
<ElButton
|
|
v-if="selectedUserIds.length > 0 && !rolesConfigured"
|
|
:icon="User"
|
|
@click="openBatchPermissionModal"
|
|
>
|
|
{{ t('admin.user.batchPermission') }} ({{ selectedUserIds.length }})
|
|
</ElButton>
|
|
<ElButton type="primary" :icon="Plus" @click="openCreateUserModal">
|
|
{{ t('admin.user.addUser') }}
|
|
</ElButton>
|
|
</ElSpace>
|
|
</template>
|
|
<template #filters>
|
|
<ElInput
|
|
v-model="userFilters.username"
|
|
clearable
|
|
class="users-filter-username"
|
|
:placeholder="t('admin.user.filterUsername')"
|
|
/>
|
|
<ElInput
|
|
v-model="userFilters.real_name"
|
|
clearable
|
|
class="users-filter-name"
|
|
:placeholder="t('admin.user.filterRealName')"
|
|
/>
|
|
<ElSelect
|
|
:model-value="userFilters.department || undefined"
|
|
class="users-filter-dept"
|
|
clearable
|
|
:placeholder="t('admin.user.allDepts')"
|
|
@update:model-value="onDepartmentChange"
|
|
>
|
|
<ElOption v-for="department in departments" :key="department.id" :label="department.name" :value="department.name" />
|
|
</ElSelect>
|
|
</template>
|
|
|
|
<ElTable
|
|
v-loading="isListLoading"
|
|
class="hx-admin-table"
|
|
:data="users"
|
|
row-key="id"
|
|
:default-sort="defaultSort"
|
|
:empty-text="t('admin.user.noUsers')"
|
|
@selection-change="onSelectionChange"
|
|
@sort-change="onSortChange"
|
|
>
|
|
<ElTableColumn type="selection" width="64" reserve-selection />
|
|
<ElTableColumn prop="username" :label="t('admin.user.headers.user')" sortable="custom" min-width="180">
|
|
<template #default="{ row }">
|
|
<span class="inline-flex items-center gap-3">
|
|
<span class="users-avatar">
|
|
{{ getUserAvatarLabel(row) }}
|
|
</span>
|
|
<span>{{ row.username }}</span>
|
|
</span>
|
|
</template>
|
|
</ElTableColumn>
|
|
<ElTableColumn prop="real_name" :label="t('admin.user.fields.realName')" min-width="120">
|
|
<template #default="{ row }">{{ row.real_name || '-' }}</template>
|
|
</ElTableColumn>
|
|
<ElTableColumn prop="department" :label="t('admin.user.headers.dept')" sortable="custom" min-width="140">
|
|
<template #default="{ row }">{{ row.department || '-' }}</template>
|
|
</ElTableColumn>
|
|
<ElTableColumn :label="t('admin.user.headers.role')" min-width="180">
|
|
<template #default="{ row }">
|
|
<ElTag :type="row.is_platform_admin ? 'warning' : 'primary'" effect="light">
|
|
{{ adminRoleLabel(t, row.role, row.role_name) }}
|
|
</ElTag>
|
|
<div
|
|
v-if="row.feature_permissions?.training_hr_manage || row.feature_permissions?.['maintenance.ledger_write']"
|
|
class="mt-2 flex flex-wrap gap-1.5"
|
|
>
|
|
<ElTag v-if="row.feature_permissions?.training_hr_manage" size="small">
|
|
{{ t('admin.user.fields.trainingHrManage') }}
|
|
</ElTag>
|
|
<ElTag v-if="row.feature_permissions?.['maintenance.ledger_write']" size="small">
|
|
{{ t('admin.permRoles.features.maintenance_ledger_write') }}
|
|
</ElTag>
|
|
</div>
|
|
</template>
|
|
</ElTableColumn>
|
|
<ElTableColumn :label="t('admin.user.headers.actions')" width="100" align="right">
|
|
<template #default="{ row }">
|
|
<ElButton link type="primary" @click="editUser(row as AdminUserRow)">{{ t('admin.edit') }}</ElButton>
|
|
</template>
|
|
</ElTableColumn>
|
|
</ElTable>
|
|
|
|
<template #pagination>
|
|
<ElPagination
|
|
v-model:current-page="userPage"
|
|
v-model:page-size="userPageSize"
|
|
:total="usersTotal"
|
|
:page-sizes="pageSizes"
|
|
layout="total, sizes, prev, pager, next"
|
|
background
|
|
/>
|
|
</template>
|
|
</AdminListPage>
|
|
|
|
<UserFormModal
|
|
:visible="showUserModal"
|
|
:form="editingUser"
|
|
:agents="agents"
|
|
:departments="departments"
|
|
:roles="roles"
|
|
:roles-configured="rolesConfigured"
|
|
:is-saving="isSaving"
|
|
@close="showUserModal = false"
|
|
@save="saveUser"
|
|
/>
|
|
|
|
<BatchEditUserModal
|
|
:visible="showBatchEditModal"
|
|
:form="batchEditForm"
|
|
:departments="departments"
|
|
:roles="roles"
|
|
:roles-configured="rolesConfigured"
|
|
:selected-count="selectedUserIds.length"
|
|
:is-saving="isSaving"
|
|
@close="showBatchEditModal = false"
|
|
@save="submitBatchEdit"
|
|
/>
|
|
|
|
<BatchPermissionModal
|
|
:visible="showBatchPermissionModal"
|
|
:form="batchPermissionForm"
|
|
:agents="agents"
|
|
:selected-count="selectedUserIds.length"
|
|
:is-saving="isSaving"
|
|
@close="showBatchPermissionModal = false"
|
|
@save="submitBatchPermission"
|
|
/>
|
|
</ElConfigProvider>
|
|
</template>
|
|
|
|
<style scoped>
|
|
:deep(.users-filter-username) {
|
|
width: 10rem;
|
|
}
|
|
|
|
:deep(.users-filter-name),
|
|
:deep(.users-filter-dept) {
|
|
width: 11rem;
|
|
}
|
|
|
|
.users-avatar {
|
|
display: flex;
|
|
height: 2.25rem;
|
|
width: 2.25rem;
|
|
flex: none;
|
|
align-items: center;
|
|
justify-content: center;
|
|
border-radius: 9999px;
|
|
background-image: linear-gradient(to bottom right, #0ea5e9, #4f46e5);
|
|
font-size: var(--el-font-size-extra-small);
|
|
font-weight: 700;
|
|
color: #fff;
|
|
}
|
|
</style>
|