feat(admin): 部门管理改为左树右成员面板的 OA 组织口径

This commit is contained in:
HTEWJ\26070045 committed 2026-10-22 09:08:04 +08:00
1 parent 396f17a15a
commit 194a51bc2d
13 files changed
+540 -313

No files matched your search

@@ -1,15 +1,17 @@
/**
* features/admin/modules/departments/DepartmentsPage.vue
* 用途:部门管理独立页面——树形表格(公司虚拟分组 + 部门层级缩进展开)+ 搜索 + 创建/编辑弹窗。
* 依赖:useDepartmentsAdmin、DepartmentFormModal、DepartmentMembersModal、DeleteConfirmDialog。
* 逻辑:路由 /admin/departments;数据来自 /admin/departments/tree;公司层为前端按 company 字段虚拟分组。
* 用途:部门管理独立页面——左树(公司虚拟分组 + 部门层级)右成员面板(OA 组织结构口径)。
* 依赖:useDepartmentsAdmin、DepartmentFormModal、DeleteConfirmDialog。
* 逻辑:路由 /admin/departments;左侧点击部门节点后,右侧按分页 + 关键词展示该部门成员
* (含下级口径可切换);原「员工」计数列已剔除,成员展示方式对齐 OA 组织结构页。
*/
<script setup lang="ts">
import { onMounted, ref } from 'vue';
import { computed, onMounted } from 'vue';
import { useI18n } from 'vue-i18n';
import {
Building2,
ChevronDown,
ChevronLeft,
ChevronRight,
Edit,
EyeOff,
@@ -22,19 +24,10 @@ import {
} from 'lucide-vue-next';
import DeleteConfirmDialog from '@/shared/components/DeleteConfirmDialog.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, AdminDepartmentTreeNode } from '@/features/admin/types/admin.types';
import { useMessage } from '@/shared/composables/useMessage';
import type { AdminDepartmentTreeNode } from '@/features/admin/types/admin.types';
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 {
displayRows,
@@ -58,35 +51,35 @@ const {
deleteDepartment,
closeDeleteConfirm,
confirmDelete,
selectedDeptId,
selectedDept,
selectedDeptPath,
memberData,
memberLoading,
memberKeyword,
memberIncludeDescendants,
memberPage,
memberPageSize,
selectDept,
setMemberKeyword,
setMemberIncludeDescendants,
setMemberPage,
setMemberPageSize,
} = useDepartmentsAdmin();
/** 成员面板总页数(由 total / page_size 推导)。 */
const memberTotalPages = computed(() => {
const data = memberData.value;
if (!data || data.page_size <= 0) return 1;
return Math.max(1, Math.ceil(data.total / data.page_size));
});
/** 公司虚拟分组行 key 去前缀,得到分组 key(用于展开状态)。 */
const companyGroupKey = (rowKey: string): string => rowKey.slice('company:'.length);
onMounted(() => {
void loadAll();
});
/** 加载并展示指定部门的员工名单(includeDescendants 控制是否含下级)。 */
const openDepartmentMembers = async (departmentId: number, include = false): Promise<void> => {
try {
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>
@@ -111,171 +104,225 @@ const companyGroupKey = (rowKey: string): string => rowKey.slice('company:'.leng
</button>
</div>
<div class="admin-table-filters--inline gap-3">
<div class="relative flex-1 min-w-[200px] max-w-sm">
<Search class="absolute left-3 top-1/2 -translate-y-1/2 w-4 h-4 text-muted pointer-events-none" />
<input
v-model="searchQuery"
type="search"
class="admin-table-filter-input w-full pl-9"
: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="flex flex-col lg:flex-row gap-4 items-start">
<!-- 左侧:组织树(公司虚拟分组 + 层级缩进) -->
<div class="w-full lg:w-[340px] shrink-0 rounded-2xl border border-subtle bg-surface flex flex-col gap-3 p-4 lg:max-h-[calc(100vh-230px)]">
<div class="flex items-center gap-2">
<div class="relative flex-1 min-w-0">
<Search class="absolute left-3 top-1/2 -translate-y-1/2 w-4 h-4 text-muted pointer-events-none" />
<input
v-model="searchQuery"
type="search"
class="admin-table-filter-input w-full pl-9"
:placeholder="t('admin.dept.searchPlaceholder')"
/>
</div>
<button
type="button"
class="p-2 text-muted hover:text-primary hover:bg-subtle rounded-lg transition-colors shrink-0"
: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 shrink-0"
:title="t('admin.dept.collapseAll')"
@click="collapseAll"
><ChevronDown class="w-4 h-4" /></button>
</div>
<div class="admin-table-wrap">
<!-- 列宽:名称(树)/ 编码 / 公司 / 员工 / 操作 -->
<table class="admin-table table-fixed">
<colgroup>
<col class="w-[40%]" />
<col class="w-[14%]" />
<col class="w-[19%]" />
<col class="w-[12%]" />
<col class="w-[15%]" />
</colgroup>
<thead>
<tr>
<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="displayRows.length === 0">
<td colspan="5" class="px-6 admin-table-empty">
{{ searchQuery.trim() ? t('admin.dept.noSearchResults') : t('admin.dept.noDepts') }}
</td>
</tr>
<div class="overflow-y-auto -mx-1 px-1 space-y-0.5">
<div v-if="displayRows.length === 0" class="py-12 text-center text-sm text-muted">
{{ searchQuery.trim() ? t('admin.dept.noSearchResults') : t('admin.dept.noDepts') }}
</div>
<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">
<div
v-if="row.kind === 'company'"
class="group flex items-center gap-1.5 py-2 pr-1 rounded-lg cursor-pointer select-none hover:bg-subtle/70"
@click="toggleCompany(companyGroupKey(row.key))"
>
<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>
<span class="flex-1" />
<button
type="button"
class="p-1.5 text-emerald-600 hover:bg-emerald-50 rounded-lg transition-colors shrink-0"
:title="t('admin.dept.addChild')"
@click.stop="openCreateModal(null, row.company)"
>
<FolderPlus class="w-4 h-4" />
</button>
</div>
<!-- 部门行(层级缩进;点击选中 → 右侧出成员面板) -->
<div
v-else
class="group flex items-center gap-1.5 py-2 pr-1 rounded-lg cursor-pointer select-none transition-colors"
:class="selectedDeptId === row.node!.id ? 'bg-brand-50 dark:bg-brand-900/30' : 'hover:bg-subtle/70'"
:style="{ paddingLeft: `${(row.depth - 1) * 16 + 4}px` }"
@click="selectDept(row.node!)"
>
<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.stop="toggleNode(row.node!.id)"
>
<component :is="row.expanded ? ChevronDown : ChevronRight" class="w-4 h-4" />
</button>
<span v-else class="w-5 shrink-0" />
<span class="font-medium text-sm truncate" :class="selectedDeptId === row.node!.id ? 'text-brand-700' : 'text-primary'" :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>
<span class="flex-1" />
<span class="hidden group-hover:flex items-center gap-0.5 shrink-0">
<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(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>
class="p-1 text-emerald-600 hover:bg-emerald-50 rounded-lg transition-colors"
:title="t('admin.dept.addChild')"
@click.stop="openCreateModal(row.node as AdminDepartmentTreeNode)"
>
<FolderPlus class="w-4 h-4" />
</button>
<button
type="button"
class="p-1 text-indigo-600 hover:bg-indigo-50 rounded-lg transition-colors"
:title="t('admin.dept.actions.edit')"
@click.stop="openEditModal(row.node!)"
>
<Edit class="w-4 h-4" />
</button>
<button
type="button"
class="p-1 text-red-600 hover:bg-red-50 rounded-lg transition-colors"
:title="t('admin.dept.actions.delete')"
@click.stop="deleteDepartment(row.node!.id)"
>
<Trash2 class="w-4 h-4" />
</button>
</span>
</div>
</template>
</tbody>
</table>
</div>
</div>
<!-- 右侧:选中部门的成员面板(OA 口径) -->
<div class="flex-1 min-w-0 w-full rounded-2xl border border-subtle bg-surface flex flex-col lg:max-h-[calc(100vh-230px)]">
<template v-if="!selectedDept">
<div class="flex-1 flex flex-col items-center justify-center py-24 text-muted">
<Users class="w-10 h-10 mb-3 opacity-50" />
<p class="text-sm">{{ t('admin.dept.members.hint') }}</p>
</div>
</template>
<template v-else>
<header class="px-5 py-4 border-b border-subtle flex items-start justify-between gap-4 flex-wrap">
<div class="min-w-0">
<h3 class="text-base font-bold text-primary truncate">{{ selectedDept.name }}</h3>
<p class="text-xs text-muted mt-0.5 truncate" :title="selectedDeptPath.join(' / ')">
{{ selectedDeptPath.join(' / ') }}
</p>
</div>
<label class="flex items-center gap-2 text-sm text-muted cursor-pointer select-none shrink-0">
<input
type="checkbox"
class="accent-brand-600 w-4 h-4"
:checked="memberIncludeDescendants"
@change="setMemberIncludeDescendants(($event.target as HTMLInputElement).checked)"
/>
{{ t('admin.dept.members.includeDescendants') }}
</label>
</header>
<div class="px-5 py-3 border-b border-subtle">
<div class="relative max-w-sm">
<Search class="absolute left-3 top-1/2 -translate-y-1/2 w-4 h-4 text-muted pointer-events-none" />
<input
:value="memberKeyword"
type="search"
class="admin-table-filter-input w-full pl-9"
:placeholder="t('admin.dept.members.searchPlaceholder')"
@input="setMemberKeyword(($event.target as HTMLInputElement).value)"
/>
</div>
</div>
<div class="flex-1 overflow-y-auto">
<table class="w-full text-sm">
<thead class="text-xs text-muted bg-subtle/50 sticky top-0">
<tr>
<th class="px-5 py-3 text-left font-medium">{{ t('admin.dept.members.columns.name') }}</th>
<th class="px-5 py-3 text-left font-medium">{{ t('admin.dept.members.columns.contact') }}</th>
<th class="px-5 py-3 text-left font-medium">{{ t('admin.dept.members.columns.employeeId') }}</th>
<th class="px-5 py-3 text-left font-medium">{{ t('admin.dept.members.columns.department') }}</th>
<th class="px-5 py-3 text-left font-medium">{{ t('admin.dept.members.columns.status') }}</th>
</tr>
</thead>
<tbody>
<tr v-if="memberLoading">
<td colspan="5" class="px-5 py-10 text-center text-muted">
<div class="animate-spin rounded-full h-7 w-7 border-b-2 border-brand-600 mx-auto" />
</td>
</tr>
<tr v-else-if="!memberData || memberData.members.length === 0">
<td colspan="5" class="px-5 py-10 text-center text-muted">{{ t('admin.dept.members.empty') }}</td>
</tr>
<template v-else>
<tr v-for="member in memberData.members" :key="member.id" class="border-t border-subtle hover:bg-subtle/40">
<td class="px-5 py-3">
<div class="font-medium text-primary truncate">{{ member.real_name || member.username }}</div>
<div class="text-xs text-muted truncate">{{ member.username }}</div>
</td>
<td class="px-5 py-3 text-muted truncate">{{ member.phone_number || member.email || '-' }}</td>
<td class="px-5 py-3 text-muted font-mono whitespace-nowrap">{{ member.employee_id || '-' }}</td>
<td class="px-5 py-3 text-muted truncate">{{ member.department || '-' }}</td>
<td class="px-5 py-3">
<span class="inline-flex px-2 py-0.5 rounded-full text-xs bg-blue-50 text-blue-600 whitespace-nowrap">{{ member.status || '-' }}</span>
</td>
</tr>
</template>
</tbody>
</table>
</div>
<footer v-if="memberData" class="px-5 py-3 border-t border-subtle flex items-center justify-between gap-3 flex-wrap text-sm text-muted">
<span>{{ t('admin.dept.members.total', { total: memberData.total }) }}</span>
<div class="flex items-center gap-2">
<select
:value="memberPageSize"
class="admin-table-filter-input py-1.5 px-2"
@change="setMemberPageSize(Number(($event.target as HTMLSelectElement).value))"
>
<option :value="20">20</option>
<option :value="50">50</option>
<option :value="100">100</option>
</select>
<button
type="button"
class="p-1.5 rounded-lg hover:bg-subtle disabled:opacity-40 disabled:pointer-events-none"
:disabled="memberPage <= 1"
:title="t('admin.dept.members.prevPage')"
@click="setMemberPage(memberPage - 1)"
><ChevronLeft class="w-4 h-4" /></button>
<span class="whitespace-nowrap">{{ memberPage }} / {{ memberTotalPages }}</span>
<button
type="button"
class="p-1.5 rounded-lg hover:bg-subtle disabled:opacity-40 disabled:pointer-events-none"
:disabled="memberPage >= memberTotalPages"
:title="t('admin.dept.members.nextPage')"
@click="setMemberPage(memberPage + 1)"
><ChevronRight class="w-4 h-4" /></button>
</div>
</footer>
</template>
</div>
</div>
</template>
@@ -287,13 +334,6 @@ const companyGroupKey = (rowKey: string): string => rowKey.slice('company:'.leng
@close="closeFormModal"
@save="saveDepartment"
/>
<DepartmentMembersModal
:visible="showMembersModal"
:data="departmentMembers"
:include-descendants="includeDescendants"
@close="showMembersModal = false"
@set-include="toggleIncludeDescendants"
/>
<DeleteConfirmDialog
v-model="showDeleteConfirmDialog"