From af2e38d1d72633d03afdb6e75f8d30b7ef0d0aee Mon Sep 17 00:00:00 2001
From: 闫增涛 <1829501689@qq.com>
Date: 星期一, 29 七月 2024 15:24:30 +0800
Subject: [PATCH] 图书详情页添加对云学习目录整体购买的判断

---
 packageBookService/pages/bookServices/detail/buyResource/index.js |  727 +++++++++++++++++++++++++++++++++++++++++++++----------
 1 files changed, 587 insertions(+), 140 deletions(-)

diff --git a/packageBookService/pages/bookServices/detail/buyResource/index.js b/packageBookService/pages/bookServices/detail/buyResource/index.js
index bace5c3..4b9e9b3 100644
--- a/packageBookService/pages/bookServices/detail/buyResource/index.js
+++ b/packageBookService/pages/bookServices/detail/buyResource/index.js
@@ -1,108 +1,245 @@
 // packageBookService/pages/bookServices/detail/buyResource/index.js
-const app = getApp()
+const app = getApp();
 Page({
-
   /**
    * 椤甸潰鐨勫垵濮嬫暟鎹�
    */
   data: {
-    barHeight: '',
-    navBarHeight: '',
+    barHeight: "",
+    navBarHeight: "",
     learn: [],
     openLearnids: [],
-    bookId: '',
-    loading: false,
+    bookId: "",
+    loading: true,
     shoppingList: [],
     shoppingCartGetId: [],
+    methodId: [],
+    paymentPage: false,
+    superior: false,
+    checkAllState: true,
+    productLinkPath: '',
+    refCode: '',
+    haider: false,
+    naData: false,
+    bookRefCode: []
   },
 
   /**
    * 鐢熷懡鍛ㄦ湡鍑芥暟--鐩戝惉椤甸潰鍔犺浇
    */
   onLoad(options) {
+    this.getBookInfo(options.bookId);
     const systInfo = wx.getSystemInfoSync();
     const menu = wx.getMenuButtonBoundingClientRect(); // 鑳跺泭淇℃伅
-    const navBarHeight = (menu.top - systInfo.statusBarHeight) * 2 + menu.height; // 瀵艰埅鏍忛珮搴�
+    const navBarHeight =
+      (menu.top - systInfo.statusBarHeight) * 2 + menu.height; // 瀵艰埅鏍忛珮搴�
     this.setData({
       barHeight: systInfo.statusBarHeight,
       navBarHeight: navBarHeight,
-      bookId: options.bookId
+      bookId: options.bookId,
+      productLinkPath: options.productLinkPath,
+      refCode: options.refCode
     });
-    this.getResourceClass()
-    console.log(options);
-    this.getResourceData({ productLinkPath: options.productLinkPath, refCode: options.refCode })
-    this.getShoppingCartProductGet()
-    this.getBookInfo(options.bookId)
+    this.getResourceClass();
+    this.getShoppingCartProductGet();
   },
 
   /**
    * 鐢熷懡鍛ㄦ湡鍑芥暟--鐩戝惉椤甸潰鍒濇娓叉煋瀹屾垚
    */
-  onReady() {
-
-  },
+  onReady() {},
 
   /**
    * 鐢熷懡鍛ㄦ湡鍑芥暟--鐩戝惉椤甸潰鏄剧ず
    */
-  onShow() {
-
-  },
+  onShow() {},
 
   /**
    * 鐢熷懡鍛ㄦ湡鍑芥暟--鐩戝惉椤甸潰闅愯棌
    */
-  onHide() {
-
-  },
+  onHide() {},
 
   /**
    * 鐢熷懡鍛ㄦ湡鍑芥暟--鐩戝惉椤甸潰鍗歌浇
    */
-  onUnload() {
-
-  },
+  onUnload() {},
 
   /**
    * 椤甸潰鐩稿叧浜嬩欢澶勭悊鍑芥暟--鐩戝惉鐢ㄦ埛涓嬫媺鍔ㄤ綔
    */
-  onPullDownRefresh() {
-
-  },
+  onPullDownRefresh() {},
 
   /**
    * 椤甸潰涓婃媺瑙﹀簳浜嬩欢鐨勫鐞嗗嚱鏁�
    */
-  onReachBottom() {
-
-  },
+  onReachBottom() {},
 
   /**
    * 鐢ㄦ埛鐐瑰嚮鍙充笂瑙掑垎浜�
    */
-  onShareAppMessage() {
-
+  onShareAppMessage() {},
+  changeCheckAll(e) {
+    this.setData({
+      checkAllState: e.detail.value,
+    });
   },
   goBack() {
-    wx.navigateBack()
+    wx.navigateBack();
   },
-  //  鑾峰彇鏁欏璧勬簮  浜戝涔�  浜戞祴璇�
-  getResourceData(type) {
+
+  // 鑾峰彇鎵�鏈夊瓙椤规暟閲�
+  getChildrenItem(data) {
+    let num = 0
+
+    function getNum(data) {
+      for (let index = 0; index < data.length; index++) {
+        const item = data[index];
+        if (item.sysType == 'CmsItem') {
+          num += 1
+        } else if (item.sysType == 'CmsFolder') {
+          if (item.children && item.children.length)
+            getNum(item.children)
+        }
+      }
+    }
+    getNum(data)
+    return num
+  },
+
+  // 缁櫬风洰褰曟坊鍔犲瓙椤规暟閲�
+  changeResourceChildren(list) {
+    const changeList = (list) => {
+      for (let index = 0; index < list.length; index++) {
+        const item = list[index];
+        if (item.sysType == 'CmsFolder' && item.children && item.children.length) {
+          item.childrenItem = this.getChildrenItem([item])
+          changeList(item.children)
+        } else if (item.sysType == 'CmsFolder' && item.children && !item.children.length) {
+          item.childrenItem = 0
+        } else if (item.sysType == 'CmsFolder' && !item.children) {
+          item.childrenItem = 0
+        }
+      }
+    }
+    changeList(list)
+    return list
+  },
+
+  // 鎵佸钩鍖栨暟鎹浆鎹ree
+  getTreeList(rootList, pathLength, newArr, path) {
+    for (const item of rootList) {
+      if ((pathLength - item.productLinkPath.length >= 0 && pathLength - item.productLinkPath.length <= 3) && item.productLinkPath.includes(path)) {
+        if (item.sysType == 'CmsItem') {
+          if (item.selectType == 'webpage') {
+            item.disabled = true
+          } else {
+            if (item.isDownload != 1) {
+              item.disabled = true
+            }
+          }
+          if (item.file && item.fileMap && item.fileMap[item.file]) {
+            if (item.fileMap[item.file].protectType == 'Private') item.disabled = true
+          }
+        }
+        // newArr.push(item)
+        // 鍦ㄦ彃鍏ヨ繃绋嬩腑瀵规暟鎹繘琛屾帓搴�  骞朵笖鍘婚櫎宸茬粡棰嗗彇鍜岃喘涔扮殑
+        if (item.sysType == 'CmsFolder') {
+          newArr = this.insertAndSortObjectsByProductLinkPath(newArr, item)
+        } else if (item.sysType == 'CmsItem' && this.resourceIsBuy(item)) {
+          newArr = this.insertAndSortObjectsByProductLinkPath(newArr, item)
+        }
+      }
+    }
+    //缁欐暟缁勯噷闈㈠啀娣诲姞涓�涓猚hildren鐨勭┖鏁扮粍
+    for (const i of newArr) {
+      i.children = []
+      this.getTreeList(rootList, pathLength + 6, i.children, i.productLinkPath)
+      if (i.children.length == 0) {
+        delete i.children
+      }
+    }
+    return newArr
+  },
+
+  // 鍘婚櫎鏍戠粨鏋勫浣欓」
+  ensureTreeConsistency(tree) {
+    for (let index = 0; index < tree.length; index++) {
+      const item = tree[index];
+      if (item.children && item.children.length) {
+        const isFloder = item.children.findIndex(citem => citem.sysType == 'CmsFolder')
+        const isItem = item.children.findIndex(citem => citem.sysType == 'CmsItem')
+        if (isFloder > -1 && isItem > -1) {
+          item.children = item.children.filter(ditem => ditem.sysType == 'CmsItem')
+        }
+        this.ensureTreeConsistency(item.children)
+      }
+    }
+    return tree
+  },
+
+  // 鎺掑簭鏁扮粍 鎸夌収productLinkPath
+  insertAndSortObjectsByProductLinkPath(array, newObj) {
+    // 鏌ユ壘鏂板璞″簲璇ユ彃鍏ョ殑浣嶇疆  
+    let insertIndex = array.findIndex(obj => Number(newObj.productLinkPath.substring(newObj.productLinkPath.length - 6, newObj.productLinkPath.length)) < Number(obj.productLinkPath.substring(obj.productLinkPath.length - 6, obj.productLinkPath.length)));
+
+    // 濡傛灉娌℃湁鎵惧埌鍚堥�傜殑浣嶇疆锛屽垯鏀惧湪鏁扮粍鏈熬  
+    if (insertIndex === -1) {
+      insertIndex = array.length;
+    }
+    // 鎻掑叆鏂板璞″埌鏁扮粍  
+    array.splice(insertIndex, 0, newObj);
+    // 娴嬭瘯6 姝e紡5
+    // 瀵规暟缁勮繘琛屾帓搴�  
+    array.sort((a, b) => {
+      if (Number(a.productLinkPath.substring(a.productLinkPath.length - 5, a.productLinkPath.length)) < Number(b.productLinkPath.substring(b.productLinkPath.length - 5, b.productLinkPath.length))) {
+        return -1;
+      }
+      if (Number(a.productLinkPath.substring(a.productLinkPath.length - 5, a.productLinkPath.length)) > Number(b.productLinkPath.substring(b.productLinkPath.length - 5, b.productLinkPath.length))) {
+        return 1;
+      }
+      // a must be equal to b  
+      return 0;
+    });
+
+    // 杩斿洖鏇存柊鍚庣殑鏁扮粍  
+    return array;
+  },
+
+  // 閬嶅巻鏍戠粨鏋勮浆鎹负鏁扮粍鏂规硶
+  handleTreeData(array) {
+    const flattenedArray = [];
+    array.forEach((node) => {
+      // 灏嗗綋鍓嶈妭鐐规坊鍔犲埌灞曞紑鐨勬暟缁勪腑
+      flattenedArray.push(node);
+      // 閫掑綊澶勭悊瀛愯妭鐐�
+      if (node.children && node.children.length > 0) {
+        const childrenArray = this.handleTreeData(node.children);
+        flattenedArray.push(...childrenArray);
+      }
+    });
+    return flattenedArray.filter((item) => item.sysType == "CmsItem");
+  },
+
+  // 鏁欏璧勬簮 浜戝涔� 鑾峰彇
+  getResourceDataList(type) {
     this.setData({
       loading: true,
-      noResources: false
-    })
+      noResources: false,
+    });
     let query = {
       path: '*',
       queryType: '*',
-      productId: this.data.bookId,
+      productId: this.data.bookDetail.id,
       cmsPath: type.productLinkPath,
+      cmsType: '*',
       itemFields: {
         SysType: 'CmsFolder',
         // 璧勬簮绫诲瀷锛岃瘯璇绘枃浠讹紝鏄惁鍏佽涓嬭浇绛夊弬鏁�
+        learnSelectType: [],
         selectType: [],
         freeFile: [],
         file: [],
+        protectedFile: [],
         resourcesClassification: [],
         isDownload: [],
         jsek_resourceBrief: [],
@@ -114,80 +251,190 @@
         size: 999
       },
     }
+    app.MG.store.getProductDetail(query).then((res) => {
+      if (!res.datas.cmsDatas[0].datas.length) {
+        return this.setData({
+          noResources: true,
+          loading: false,
+          haider: true,
+        });
+      }
+      let list = []
+      // 娴嬭瘯 6浣�// 姝e紡 5浣�//  娴嬭瘯璋冪敤浼�20锛屽唴閮�7  姝e紡璋冪敤浼�17 鍐呴儴浼�6
+      //鏁欏璧勬簮 浜戝涔�
+      if (
+        type.refCode == "jsek_teachingResources" ||
+        type.refCode == "jsek_cloudLearning"
+      ) {
+        res.datas.cmsDatas[0].datas.forEach((item) => {
+          item.checked = false;
+          if (item.sysType == 'CmsItem') {
+            // 鏁欏璧勬簮 绫诲瀷鍚嶇О璧嬪��
+            this.data.resourceClassList.forEach((type) => {
+              if (type.value == item.resourcesClassification)
+                item.resourceClass = type.name;
+            });
+            if (this.data.tabValue == "jsek_teachingResources") {} else if (this.data.tabValue == "jsek_cloudLearning") {
+              item.isbuy = this.isShowNeedBuy(item);
+              item.isShopCar = this.isShoppingCart(item);
+            }
+          }
+        });
+        // 鏄剧ず 闇�瑕佽喘涔扮殑鍜岄渶瑕佸厤璐归鍙栫殑
+        // const arr = res.datas.cmsDatas[0].datas.filter(item => this.resourceIsBuy(item))
+        // if (!arr.length) {
+        //   return this.setData({
+        //     loading: false,
+        //     haider: true,
+        //     noData: true
+        //   })
+        // }
+        // debugger
+        const num = query.cmsPath.length + 6
+        this.getTreeList(res.datas.cmsDatas[0].datas, num, list, '\\')
+        list = this.ensureTreeConsistency(list)
+        list = this.changeResourceChildren(list)
+        console.log('淇敼鍚庝簯', list);
+        let result = [];
+        if (type.refCode == "jsek_teachingResources") {
+          this.findChildIds(list, result);
+          this.setData({
+            teach: list,
+            loading: false,
+            openTeachids: result,
+          });
+        } else if (type.refCode == "jsek_cloudLearning") {
+          let arr = this.handleTreeData(list).filter(
+            (item) => item.saleMethod[0]
+          );
+          let freeIds = arr.filter((item) => {
+            return item.saleMethod[0].Price == 0;
+          });
+          if (!freeIds.length)
+            this.setData({
+              isshowDrawBtn: false,
+            });
+          this.findChildIds(list, result);
+          this.setData({
+            learn: list,
+            loading: false,
+            openLearnids: result,
+            haider: true,
+          });
+        }
+      }
+    })
+  },
+
+  //  鑾峰彇鏁欏璧勬簮  浜戝涔�  浜戞祴璇�
+  getResourceData(type) {
+    this.setData({
+      loading: true,
+      noResources: false,
+    });
+    let query = {
+      path: "*",
+      queryType: "*",
+      productId: this.data.bookId,
+      cmsPath: type.productLinkPath,
+      itemFields: {
+        SysType: "CmsFolder",
+        // 璧勬簮绫诲瀷锛岃瘯璇绘枃浠讹紝鏄惁鍏佽涓嬭浇绛夊弬鏁�
+        selectType: [],
+        freeFile: [],
+        file: [],
+        resourcesClassification: [],
+        isDownload: [],
+        jsek_resourceBrief: [],
+        jsek_link: [],
+        jsek_questionBank: [],
+      },
+      pading: {
+        start: 0,
+        size: 999,
+      },
+    };
     app.MG.store
       .getProductDetail(query)
       .then(async (res) => {
         if (!res.datas.cmsDatas[0].datas.length) {
           return this.setData({
             noResources: true,
-            loading: false
-          })
+            loading: false,
+            haider: true,
+          });
         }
         //鏁欏璧勬簮 浜戝涔�
-        if (type.refCode == 'jsek_teachingResources' || type.refCode == 'jsek_cloudLearning') {
+        if (
+          type.refCode == "jsek_teachingResources" ||
+          type.refCode == "jsek_cloudLearning"
+        ) {
           if (res.datas.cmsDatas[0].datas.length > 0) {
-            res.datas.cmsDatas[0].datas.forEach(item => {
-              item.checked = false,
-                item.checkAll = true
-            })
-            const list = await this.getAllResource(res.datas.cmsDatas[0].datas)
-            if (type.refCode == 'jsek_teachingResources') {
+            res.datas.cmsDatas[0].datas.forEach((item) => {
+              (item.checked = false), (item.checkAll = true);
+            });
+            const list = await this.getAllResource(res.datas.cmsDatas[0].datas);
+            if (type.refCode == "jsek_teachingResources") {
               this.setData({
                 teach: list,
-                loading: false
-              })
-              this.findChildIds(this.data.teach, this.data.openTeachids = [])
-            } else if (type.refCode == 'jsek_cloudLearning') {
-              const result = []
-              res.datas.cmsDatas[0].datas.forEach(item => {
-                item.checked = false
-              })
-              this.findChildIds(list, result)
+                loading: false,
+                haider: true
+              });
+              this.findChildIds(this.data.teach, (this.data.openTeachids = []));
+            } else if (type.refCode == "jsek_cloudLearning") {
+              const result = [];
+              res.datas.cmsDatas[0].datas.forEach((item) => {
+                item.checked = false;
+              });
+              this.findChildIds(list, result);
               this.setData({
                 learn: list,
                 loading: false,
-                openLearnids: result
-              })
+                openLearnids: result,
+                haider: true
+              });
             }
+            console.log(list, '浜戝缈�');
           } else {
             // 鏃犳暟鎹�
           }
-        } else if (type.refCode == 'jsek_questionBank') {
+        } else if (type.refCode == "jsek_questionBank") {
           // const list = await this.getAllResource(res.datas.cmsDatas[0].datas)
           this.setData({
             test: res.datas.cmsDatas[0].datas,
-            loading: false
-          })
+            loading: false,
+            haider: true
+          });
         } else {
-          console.log('鍏朵粬');
+          console.log("鍏朵粬");
         }
       })
       .catch((e) => {
-        console.log(e)
-      })
+        console.log(e);
+      });
   },
   // 鑾峰彇tag涓嬫墍鏈夎祫婧�
   async getAllResource(data) {
-    if (!data.length) return false
+    if (!data.length) return false;
     for (let i = 0; i < data.length; i++) {
-      let item = data[i]
-      if (item.sysType == 'CmsFolder' && item.childrenCount > 0) {
-        item.children = []
-        item.children = await this.getFolderItem(item.productLinkPath)
-        await this.getAllResource(item.children)
+      let item = data[i];
+      if (item.sysType == "CmsFolder" && item.childrenCount > 0) {
+        item.children = [];
+        item.children = await this.getFolderItem(item.productLinkPath);
+        await this.getAllResource(item.children);
       }
     }
-    return data
+    return data;
   },
   // 鑾峰彇璧勬簮鎺ュ彛
   async getFolderItem(path) {
     let query = {
-      path: '*',
-      queryType: '*',
+      path: "*",
+      queryType: "*",
       productId: this.data.bookId,
       cmsPath: path,
       itemFields: {
-        SysType: 'CmsFolder',
+        SysType: "CmsFolder",
         // 璧勬簮绫诲瀷锛岃瘯璇绘枃浠讹紝鏄惁鍏佽涓嬭浇绛夊弬鏁�
         selectType: [],
         freeFile: [],
@@ -197,52 +444,55 @@
         isDownload: [],
         jsek_resourceBrief: [],
         jsek_link: [],
-        accessType: []
+        accessType: [],
+        learnSelectType: [],
       },
       pading: {
         start: 0,
-        size: 999
+        size: 999,
       },
-    }
-    const data = await app.MG.store.getProductDetail(query)
+    };
+    const data = await app.MG.store.getProductDetail(query);
     data.datas.cmsDatas[0].datas.forEach((item) => {
       this.data.resourceClassList.forEach((type) => {
-        if (type.value == item.resourcesClassification) item.resourceClass = type.name
-      })
+        if (type.value == item.resourcesClassification)
+          item.resourceClass = type.name;
+      });
       // 缃戦〉 涓嶈兘涓嬭浇 绉佹湁鏂囦欢
-      if (item.sysType == 'CmsItem') {
-        if (item.selectType == 'webpage') {
-          item.disabled = true
+      if (item.sysType == "CmsItem") {
+        if (item.selectType == "webpage") {
+          item.disabled = true;
         } else {
           if (item.isDownload != 1) {
-            item.disabled = true
+            item.disabled = true;
           }
         }
         if (item.file) {
-          if (item.fileMap[item.file].protectType == 'Private') item.disabled = true
+          if (item.fileMap[item.file].protectType == "Private")
+            item.disabled = true;
         }
       }
-    })
-    data.datas.cmsDatas[0].datas.forEach(item => {
-      item.checked = false
-      item.isbuy = this.resourceIsBuy(item)
-      item.isShopCar = this.isShoppingCart(item)
-    })
-    return data.datas.cmsDatas[0].datas
+    });
+    data.datas.cmsDatas[0].datas.forEach((item) => {
+      item.checked = false;
+      item.isbuy = this.isShowNeedBuy(item);
+      item.isShopCar = this.isShoppingCart(item);
+    });
+    return data.datas.cmsDatas[0].datas;
   },
-  getBookInfo(id) {
+  async getBookInfo(id) {
     this.setData({
       "mockData'.id": 0,
       "mockData.price": 0,
-      "mockData.count": 0
-    })
+      "mockData.count": 0,
+    });
     const query = {
-      path: '*',
-      queryType: '*',
+      path: "*",
+      queryType: "*",
       productId: id,
-      favoriteTypes: 'FavoriteBookCity',
+      favoriteTypes: "FavoriteBookCity",
       itemFields: {
-        'SysType=': 'CmsFolder',
+        "SysType=": "CmsFolder",
       },
       coverSize: {
         height: 300,
@@ -268,13 +518,43 @@
         freeEpubPage: [], //epub璇曡鐧惧垎姣�
       },
     };
-    app.MG.store.getProductDetail(query).then(async (res) => {
+    await app.MG.store.getProductDetail(query).then(async (res) => {
       this.setData({
         bookDetail: res.datas,
-        buyIdList: res.datas.purchasedSaleMethodIdList
+        buyIdList: res.datas.purchasedSaleMethodIdList,
       });
+      // this.getResourceData({
+      //   productLinkPath: this.data.productLinkPath,
+      //   refCode: this.data.refCode,
+      // });
+      // this.getResourceCode(
+      //   res.datas.id,
+      //   res.datas.rootCmsItemId
+      // )
+      this.getResourceDataList({
+        productLinkPath: this.data.productLinkPath,
+        refCode: this.data.refCode,
+      })
     });
 
+  },
+  getResourceCode(bookId, cmsId) {
+    let query = {
+      path: '*',
+      queryType: '*',
+      productId: bookId,
+      cmsPath: cmsId,
+      itemFields: {
+        SysType: 'Cms'
+      },
+      pading: {
+        start: 99,
+        size: 0
+      }
+    }
+    app.MG.store.getProductDetail(query).then(res => {
+      console.log('鍥句功璧勬簮', res.datas.cmsDatas[0].datas);
+    })
   },
   // 鑾峰彇宸茶喘涔板晢鍝�
   getShoppingCartProductGet() {
@@ -282,87 +562,254 @@
       start: 0,
       size: 999,
       filterList: [],
-      searchList: []
-    }
+      searchList: [],
+    };
     app.MG.store.getShoppingCartProductList(query).then((res) => {
-      const list = []
+      const list = [];
       res.datas.forEach((item) => {
-        list.push(item.saleMethod.id)
-      })
+        list.push(item.saleMethod.id);
+      });
       this.setData({
         shoppingList: res.datas,
-        shoppingCartGetId: list
-      })
-    })
+        shoppingCartGetId: list,
+      });
+    });
   },
   // 鍒ゆ柇璧勬簮鏄惁璐拱
   resourceIsBuy(data) {
     if (data.saleMethod && data.saleMethod.length) {
-      const isSHow = this.data.buyIdList.some((item) => item == data.saleMethod[0].Id)
-      return !isSHow
+      const isSHow = this.data.buyIdList.some(
+        (item) => item == data.saleMethod[0].Id
+      );
+      return !isSHow;
     } else {
-      return false
+      return false;
+    }
+  },
+
+  isShowNeedBuy(data) {
+    if (data.saleMethod && data.saleMethod.length) {
+      const isSHow = this.data.buyIdList.some(
+        (item) => item == data.saleMethod[0].Id
+      );
+      if (isSHow) {
+        return false;
+      } else {
+        return data.saleMethod[0].Price > 0 ? true : false;
+      }
+      return !isSHow;
+    } else {
+      return false;
     }
   },
   // 鍒ゆ柇璧勬簮鍔犲叆璐墿杞︽寜閽槸鍚︽樉绀�
   isShoppingCart(data) {
     if (data.saleMethod && data.saleMethod.length) {
-      if (data.saleMethod[0].Price <= 0) return false
+      if (data.saleMethod[0].Price <= 0) return false;
       // 鍐嶅垽鏂槸鍚﹁喘涔�
-      const isBuy = this.data.buyIdList.some((item) => item == data.saleMethod[0].Id)
+      const isBuy = this.data.buyIdList.some(
+        (item) => item == data.saleMethod[0].Id
+      );
       if (isBuy) {
         // 璐拱浜�
-        return false
+        return false;
       } else {
         // 鍒ゆ柇鏄惁鍔犲叆璐墿杞�
-        const isSHow = this.data.shoppingCartGetId.some((item) => item == data.saleMethod[0].Id)
-        return !isSHow
+        const isSHow = this.data.shoppingCartGetId.some(
+          (item) => item == data.saleMethod[0].Id
+        );
+        return !isSHow;
       }
     } else {
-      return false
+      return false;
     }
   },
   // 鑾峰彇灞曞紑椤�
   findChildIds(data, result) {
-    let index = 0
+    let index = 0;
     for (let i = 0; i < data.length; i++) {
       if (index < 3) {
-        const item = data[i]
-        if (item.childrenFolderCount > 0) {
-          result.push(item.id)
+        const item = data[i];
+        if (item.children && item.children.length > 0) {
+          result.push(item.id);
           for (let j = 0; j < item.children.length; j++) {
             if (index < 3) {
-              const childrenItme = item.children[j]
-              if (item.childrenCount > 0) {
-                result.push(childrenItme.id)
-                index += 1
+              const childrenItme = item.children[j];
+              if (item.children && item.children.length > 0) {
+                result.push(childrenItme.id);
+                index += 1;
               }
             } else {
-              break
+              break;
             }
           }
-        } else if (item.childrenCount > 0) {
-          result.push(item.id)
-          index += 1
+        } else if (item.children && item.children.length > 0) {
+          result.push(item.id);
+          index += 1;
         }
       } else {
-        break
+        break;
       }
     }
   },
   // 鑾峰彇璧勬簮鎵�灞炲垎绫�
   getResourceClass() {
     let query = {
-      refCodes: ['resourcesClassification']
-    }
+      refCodes: ["resourcesClassification"],
+    };
     app.MG.store.getProductTypeField(query).then((res) => {
       this.setData({
-        resourceClassList: JSON.parse(res[0].config).option
-      })
-    })
+        resourceClassList: JSON.parse(res[0].config).option,
+      });
+
+    });
   },
   checkAll() {
-    const child = this.selectComponent('#tree')
-    child.checkAll()
+    const child = this.selectComponent("#tree");
+    child.checkAll();
+  },
+  //璐拱鎸夐挳
+  batchPurchase() {
+    if (!this.data.learn.length) {
+      return wx.showToast({
+        icon: "error",
+        title: '鏆傛棤鏁版嵁',
+      })
+    }
+    let saleMethodIds = [];
+    let requests = [];
+    let item = [];
+    let selectAllId = [];
+    const child = this.selectComponent("#tree");
+    item = child.data.superiorPurchaseSaleMethodId;
+    const sonItem = child.data.sonPurchaseSaleMethodId;
+    let selectAll = child.data.selectAll;
+    if (selectAll) {
+      selectAll.forEach((item) => {
+        if (item.saleMethod != 0) {
+          selectAllId.push(item.saleMethod[0].Id);
+        }
+      });
+      app.MG.store
+        .initOrder({
+          requests: selectAllId.map((id) => ({
+            saleMethodId: id,
+            count: 1,
+          })),
+        })
+        .then((res) => {
+          if (res) {
+            wx.navigateTo({
+              url: "/pages/cart/paymentPage/index?orderNumber=" + res.orderNumber,
+            });
+            selectAllId = [];
+            child.setData({
+              selectAll: [],
+            });
+          }
+        });
+    }
+    if (item) {
+      // 鎷垮埌id
+      item.forEach((items) => {
+        if (items.saleMethod != 0) {
+          saleMethodIds.push(items.saleMethod[0].Id);
+        }
+      });
+      app.MG.store
+        .initOrder({
+          requests: saleMethodIds.map((id) => ({
+            saleMethodId: id,
+            count: 1,
+          })),
+        })
+        .then((res) => {
+          if (res) {
+            wx.navigateTo({
+              url: "/pages/cart/paymentPage/index?orderNumber=" + res.orderNumber,
+            });
+
+            child.setData({
+              superiorPurchaseSaleMethodId: [], // 浣跨敤setData鏂规硶娓呯┖sonPurchaseSaleMethodId鏁版嵁
+            });
+          }
+        });
+    }
+    if (item.length == 0 && sonItem) {
+      app.MG.store
+        .initOrder({
+          requests: sonItem.map((id) => ({
+            saleMethodId: id,
+            count: 1,
+          })),
+        })
+        .then((res) => {
+          if (res) {
+            wx.navigateTo({
+              url: "/pages/cart/paymentPage/index?orderNumber=" + res.orderNumber,
+            });
+            this.setData({
+              paymentPage: true,
+            });
+            child.setData({
+              sonPurchaseSaleMethodId: [], // 浣跨敤setData鏂规硶娓呯┖sonPurchaseSaleMethodId鏁版嵁
+            });
+          }
+        });
+    }
+  },
+
+  // 绔犺妭鍕鹃�夋柟娉�
+  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
+});
\ No newline at end of file

--
Gitblit v1.9.1