liu
14 小时以前 611dd8268e40caa4d97e8954b85b5adaaef99afa
src/views/hrm/transfer/modules/form.vue
@@ -17,7 +17,8 @@
const emit = defineEmits(['success']);
const formType = ref<FormType>('create');
const currentEmployeeId = ref<number>();
/** 员工精简列表缓存(选人时用于带出原部门 / 原岗位) */
let employeeCache: any[] = [];
const isDetail = computed(() => formType.value === 'detail');
const getTitle = computed(() => {
@@ -26,6 +27,34 @@
  }
  return formType.value === 'update' ? '修改调岗申请' : '新增调岗申请';
});
/**
 * 根据选中的员工,自动带出原部门 / 原岗位(含 ID,便于后端落库)
 */
async function fillCurrentPosition(employeeId?: number) {
  if (!employeeId) {
    await formApi.setValues({
      currentDeptId: undefined,
      currentDeptName: undefined,
      currentPostId: undefined,
      currentPostName: undefined,
    });
    return;
  }
  if (employeeCache.length === 0) {
    employeeCache = (await getEmployeeSimpleList()) || [];
  }
  const emp = employeeCache.find((item) => item.id === employeeId);
  if (!emp) {
    return;
  }
  await formApi.setValues({
    currentDeptId: emp.deptId,
    currentDeptName: emp.deptName,
    currentPostId: emp.postIds?.[0],
    currentPostName: emp.postName,
  });
}
const [Form, formApi] = useVbenForm({
  commonConfig: {
@@ -39,6 +68,12 @@
  layout: 'horizontal',
  schema: [],
  showDefaultActions: false,
  // 选择「调岗人」后,自动带出其当前部门 / 岗位
  async handleValuesChange(values: any, changedFields: string[]) {
    if (changedFields?.includes('userId')) {
      await fillCurrentPosition(values?.userId);
    }
  },
});
const [Modal, modalApi] = useVbenModal({
@@ -53,7 +88,6 @@
    }
    modalApi.lock();
    const data = (await formApi.getValues()) as HrmTransferApi.Transfer;
    data.employeeId = currentEmployeeId.value;
    try {
      if (data.id) {
        await updateTransfer(data);
@@ -71,30 +105,31 @@
    if (!isOpen) {
      return;
    }
    employeeCache = [];
    const data = modalApi.getData<{ formType: FormType; id?: number }>();
    formType.value = data.formType;
    formApi.setState({ schema: useFormSchema(data.formType) });
    formApi.setDisabled(data.formType === 'detail');
    modalApi.setState({ showConfirmButton: data.formType !== 'detail' });
    if (!data?.id) {
      // 新增时,自动获取当前登录员工的部门和岗位信息
      const employees = await getEmployeeSimpleList();
      // TODO: 这里应该根据登录用户获取对应的员工信息,暂时取第一个员工作为示例
      const currentEmployee = employees[0];
      if (currentEmployee) {
        currentEmployeeId.value = currentEmployee.id;
        await formApi.setValues({
          currentDeptName: currentEmployee.deptName,
          currentPostName: currentEmployee.postName,
        });
      }
      // 新增:清空表单,由用户选择调岗人
      await formApi.setValues({
        userId: undefined,
        currentDeptId: undefined,
        currentDeptName: undefined,
        currentPostId: undefined,
        currentPostName: undefined,
      });
      return;
    }
    modalApi.lock();
    try {
      const transfer = await getTransfer(data.id);
      currentEmployeeId.value = transfer.employeeId;
      await formApi.setValues(transfer);
      // 历史数据可能没有原部门/岗位,按调岗人补齐
      if (transfer.userId && !transfer.currentDeptId) {
        await fillCurrentPosition(transfer.userId);
      }
      if (transfer.attachmentList?.length) {
        await formApi.setFieldValue('blobIds', transfer.attachmentList.map(item => ({
          uid: String(item.id),
@@ -112,7 +147,7 @@
</script>
<template>
  <Modal :title="getTitle" class="w-[600px]">
  <Modal :title="getTitle" class="w-[700px]">
    <Form class="mx-4" />
  </Modal>
</template>
</template>