487 lines
16 KiB
Vue
487 lines
16 KiB
Vue
/**
|
||
* features/admin/modules/departments/DepartmentsPage.vue
|
||
* 用途:部门管理独立页面——左树(公司虚拟分组 + 部门层级)右成员面板(OA 组织结构口径)。
|
||
* 依赖:useDepartmentsAdmin、DepartmentFormModal、element-plus、@element-plus/icons-vue。
|
||
* 逻辑:路由 /admin/departments。左侧点击部门节点后,右侧按分页 + 关键词展示该部门成员
|
||
* (含下级口径可切换);删除确认走 ElMessageBox。控件按 ELEMENT_MIGRATION.md 全量 Element 化,
|
||
* 树本身是自定义层级结构,不是 ElTable/ElTree 列表页,故不走 AdminListPage 骨架。
|
||
*/
|
||
<script setup lang="ts">
|
||
import { computed, onMounted } from 'vue';
|
||
import { useI18n } from 'vue-i18n';
|
||
import { ElButton, ElCheckbox, ElConfigProvider, ElEmpty, ElIcon, ElInput, ElLoading, ElPagination, ElTable, ElTableColumn, ElTag } from 'element-plus';
|
||
import {
|
||
ArrowDown,
|
||
ArrowRight,
|
||
Delete,
|
||
Edit,
|
||
Expand,
|
||
Fold,
|
||
FolderAdd,
|
||
Hide,
|
||
OfficeBuilding,
|
||
Plus,
|
||
Search,
|
||
} from '@element-plus/icons-vue';
|
||
import DepartmentFormModal from './components/DepartmentFormModal.vue';
|
||
import { useDepartmentsAdmin } from './composables/useDepartmentsAdmin';
|
||
import { useElementProvider } from '@/features/admin/composables/useElementProvider';
|
||
import type { AdminDepartmentTreeNode } from '@/features/admin/types/admin.types';
|
||
import 'element-plus/dist/index.css';
|
||
import 'element-plus/theme-chalk/dark/css-vars.css';
|
||
import '@/features/admin/styles/element-trial.css';
|
||
|
||
const vLoading = ElLoading.directive;
|
||
|
||
const { t, te } = useI18n();
|
||
const { elementLocale } = useElementProvider();
|
||
|
||
const {
|
||
displayRows,
|
||
parentOptions,
|
||
form,
|
||
searchQuery,
|
||
isLoading,
|
||
isSaving,
|
||
showFormModal,
|
||
loadAll,
|
||
toggleNode,
|
||
toggleCompany,
|
||
expandAll,
|
||
collapseAll,
|
||
openCreateModal,
|
||
openEditModal,
|
||
closeFormModal,
|
||
saveDepartment,
|
||
deleteDepartment,
|
||
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));
|
||
});
|
||
|
||
/** 左树空状态文案:搜索无结果或尚无部门。 */
|
||
const treeEmptyText = computed(() => (
|
||
searchQuery.value.trim() ? t('admin.dept.noSearchResults') : t('admin.dept.noDepts')
|
||
));
|
||
|
||
/** 成员状态 → 本地化标签;未知状态回退显示原值。 */
|
||
const statusLabel = (status?: string): string => {
|
||
if (!status) return '-';
|
||
const key = `admin.dept.members.status.${status}`;
|
||
return te(key) ? t(key) : status;
|
||
};
|
||
|
||
/** 成员状态 → ElTag 类型:启用 success,待审核 warning,其余 info。 */
|
||
const statusTagType = (status?: string): 'success' | 'warning' | 'info' => {
|
||
if (status === 'active') return 'success';
|
||
if (status === 'pending') return 'warning';
|
||
return 'info';
|
||
};
|
||
|
||
/** 公司虚拟分组行 key 去前缀,得到分组 key(用于展开状态)。 */
|
||
const companyGroupKey = (rowKey: string): string => rowKey.slice('company:'.length);
|
||
|
||
onMounted(() => {
|
||
void loadAll();
|
||
});
|
||
</script>
|
||
|
||
<template>
|
||
<ElConfigProvider :locale="elementLocale">
|
||
<section class="admin-table-section admin-dept-page">
|
||
<header class="admin-table-header admin-table-header--stacked">
|
||
<div>
|
||
<h2 class="section-title">{{ t('admin.dept.title') }}</h2>
|
||
<p class="section-desc mt-1">{{ t('admin.dept.subtitle') }}</p>
|
||
</div>
|
||
<ElButton type="primary" :icon="Plus" @click="openCreateModal()">
|
||
{{ t('admin.dept.add') }}
|
||
</ElButton>
|
||
</header>
|
||
|
||
<!-- 筛选条:左树搜索 + 展开/收起,一条分隔线与内容区分开 -->
|
||
<div class="admin-dept-filter">
|
||
<ElInput
|
||
v-model="searchQuery"
|
||
class="admin-dept-filter__search"
|
||
:prefix-icon="Search"
|
||
clearable
|
||
:placeholder="t('admin.dept.searchPlaceholder')"
|
||
/>
|
||
<ElButton text :icon="Expand" :title="t('admin.dept.expandAll')" @click="expandAll" />
|
||
<ElButton text :icon="Fold" :title="t('admin.dept.collapseAll')" @click="collapseAll" />
|
||
</div>
|
||
|
||
<div v-loading="isLoading" class="admin-dept-body">
|
||
<!-- 左侧:组织树(公司虚拟分组 + 层级缩进);定宽写在 style 上,避免 HMR 样式层滞后导致宽度不生效 -->
|
||
<div class="admin-dept-tree" style="width: max-content; min-width: 260px; max-width: 45%; flex-shrink: 0;">
|
||
<ElEmpty v-if="displayRows.length === 0" :description="treeEmptyText" :image-size="80" />
|
||
<template v-for="row in displayRows" :key="row.key">
|
||
<!-- 公司虚拟分组行(不入库,前端按 company 字段归堆) -->
|
||
<div
|
||
v-if="row.kind === 'company'"
|
||
class="admin-dept-tree__row admin-dept-tree__row--company"
|
||
@click="toggleCompany(companyGroupKey(row.key))"
|
||
>
|
||
<ElIcon class="admin-dept-tree__chevron">
|
||
<component :is="row.expanded ? ArrowDown : ArrowRight" />
|
||
</ElIcon>
|
||
<ElIcon class="admin-dept-tree__company-icon"><OfficeBuilding /></ElIcon>
|
||
<span class="admin-dept-tree__name" :title="row.company">{{ row.company }}</span>
|
||
<ElButton
|
||
link
|
||
type="primary"
|
||
:icon="FolderAdd"
|
||
:title="t('admin.dept.addChild')"
|
||
@click.stop="openCreateModal(null, row.company)"
|
||
/>
|
||
</div>
|
||
<!-- 部门行(层级缩进;点击选中 → 右侧出成员面板) -->
|
||
<div
|
||
v-else
|
||
class="admin-dept-tree__row"
|
||
:class="{ 'is-selected': selectedDeptId === row.node!.id }"
|
||
:style="{ paddingLeft: `${(row.depth - 1) * 16 + 4}px` }"
|
||
@click="selectDept(row.node!)"
|
||
>
|
||
<button
|
||
v-if="row.hasChildren"
|
||
type="button"
|
||
class="admin-dept-tree__toggle"
|
||
:title="row.expanded ? t('admin.dept.collapse') : t('admin.dept.expand')"
|
||
@click.stop="toggleNode(row.node!.id)"
|
||
>
|
||
<ElIcon>
|
||
<component :is="row.expanded ? ArrowDown : ArrowRight" />
|
||
</ElIcon>
|
||
</button>
|
||
<span v-else class="admin-dept-tree__toggle" />
|
||
<span class="admin-dept-tree__name" :title="row.node!.name">{{ row.node!.name }}</span>
|
||
<ElTag
|
||
v-if="row.node!.is_hidden"
|
||
type="warning"
|
||
effect="light"
|
||
size="small"
|
||
:icon="Hide"
|
||
:title="t('admin.dept.fields.hiddenHint')"
|
||
>
|
||
{{ t('admin.dept.tags.hidden') }}
|
||
</ElTag>
|
||
<span class="admin-dept-tree__spacer" />
|
||
<span class="admin-dept-tree__actions" @click.stop>
|
||
<ElButton
|
||
link
|
||
type="primary"
|
||
:icon="FolderAdd"
|
||
:title="t('admin.dept.addChild')"
|
||
@click="openCreateModal(row.node as AdminDepartmentTreeNode)"
|
||
/>
|
||
<ElButton
|
||
link
|
||
type="primary"
|
||
:icon="Edit"
|
||
:title="t('admin.dept.actions.edit')"
|
||
@click="openEditModal(row.node!)"
|
||
/>
|
||
<ElButton
|
||
link
|
||
type="danger"
|
||
:icon="Delete"
|
||
:title="t('admin.dept.actions.delete')"
|
||
@click="deleteDepartment(row.node!.id)"
|
||
/>
|
||
</span>
|
||
</div>
|
||
</template>
|
||
</div>
|
||
|
||
<!-- 右侧:选中部门的成员面板(OA 口径) -->
|
||
<div class="admin-dept-members">
|
||
<template v-if="!selectedDept">
|
||
<ElEmpty class="admin-dept-members__empty" :description="t('admin.dept.members.hint')" :image-size="80" />
|
||
</template>
|
||
<template v-else>
|
||
<header class="admin-dept-members__header">
|
||
<div class="admin-dept-members__title">
|
||
<h3>{{ selectedDept.name }}</h3>
|
||
<p :title="selectedDeptPath.join(' / ')">{{ selectedDeptPath.join(' / ') }}</p>
|
||
</div>
|
||
<ElCheckbox
|
||
:model-value="memberIncludeDescendants"
|
||
@change="setMemberIncludeDescendants($event as boolean)"
|
||
>
|
||
{{ t('admin.dept.members.includeDescendants') }}
|
||
</ElCheckbox>
|
||
</header>
|
||
|
||
<div class="admin-dept-members__toolbar">
|
||
<ElInput
|
||
:model-value="memberKeyword"
|
||
class="admin-dept-members__search"
|
||
:prefix-icon="Search"
|
||
clearable
|
||
:placeholder="t('admin.dept.members.searchPlaceholder')"
|
||
@input="setMemberKeyword(String($event))"
|
||
/>
|
||
</div>
|
||
|
||
<div v-loading="memberLoading" class="admin-dept-members__tablewrap">
|
||
<ElTable
|
||
:data="memberData?.members ?? []"
|
||
row-key="id"
|
||
stripe
|
||
height="100%"
|
||
:empty-text="t('admin.dept.members.empty')"
|
||
>
|
||
<ElTableColumn :label="t('admin.dept.members.columns.name')" width="170">
|
||
<template #default="{ row }">
|
||
<div class="admin-dept-member__name">{{ row.real_name || row.username }}</div>
|
||
<div class="admin-dept-member__sub">{{ row.username }}</div>
|
||
</template>
|
||
</ElTableColumn>
|
||
<ElTableColumn :label="t('admin.dept.members.columns.contact')" width="150" show-overflow-tooltip>
|
||
<template #default="{ row }">{{ row.phone_number || row.email || '-' }}</template>
|
||
</ElTableColumn>
|
||
<ElTableColumn prop="employee_id" :label="t('admin.dept.members.columns.employeeId')" width="90" show-overflow-tooltip />
|
||
<ElTableColumn prop="department" :label="t('admin.dept.members.columns.department')" width="110" show-overflow-tooltip />
|
||
<ElTableColumn :label="t('admin.dept.members.columns.status')" width="80">
|
||
<template #default="{ row }">
|
||
<ElTag :type="statusTagType(row.status)" effect="light" size="small">
|
||
{{ statusLabel(row.status) }}
|
||
</ElTag>
|
||
</template>
|
||
</ElTableColumn>
|
||
</ElTable>
|
||
</div>
|
||
|
||
<footer v-if="memberData" class="admin-dept-members__footer">
|
||
<ElPagination
|
||
background
|
||
layout="total, sizes, prev, pager, next"
|
||
:total="memberData.total"
|
||
:current-page="memberPage"
|
||
:page-size="memberPageSize"
|
||
:page-sizes="[20, 50, 100]"
|
||
@current-change="setMemberPage"
|
||
@size-change="setMemberPageSize"
|
||
/>
|
||
<span class="admin-dept-members__pages">{{ memberPage }} / {{ memberTotalPages }}</span>
|
||
</footer>
|
||
</template>
|
||
</div>
|
||
</div>
|
||
</section>
|
||
|
||
<DepartmentFormModal
|
||
:visible="showFormModal"
|
||
:form="form"
|
||
:parent-options="parentOptions"
|
||
:is-saving="isSaving"
|
||
@close="closeFormModal"
|
||
@save="saveDepartment"
|
||
/>
|
||
</ElConfigProvider>
|
||
</template>
|
||
|
||
<style scoped>
|
||
/* 页内布局(非 Element 控件内部),间距对齐迁移规范:筛选条、面板分隔、分页右侧 */
|
||
/* AdminLayout 内容列高度确定(h-dvh 壳内 flex-1 min-h-0),本页卡片填满后左右两栏各自滚动 */
|
||
.admin-dept-page {
|
||
height: 100%;
|
||
min-height: 24rem;
|
||
display: flex;
|
||
flex-direction: column;
|
||
}
|
||
.admin-dept-filter {
|
||
display: flex;
|
||
align-items: center;
|
||
gap: 8px;
|
||
padding: 12px 32px;
|
||
border-bottom: 1px solid var(--el-border-color-lighter);
|
||
}
|
||
.admin-dept-filter__search {
|
||
width: 16rem;
|
||
}
|
||
.admin-dept-body {
|
||
flex: 1;
|
||
min-height: 0;
|
||
display: flex;
|
||
align-items: stretch;
|
||
overflow-x: auto;
|
||
}
|
||
.admin-dept-tree {
|
||
width: max-content;
|
||
min-width: 260px;
|
||
max-width: 45%;
|
||
flex-shrink: 0;
|
||
padding: 12px 8px 12px 24px;
|
||
border-right: 1px solid var(--el-border-color-lighter);
|
||
overflow-y: auto;
|
||
}
|
||
.admin-dept-tree__row {
|
||
display: flex;
|
||
align-items: center;
|
||
gap: 4px;
|
||
padding: 6px 4px;
|
||
border-radius: var(--el-border-radius-base);
|
||
cursor: pointer;
|
||
user-select: none;
|
||
min-width: 0;
|
||
}
|
||
.admin-dept-tree__row:hover {
|
||
background: var(--el-fill-color-light);
|
||
}
|
||
.admin-dept-tree__row--company {
|
||
font-weight: 600;
|
||
}
|
||
.admin-dept-tree__row.is-selected {
|
||
background: var(--el-color-primary-light-9);
|
||
}
|
||
.admin-dept-tree__row.is-selected .admin-dept-tree__name {
|
||
color: var(--el-color-primary);
|
||
}
|
||
.admin-dept-tree__chevron,
|
||
.admin-dept-tree__toggle {
|
||
flex-shrink: 0;
|
||
width: 20px;
|
||
display: inline-flex;
|
||
justify-content: center;
|
||
color: var(--el-text-color-secondary);
|
||
background: none;
|
||
border: none;
|
||
padding: 0;
|
||
cursor: pointer;
|
||
}
|
||
.admin-dept-tree__company-icon {
|
||
flex-shrink: 0;
|
||
color: var(--el-color-primary);
|
||
}
|
||
.admin-dept-tree__name {
|
||
font-size: var(--el-font-size-base);
|
||
color: var(--el-text-color-primary);
|
||
white-space: nowrap;
|
||
overflow: hidden;
|
||
text-overflow: ellipsis;
|
||
}
|
||
.admin-dept-tree__spacer {
|
||
flex: 1;
|
||
}
|
||
.admin-dept-tree__actions {
|
||
display: none;
|
||
align-items: center;
|
||
flex-shrink: 0;
|
||
}
|
||
.admin-dept-tree__row:hover .admin-dept-tree__actions {
|
||
display: inline-flex;
|
||
}
|
||
.admin-dept-members {
|
||
flex: 1;
|
||
min-width: 280px;
|
||
min-height: 0;
|
||
display: flex;
|
||
flex-direction: column;
|
||
}
|
||
.admin-dept-members__empty {
|
||
flex: 1;
|
||
margin: auto;
|
||
}
|
||
.admin-dept-members__header {
|
||
/* width 由 .admin-dept-members 统一定宽 */
|
||
display: flex;
|
||
align-items: center;
|
||
justify-content: space-between;
|
||
gap: 16px;
|
||
flex-wrap: wrap;
|
||
padding: 16px 24px 8px;
|
||
}
|
||
.admin-dept-members__title {
|
||
min-width: 0;
|
||
}
|
||
.admin-dept-members__title h3 {
|
||
font-size: var(--el-font-size-large);
|
||
font-weight: 600;
|
||
color: var(--el-text-color-primary);
|
||
margin: 0;
|
||
white-space: nowrap;
|
||
overflow: hidden;
|
||
text-overflow: ellipsis;
|
||
}
|
||
.admin-dept-members__title p {
|
||
font-size: var(--el-font-size-small);
|
||
color: var(--el-text-color-secondary);
|
||
margin: 2px 0 0;
|
||
white-space: nowrap;
|
||
overflow: hidden;
|
||
text-overflow: ellipsis;
|
||
}
|
||
.admin-dept-members__toolbar {
|
||
padding: 8px 24px 12px;
|
||
border-bottom: 1px solid var(--el-border-color-lighter);
|
||
}
|
||
.admin-dept-members__search {
|
||
width: 16rem;
|
||
}
|
||
.admin-dept-members__tablewrap {
|
||
/* 宽度须与上方五列 width 之和(150+150+90+110+80)保持一致;居中使剩余空白均匀分两侧 */
|
||
width: 580px;
|
||
max-width: 100%;
|
||
margin-inline: auto;
|
||
}
|
||
.admin-dept-members__footer {
|
||
/* 与表格同宽并同样居中,分页对齐表格右缘 */
|
||
width: 580px;
|
||
max-width: 100%;
|
||
margin-inline: auto;
|
||
display: flex;
|
||
align-items: center;
|
||
justify-content: flex-end;
|
||
flex-wrap: wrap;
|
||
row-gap: 8px;
|
||
gap: 12px;
|
||
padding: 12px 0;
|
||
border-top: 1px solid var(--el-border-color-lighter);
|
||
}
|
||
.admin-dept-members__pages {
|
||
font-size: var(--el-font-size-small);
|
||
color: var(--el-text-color-secondary);
|
||
white-space: nowrap;
|
||
}
|
||
.admin-dept-member__name {
|
||
color: var(--el-text-color-primary);
|
||
}
|
||
.admin-dept-member__sub {
|
||
font-size: var(--el-font-size-extra-small);
|
||
color: var(--el-text-color-secondary);
|
||
margin-top: 2px;
|
||
}
|
||
@media (max-width: 1023px) {
|
||
.admin-dept-body {
|
||
flex-direction: column;
|
||
}
|
||
.admin-dept-tree {
|
||
max-height: 40vh;
|
||
}
|
||
.admin-dept-members {
|
||
width: 100%;
|
||
}
|
||
}
|
||
</style>
|