litian
2024-03-12 b125d3ea74d61fb8e4d7238c83906aeea52c6b4c
pages/bookServices/detail/components/tree/index.js
@@ -1,3 +1,4 @@
import Message from 'tdesign-miniprogram/message/message';
const app = getApp()
Component({
@@ -5,6 +6,10 @@
    treeList: {
      type: Array,
      value: []
    },
    bookInfo: {
      type: Object,
      value: {}
    },
    buyIds: {
      type: Array,
@@ -17,6 +22,14 @@
    applyState: {
      type: String,
      value: ''
    },
    openTeachids: {
      type: Array,
      value: ''
    },
    openLearnids: {
      type: Array,
      value: ""
    }
  },
  data: {
@@ -43,7 +56,6 @@
      }
    },
    downloadTeach(e) {
      console.log('点击了', e);
      const value = e.currentTarget.dataset.value
      var myEventDetail = {
        value,
@@ -53,15 +65,128 @@
        composed: true
      } // 触发事件的选项
      this.triggerEvent('downloadTeach', myEventDetail, myEventOption)
    },
    // 判断资源是否购买
resourceIsBuy (data) {
  if (data.saleMethod && data.saleMethod.length) {
    const isSHow = buyIdList.value.some((item) => item == data.saleMethod[0].Id)
    return !isSHow
  } else {
    return false
  }
}
    resourceIsBuy(data) {
      if (data.saleMethod && data.saleMethod.length) {
        const isSHow = buyIdList.value.some((item) => item == data.saleMethod[0].Id)
        return !isSHow
      } else {
        return false
      }
    },
    // 跳转音视频播放器
    goPlayer(e) {
      const item = e.currentTarget.dataset.item
      const parent = e.currentTarget.dataset.parent
      //   "pages/resourceDetails/myAudio/index",
      // "pages/resourceDetails/myVideo/index",
      // "pages/resourceDetails/document/index",
      let url
      if (item.selectType == 'video') {
        url = '/pages/resourceDetails/myVideo/index'
      } else if (item.selectType == 'audio') {
        url = '/pages/resourceDetails/myAudio/index'
      } else {
        url = '/pages/resourceDetails/document/index'
      }
      wx.navigateTo({
        url: `${url}?productLinkPath=${item.productLinkPath}&parentProductLinkPath=${parent.productLinkPath}&parentName=${parent.name}&activeId=${item.id}&bookId=${this.properties.bookInfo.id}&bookName=${this.properties.bookInfo.name}&cmsId=${this.properties.bookInfo.rootCmsItemId}`,
      })
    },
    // 拿到所有项
    getAllChildren(id) {
      let result = [];
      function findChildren(item) {
        if (item.id === id) {
          if (item.children && item.children.length > 0) {
            item.children.forEach(child => {
              findChildren(child);
            });
          }
        } else {
          if (item.children && item.children.length > 0) {
            item.children.forEach(child => {
              findChildren(child);
            });
          }
        }
        if (item.children && item.children.length > 0) {
          result.push(...item.children);
        }
      }
      this.properties.treeList.forEach(item => {
        findChildren(item);
      });
      return result;
    },
    // 拿到当前项子项
    flattenTree(tree) {
      let result = [];
      function flatten(node) {
        result.push(node);
        if (node.children && node.children.length > 0) {
          node.children.forEach(child => {
            flatten(child);
          });
        }
      }
      tree.forEach(node => {
        flatten(node);
      });
      return result
    },
    findAndUpdateItemById(tree, id) {
      function findAndUpdate(node) {
        if (node.id === id) {
          node.check = true; // 将目标项的 check 属性设置为 true
          return true; // 返回 true 表示找到了目标项
        }
        if (node.children && node.children.length > 0) {
          for (let child of node.children) {
            if (findAndUpdate(child)) {
              return true; // 如果在子节点中找到了目标项,则直接返回 true
            }
          }
        }
        return false; // 表示未找到目标项
      }
      for (let node of tree) {
        if (findAndUpdate(node)) {
          break; // 如果在顶层节点中找到了目标项,直接退出循环
        }
      }
    },
    // 变为true
    findAndUpdateItemsByIds(tree, ids) {
      function findAndUpdate(node) {
        if (ids.includes(node.id)) {
          node.checked = true; // 将目标项的 check 属性设置为 true
        }
        if (node.children && node.children.length > 0) {
          for (let child of node.children) {
            findAndUpdate(child); // 递归处理子节点
          }
        }
      }
      for (let node of tree) {
        findAndUpdate(node); // 对每个顶层节点执行查找和更新操作
      }
      return tree; // 返回修改后的完整数组
    },
    // 章节勾选
    checkResourceTitle(e) {
      const item = e.currentTarget.dataset.item
      let list = this.flattenTree([item])
      let ids = []
      list.forEach(item => {
        ids.push(item.id)
      })
      const tab = this.findAndUpdateItemsByIds([item], ids)
      console.log(ids, tab);
    }
  }
})