fix: 修改文件上传功能,调整接口调用和数据处理逻辑以支持多文件上传
已修改1个文件
89 ■■■■■ 文件已修改
src/views/personnelManagement/contractManagement/filesDia.vue 89 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/views/personnelManagement/contractManagement/filesDia.vue
@@ -13,7 +13,7 @@
            :action="uploadUrl"
            :on-success="handleUploadSuccess"
            :on-error="handleUploadError"
            name="file"
            name="files"
            :show-file-list="false"
            :headers="headers"
            style="display: inline;margin-right: 10px"
@@ -23,16 +23,15 @@
        <el-button type="danger" plain @click="handleDelete">删除</el-button>
      </div>
      <PIMTable
          rowKey="id"
          rowKey="storageAttachmentId"
          :column="tableColumn"
          :tableData="tableData"
          :page="page"
          :tableLoading="tableLoading"
          :isSelection="true"
          :page="page"
          @selection-change="handleSelectionChange"
          height="500"
          @pagination="paginationSearch"
          :total="page.total"
          height="500"
      >
      </PIMTable>
      <template #footer>
@@ -47,22 +46,20 @@
<script setup>
import {ref} from "vue";
import filePreview from '@/components/filePreview/index.vue'
import {ElMessageBox} from "element-plus";
import {getToken} from "@/utils/auth.js";
import filePreview from '@/components/filePreview/index.vue'
import {
  fileAdd,
  fileDel,
  fileListPage
} from "@/api/financialManagement/revenueManagement.js";
import Pagination from "@/components/PIMTable/Pagination.vue";
  createAttachment,
  deleteAttachment,
  attachmentList
} from "@/api/basicData/storageAttachment.js";
const { proxy } = getCurrentInstance()
const emit = defineEmits(['close'])
const dialogFormVisible = ref(false);
const currentId = ref('')
const selectedRows = ref([]);
const filePreviewRef = ref()
const tableColumn = ref([
  {
    label: "文件名称",
@@ -93,20 +90,19 @@
const page = reactive({
    current: 1,
    size: 100,
  total: 0,
});
const total = ref(0);
const tableData = ref([]);
const fileList = ref([]);
const tableLoading = ref(false);
const accountType = ref('')
const filePreviewRef = ref()
const headers = ref({
  Authorization: "Bearer " + getToken(),
});
const uploadUrl = ref(import.meta.env.VITE_APP_BASE_API + "/file/upload"); // 上传的图片服务器地址
const uploadUrl = ref(import.meta.env.VITE_APP_BASE_API + "/common/upload"); // 上传的服务器地址
// 打开弹框
const openDialog = (row,type) => {
  accountType.value = type;
const openDialog = (row) => {
  dialogFormVisible.value = true;
  currentId.value = row.id;
  getList()
@@ -117,9 +113,15 @@
    getList();
};
const getList = () => {
  fileListPage({accountId: currentId.value,accountType:accountType.value, ...page}).then(res => {
    tableData.value = res.data.records;
    page.total = res.data.total;
  tableLoading.value = true;
  attachmentList({recordId: currentId.value, recordType: 'staff_contract'}).then(res => {
    tableData.value = (res.data || []).map(item => ({
      ...item,
      name: item.originalFilename || item.name
    }));
    page.total = res.data ? res.data.length : 0;
  }).finally(() => {
    tableLoading.value = false;
  })
}
// 表格选择数据
@@ -132,25 +134,42 @@
  dialogFormVisible.value = false;
  emit('close')
};
let pendingFiles = [];
let uploadTimer = null;
// 上传成功处理
function handleUploadSuccess(res, file) {
  // 如果上传成功
  if (res.code == 200) {
    const fileRow = {}
    fileRow.name = res.data.originalName
    fileRow.url = res.data.tempPath
    uploadFile(fileRow)
  if (res.code == 200 && res.data && res.data.length > 0) {
    const newFiles = res.data.map(item => ({
      ...item,
      name: item.originalFilename || item.name
    }));
    pendingFiles = [...pendingFiles, ...newFiles];
    // 防抖处理,避免多文件同时上传导致列表被覆盖
    if (uploadTimer) clearTimeout(uploadTimer);
    uploadTimer = setTimeout(() => {
      const mergedFiles = [...(tableData.value || []), ...pendingFiles];
      const storageAttachmentDTO = {
        recordType: 'staff_contract',
        recordId: currentId.value,
        application: "file",
        storageBlobDTOs: mergedFiles
      };
      createAttachment(storageAttachmentDTO).then(r => {
        proxy.$modal.msgSuccess("文件上传成功");
        getList()
      }).catch(() => {
        proxy.$modal.msgError("文件上传失败");
      }).finally(() => {
        pendingFiles = []; // 重置
      });
    }, 500);
  } else {
    proxy.$modal.msgError("文件上传失败");
  }
}
function uploadFile(file) {
  file.accountId = currentId.value;
  file.accountType = accountType.value;
  fileAdd(file).then(res => {
    proxy.$modal.msgSuccess("文件上传成功");
    getList()
  })
}
// 上传失败处理
function handleUploadError() {
@@ -164,7 +183,7 @@
const handleDelete = () => {
  let ids = [];
  if (selectedRows.value.length > 0) {
    ids = selectedRows.value.map((item) => item.id);
    ids = selectedRows.value.map((item) => item.storageAttachmentId);
  } else {
    proxy.$modal.msgWarning("请选择数据");
    return;
@@ -174,7 +193,7 @@
    cancelButtonText: "取消",
    type: "warning",
  }).then(() => {
    fileDel(ids).then((res) => {
    deleteAttachment(ids).then((res) => {
      proxy.$modal.msgSuccess("删除成功");
      getList();
    });