From bd3c5bd24f0d5c5b8669bb034a75d46d7a10599d Mon Sep 17 00:00:00 2001
From: 闫增涛 <1829501689@qq.com>
Date: 星期三, 10 四月 2024 11:15:39 +0800
Subject: [PATCH] 资源购买页tree组件勾选联通上下级功能完善

---
 packageBookService/pages/bookServices/detail/buyResource/index.js       |   54 +++++++++++++
 packageBookService/pages/bookServices/detail/components/tree/index.wxml |    1 
 packageBookService/pages/bookServices/detail/components/tree/index.js   |  181 +++++++++++++++++++++++++++++++++++----------
 packageBookService/pages/bookServices/detail/buyResource/index.wxml     |    3 
 4 files changed, 198 insertions(+), 41 deletions(-)

diff --git a/packageBookService/pages/bookServices/detail/buyResource/index.js b/packageBookService/pages/bookServices/detail/buyResource/index.js
index a6385ef..34db152 100644
--- a/packageBookService/pages/bookServices/detail/buyResource/index.js
+++ b/packageBookService/pages/bookServices/detail/buyResource/index.js
@@ -455,4 +455,58 @@
         });
     }
   },
+
+  // 绔犺妭鍕鹃�夋柟娉�
+  findAndUpdateItemsByIds(tree, ids, state) {
+    function findAndUpdate(node) {
+      if (ids.includes(node.id)) {
+        // console.log(node, 'node');
+        node.checked = state; // 灏嗙洰鏍囬」鐨� check 灞炴�ц缃负 true
+      }
+      if (node.children && node.children.length > 0) {
+        for (let child of node.children) {
+          findAndUpdate(child); // 閫掑綊澶勭悊瀛愯妭鐐�
+        }
+      }
+    }
+    for (let node of tree) {
+      findAndUpdate(node); // 瀵规瘡涓《灞傝妭鐐规墽琛屾煡鎵惧拰鏇存柊鎿嶄綔
+    }
+    // console.log(tree, 'tree345');
+    return tree; // 杩斿洖淇敼鍚庣殑瀹屾暣鏁扮粍
+  },
+  // 鍗曚釜鍕鹃�夋柟娉�
+  checkResoucrceInfo(tree, id) {
+    function findAndUpdate(node) {
+      if (node.sysType == "CmsFolder" && node.children.length > 0) {
+        for (let index = 0; index < node.children.length; index++) {
+          const element = node.children[index];
+          findAndUpdate(element);
+        }
+      } else if (id == node.id && node.sysType == "CmsItem") {
+        node.checked = !node.checked; // 灏嗙洰鏍囬」鐨� check 灞炴�ц缃负 true
+      }
+    }
+    for (let node of tree) {
+      findAndUpdate(node); // 瀵规瘡涓《灞傝妭鐐规墽琛屾煡鎵惧拰鏇存柊鎿嶄綔
+    }
+    return tree; // 杩斿洖淇敼鍚庣殑瀹屾暣鏁扮粍
+  },
+  // 绔犺妭鍕鹃�変簨浠�
+  changeListChecked(e) {
+    const ids = e.detail.ids
+    const state = e.detail.flag
+    const updated = this.findAndUpdateItemsByIds(this.data.learn, ids, state)
+    this.setData({
+      learn: updated
+    })
+  },
+  // 鍗曚釜鍕鹃�変簨浠�
+  changeResourceChecked(e) {
+    const id = e.detail.id
+    const updated = this.checkResoucrceInfo(this.data.learn, id)
+    this.setData({
+      learn: updated
+    })
+  }
 });
\ No newline at end of file
diff --git a/packageBookService/pages/bookServices/detail/buyResource/index.wxml b/packageBookService/pages/bookServices/detail/buyResource/index.wxml
index f70f6ae..9cd0347 100644
--- a/packageBookService/pages/bookServices/detail/buyResource/index.wxml
+++ b/packageBookService/pages/bookServices/detail/buyResource/index.wxml
@@ -24,11 +24,14 @@
     paymentPage="{{paymentPage}}"
     superior="{{superior}}"
     treeList="{{learn}}"
+    learnList="{{learn}}"
     tab="{{'jsek_cloudLearning'}}"
     isShowCheck="{{true}}"
     openIds="{{openLearnids}}"
     checkAllState="{{checkAllState}}"
     bind:changeCheckAll="changeCheckAll"
+    bind:changeListChecked="changeListChecked"
+    bind:changeResourceChecked="changeResourceChecked"
   />
 </view>
 
diff --git a/packageBookService/pages/bookServices/detail/components/tree/index.js b/packageBookService/pages/bookServices/detail/components/tree/index.js
index c4f9ab8..21592ed 100644
--- a/packageBookService/pages/bookServices/detail/components/tree/index.js
+++ b/packageBookService/pages/bookServices/detail/components/tree/index.js
@@ -4,12 +4,14 @@
 } from "../../../../../../assets/js/login";
 const app = getApp();
 Component({
-  // checked: false,
-
   properties: {
     treeList: {
       type: Array,
       value: [],
+    },
+    learnList: {
+      type: Array,
+      value: []
     },
     bookInfo: {
       type: Object,
@@ -63,10 +65,6 @@
     this.setData({
       sonPurchaseSaleMethodId: [],
     });
-    console.log(
-      this.data.sonPurchaseSaleMethodId,
-      "sonPurchaseSaleMethodId789"
-    );
   },
   observers: {
     treeList: function (newValue) {
@@ -311,20 +309,30 @@
       // const flag =
       //   list.findIndex((item) => item.checked == false) > -1 ? true : false;
       const flag = this.properties.checkAllState;
-      const updataList = this.findAndUpdateItemsByIds(
-        this.properties.treeList,
+      // const updataList = this.findAndUpdateItemsByIds(
+      //   this.properties.treeList,
+      //   ids,
+      //   flag
+      // );
+      // this.setData({
+      //   treeList: updataList,
+      // });
+
+      var myEventDetail = {
         ids,
-        flag
-      );
-      console.log(checked, "list");
+        flag,
+      }; // detail瀵硅薄锛屾彁渚涚粰浜嬩欢鐩戝惉鍑芥暟
+      var myEventOption = {
+        bubbles: true,
+        composed: true,
+      }; // 瑙﹀彂浜嬩欢鐨勯�夐」
+      this.triggerEvent("changeListChecked", myEventDetail, myEventOption);
+
       if (!checked) {
         this.setData({
           selectAll: list,
         });
       }
-      this.setData({
-        treeList: updataList,
-      });
       var myEventDetail = {
         value: !flag,
       }; // detail瀵硅薄锛屾彁渚涚粰浜嬩欢鐩戝惉鍑芥暟
@@ -341,27 +349,43 @@
       // 杩欎釜鏃跺�檆hecked鐨勫�兼槸fals
       let checkResourceTitleList = [];
       const item = e.currentTarget.dataset.item;
-      //
+      const parent = this.findParentById(this.properties.learnList, item.id)
+      let childrenList
+      let flagNum
+      if (parent) childrenList = parent.children.filter(citem => citem.id !== item.id)
+      if (childrenList) flagNum = childrenList.findIndex(item => item.checked == false)
       let list = this.flattenTree([item]);
       let ids = [];
       list.forEach((item) => {
-        ids.push(item.id);
+        if (item.sysType == 'CmsFolder' || (item.sysType == 'CmsItem' && item.saleMethod.length))
+          ids.push(item.id);
       });
-      const updatedTreeList = this.findAndUpdateItemsByIds(
-        this.properties.treeList,
+      var myEventDetail = {
         ids,
-        flag
-      );
-      this.setData({
-        treeList: updatedTreeList, // 鏇存柊 treeList 鏁版嵁
-      });
-      console.log(list, "list.checked");
+        flag,
+      }; // detail瀵硅薄锛屾彁渚涚粰浜嬩欢鐩戝惉鍑芥暟
+      var myEventOption = {
+        bubbles: true,
+        composed: true,
+      }; // 瑙﹀彂浜嬩欢鐨勯�夐」
+      this.triggerEvent("changeListChecked", myEventDetail, myEventOption);
+      if (flagNum == -1) {
+        let checkedState
+        var myEventDetail = {
+          ids: [parent.id],
+          flag,
+        }; // detail瀵硅薄锛屾彁渚涚粰浜嬩欢鐩戝惉鍑芥暟
+        var myEventOption = {
+          bubbles: true,
+          composed: true,
+        }; // 瑙﹀彂浜嬩欢鐨勯�夐」
+        this.triggerEvent("changeListChecked", myEventDetail, myEventOption);
+      }
       if (!list.checked) {
         this.setData({
           superiorPurchaseSaleMethodId: list,
         });
       }
-      // console.log(this.data.superiorPurchaseSaleMethodId, 'superiorPurchaseSaleMethodId');
       list.forEach((items) => {
         console.log(items.checked, "itesasd");
         if (!items.checked) {
@@ -378,17 +402,60 @@
     checkResource(e) {
       const citem = e.currentTarget.dataset.item;
       const parent = e.currentTarget.dataset.parent
-      const childrenList = parent.children.filter(item => item.id !== citem.id)
+      let outermost
+      if (parent) outermost = this.findParentById(this.properties.learnList, parent.id)
+      const childrenList = parent.children.filter(item => item.id !== citem.id && item.saleMethod.length)
       const flag = childrenList.findIndex(item => item.checked == false)
-      const updataList = this.checkResoucrceInfo(
-        this.properties.treeList,
-        citem.id
-      );
+      // 鏀瑰彉璇ラ」checked
+      var myEventDetail = {
+        id: citem.id
+      };
+      var myEventOption = {
+        bubbles: true,
+        composed: true,
+      };
+      this.triggerEvent("changeResourceChecked", myEventDetail, myEventOption);
+      // 閫氳繃鍒ゆ柇,鑱旈�氱埗绾hecked
       if (flag == -1) {
-        if (citem.checked == false) {
-          this.findAndUpdateItemsByIds(this.properties.treeList, [parent.id], true)
-        } else {
-          this.findAndUpdateItemsByIds(this.properties.treeList, [parent.id], false)
+        let checkedState
+        citem.checked == false ? checkedState = true : checkedState = false
+        var myEventDetail = {
+          ids: [parent.id],
+          flag: checkedState,
+        };
+        var myEventOption = {
+          bubbles: true,
+          composed: true,
+        };
+        this.triggerEvent("changeListChecked", myEventDetail, myEventOption);
+      }
+      // 鑱旈�氭渶澶栧眰绾hecked ,濡傛灉鏈変竴涓彇娑堝嬀閫�,涓斿綋鍓嶆渶澶栧眰鍕鹃��,鍒欏彇娑堟渶澶栧眰鐨勫嬀閫�
+      if (citem.checked && outermost && outermost.checked) {
+        var myEventDetail = {
+          ids: [outermost.id],
+          flag: false,
+        };
+        var myEventOption = {
+          bubbles: true,
+          composed: true,
+        };
+        this.triggerEvent("changeListChecked", myEventDetail, myEventOption);
+      }
+      // 鑱旈�氭渶澶栧眰绾hecked, 濡傛灉鎵�鏈夊瓙椤瑰嬀閫�, 涓斿綋鍓嶆渶澶栧眰鏈嬀閫�, 鍒欏姞涓婃渶澶栧眰鐨勫嬀閫�
+      if (!citem.checked && outermost) {
+        let allchildren = this.flattenTree([outermost])
+        allchildren = allchildren.filter(item => item.sysType == 'CmsItem' && item.id !== citem.id && item.saleMethod.length)
+        const satate = allchildren.findIndex(item => item.checked == false)
+        if (satate == -1) {
+          var myEventDetail = {
+            ids: [outermost.id],
+            flag: true,
+          };
+          var myEventOption = {
+            bubbles: true,
+            composed: true,
+          };
+          this.triggerEvent("changeListChecked", myEventDetail, myEventOption);
         }
       }
       if (!citem.checked) {
@@ -404,13 +471,9 @@
         this.setData({
           sonPurchaseSaleMethodId: sonPurchaseSaleMethodId,
         });
-        console.log(this.data.sonPurchaseSaleMethodId);
       } else {
         // 鍙栨秷閫変腑鐨勬椂鍊欏垹闄d
       }
-      this.setData({
-        treeList: updataList,
-      });
     },
     // 鍔犲叆璐墿杞�
     async onCloudShoppingCart(e) {
@@ -459,9 +522,45 @@
     sadd() {
       this.triggerEvent("updateShoppingCartHidden");
       this.triggerEvent("updateCloudLearning");
-      console.log(789);
     },
-  },
+    findParentById(arr, id) {
+      for (let i = 0; i < arr.length; i++) {
+        const item = arr[i];
+        // 妫�鏌ュ綋鍓嶉」鏄惁鍖归厤锛堝嵆瀹冩槸涓嶆槸鎴戜滑瑕佹壘鐨勯」锛�  
+        if (item.id === id) {
+          // 濡傛灉褰撳墠椤瑰氨鏄垜浠鎵剧殑椤癸紝鍒欏畠娌℃湁鐖剁骇锛岃繑鍥� null  
+          return null;
+        }
+        // 閫掑綊鏌ユ壘褰撳墠椤圭殑瀛愰」  
+        const child = this.findChildById(item.children, id);
+        if (child) {
+          // 濡傛灉鍦ㄥ瓙椤逛腑鎵惧埌鍖归厤鐨勯」锛岃繑鍥炲綋鍓嶉」浣滀负鐖剁骇  
+          return item;
+        }
+      }
+      // 濡傛灉娌℃湁鎵惧埌鍖归厤鐨勭埗椤癸紝杩斿洖 null  
+      return null;
+    },
 
-  catchtap() {},
+    // 杈呭姪鍑芥暟锛岀敤浜庨�掑綊鏌ユ壘瀛愰」  
+    findChildById(children, id) {
+      if (!children) {
+        return null;
+      }
+      for (let i = 0; i < children.length; i++) {
+        const child = children[i];
+        if (child.id === id) {
+          // 濡傛灉鎵惧埌鍖归厤鐨勯」锛岃繑鍥炲畠  
+          return child;
+        }
+        // 閫掑綊妫�鏌ュ瓙椤圭殑瀛愰」  
+        const subChild = this.findChildById(child.children, id);
+        if (subChild) {
+          return subChild;
+        }
+      }
+      // 濡傛灉娌℃湁鎵惧埌鍖归厤鐨勯」锛岃繑鍥� null  
+      return null;
+    }
+  },
 });
\ No newline at end of file
diff --git a/packageBookService/pages/bookServices/detail/components/tree/index.wxml b/packageBookService/pages/bookServices/detail/components/tree/index.wxml
index 4d66683..a949286 100644
--- a/packageBookService/pages/bookServices/detail/components/tree/index.wxml
+++ b/packageBookService/pages/bookServices/detail/components/tree/index.wxml
@@ -167,6 +167,7 @@
           isShowCheck="{{isShowCheck}}"
           bookInfo="{{bookInfo}}"
           treeList="{{[citem]}}"
+          learnList="{{learnList}}"
           itemId="{{itemId}}"
           tab="{{tab}}"
           buyIds="{{buyIds}}"

--
Gitblit v1.9.1