gaoluyang
昨天 0f075e80d7c9fbe731419891ee55948be6f8fb65
src/views/basicData/product/index.vue
@@ -1,7 +1,7 @@
<template>
  <div class="app-container product-view">
    <div class="left">
      <div>
      <div class="left-header">
        <el-input
          v-model="search"
          style="width: 210px"
@@ -11,12 +11,17 @@
          clearable
          prefix-icon="Search"
        />
        <el-button
          type="primary"
          @click="openProDia('addOne')"
          style="margin-left: 10px"
          >新增产品大类</el-button
        >
        <div class="button-group">
          <el-button
            type="primary"
            @click="openProDia('addOne')"
          >新增产品大类</el-button>
          <el-button
            type="success"
            @click="handleImport"
            icon="Upload"
          >导入</el-button>
        </div>
      </div>
      <div ref="containerRef">
        <el-tree
@@ -124,8 +129,7 @@
        </div>
      </template>
    </el-dialog>
    <el-dialog
      v-model="modelDia"
    <el-dialog v-model="modelDia"
      title="规格型号"
      width="400px"
      @close="closeModelDia"
@@ -182,12 +186,47 @@
        </div>
      </template>
    </el-dialog>
    <el-dialog v-model="importDia" title="产品导入" width="600px">
      <el-upload
        ref="importUploadRef"
        :limit="1"
        accept=".xlsx,.xls"
        :action="importUpload.url"
        :headers="importUpload.headers"
        :before-upload="importUpload.beforeUpload"
        :on-success="importUpload.onSuccess"
        :on-error="importUpload.onError"
        :on-progress="importUpload.onProgress"
        :on-change="importUpload.onChange"
        :auto-upload="false"
        drag
      >
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">
          将文件拖到此处,或<em>点击上传</em>
        </div>
        <template #tip>
          <div class="el-upload__tip">
            仅支持 xls/xlsx,大小不超过 10MB。
            <el-button link type="primary" @click="importTemplate">下载导入模板</el-button>
          </div>
        </template>
      </el-upload>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitImport">确认导入</el-button>
          <el-button @click="importDia = false">取消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>
<script setup>
import { ref } from "vue";
import { ElMessageBox } from "element-plus";
import { getToken } from "@/utils/auth.js";
import { FileUpload } from "@/components/Upload";
import {
  addOrEditProduct,
  addOrEditProductModel,
@@ -195,15 +234,18 @@
  delProductModel,
  modelListPage,
  productTreeList,
  downloadTemplate,
} from "@/api/basicData/product.js";
import ImportExcel from "./ImportExcel/index.vue";
const { proxy } = getCurrentInstance();
const tree = ref(null);
const containerRef = ref(null);
const importUploadRef = ref(null);
const productDia = ref(false);
const modelDia = ref(false);
const importDia = ref(false);
const modelOperationType = ref("");
const search = ref("");
const currentId = ref("");
@@ -271,6 +313,53 @@
  },
});
const { form, rules, modelForm, modelRules } = toRefs(data);
const importUpload = reactive({
  title: "产品导入",
  open: false,
  url: import.meta.env.VITE_APP_BASE_API + "/basic/product/import",
  headers: { Authorization: "Bearer " + getToken() },
  isUploading: false,
  beforeUpload: (file) => {
    const isExcel = file.name.endsWith('.xlsx') || file.name.endsWith('.xls');
    const isLt10M = file.size / 1024 / 1024 < 10;
    if (!isExcel) {
      proxy.$modal.msgError("上传文件只能是 xlsx/xls 格式!");
      return false;
    }
    if (!isLt10M) {
      proxy.$modal.msgError("上传文件大小不能超过 10MB!");
      return false;
    }
    return true;
  },
  onChange: (file, fileList) => {
    console.log('文件状态改变', file, fileList);
  },
  onProgress: (event, file, fileList) => {
    console.log('上传中...', event.percent);
  },
  onSuccess: (response, file, fileList) => {
    console.log('上传成功', response, file, fileList);
    importUpload.isUploading = false;
    if (response.code === 200) {
      proxy.$modal.msgSuccess("导入成功");
      importDia.value = false;
      if (importUploadRef.value) {
        importUploadRef.value.clearFiles();
      }
      getProductTreeList();
    } else {
      proxy.$modal.msgError(response.msg || "导入失败");
    }
  },
  onError: (error, file, fileList) => {
    console.log('上传失败', error, file, fileList);
    importUpload.isUploading = false;
    proxy.$modal.msgError("导入失败");
  }
});
// 查询产品树
const getProductTreeList = () => {
  treeLoad.value = true;
@@ -478,6 +567,22 @@
  // 没匹配到返回false
  return false;
};
const handleImport = () => {
  importDia.value = true;
  if (importUploadRef.value) {
    importUploadRef.value.clearFiles();
  }
};
const submitImport = () => {
  importUploadRef.value.submit();
};
const importTemplate = () => {
  proxy.download("/basic/product/downloadTemplate", {}, "产品导入模板.xlsx");
};
getProductTreeList();
</script>
@@ -491,6 +596,16 @@
  padding: 16px;
  background: #ffffff;
}
.left-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 16px;
}
.button-group {
  display: flex;
  gap: 10px;
}
.right {
  flex: 1;
  min-width: 0;