From d1d3ea967d916d798dbb1979130137bdd5b6152c Mon Sep 17 00:00:00 2001
From: yuan <123@>
Date: 星期一, 18 五月 2026 10:22:37 +0800
Subject: [PATCH] feat(search): 基础产品维护子节点模糊查询

---
 src/views/basicData/product/index.vue |   91 ++++++++++++++++++++++++---------------------
 1 files changed, 49 insertions(+), 42 deletions(-)

diff --git a/src/views/basicData/product/index.vue b/src/views/basicData/product/index.vue
index 99ab028..b05b215 100644
--- a/src/views/basicData/product/index.vue
+++ b/src/views/basicData/product/index.vue
@@ -5,7 +5,7 @@
         <el-input v-model="search"
                   style="width: 210px"
                   placeholder="杈撳叆鍏抽敭瀛楄繘琛屾悳绱�"
-                  @change="searchFilter"
+                  @input="debouncedSearch"
                   @clear="searchFilter"
                   clearable
                   prefix-icon="Search" />
@@ -43,12 +43,12 @@
                 <el-button type="primary"
                            link
                            :disabled="isTopLevelNode(data, node)"
-                           @click="openProDia('edit', data)">
+                           @click="openProDia('edit', data, node)">
                   缂栬緫
                 </el-button>
                 <el-button type="primary"
                            link
-                           @click="openProDia('add', data)">
+                           @click="openProDia('add', data, node)">
                   娣诲姞浜у搧
                 </el-button>
                 <el-button v-if="!node.childNodes.length"
@@ -285,6 +285,8 @@
   const search = ref("");
   const currentId = ref("");
   const currentParentId = ref("");
+  /** 浜у搧寮圭獥锛歛dd 瀛樼埗鑺傜偣 id锛沞dit 瀛樺綋鍓嶈妭鐐� id 涓� parentId锛堜笉渚濊禆鏍戦�変腑椤癸級 */
+  const productDialogTarget = ref(null);
   const operationType = ref("");
   const treeLoad = ref(false);
   const list = ref([]);
@@ -388,17 +390,28 @@
     return [null, undefined, "", 0, "0"].includes(data?.parentId);
   };
   // 鎵撳紑浜у搧寮规
-  const openProDia = (type, data) => {
-    if (data && type === "edit" && isTopLevelNode(data)) {
+  const openProDia = (type, data, node) => {
+    if (data && type === "edit" && isTopLevelNode(data, node)) {
       proxy.$modal.msgWarning("涓�绾ц妭鐐逛笉鑳界紪杈戞垨鍒犻櫎");
       return;
     }
     operationType.value = type;
-    productDia.value = true;
-    form.value.productName = "";
-    if (type === "edit") {
-      form.value.productName = data.productName;
+    productDialogTarget.value = null;
+    if (type === "add" && data) {
+      productDialogTarget.value = { parentId: data.id };
+    } else if (type === "edit" && data) {
+      let parentId = data.parentId;
+      if (
+        [null, undefined, ""].includes(parentId) &&
+        node?.parent?.data?.id != null
+      ) {
+        parentId = node.parent.data.id;
+      }
+      productDialogTarget.value = { id: data.id, parentId };
     }
+    productDia.value = true;
+    form.value.productName =
+      type === "edit" && data ? data.productName : "";
   };
   // 鎵撳紑瑙勬牸鍨嬪彿寮规
   const openModelDia = (type, data) => {
@@ -417,14 +430,16 @@
     proxy.$refs.formRef.validate(valid => {
       if (valid) {
         if (operationType.value === "add") {
-          form.value.parentId = currentId.value;
+          form.value.parentId =
+            productDialogTarget.value?.parentId ?? currentId.value;
           form.value.id = "";
         } else if (operationType.value === "addOne") {
           form.value.id = "";
           form.value.parentId = "";
         } else {
-          form.value.id = currentId.value;
-          form.value.parentId = "";
+          form.value.id =
+            productDialogTarget.value?.id ?? currentId.value;
+          form.value.parentId = productDialogTarget.value?.parentId ?? "";
         }
         addOrEditProduct(form.value).then(res => {
           proxy.$modal.msgSuccess("鎻愪氦鎴愬姛");
@@ -437,6 +452,7 @@
   // 鍏抽棴浜у搧寮规
   const closeProDia = () => {
     proxy.$refs.formRef.resetFields();
+    productDialogTarget.value = null;
     productDia.value = false;
   };
 
@@ -549,40 +565,31 @@
         proxy.$modal.msg("宸插彇娑�");
       });
   };
-  // 璋冪敤tree杩囨护鏂规硶 涓枃鑻辫繃婊�
-  const filterNode = (value, data, node) => {
-    if (!value) {
-      //濡傛灉鏁版嵁涓虹┖锛屽垯杩斿洖true,鏄剧ず鎵�鏈夌殑鏁版嵁椤�
-      return true;
-    }
-    // 鏌ヨ鍒楄〃鏄惁鏈夊尮閰嶆暟鎹紝灏嗗�煎皬鍐欙紝鍖归厤鑻辨枃鏁版嵁
-    let val = value.toLowerCase();
-    return chooseNode(val, data, node); // 璋冪敤杩囨护浜屽眰鏂规硶
+  const debounce = (fn, delay = 300) => {
+    let timer;
+    return (...args) => {
+      clearTimeout(timer);
+      timer = setTimeout(() => fn(...args), delay);
+    };
   };
-  // 杩囨护鐖惰妭鐐� / 瀛愯妭鐐� (濡傛灉杈撳叆鐨勫弬鏁版槸鐖惰妭鐐逛笖鑳藉尮閰嶏紝鍒欒繑鍥炶鑺傜偣浠ュ強鍏朵笅鐨勬墍鏈夊瓙鑺傜偣锛涘鏋滃弬鏁版槸瀛愯妭鐐癸紝鍒欒繑鍥炶鑺傜偣鐨勭埗鑺傜偣銆俷ame鏄腑鏂囧瓧绗︼紝enName鏄嫳鏂囧瓧绗�.
-  const chooseNode = (value, data, node) => {
-    if (data.label.indexOf(value) !== -1) {
+
+  const debouncedSearch = debounce(() => {
+    searchFilter();
+  }, 300);
+
+  const filterNode = (value, data) => {
+    if (!value) return true;
+    return chooseNode(value.toLowerCase(), data);
+  };
+
+  const chooseNode = (value, data) => {
+    const label = (data.label || '').toLowerCase();
+    if (label.indexOf(value) !== -1) {
       return true;
     }
-    const level = node.level;
-    // 濡傛灉浼犲叆鐨勮妭鐐规湰韬氨鏄竴绾ц妭鐐瑰氨涓嶇敤鏍¢獙浜�
-    if (level === 1) {
-      return false;
+    if (data.children && data.children.length > 0) {
+      return data.children.some(child => chooseNode(value, child));
     }
-    // 鍏堝彇褰撳墠鑺傜偣鐨勭埗鑺傜偣
-    let parentData = node.parent;
-    // 閬嶅巻褰撳墠鑺傜偣鐨勭埗鑺傜偣
-    let index = 0;
-    while (index < level - 1) {
-      // 濡傛灉鍖归厤鍒扮洿鎺ヨ繑鍥烇紝姝ゅname鍊兼槸涓枃瀛楃锛宔nName鏄嫳鏂囧瓧绗︺�傚垽鏂尮閰嶄腑鑻辨枃杩囨护
-      if (parentData.data.label.indexOf(value) !== -1) {
-        return true;
-      }
-      // 鍚﹀垯鐨勮瘽鍐嶅線涓婁竴灞傚仛鍖归厤
-      parentData = parentData.parent;
-      index++;
-    }
-    // 娌″尮閰嶅埌杩斿洖false
     return false;
   };
   getProductTreeList();

--
Gitblit v1.9.3