feat(admin): 部门管理改造为组织树形结构
This commit is contained in:
1 parent
c5b7cc662b
commit
48fe19ed71
21 files changed
+1086
-234
No files matched your search
@@ -1,40 +1,56 @@
|
||||
/**
|
||||
* features/admin/modules/departments/DepartmentsPage.vue
|
||||
* 用途:部门管理独立页面——全宽表格列表 + 列排序 + 创建/编辑居中弹窗。
|
||||
* 依赖:useDepartmentsAdmin、SortableTableHeader、DepartmentFormModal、DeleteConfirmDialog。
|
||||
* 逻辑:路由 /admin/departments;表头排序复用 shared SortableTableHeader + useTableSort。
|
||||
* 用途:部门管理独立页面——树形表格(公司虚拟分组 + 部门层级缩进展开)+ 搜索 + 创建/编辑弹窗。
|
||||
* 依赖:useDepartmentsAdmin、DepartmentFormModal、DepartmentMembersModal、DeleteConfirmDialog。
|
||||
* 逻辑:路由 /admin/departments;数据来自 /admin/departments/tree;公司层为前端按 company 字段虚拟分组。
|
||||
*/
|
||||
<script setup lang="ts">
|
||||
import { onMounted, ref } from 'vue';
|
||||
import { useI18n } from 'vue-i18n';
|
||||
import { Edit, Plus, Search, Trash2, Users } from 'lucide-vue-next';
|
||||
import {
|
||||
Building2,
|
||||
ChevronDown,
|
||||
ChevronRight,
|
||||
Edit,
|
||||
EyeOff,
|
||||
FolderPlus,
|
||||
ListTree,
|
||||
Plus,
|
||||
Search,
|
||||
Trash2,
|
||||
Users,
|
||||
} from 'lucide-vue-next';
|
||||
import DeleteConfirmDialog from '@/shared/components/DeleteConfirmDialog.vue';
|
||||
import SortableTableHeader from '@/shared/components/SortableTableHeader.vue';
|
||||
import DepartmentFormModal from './components/DepartmentFormModal.vue';
|
||||
import DepartmentMembersModal from './components/DepartmentMembersModal.vue';
|
||||
import { useDepartmentsAdmin } from './composables/useDepartmentsAdmin';
|
||||
import { listAdminDepartmentMembers } from '@/features/admin/api/admin.api';
|
||||
import type { AdminDepartmentMembersResponse } from '@/features/admin/types/admin.types';
|
||||
import type { AdminDepartmentMembersResponse, AdminDepartmentTreeNode } from '@/features/admin/types/admin.types';
|
||||
import { useMessage } from '@/shared/composables/useMessage';
|
||||
|
||||
const { t } = useI18n();
|
||||
const msg = useMessage();
|
||||
const showMembersModal = ref(false);
|
||||
const departmentMembers = ref<AdminDepartmentMembersResponse | null>(null);
|
||||
/** 成员弹窗当前部门 id 与「含下级」开关 */
|
||||
const membersDeptId = ref<number | null>(null);
|
||||
const includeDescendants = ref(false);
|
||||
|
||||
const {
|
||||
filteredDepartments,
|
||||
displayRows,
|
||||
parentOptions,
|
||||
form,
|
||||
searchQuery,
|
||||
sortKey,
|
||||
sortDir,
|
||||
isLoading,
|
||||
isSaving,
|
||||
showFormModal,
|
||||
showDeleteConfirmDialog,
|
||||
deleteConfirmMessage,
|
||||
loadAll,
|
||||
toggleSort,
|
||||
toggleNode,
|
||||
toggleCompany,
|
||||
expandAll,
|
||||
collapseAll,
|
||||
openCreateModal,
|
||||
openEditModal,
|
||||
closeFormModal,
|
||||
@@ -48,15 +64,29 @@ onMounted(() => {
|
||||
void loadAll();
|
||||
});
|
||||
|
||||
/** 加载并展示指定部门的员工名单。 */
|
||||
const openDepartmentMembers = async (departmentId: number): Promise<void> => {
|
||||
/** 加载并展示指定部门的员工名单(includeDescendants 控制是否含下级)。 */
|
||||
const openDepartmentMembers = async (departmentId: number, include = false): Promise<void> => {
|
||||
try {
|
||||
departmentMembers.value = await listAdminDepartmentMembers(departmentId) as AdminDepartmentMembersResponse;
|
||||
membersDeptId.value = departmentId;
|
||||
includeDescendants.value = include;
|
||||
departmentMembers.value = await listAdminDepartmentMembers(
|
||||
departmentId,
|
||||
include,
|
||||
) as AdminDepartmentMembersResponse;
|
||||
showMembersModal.value = true;
|
||||
} catch (error) {
|
||||
msg.error((error as Error).message || t('admin.msg.requestFailed'));
|
||||
}
|
||||
};
|
||||
|
||||
/** 切换成员弹窗「含下级部门」口径并重新拉取名单。 */
|
||||
const toggleIncludeDescendants = (value: boolean): void => {
|
||||
if (membersDeptId.value == null) return;
|
||||
void openDepartmentMembers(membersDeptId.value, value);
|
||||
};
|
||||
|
||||
/** 公司虚拟分组行 key 去前缀,得到分组 key(用于展开状态)。 */
|
||||
const companyGroupKey = (rowKey: string): string => rowKey.slice('company:'.length);
|
||||
</script>
|
||||
|
||||
<template>
|
||||
@@ -74,7 +104,7 @@ const openDepartmentMembers = async (departmentId: number): Promise<void> => {
|
||||
<button
|
||||
type="button"
|
||||
class="flex items-center gap-2 px-4 py-2 bg-brand-600 text-white rounded-xl hover:bg-brand-700 transition-all shadow-lg shadow-brand-200 active:scale-95 text-sm font-medium"
|
||||
@click="openCreateModal"
|
||||
@click="openCreateModal()"
|
||||
>
|
||||
<Plus class="w-4 h-4" />
|
||||
<span>{{ t('admin.dept.add') }}</span>
|
||||
@@ -91,103 +121,159 @@ const openDepartmentMembers = async (departmentId: number): Promise<void> => {
|
||||
:placeholder="t('admin.dept.searchPlaceholder')"
|
||||
/>
|
||||
</div>
|
||||
<div class="flex items-center gap-1">
|
||||
<button
|
||||
type="button"
|
||||
class="p-2 text-muted hover:text-primary hover:bg-subtle rounded-lg transition-colors"
|
||||
:title="t('admin.dept.expandAll')"
|
||||
@click="expandAll"
|
||||
><ListTree class="w-4 h-4" /></button>
|
||||
<button
|
||||
type="button"
|
||||
class="p-2 text-muted hover:text-primary hover:bg-subtle rounded-lg transition-colors"
|
||||
:title="t('admin.dept.collapseAll')"
|
||||
@click="collapseAll"
|
||||
><ChevronDown class="w-4 h-4" /></button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="admin-table-wrap">
|
||||
<!-- 列宽:名称 / 编码 / 公司 / 员工 / 操作 -->
|
||||
<!-- 列宽:名称(树)/ 编码 / 公司 / 员工 / 操作 -->
|
||||
<table class="admin-table table-fixed">
|
||||
<colgroup>
|
||||
<col class="w-[25%]" />
|
||||
<col class="w-[20%]" />
|
||||
<col class="w-[30%]" />
|
||||
<col class="w-[40%]" />
|
||||
<col class="w-[14%]" />
|
||||
<col class="w-[19%]" />
|
||||
<col class="w-[12%]" />
|
||||
<col class="w-[13%]" />
|
||||
<col class="w-[15%]" />
|
||||
</colgroup>
|
||||
<thead>
|
||||
<tr>
|
||||
<th class="px-6 py-4 text-left text-xs">
|
||||
<SortableTableHeader
|
||||
column-key="name"
|
||||
:label="t('admin.dept.headers.name')"
|
||||
:active-key="sortKey"
|
||||
:direction="sortDir"
|
||||
@sort="toggleSort"
|
||||
/>
|
||||
</th>
|
||||
<th class="px-6 py-4 text-left text-xs">
|
||||
<SortableTableHeader
|
||||
column-key="code"
|
||||
:label="t('admin.dept.headers.code')"
|
||||
:active-key="sortKey"
|
||||
:direction="sortDir"
|
||||
@sort="toggleSort"
|
||||
/>
|
||||
</th>
|
||||
<th class="px-6 py-4 text-left text-xs">
|
||||
<SortableTableHeader
|
||||
column-key="company"
|
||||
:label="t('admin.dept.headers.company')"
|
||||
:active-key="sortKey"
|
||||
:direction="sortDir"
|
||||
@sort="toggleSort"
|
||||
/>
|
||||
</th>
|
||||
<th class="px-6 py-4 text-left text-xs">{{ t('admin.dept.headers.name') }}</th>
|
||||
<th class="px-6 py-4 text-left text-xs">{{ t('admin.dept.headers.code') }}</th>
|
||||
<th class="px-6 py-4 text-left text-xs">{{ t('admin.dept.headers.company') }}</th>
|
||||
<th class="px-6 py-4 text-center text-xs">{{ t('admin.dept.headers.members') }}</th>
|
||||
<th class="px-6 py-4 text-right text-xs">{{ t('admin.dept.headers.actions') }}</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<tr v-if="filteredDepartments.length === 0">
|
||||
<tr v-if="displayRows.length === 0">
|
||||
<td colspan="5" class="px-6 admin-table-empty">
|
||||
{{ searchQuery.trim() ? t('admin.dept.noSearchResults') : t('admin.dept.noDepts') }}
|
||||
</td>
|
||||
</tr>
|
||||
<tr v-for="d in filteredDepartments" :key="d.id" class="group">
|
||||
<td class="px-6 py-4">
|
||||
<div class="font-medium text-primary text-sm truncate" :title="d.name">{{ d.name }}</div>
|
||||
<div
|
||||
v-if="d.description"
|
||||
class="text-xs text-muted truncate mt-0.5"
|
||||
:title="d.description"
|
||||
>
|
||||
{{ d.description }}
|
||||
</div>
|
||||
</td>
|
||||
<td class="px-6 py-4 text-sm text-muted font-mono whitespace-nowrap truncate" :title="d.code || '-'">
|
||||
{{ d.code || '-' }}
|
||||
</td>
|
||||
<td class="px-6 py-4 text-sm text-muted truncate" :title="d.company || '-'">
|
||||
{{ d.company || '-' }}
|
||||
</td>
|
||||
<td class="px-6 py-4 text-center">
|
||||
<button
|
||||
type="button"
|
||||
class="inline-flex items-center justify-center rounded-lg bg-blue-50 p-2 text-blue-600 hover:bg-blue-100"
|
||||
:title="t('admin.dept.actions.viewMembers')"
|
||||
@click="openDepartmentMembers(d.id)"
|
||||
><Users class="w-5 h-5" /><span class="ml-1 text-xs">{{ d.member_count || 0 }}</span></button>
|
||||
</td>
|
||||
<td class="px-6 py-4">
|
||||
<div class="flex justify-end gap-1">
|
||||
<template v-for="row in displayRows" :key="row.key">
|
||||
<!-- 公司虚拟分组行(不入库,前端按 company 字段归堆) -->
|
||||
<tr v-if="row.kind === 'company'" class="bg-subtle/40 cursor-pointer select-none" @click="toggleCompany(companyGroupKey(row.key))">
|
||||
<td class="px-6 py-3">
|
||||
<div class="flex items-center gap-2">
|
||||
<component
|
||||
:is="row.expanded ? ChevronDown : ChevronRight"
|
||||
class="w-4 h-4 text-muted shrink-0"
|
||||
/>
|
||||
<Building2 class="w-4 h-4 text-brand-600 shrink-0" />
|
||||
<span class="font-semibold text-primary text-sm truncate" :title="row.company">{{ row.company }}</span>
|
||||
</div>
|
||||
</td>
|
||||
<td class="px-6 py-3 text-xs text-muted">—</td>
|
||||
<td class="px-6 py-3 text-xs text-muted">
|
||||
<span class="inline-flex items-center px-2 py-0.5 rounded-full bg-brand-50 text-brand-700">{{ t('admin.dept.headers.company') }}</span>
|
||||
</td>
|
||||
<td class="px-6 py-3 text-center">
|
||||
<span class="inline-flex items-center text-sm text-muted" :title="t('admin.dept.members.countTitle', { count: row.totalMembers })">
|
||||
<Users class="w-4 h-4 mr-1" />{{ row.totalMembers }}
|
||||
</span>
|
||||
</td>
|
||||
<td class="px-6 py-3">
|
||||
<div class="flex justify-end">
|
||||
<button
|
||||
type="button"
|
||||
class="p-1.5 text-indigo-600 hover:bg-indigo-50 rounded-lg transition-colors"
|
||||
:title="t('admin.dept.addChild')"
|
||||
@click.stop="openCreateModal(null, row.company)"
|
||||
>
|
||||
<FolderPlus class="w-4 h-4" />
|
||||
</button>
|
||||
</div>
|
||||
</td>
|
||||
</tr>
|
||||
<!-- 部门行(层级缩进) -->
|
||||
<tr v-else class="group">
|
||||
<td class="px-6 py-4">
|
||||
<div
|
||||
class="flex items-center gap-2 min-w-0"
|
||||
:style="{ paddingLeft: `${(row.depth - 1) * 20}px` }"
|
||||
>
|
||||
<button
|
||||
v-if="row.hasChildren"
|
||||
type="button"
|
||||
class="p-0.5 text-muted hover:text-primary rounded transition-colors shrink-0"
|
||||
:title="row.expanded ? t('admin.dept.collapse') : t('admin.dept.expand')"
|
||||
@click="toggleNode(row.node!.id)"
|
||||
>
|
||||
<component :is="row.expanded ? ChevronDown : ChevronRight" class="w-4 h-4" />
|
||||
</button>
|
||||
<span v-else class="w-5 h-5 shrink-0 inline-block rounded-full border border-dashed border-subtle" />
|
||||
<span class="font-medium text-primary text-sm truncate" :title="row.node!.name">{{ row.node!.name }}</span>
|
||||
<span
|
||||
v-if="row.node!.is_hidden"
|
||||
class="inline-flex items-center gap-1 px-1.5 py-0.5 rounded text-[10px] bg-amber-50 text-amber-700 shrink-0"
|
||||
:title="t('admin.dept.fields.hiddenHint')"
|
||||
><EyeOff class="w-3 h-3" />{{ t('admin.dept.tags.hidden') }}</span>
|
||||
</div>
|
||||
<div
|
||||
v-if="row.node!.description"
|
||||
class="text-xs text-muted truncate mt-0.5"
|
||||
:style="{ paddingLeft: `${(row.depth - 1) * 20 + 28}px` }"
|
||||
:title="row.node!.description"
|
||||
>
|
||||
{{ row.node!.description }}
|
||||
</div>
|
||||
</td>
|
||||
<td class="px-6 py-4 text-sm text-muted font-mono whitespace-nowrap truncate" :title="row.node!.code || '-'">
|
||||
{{ row.node!.code || '-' }}
|
||||
</td>
|
||||
<td class="px-6 py-4 text-sm text-muted truncate" :title="row.node!.company || '-'">
|
||||
{{ row.node!.company || '-' }}
|
||||
</td>
|
||||
<td class="px-6 py-4 text-center">
|
||||
<button
|
||||
type="button"
|
||||
class="p-1.5 text-indigo-600 hover:bg-indigo-50 rounded-lg transition-colors"
|
||||
:title="t('admin.dept.actions.edit')"
|
||||
@click="openEditModal(d)"
|
||||
>
|
||||
<Edit class="w-4 h-4" />
|
||||
</button>
|
||||
<button
|
||||
type="button"
|
||||
class="p-1.5 text-red-600 hover:bg-red-50 rounded-lg transition-colors"
|
||||
:title="t('admin.dept.actions.delete')"
|
||||
@click="deleteDepartment(d.id)"
|
||||
>
|
||||
<Trash2 class="w-4 h-4" />
|
||||
</button>
|
||||
</div>
|
||||
</td>
|
||||
</tr>
|
||||
class="inline-flex items-center justify-center rounded-lg bg-blue-50 p-2 text-blue-600 hover:bg-blue-100"
|
||||
:title="t('admin.dept.actions.viewMembers')"
|
||||
@click="openDepartmentMembers(row.node!.id, false)"
|
||||
><Users class="w-5 h-5" /><span class="ml-1 text-xs">{{ row.totalMembers }}</span></button>
|
||||
</td>
|
||||
<td class="px-6 py-4">
|
||||
<div class="flex justify-end gap-1">
|
||||
<button
|
||||
type="button"
|
||||
class="p-1.5 text-emerald-600 hover:bg-emerald-50 rounded-lg transition-colors"
|
||||
:title="t('admin.dept.addChild')"
|
||||
@click="openCreateModal(row.node as AdminDepartmentTreeNode)"
|
||||
>
|
||||
<FolderPlus class="w-4 h-4" />
|
||||
</button>
|
||||
<button
|
||||
type="button"
|
||||
class="p-1.5 text-indigo-600 hover:bg-indigo-50 rounded-lg transition-colors"
|
||||
:title="t('admin.dept.actions.edit')"
|
||||
@click="openEditModal(row.node!)"
|
||||
>
|
||||
<Edit class="w-4 h-4" />
|
||||
</button>
|
||||
<button
|
||||
type="button"
|
||||
class="p-1.5 text-red-600 hover:bg-red-50 rounded-lg transition-colors"
|
||||
:title="t('admin.dept.actions.delete')"
|
||||
@click="deleteDepartment(row.node!.id)"
|
||||
>
|
||||
<Trash2 class="w-4 h-4" />
|
||||
</button>
|
||||
</div>
|
||||
</td>
|
||||
</tr>
|
||||
</template>
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
@@ -196,6 +282,7 @@ const openDepartmentMembers = async (departmentId: number): Promise<void> => {
|
||||
<DepartmentFormModal
|
||||
:visible="showFormModal"
|
||||
:form="form"
|
||||
:parent-options="parentOptions"
|
||||
:is-saving="isSaving"
|
||||
@close="closeFormModal"
|
||||
@save="saveDepartment"
|
||||
@@ -203,7 +290,9 @@ const openDepartmentMembers = async (departmentId: number): Promise<void> => {
|
||||
<DepartmentMembersModal
|
||||
:visible="showMembersModal"
|
||||
:data="departmentMembers"
|
||||
:include-descendants="includeDescendants"
|
||||
@close="showMembersModal = false"
|
||||
@set-include="toggleIncludeDescendants"
|
||||
/>
|
||||
|
||||
<DeleteConfirmDialog
|
||||
|
||||
Reference in new issue
Block a user