gaoluyang
6 小时以前 04687ca035e6fa517e88470aac7247812f85eb95
src/views/productionManagement/productStructure/Detail/index.vue
@@ -1,6 +1,6 @@
<template>
  <div class="app-container">
    <PageHeader content="产品结构详情">
  <div :class="embedded ? 'embedded-container' : 'app-container'">
    <PageHeader v-if="!embedded" content="产品结构详情">
      <template #right-button>
        <el-button v-if="!dataValue.isEdit && !isOrderPage"
                   type="primary"
@@ -115,11 +115,11 @@
                    <el-input v-model="row.unit"
                              placeholder="请输入单位"
                              clearable
                               :disabled="!dataValue.isEdit || dataValue.dataList.some(item => (item as any).tempId === row.tempId)" />
                              :disabled="!dataValue.isEdit || dataValue.dataList.some(item => (item as any).tempId === row.tempId)" />
                  </el-form-item>
                </template>
              </el-table-column>
              <el-table-column label="操作"
              <el-table-column v-if="!embedded" label="操作"
                               fixed="right"
                               width="200">
                <template #default="{ row, $index }">
@@ -174,6 +174,18 @@
  const ProductSelectDialog = defineAsyncComponent(
    () => import("@/views/basicData/product/ProductSelectDialog.vue")
  );
  const props = defineProps({
    embedded: {
      type: Boolean,
      default: false,
    },
    // 显式指定BOM主键(用于嵌入到“工艺路线项目”等页面时,路由 query.id 不是 bomId 的情况)
    bomId: {
      type: [String, Number],
      default: undefined,
    },
  });
  const embedded = computed(() => props.embedded);
  const emit = defineEmits(["update:router"]);
  const form = ref();
@@ -181,7 +193,8 @@
  const router = useRouter();
  const routeId = computed({
    get() {
      return route.query.id;
      // 优先使用外部传入的 bomId,其次使用路由的 bomId,最后回退到路由的 id(兼容原页面)
      return props.bomId ?? route.query.bomId ?? route.query.id;
    },
    set(val) {
@@ -227,29 +240,27 @@
  };
  const fetchData = async () => {
    if (isOrderPage.value) {
      // 订单情况:使用订单的产品结构接口
      const { data } = await listProcessBom({ orderId: routeOrderId.value });
      dataValue.dataList = (data as any) || [];
    } else {
      // 非订单情况:使用原来的接口
      const { data } = await queryList(routeId.value);
      dataValue.dataList = (data as any) || [];
      // 为所有项及其子项设置name属性
      const setNameRecursively = (items: any[]) => {
        items.forEach((item: any) => {
          item.tempId = item.id;
          item.processName =
            dataValue.processOptions.find(option => option.id === item.processId)
              ?.name || "";
          if (item.children && item.children.length > 0) {
            setNameRecursively(item.children);
          }
        });
      };
      setNameRecursively(dataValue.dataList);
      console.log(dataValue.dataList, "dataValue.dataList");
    const setNameRecursively = (items: any[]) => {
      items.forEach((item: any) => {
        item.tempId = item.tempId || item.id || new Date().getTime() + Math.random();
        item.processName =
          dataValue.processOptions.find(option => option.id === item.processId)?.name || item.processName || "";
        if (item.children && item.children.length > 0) {
          setNameRecursively(item.children);
        }
      });
    };
    // 统一使用 BOM 查询产品结构:/productStructure/listBybomId/{bomId}
    // 说明:订单页也会从路由/父组件带入 bomId(route.query.bomId 或 props.bomId)
    const bomId = routeId.value;
    if (!bomId) {
      dataValue.dataList = [];
      return;
    }
    const { data } = await queryList(bomId);
    dataValue.dataList = (data as any) || [];
    setNameRecursively(dataValue.dataList);
  };
  const fetchProcessOptions = async () => {
@@ -264,15 +275,20 @@
    const productData = row[0];
    //  最外层组件中,与当前产品相同的产品只能有一个
    const isTopLevel = dataValue.dataList.some(item => (item as any).tempId === dataValue.currentRowName);
    const isTopLevel = dataValue.dataList.some(
      item => (item as any).tempId === dataValue.currentRowName
    );
    if (isTopLevel) {
      if (productData.productName === tableData[0].productName &&
        productData.model === tableData[0].model) {
      if (
        productData.productName === tableData[0].productName &&
        productData.model === tableData[0].model
      ) {
        //  查找是否已经有其他顶层行已经是这个产品
        const hasOther = dataValue.dataList.some(item =>
          (item as any).tempId !== dataValue.currentRowName &&
          (item as any).productName === tableData[0].productName &&
          (item as any).model === tableData[0].model
        const hasOther = dataValue.dataList.some(
          item =>
            (item as any).tempId !== dataValue.currentRowName &&
            (item as any).productName === tableData[0].productName &&
            (item as any).model === tableData[0].model
        );
        if (hasOther) {
          ElMessage.warning("最外层和当前产品一样的一级只能有一个");
@@ -390,7 +406,7 @@
    }
  };
  const removeItem = (tempId:string) => {
  const removeItem = (tempId: string) => {
    // 先尝试从顶层删除
    const topIndex = dataValue.dataList.findIndex(item => item.tempId === tempId);
    if (topIndex !== -1) {
@@ -513,4 +529,11 @@
    await fetchProcessOptions();
    await fetchData();
  });
</script>
</script>
<style scoped>
.embedded-container {
  padding: 0;
  margin: 0;
}
</style>