张诺
20 小时以前 231334ac73df257d0f2a2d59f3697273b769070d
src/views/collaborativeApproval/knowledgeBase/index.vue
@@ -133,6 +133,29 @@
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="附件材料" prop="files">
          <el-upload
            v-model:file-list="fileList"
            :action="upload.url"
            multiple
            ref="fileUpload"
            auto-upload
            :headers="upload.headers"
            :before-upload="handleBeforeUpload"
            :on-error="handleUploadError"
            :on-success="handleUploadSuccess"
            :on-remove="handleRemove"
            :on-preview="handlePreview"
            :show-file-list="true"
          >
            <el-button type="primary">上传</el-button>
            <template #tip>
              <div class="el-upload__tip">
                支持文档(doc, docx, xls, xlsx, pdf, txt)和图片(jpg, jpeg, png, gif)格式
              </div>
            </template>
          </el-upload>
        </el-form-item>
      </el-form>
    </FormDialog>
@@ -198,6 +221,17 @@
          </div>
        </div>
        <div class="detail-section" v-if="currentKnowledge.commonFileList && currentKnowledge.commonFileList.length > 0">
          <h4>附件材料</h4>
          <div class="file-list">
            <div v-for="file in currentKnowledge.commonFileList" :key="file.id" class="file-item">
              <div class="file-info">
                <el-link :href="file.url" target="_blank" :download="file.name">{{ file.name }}</el-link>
              </div>
            </div>
          </div>
        </div>
        <div class="detail-section">
          <h4>使用统计</h4>
          <div class="usage-stats">
@@ -234,9 +268,10 @@
import { ElMessage, ElMessageBox } from "element-plus";
import PIMTable from "@/components/PIMTable/PIMTable.vue";
import FormDialog from '@/components/Dialog/FormDialog.vue';
import { listKnowledgeBase, delKnowledgeBase,addKnowledgeBase,updateKnowledgeBase } from "@/api/collaborativeApproval/knowledgeBase.js";
import { listKnowledgeBase, delKnowledgeBase,addKnowledgeBase,updateKnowledgeBase, delKnowledgeBaseFile } from "@/api/collaborativeApproval/knowledgeBase.js";
import useUserStore from '@/store/modules/user';
import { userListNoPageByTenantId } from '@/api/system/user.js';
import { getToken } from "@/utils/auth";
// 表单验证规则
const rules = {
@@ -268,6 +303,13 @@
  },
  tableData: [],
  selectedIds: [],
  fileList: [],
  upload: {
    // 上传的地址
    url: import.meta.env.VITE_APP_BASE_API + "/file/upload",
    // 设置上传的请求头部
    headers: { Authorization: "Bearer " + getToken() },
  },
  form: {
    title: "",
    type: "",
@@ -277,7 +319,8 @@
    solution: "",
    keyPoints: "",
    creator: "",
    usageCount: 0
    usageCount: 0,
    files: []
  },
  dialogVisible: false,
  dialogTitle: "",
@@ -292,6 +335,8 @@
  page,
  tableData,
  selectedIds,
  fileList,
  upload,
  form,
  dialogVisible,
  dialogTitle,
@@ -425,8 +470,15 @@
  listKnowledgeBase({...page.value, ...searchForm.value})
  .then(res => {
    tableLoading.value = false;
    tableData.value = res.data.records
    page.value.total = res.data.total;
    // 如果当前页数超过总页数,重置到第1页并重新查询
    const maxPage = Math.ceil(res.data.total / page.value.size) || 1;
    if (page.value.current > maxPage && maxPage > 0) {
      page.value.current = 1;
      // 重新查询第1页数据
      return getList();
    }
    tableData.value = res.data.records;
  }).catch(err => {
    tableLoading.value = false;
  })
@@ -434,9 +486,14 @@
// 分页处理
const pagination = (obj) => {
  const oldSize = page.value.size;
  page.value.current = obj.page;
  page.value.size = obj.limit;
  handleQuery();
  // 如果 size 改变了,重置到第1页,避免当前页超出范围
  if (oldSize !== obj.limit) {
    page.value.current = 1;
  }
  getList();
};
// 选择变化处理
@@ -450,6 +507,7 @@
  if (type === "add") {
    dialogTitle.value = "新增知识";
    // 重置表单,默认创建人为当前用户
    fileList.value = [];
    Object.assign(form.value, {
      title: "",
      type: "",
@@ -459,10 +517,12 @@
      solution: "",
      keyPoints: "",
      creator: userStore.nickName || "",
      usageCount: 0
      usageCount: 0,
      files: []
    });
  } else if (type === "edit" && row) {
    dialogTitle.value = "编辑知识";
    fileList.value = row.files || [];
    Object.assign(form.value, {
      id: row.id,
      title: row.title,
@@ -473,7 +533,9 @@
      solution: row.solution,
      keyPoints: row.keyPoints,
      creator: row.creator,
      usageCount: row.usageCount
      usageCount: row.usageCount,
      files: row.files || [],
    });
  }
  dialogVisible.value = true;
@@ -565,6 +627,7 @@
// 关闭知识表单对话框
const closeKnowledgeDialog = () => {
  // 清空表单数据,默认创建人为当前用户
  fileList.value = [];
  Object.assign(form.value, {
    id: undefined,
    title: "",
@@ -575,7 +638,8 @@
    solution: "",
    keyPoints: "",
    creator: userStore.nickName || "",
    usageCount: 0
    usageCount: 0,
    files: []
  });
  // 清除表单验证状态
  if (formRef.value) {
@@ -594,6 +658,67 @@
  copyKnowledge();
  closeViewDialog();
};
// 上传前校检
function handleBeforeUpload(file) {
  proxy.$modal.loading("正在上传文件,请稍候...");
  return true;
}
// 上传失败
function handleUploadError(err) {
  proxy.$modal.msgError("上传文件失败");
  proxy.$modal.closeLoading();
}
// 上传成功回调
function handleUploadSuccess(res, file, uploadFiles) {
  proxy.$modal.closeLoading();
  if (res.code === 200) {
    if (!form.value.files) {
      form.value.files = [];
    }
    form.value.files.push({url:res.data.tempPath,name:res.data.originalName});
    proxy.$modal.msgSuccess("上传成功");
  } else {
    proxy.$modal.msgError(res.msg);
    proxy.$refs.fileUpload.handleRemove(file);
  }
}
// 移除文件
function handleRemove(file, fileList) {
  // 如果是编辑模式且文件有id,调用API删除
  if (dialogType.value === "edit" && file.id) {
    delKnowledgeBaseFile([file.id]).then((res) => {
      if (res.code === 200) {
        proxy.$modal.msgSuccess("删除成功");
        // 从form.value.files中移除
        const index = form.value.files.findIndex(f => f.id === file.id);
        if (index > -1) {
          form.value.files.splice(index, 1);
        }
      } else {
        proxy.$modal.msgError(res.msg || "删除失败");
        // 如果删除失败,重新添加回文件列表
        fileList.push(file);
      }
    }).catch((error) => {
      proxy.$modal.msgError("删除失败");
      // 如果删除失败,重新添加回文件列表
      fileList.push(file);
    });
  } else {
    // 新上传的文件,直接从form.value.files中移除
    const index = form.value.files.findIndex(f => f.name === file.name && f.url === file.url);
    if (index > -1) {
      form.value.files.splice(index, 1);
    }
  }
}
// 文件预览/下载
function handlePreview(file) {
  // 创建一个隐藏的a标签来触发下载
  proxy.$download.name(file.url);
}
// 提交知识表单
const submitForm = async () => {
@@ -707,12 +832,12 @@
}
.detail-content {
  background: #f8f9fa;
  padding: 16px;
  border-radius: 6px;
  line-height: 1.6;
  color: #606266;
  white-space: pre-wrap;
}
.detail-section {
  margin-top: 24px;
}
.key-points {