gaoluyang
2026-02-03 830adfef9c17ff8915761ff218f9d8e8a04bc3ea
src/views/basicData/product/index.vue
@@ -25,18 +25,13 @@
          :data="list"
          @node-click="handleNodeClick"
          :expand-on-click-node="false"
          default-expand-all
          :default-expanded-keys="expandedKeys"
          :draggable="true"
          :filter-node-method="filterNode"
          :props="{ children: 'children', label: 'label' }"
          highlight-current
          node-key="id"
          style="
            height: calc(100vh - 190px);
            overflow-y: scroll;
            scrollbar-width: none;
          "
          class="product-tree-scroll"
          style="height: calc(100vh - 190px); overflow-y: auto"
        >
          <template #default="{ node, data }">
            <div class="custom-tree-node">
@@ -45,7 +40,7 @@
                  <component :is="data.children && data.children.length > 0
                  ? node.expanded ? 'FolderOpened' : 'Folder' : 'Tickets'" />
                </el-icon>
                {{ data.label }}
                <span class="tree-node-label">{{ data.label }}</span>
              </span>
              <div>
                <el-button
@@ -55,7 +50,7 @@
                >
                  编辑
                </el-button>
                <el-button type="primary" link @click="openProDia('add', data)">
                <el-button type="primary" link @click="openProDia('add', data)" :disabled="node.level >= 3">
                  添加产品
                </el-button>
                <el-button
@@ -97,9 +92,20 @@
        @selection-change="handleSelectionChange"
        :tableLoading="tableLoading"
        @pagination="pagination"
      ></PIMTable>
      >
        <template #productImage="{ row }">
          <img
            v-if="row.url"
            class="upload-img"
            :src="javaApiUrl + row.url"
            @click="previewImage(row.url)"
            style="cursor: pointer"
          />
          <span v-else style="color: #909399">暂无图片</span>
        </template>
      </PIMTable>
    </div>
    <el-dialog v-model="productDia" title="产品" width="400px">
    <el-dialog v-model="productDia" title="产品" width="400px" @keydown.enter.prevent>
      <el-form
        :model="form"
        label-width="140px"
@@ -113,7 +119,10 @@
              <el-input
                v-model="form.productName"
                placeholder="请输入产品名称"
                maxlength="20"
                show-word-limit
                clearable
                @keydown.enter.prevent
              />
            </el-form-item>
          </el-col>
@@ -129,8 +138,9 @@
    <el-dialog
      v-model="modelDia"
      title="规格型号"
      width="400px"
      width="600px"
      @close="closeModelDia"
      @keydown.enter.prevent
    >
      <el-form
        :model="modelForm"
@@ -139,25 +149,98 @@
        :rules="modelRules"
        ref="modelFormRef"
      >
        <el-row>
          <el-col :span="24">
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="规格型号:" prop="model">
              <el-input
                v-model="modelForm.model"
                placeholder="请输入规格型号"
                clearable
                @keydown.enter.prevent
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
          <el-col :span="12">
            <el-form-item label="单位:" prop="unit">
              <el-input
                v-model="modelForm.unit"
                placeholder="请输入单位"
                clearable
                @keydown.enter.prevent
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="高度:" prop="height">
              <el-input
                v-model="modelForm.height"
                placeholder="请输入高度"
                clearable
                @keydown.enter.prevent
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="每件数量/支:" prop="boxNum">
              <el-input-number
                :step="1"
                :min="0"
                style="width: 100%"
                v-model="modelForm.boxNum"
                @change="calculateTotalPrice"
                placeholder="请输入每件数量"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="单价(元)/件:" prop="taxInclusiveUnitPrice">
              <el-input-number
                :step="0.01"
                :min="0"
                style="width: 100%"
                v-model="modelForm.taxInclusiveUnitPrice"
                @change="calculateTotalPrice"
                placeholder="请输入单价"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="单价(美元)/件:" prop="dollarPrice">
              <el-input-number
                :step="0.01"
                :min="0"
                style="width: 100%"
                v-model="modelForm.dollarPrice"
                placeholder="请输入美元单价"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="24">
            <el-form-item label="产品图片:" prop="url">
              <el-upload
                :action="uploadUrl"
                :before-upload="handleBeforeUpload"
                :on-success="(res, file) => handleUploadSuccess(res, file)"
                :on-error="handleUploadError"
                name="file"
                :show-file-list="false"
                :headers="headers"
                accept="image/*"
                :data="{ type: 13 }"
              >
                <img
                  v-if="modelForm.url"
                  class="upload-img-dialog"
                  :src="javaApiUrl + modelForm.url"
                />
                <el-icon v-else class="avatar-uploader-icon-dialog"><Plus /></el-icon>
              </el-upload>
            </el-form-item>
          </el-col>
        </el-row>
@@ -173,8 +256,10 @@
</template>
<script setup>
import { ref } from "vue";
import { ref, reactive, toRefs, getCurrentInstance, nextTick } from "vue";
import { ElMessageBox } from "element-plus";
import { Plus } from "@element-plus/icons-vue";
import { getToken } from "@/utils/auth";
import {
  addOrEditProduct,
  addOrEditProductModel,
@@ -184,6 +269,7 @@
  productTreeList,
} from "@/api/basicData/product.js";
import ImportExcel from "./ImportExcel/index.vue";
import PIMTable from "@/components/PIMTable/PIMTable.vue";
const { proxy } = getCurrentInstance();
const tree = ref(null);
@@ -201,12 +287,40 @@
const expandedKeys = ref([]);
const tableColumn = ref([
  {
    label: "产品图片",
    prop: "url",
    dataType: "slot",
    slot: "productImage",
    align: "center",
    width: 100,
  },
  {
    label: "规格型号",
    prop: "model",
  },
  {
    label: "单位",
    prop: "unit",
  },
  {
    label: "高度",
    prop: "height",
    width: 120,
  },
  {
    label: "每件数量/支",
    prop: "boxNum",
    width: 120,
  },
  {
    label: "单价(元)/件",
    prop: "taxInclusiveUnitPrice",
    width: 120,
  },
  {
    label: "单价(美元)/件",
    prop: "dollarPrice",
    width: 130,
  },
  {
    dataType: "action",
@@ -227,6 +341,11 @@
const tableLoading = ref(false);
const isShowButton = ref(false);
const selectedRows = ref([]);
// 上传配置
const uploadUrl = ref(import.meta.env.VITE_APP_BASE_API + "/file/upload"); // 上传的图片服务器地址
const headers = ref({ Authorization: "Bearer " + getToken() });
const javaApiUrl = proxy.javaApi || import.meta.env.VITE_APP_BASE_API;
const page = reactive({
  current: 1,
  size: 10,
@@ -237,15 +356,28 @@
    productName: "",
  },
  rules: {
    productName: [{ required: true, message: "请输入", trigger: "blur" }],
    productName: [
      { required: true, message: "请输入", trigger: "blur" },
      { max: 20, message: "产品名称不能超过20个字符", trigger: "blur" },
    ],
  },
  modelForm: {
    model: "",
    unit: "",
    url: "",
    height: "",
    boxNum: null,
    taxInclusiveUnitPrice: null,
    dollarPrice: null,
  },
  modelRules: {
    model: [{ required: true, message: "请输入", trigger: "blur" }],
    unit: [{ required: true, message: "请输入", trigger: "blur" }],
    model: [{ required: true, message: "请输入规格型号", trigger: "blur" }],
    unit: [{ required: true, message: "请输入单位", trigger: "blur" }],
    url: [{ required: true, message: "请上传产品图片", trigger: "change" }],
    height: [{ required: true, message: "请输入高度", trigger: "blur" }],
    boxNum: [{ required: true, message: "请输入每件数量/支", trigger: "change" }],
    taxInclusiveUnitPrice: [{ required: true, message: "请输入单价(元)/件", trigger: "change" }],
    dollarPrice: [{ required: true, message: "请输入单价(美元)/件", trigger: "change" }],
  },
});
const { form, rules, modelForm, modelRules } = toRefs(data);
@@ -281,11 +413,30 @@
const openModelDia = (type, data) => {
  modelOperationType.value = type;
  modelDia.value = true;
  modelForm.value.model = "";
  modelForm.value.model = "";
  modelForm.value.id = "";
  if (type === "edit") {
    modelForm.value = { ...data };
  // 重置表单
  modelForm.value = {
    model: "",
    unit: "",
    url: "",
    height: "",
    boxNum: null,
    taxInclusiveUnitPrice: null,
    dollarPrice: null,
    id: "",
  };
  if (type === "edit" && data) {
    // 如果 url 是 Windows 路径,需要转换
    let url = data.url || "";
    if (url && url.indexOf("\\") > -1) {
      url = processFileUrl(url);
    }
    modelForm.value = {
      ...data,
      url: url,
      boxNum: data.boxNum || null,
      taxInclusiveUnitPrice: data.taxInclusiveUnitPrice || null,
      dollarPrice: data.dollarPrice || null,
    };
  }
};
// 提交产品名称修改
@@ -315,6 +466,7 @@
  proxy.$refs.formRef.resetFields();
  productDia.value = false;
};
// 删除产品
const remove = (node, data) => {
  let ids = [];
@@ -386,10 +538,107 @@
    size: page.size,
  }).then((res) => {
    console.log("res", res);
    tableData.value = res.records;
    // 处理返回的数据,转换 Windows 路径为可访问的 URL
    tableData.value = (res.records || []).map((item) => {
      if (item.url && item.url.indexOf("\\") > -1) {
        item.url = processFileUrl(item.url);
      }
      return item;
    });
    page.total = res.total;
    tableLoading.value = false;
  });
};
// 上传前校验
const handleBeforeUpload = (file) => {
  const isImage = file.type.startsWith("image/");
  const isLt5M = file.size / 1024 / 1024 < 5;
  if (!isImage) {
    proxy.$modal.msgError("上传文件只能是图片格式!");
    return false;
  }
  if (!isLt5M) {
    proxy.$modal.msgError("上传图片大小不能超过 5MB!");
    return false;
  }
  return true;
};
// 处理文件路径:将 Windows 路径转换为可访问的 URL
const processFileUrl = (filePath) => {
  if (!filePath) return "";
  // 如果路径是 Windows 路径格式(包含反斜杠),需要转换
  if (filePath && filePath.indexOf("\\") > -1) {
    // 查找 temp 或 uploads 关键字的位置
    const tempIndex = filePath.toLowerCase().indexOf("temp");
    const uploadsIndex = filePath.toLowerCase().indexOf("uploads");
    if (tempIndex > -1) {
      // 从 temp 开始提取相对路径,并将反斜杠替换为正斜杠
      const relativePath = filePath.substring(tempIndex).replace(/\\/g, "/");
      filePath = "/" + relativePath;
    } else if (uploadsIndex > -1) {
      // 从 uploads 开始提取相对路径
      const relativePath = filePath.substring(uploadsIndex).replace(/\\/g, "/");
      filePath = "/" + relativePath;
    } else {
      // 如果没有找到关键字,提取文件名
      const parts = filePath.split("\\");
      const fileName = parts[parts.length - 1];
      filePath = "/temp/uploads/" + fileName;
    }
  }
  // 确保路径以 / 开头
  if (filePath && !filePath.startsWith("/")) {
    filePath = "/" + filePath;
  }
  return filePath;
};
// 上传成功
const handleUploadSuccess = (res, file) => {
  if (res.code === 200) {
    // 从 res.data 中获取 tempPath,并转换为可访问的 URL
    const tempPath = res.data?.tempPath || res.tempPath;
    if (tempPath) {
      const relativePath = processFileUrl(tempPath);
      modelForm.value.url = relativePath;
      proxy.$modal.msgSuccess("上传成功");
      // 触发表单验证
      nextTick(() => {
        proxy.$refs.modelFormRef?.validateField("url");
      });
    } else {
      proxy.$modal.msgError("上传成功但未返回文件路径");
    }
  } else {
    proxy.$modal.msgError(res.msg || "上传失败");
  }
};
// 上传失败
const handleUploadError = () => {
  proxy.$modal.msgError("上传失败,请重试");
};
// 计算总价
const calculateTotalPrice = () => {
  // 如果需要计算总价,可以在这里添加逻辑
  // if (modelForm.value.boxNum && modelForm.value.taxInclusiveUnitPrice) {
  //   modelForm.value.totalPrice = modelForm.value.boxNum * modelForm.value.taxInclusiveUnitPrice;
  // }
};
// 预览图片
const previewImage = (url) => {
  if (url) {
    window.open(javaApiUrl + url, "_blank");
  }
};
// 删除规格型号
const handleDelete = () => {
@@ -464,18 +713,21 @@
  display: flex;
}
.left {
  width: 380px;
  width: 450px;
  min-width: 450px;
  padding: 16px;
  background: #ffffff;
}
.right {
  width: calc(100% - 380px);
  flex: 1;
  min-width: 0;
  padding: 16px;
  margin-left: 20px;
  background: #ffffff;
}
.custom-tree-node {
  flex: 1;
  min-width: 0;
  display: flex;
  align-items: center;
  justify-content: space-between;
@@ -483,13 +735,92 @@
  padding-right: 8px;
}
.tree-node-content {
  flex: 1;
  min-width: 0;
  display: flex;
  align-items: center; /* 垂直居中 */
  align-items: center;
  height: 100%;
  overflow: hidden;
}
.tree-node-content .orange-icon {
  flex-shrink: 0;
}
.tree-node-label {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.orange-icon {
  color: orange;
  font-size: 18px;
  margin-right: 8px; /* 图标与文字之间加点间距 */
}
.product-tree-scroll {
  scrollbar-width: thin;
  scrollbar-color: #c0c4cc #f5f7fa;
}
.product-tree-scroll::-webkit-scrollbar {
  width: 8px;
}
.product-tree-scroll::-webkit-scrollbar-track {
  background: #f5f7fa;
  border-radius: 4px;
}
.product-tree-scroll::-webkit-scrollbar-thumb {
  background: #c0c4cc;
  border-radius: 4px;
}
.product-tree-scroll::-webkit-scrollbar-thumb:hover {
  background: #909399;
}
.upload-img {
  width: 50px;
  height: 50px;
  object-fit: cover;
  cursor: pointer;
  border-radius: 4px;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 50px;
  height: 50px;
  line-height: 50px;
  text-align: center;
  border: 1px dashed #d9d9d9;
  border-radius: 4px;
  cursor: pointer;
}
.avatar-uploader-icon:hover {
  border-color: #409eff;
}
.upload-img-dialog {
  width: 100px;
  height: 100px;
  object-fit: cover;
  cursor: pointer;
  border-radius: 4px;
  border: 1px solid #dcdfe6;
}
.avatar-uploader-icon-dialog {
  font-size: 28px;
  color: #8c939d;
  width: 100px;
  height: 100px;
  line-height: 100px;
  text-align: center;
  border: 1px dashed #d9d9d9;
  border-radius: 4px;
  cursor: pointer;
  display: block;
}
.avatar-uploader-icon-dialog:hover {
  border-color: #409eff;
}
</style>