From bbba5d26e9e26c910ed337dcb65f462752ee6dce Mon Sep 17 00:00:00 2001
From: litian <C21AF165>
Date: 星期四, 12 九月 2024 09:11:47 +0800
Subject: [PATCH] fenxiang

---
 packageBookService/pages/bookServices/detail/buyResource/index.js |  338 ++++++++++++++++++++++++++++++++++---------------------
 1 files changed, 209 insertions(+), 129 deletions(-)

diff --git a/packageBookService/pages/bookServices/detail/buyResource/index.js b/packageBookService/pages/bookServices/detail/buyResource/index.js
index 8ec26df..dfee441 100644
--- a/packageBookService/pages/bookServices/detail/buyResource/index.js
+++ b/packageBookService/pages/bookServices/detail/buyResource/index.js
@@ -10,7 +10,7 @@
     learn: [],
     openLearnids: [],
     bookId: "",
-    loading: false,
+    loading: true,
     shoppingList: [],
     shoppingCartGetId: [],
     methodId: [],
@@ -18,7 +18,9 @@
     superior: false,
     checkAllState: true,
     productLinkPath: '',
-    refCode: ''
+    refCode: '',
+    naData: false,
+    bookRefCode: []
   },
 
   /**
@@ -75,6 +77,7 @@
    * 鐢ㄦ埛鐐瑰嚮鍙充笂瑙掑垎浜�
    */
   onShareAppMessage() {},
+  onShareTimeline() {},
   changeCheckAll(e) {
     this.setData({
       checkAllState: e.detail.value,
@@ -83,111 +86,148 @@
   goBack() {
     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, newArr, path) {
+    for (const item of rootList) {
+      if (path == item.productLinkPath.replace('\\' + item.id, '')) {
+        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
+          }
+        }
+        // 鍦ㄦ彃鍏ヨ繃绋嬩腑瀵规暟鎹繘琛屾帓搴�  骞朵笖鍘婚櫎宸茬粡棰嗗彇鍜岃喘涔扮殑
+        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, 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 => newObj.id < obj.id);
+    // 濡傛灉娌℃湁鎵惧埌鍚堥�傜殑浣嶇疆锛屽垯鏀惧湪鏁扮粍鏈熬  
+    if (insertIndex === -1) {
+      insertIndex = array.length;
+    }
+    // 鎻掑叆鏂板璞″埌鏁扮粍  
+    array.splice(insertIndex, 0, newObj);
+    // 娴嬭瘯6 姝e紡5
+    // 瀵规暟缁勮繘琛屾帓搴�  
+    array.sort((a, b) => {
+      if (a.id < b.id) return -1;
+      if (a.id > b.id) return 1;
+      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,
     });
     let query = {
-      path: "*",
-      queryType: "*",
-      productId: this.data.bookId,
+      path: '*',
+      queryType: '*',
+      productId: this.data.bookDetail.id,
       cmsPath: type.productLinkPath,
+      cmsType: '*',
       itemFields: {
-        SysType: "CmsFolder",
+        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,
-          });
-        }
-        //鏁欏璧勬簮 浜戝涔�
-        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") {
-              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);
-              this.setData({
-                learn: list,
-                loading: false,
-                openLearnids: result,
-              });
-            }
-          } else {
-            // 鏃犳暟鎹�
-          }
-        } 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,
-          });
-        } else {
-          console.log("鍏朵粬");
-        }
-      })
-      .catch((e) => {
-        console.log(e);
-      });
-  },
-  // 鑾峰彇tag涓嬫墍鏈夎祫婧�
-  async getAllResource(data) {
-    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);
-      }
-    }
-    return data;
-  },
-  // 鑾峰彇璧勬簮鎺ュ彛
-  async getFolderItem(path) {
-    let query = {
-      path: "*",
-      queryType: "*",
-      productId: this.data.bookId,
-      cmsPath: path,
-      itemFields: {
-        SysType: "CmsFolder",
-        // 璧勬簮绫诲瀷锛岃瘯璇绘枃浠讹紝鏄惁鍏佽涓嬭浇绛夊弬鏁�
+        learnSelectType: [],
         selectType: [],
         freeFile: [],
         file: [],
@@ -196,41 +236,74 @@
         isDownload: [],
         jsek_resourceBrief: [],
         jsek_link: [],
-        accessType: [],
+        jsek_questionBank: []
       },
       pading: {
         start: 0,
-        size: 999,
+        size: 999
       },
-    };
-    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 (item.sysType == "CmsItem") {
-        if (item.selectType == "webpage") {
-          item.disabled = true;
-        } else {
-          if (item.isDownload != 1) {
-            item.disabled = true;
+    }
+    app.MG.store.getProductDetail(query).then((res) => {
+      if (!res.datas.cmsDatas[0].datas.length) {
+        return this.setData({
+          noResources: true,
+          loading: false,
+        });
+      }
+      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;
+            });
+            item.isbuy = this.isShowNeedBuy(item);
+            item.isShopCar = this.isShoppingCart(item);
           }
-        }
-        if (item.file) {
-          if (item.fileMap[item.file].protectType == "Private")
-            item.disabled = true;
+        });
+        // 鏄剧ず 闇�瑕佽喘涔扮殑鍜岄渶瑕佸厤璐归鍙栫殑
+        this.getTreeList(res.datas.cmsDatas[0].datas, list, query.cmsPath)
+        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,
+          });
         }
       }
-    });
-    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;
+    })
   },
+
   async getBookInfo(id) {
     this.setData({
       "mockData'.id": 0,
@@ -274,10 +347,10 @@
         bookDetail: res.datas,
         buyIdList: res.datas.purchasedSaleMethodIdList,
       });
-      this.getResourceData({
+      this.getResourceDataList({
         productLinkPath: this.data.productLinkPath,
         refCode: this.data.refCode,
-      });
+      })
     });
 
   },
@@ -320,7 +393,7 @@
       if (isSHow) {
         return false;
       } else {
-        return data.saleMethod[0].price > 0 ? true : false;
+        return data.saleMethod[0].Price > 0 ? true : false;
       }
       return !isSHow;
     } else {
@@ -386,6 +459,7 @@
       this.setData({
         resourceClassList: JSON.parse(res[0].config).option,
       });
+
     });
   },
   checkAll() {
@@ -394,6 +468,12 @@
   },
   //璐拱鎸夐挳
   batchPurchase() {
+    if (!this.data.learn.length) {
+      return wx.showToast({
+        icon: "error",
+        title: '鏆傛棤鏁版嵁',
+      })
+    }
     let saleMethodIds = [];
     let requests = [];
     let item = [];

--
Gitblit v1.9.1