mirror of
https://gitee.com/qqtt-ah/ecom-dmb.git
synced 2026-11-24 21:49:19 +08:00
feat: 忘记干嘛了
This commit is contained in:
+170
-13
@@ -1,16 +1,21 @@
|
||||
<script lang="ts" setup>
|
||||
import type { AccountDTO, AccountVO } from '#/api/organization/account';
|
||||
import type { DepartmentVO } from '#/api/organization/department';
|
||||
import type { PositionVO } from '#/api/organization/position';
|
||||
import type { PageQueryDTO } from '#/types/common';
|
||||
|
||||
import { computed, ref, watch } from 'vue';
|
||||
|
||||
import { useVbenModal } from '@vben/common-ui';
|
||||
|
||||
import { Form, Input, message, Select } from 'ant-design-vue';
|
||||
import { Cascader, Form, Input, message, Select } from 'ant-design-vue';
|
||||
|
||||
import {
|
||||
admin_createAccountApi,
|
||||
admin_updateAccountApi,
|
||||
} from '#/api/organization/account';
|
||||
import { getDepartmentTreeApi } from '#/api/organization/department';
|
||||
import { getPositionPageApi } from '#/api/organization/position';
|
||||
|
||||
interface Emits {
|
||||
(e: 'success'): void;
|
||||
@@ -37,6 +42,21 @@ const formRef = ref();
|
||||
const loading = ref(false);
|
||||
const isEdit = ref(false);
|
||||
const currentAccountData = ref<AccountVO.AccountInfoVO | null>(null);
|
||||
const departmentOptionsLoading = ref(false);
|
||||
const positionOptionsLoading = ref(false);
|
||||
const selectorsLoaded = ref(false);
|
||||
|
||||
interface DepartmentCascaderOption {
|
||||
children?: DepartmentCascaderOption[];
|
||||
label: string;
|
||||
value: number;
|
||||
}
|
||||
|
||||
interface PositionSelectOption {
|
||||
departmentId?: number;
|
||||
label: string;
|
||||
value: number;
|
||||
}
|
||||
|
||||
// 表单数据
|
||||
const formData = ref<AccountDTO.CreateAccountDTO & { status?: number }>({
|
||||
@@ -50,6 +70,17 @@ const formData = ref<AccountDTO.CreateAccountDTO & { status?: number }>({
|
||||
remark: '',
|
||||
status: 0,
|
||||
});
|
||||
const departmentPath = ref<number[]>([]);
|
||||
const departmentTreeOptions = ref<DepartmentCascaderOption[]>([]);
|
||||
const allPositionOptions = ref<PositionSelectOption[]>([]);
|
||||
|
||||
const positionOptions = computed(() => {
|
||||
const selectedDepartmentId = formData.value.departmentId;
|
||||
if (!selectedDepartmentId) return allPositionOptions.value;
|
||||
return allPositionOptions.value.filter(
|
||||
(item) => !item.departmentId || item.departmentId === selectedDepartmentId,
|
||||
);
|
||||
});
|
||||
|
||||
// 模态框标题
|
||||
const modalTitle = computed(() => (isEdit.value ? '编辑账户' : '添加账户'));
|
||||
@@ -75,6 +106,7 @@ function resetForm() {
|
||||
remark: '',
|
||||
status: 0,
|
||||
};
|
||||
departmentPath.value = [];
|
||||
formRef.value?.resetFields();
|
||||
}
|
||||
|
||||
@@ -84,7 +116,111 @@ function handleCancel() {
|
||||
}
|
||||
|
||||
// 暴露给父组件的方法
|
||||
function open(accountData?: AccountVO.AccountInfoVO | null) {
|
||||
function buildDepartmentTreeOptions(
|
||||
departments: DepartmentVO.DepartmentInfoVO[],
|
||||
): DepartmentCascaderOption[] {
|
||||
return departments.map((item) => ({
|
||||
children:
|
||||
item.children && item.children.length > 0
|
||||
? buildDepartmentTreeOptions(item.children)
|
||||
: undefined,
|
||||
label: item.name,
|
||||
value: item.id,
|
||||
}));
|
||||
}
|
||||
|
||||
function findDepartmentPathById(
|
||||
options: DepartmentCascaderOption[],
|
||||
departmentId: number,
|
||||
currentPath: number[] = [],
|
||||
): number[] | null {
|
||||
for (const option of options) {
|
||||
const nextPath = [...currentPath, option.value];
|
||||
if (option.value === departmentId) return nextPath;
|
||||
if (option.children?.length) {
|
||||
const result = findDepartmentPathById(
|
||||
option.children,
|
||||
departmentId,
|
||||
nextPath,
|
||||
);
|
||||
if (result) return result;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
async function loadDepartmentOptions() {
|
||||
try {
|
||||
departmentOptionsLoading.value = true;
|
||||
const data = await getDepartmentTreeApi();
|
||||
departmentTreeOptions.value = buildDepartmentTreeOptions(data || []);
|
||||
} catch (error) {
|
||||
console.error('获取部门树失败:', error);
|
||||
message.error('获取部门树失败');
|
||||
departmentTreeOptions.value = [];
|
||||
} finally {
|
||||
departmentOptionsLoading.value = false;
|
||||
}
|
||||
}
|
||||
|
||||
async function loadPositionOptions() {
|
||||
try {
|
||||
positionOptionsLoading.value = true;
|
||||
const params: PageQueryDTO = {
|
||||
page: 1,
|
||||
pageSize: 1000,
|
||||
};
|
||||
const data = await getPositionPageApi(params);
|
||||
const list = data.records || [];
|
||||
allPositionOptions.value = list.map((item: PositionVO.PositionInfoVO) => ({
|
||||
departmentId: item.departmentId,
|
||||
label: item.name,
|
||||
value: item.id,
|
||||
}));
|
||||
} catch (error) {
|
||||
console.error('获取岗位列表失败:', error);
|
||||
message.error('获取岗位列表失败');
|
||||
allPositionOptions.value = [];
|
||||
} finally {
|
||||
positionOptionsLoading.value = false;
|
||||
}
|
||||
}
|
||||
|
||||
async function ensureSelectorOptionsLoaded() {
|
||||
if (selectorsLoaded.value) return;
|
||||
await Promise.all([loadDepartmentOptions(), loadPositionOptions()]);
|
||||
selectorsLoaded.value = true;
|
||||
}
|
||||
|
||||
function syncDepartmentPathByDepartmentId() {
|
||||
if (!formData.value.departmentId) {
|
||||
departmentPath.value = [];
|
||||
return;
|
||||
}
|
||||
const path = findDepartmentPathById(
|
||||
departmentTreeOptions.value,
|
||||
formData.value.departmentId,
|
||||
);
|
||||
departmentPath.value = path || [];
|
||||
}
|
||||
|
||||
function handleDepartmentChange(value: number[]) {
|
||||
departmentPath.value = value || [];
|
||||
const selectedDepartmentId = departmentPath.value.at(-1) || 0;
|
||||
const previousDepartmentId = formData.value.departmentId;
|
||||
formData.value.departmentId = selectedDepartmentId;
|
||||
|
||||
if (
|
||||
previousDepartmentId !== selectedDepartmentId &&
|
||||
formData.value.positionId &&
|
||||
!positionOptions.value.some((item) => item.value === formData.value.positionId)
|
||||
) {
|
||||
formData.value.positionId = 0;
|
||||
}
|
||||
}
|
||||
|
||||
async function open(accountData?: AccountVO.AccountInfoVO | null) {
|
||||
await ensureSelectorOptionsLoaded();
|
||||
isEdit.value = !!accountData;
|
||||
currentAccountData.value = accountData || null;
|
||||
|
||||
@@ -104,6 +240,8 @@ function open(accountData?: AccountVO.AccountInfoVO | null) {
|
||||
};
|
||||
}
|
||||
|
||||
syncDepartmentPathByDepartmentId();
|
||||
|
||||
modalApi.open();
|
||||
}
|
||||
|
||||
@@ -144,6 +282,18 @@ watch(
|
||||
},
|
||||
);
|
||||
|
||||
watch(
|
||||
() => formData.value.departmentId,
|
||||
() => {
|
||||
if (
|
||||
formData.value.positionId &&
|
||||
!positionOptions.value.some((item) => item.value === formData.value.positionId)
|
||||
) {
|
||||
formData.value.positionId = 0;
|
||||
}
|
||||
},
|
||||
);
|
||||
|
||||
// 暴露方法给父组件
|
||||
defineExpose({
|
||||
open,
|
||||
@@ -217,26 +367,33 @@ defineExpose({
|
||||
</Form.Item>
|
||||
|
||||
<Form.Item
|
||||
label="部门ID"
|
||||
label="所属部门"
|
||||
name="departmentId"
|
||||
:rules="[{ required: true, message: '请输入部门ID' }]"
|
||||
:rules="[{ required: true, message: '请选择所属部门' }]"
|
||||
>
|
||||
<Input
|
||||
v-model:value="formData.departmentId"
|
||||
type="number"
|
||||
placeholder="请输入部门ID"
|
||||
<Cascader
|
||||
:value="departmentPath"
|
||||
:options="departmentTreeOptions"
|
||||
:loading="departmentOptionsLoading"
|
||||
placeholder="请选择所属部门"
|
||||
:field-names="{ label: 'label', value: 'value', children: 'children' }"
|
||||
change-on-select
|
||||
class="w-full"
|
||||
@change="(value) => handleDepartmentChange((value || []) as number[])"
|
||||
/>
|
||||
</Form.Item>
|
||||
|
||||
<Form.Item
|
||||
label="岗位ID"
|
||||
label="职位"
|
||||
name="positionId"
|
||||
:rules="[{ required: true, message: '请输入岗位ID' }]"
|
||||
:rules="[{ required: true, message: '请选择职位' }]"
|
||||
>
|
||||
<Input
|
||||
<Select
|
||||
v-model:value="formData.positionId"
|
||||
type="number"
|
||||
placeholder="请输入岗位ID"
|
||||
:loading="positionOptionsLoading"
|
||||
:options="positionOptions"
|
||||
placeholder="请选择职位"
|
||||
:disabled="positionOptionsLoading"
|
||||
/>
|
||||
</Form.Item>
|
||||
|
||||
|
||||
Reference in New Issue
Block a user