闫增涛
2025-03-31 04c7b0163caeae4ab1c5da62e301fa993101cfad
packageBookService/pages/bookServices/detail/components/tree/index.js
@@ -51,26 +51,28 @@
    deadline: {
      type: String,
    },
    cloundMenuIsBuy: {
      type: Boolean
    },
    learnMenu: {
      type: Object
    },
    isCloundHaveSaleMethod: {
      type: Boolean
    }
  },
  data: {
    sonPurchaseSaleMethodId: "",
    superiorPurchaseSaleMethodId: [],
    activeValues: [1, 2],
    webpageSrc: "",
    cloudShoppingCart: [],
    selectAll: [],
    shoppingCart: true,
    stop: false,
    cart: true,
    canExecute: true
    canExecute: true,
    purchaseList: []
  },
  ready() {},
  onLoad() {
    this.setData({
      sonPurchaseSaleMethodId: [],
    });
  },
  onLoad() {},
  observers: {
    treeList: function (newValue) {
      const list = [];
@@ -94,9 +96,22 @@
        }
      }
    },
    // 遍历树结构转换为数组方法
    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;
    },
    // 节点展开
    handleChange(e) {
      console.log(e.detail.value);
      this.setData({
        openIds: e.detail.value,
      });
@@ -124,11 +139,25 @@
    // 判断资源是否购买
    resourceIsBuy(data) {
      if (data.saleMethod && data.saleMethod.length) {
        if (data.saleMethod[0].State == "Disable") return false;
        const saleData = data.saleMethod.find((item) => item.SaleType == 'Normal')
        if (saleData.State == 'Disable') return false
        const isSHow = this.properties.buyIds.some(
          (item) => item == data.saleMethod[0].Id
          (item) => item == saleData.Id
        );
        return !isSHow;
        // 已经购买
        if (isShow) return false
        // 未购买,查看销售方式是否过期
        if (!isShow) {
          const flag = new Date(saleData.EndDate).getTime() >= new Date().getTime() &&
            new Date(saleData.BeginDate).getTime() <= new Date().getTime()
          if (flag) {
            // 为过期 ,需要购买
            return true
          } else {
            // 已过期 无需购买
            return false
          }
        }
      } else {
        return false;
      }
@@ -136,138 +165,106 @@
    // 跳转音视频播放器
    goPlayer(e) {
      // 检查登录状态
      const token = wx.getStorageSync(app.config.tokenKey);
      if (token) {
      const goPlayerFun = () => {
        const item = e.currentTarget.dataset.item;
        const parent = e.currentTarget.dataset.parent;
        console.log('item', item);
        const parentProductLinkPath = parent ?
          parent.productLinkPath :
          wx.getStorageSync("teachResourcesPath");
        const parentName = parent ? parent.name : "教学资源";
        let url;
        if (item.selectType == 'zip') {
          if (this.properties.applyState == 'Normal') {
            return wx.showToast({
              icon: 'error',
              title: '请前往PC端下载',
            })
          } else {
            return wx.showToast({
              icon: 'error',
              title: '请先申请下载',
            })
        // 资源购买页面(只展示未购买资源) 点击不跳转
        if (this.properties.isShowCheck) return false;
        if (item.selectType == "webpage" || item.learnSelectType == "webpage") {
          // 网页类型跳转到网址复制页,因为url传参不能传 带参数的网络地址,所以此处要把网络地址里的传参摘出来,放到url里
          let urlData = ''
          const flags = item.jsek_link.indexOf("?")
          if (flags > -1) {
            urlData = item.jsek_link.slice(flags + 1, item.jsek_link.length)
          }
          url = '/packageBookService/pages/bookServices/linkPage/index'
          return wx.navigateTo({
            url: urlData ? url + `?path=${item.jsek_link.slice(0,flags)}` + '&' + urlData : url + `?path=${item.jsek_link}`
          })
        }
        if (this.properties.isShowCheck && this.resourceIsBuy(item)) {
          return false;
        }
        if (item.selectType == "video" || item.learnSelectType == "video") {
          url = "/packageDomain/pages/resourceDetails/myVideo/index";
        } else if (
          item.selectType == "audio" ||
          item.learnSelectType == "audio"
        ) {
          url = "/packageDomain/pages/resourceDetails/myAudio/index";
        } else if (item.selectType == "picture") {
          url = "/packageDomain/pages/resourceDetails/document/index";
        }
        if (
          this.properties.tab == "jsek_cloudLearning" &&
          this.resourceIsBuy(item)
          // item.saleMethod.length > 0
        ) {
          return wx.showToast({
            icon: "error",
            title: item.saleMethod[0].Price > 0 ?
              "请先购买该资源" : "请先点击领取查看按钮",
          });
        }
        // if (
        //   item.saleMethod.length == 0 &&
        //   item.sysType == "CmsItem" &&
        //   this.properties.tab == "jsek_cloudLearning"
        // ) {
        //   return wx.showToast({
        //     icon: "error",
        //     title: "暂未开始销售,请联系管理员",
        //   });
        // }
        if (this.properties.tab == "jsek_teachingResources" && (item.file || item.freeFile)) {
          let file = item.fileMap[item.file] ? item.fileMap[item.file] : item.fileMap[item.freeFile]
          if (file && file.protectType !== "Public") {
            return wx.showToast({
              icon: "error",
              title: "资源不可用,请联系管理员",
            });
          }
        } else if ((this.properties.tab == "jsek_teachingResources" || this.properties.tab == "jsek_cloudLearning") && (!item.file && !item.freeFile && !item.protectedFile)) {
        if (!item.file && !item.freeFile && !item.protectedFile) {
          return wx.showToast({
            icon: 'error',
            title: '暂无资源',
          })
        }
        if (item.selectType == 'zip' || item.learnSelectType == 'zip') {
          return wx.showToast({
            icon: 'error',
            title: '请前往PC端下载',
          })
        }
        if (this.properties.tab == "jsek_teachingResources" && (item.file || item.freeFile)) {
          let file = item.fileMap[item.file] ? item.fileMap[item.file] : item.fileMap[item.freeFile]
          if (file && file.protectType !== "Public") {
            return wx.showToast({
              icon: "none",
              title: "资源不可用,请联系管理员",
            });
          }
        }
        if (this.properties.tab == "jsek_cloudLearning") {
          // 1.找出所有父级目录和本身(有销售方式) 和 云学习本身(有销售方式) 放到一个数组  为空则暂未开放销售
          let learn = []
          if (this.properties.isCloundHaveSaleMethod) {
            // 1.1 云学习有销售方式,加入父级数组
            learn.push(this.properties.learnMenu)
          } else {
            // 1.2 云学习无销售方式,将父级目录和本身加入数组
            learn = this.handleTreeData(this.properties.learnList).filter(
              (citem) => citem.saleMethod &&
              citem.saleMethod.find((ditem) => ditem.SaleType == 'Normal' && ditem.Price > 0) &&
              new Date(citem.saleMethod.find((ditem) => ditem.SaleType == 'Normal').EndDate).getTime() >= new Date().getTime() &&
              new Date(citem.saleMethod.find((ditem) => ditem.SaleType == 'Normal').BeginDate).getTime() <= new Date().getTime() &&
              item.productLinkPath.includes(citem.productLinkPath)
            )
          }
          // 2.找出这个数组里已购买的 形成新数组     为空则未购买
          let buyList = []
          if (learn.length) buyList = learn.filter((item) => this.properties.buyIds.includes(item.saleMethod.find((citem) => citem.SaleType == 'Normal').Id))
          // 有父级(包含销售方式),且未购买
          if (learn.length && !buyList.length && !item.freeFile) return wx.showToast({
            icon: 'error',
            title: '请先购买该资源',
          })
          if (item.protectedFile || item.freeFile) {
            let file = item.fileMap[item.protectedFile] ? item.fileMap[item.protectedFile] : item.fileMap[item.freeFile]
            if (file && file.protectType !== "Public") {
              return wx.showToast({
                icon: 'none',
                title: "资源不可用,请联系管理员",
              });
            }
          }
        }
        let url;
        if (item.selectType == "video" || item.learnSelectType == "video") {
          url = "/packageDomain/pages/resourceDetails/myVideo/index";
        }
        if (
          item.selectType == "audio" ||
          item.learnSelectType == "audio"
        ) {
          url = "/packageDomain/pages/resourceDetails/myAudio/index";
        }
        if (item.selectType == "picture" || item.selectType == 'document' || item.selectType == 'pdf' || item.learnSelectType == "picture" || item.learnSelectType == 'document' || item.learnSelectType == 'pdf') {
          url = "/packageDomain/pages/resourceDetails/document/index";
        }
        wx.navigateTo({
          url: `${url}?productLinkPath=${item.productLinkPath}&parentProductLinkPath=${parentProductLinkPath}&parentName=${parentName}&activeId=${item.id}&bookId=${this.properties.bookInfo.id}&bookName=${this.properties.bookInfo.name}&cmsId=${this.properties.bookInfo.rootCmsItemId}&formPath=${this.properties.tab}&applyState=${this.properties.applyState}&deadline=${this.properties.deadline}`,
        });
        // debugger
        if (item.selectType == "pdf" || item.selectType == "document") {
          const fileLink = item.file ? item.file : item.freeFile
          console.log(fileLink);
          wx.navigateTo({
            url: "/packageBookService/pages/components/webView/index?md5=" +
              fileLink +
              "&fileName=" +
              item.name +
              "&fileType=pdf" + "&bookBuy=true"
          });
        }
        // if (item.selectType == "document") {
        //   // freeFile
        //   // if (item.selectType == "document") {
        //   //   item.selectType = "pptx";
        //   // }
        //   const fileLink = item.file ?
        //     app.config.requestCtx + "/file/api/ApiDownload?md5=" + item.file :
        //     app.config.requestCtx +
        //     "/file/api/ApiDownload?md5=" +
        //     item.freeFile;
        //   console.log(fileLink, "fileLink");
        //   //提示加载中
        //   // 单次下载允许的最大文件为 200MB
        //   wx.downloadFile({
        //     url: fileLink,
        //     // filePath: wx.env.USER_DATA_PATH + `/${item.name}.${item.selectType}`,
        //     filePath: wx.env.USER_DATA_PATH + `/${item.name}.${item.selectType}`,
        //     success: function (res) {
        //       console.log(res, "wx.downloadFile success res");
        //       if (res.statusCode != 200) {
        //         util.hideLoadingWithErrorTips();
        //         return false;
        //       }
        //       var Path = res.tempFilePath; //返回的文件临时地址,用于后面打开本地预览所用
        //       let data = res.filePath;
        //       wx.openDocument({
        //         filePath: data,
        //         showMenu: true,
        //         success: function (res) {
        //           console.log("打开成功");
        //           util.hideLoading();
        //         },
        //       });
        //     },
        //     fail: function (err) {
        //       console.log(err, "wx.downloadFile fail err");
        //       util.hideLoadingWithErrorTips();
        //     },
        //   });
        // }
        if (item.selectType == "webpage") {
          this.setData({
            webpageSrc: item.jsek_link,
          });
        }
      }
      const token = wx.getStorageSync(app.config.tokenKey);
      if (!token && this.data.tab != 'jsek_teachingResources') {
        loginInfo(app, (data) => {
          if (data) {}
        })
      } else {
        goPlayerFun()
      }
    },
@@ -293,7 +290,6 @@
          result.push(...item.children);
        }
      }
      console.log(this.properties.treeList, "this.properties.treeList");
      this.properties.treeList.forEach((item) => {
        findChildren(item);
      });
@@ -314,7 +310,6 @@
      tree.forEach((node) => {
        flatten(node);
      });
      // console.log(result, 'result');
      return result;
    },
@@ -322,7 +317,6 @@
    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) {
@@ -337,63 +331,32 @@
      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; // 返回修改后的完整数组
    },
    // 全选
    checkAll() {
      wx.showLoading()
      const ids = [];
      const list = [];
      let list = [];
      let checked = "";
      this.properties.treeList.forEach((item) => {
        if (item.children && item.children.length > 0) {
          list.push(...this.flattenTree([item]));
        }
      });
      list = list.filter(item => !item.disabled)
      list.forEach((item) => {
        ids.push(item.id);
        checked = item.checked;
      });
      // const flag =
      //   list.findIndex((item) => item.checked == false) > -1 ? true : false;
      const flag = this.properties.checkAllState;
      // const updataList = this.findAndUpdateItemsByIds(
      //   this.properties.treeList,
      //   ids,
      //   flag
      // );
      // this.setData({
      //   treeList: updataList,
      // });
      var myEventDetail = {
        ids,
        flag,
      }; // detail对象,提供给事件监听函数
      };
      var myEventOption = {
        bubbles: true,
        composed: true,
      }; // 触发事件的选项
      };
      this.triggerEvent("changeListChecked", myEventDetail, myEventOption);
      if (!checked) {
        this.setData({
          selectAll: list,
        });
      }
      var myEventDetail = {
        value: !flag,
      }; // detail对象,提供给事件监听函数
@@ -402,29 +365,26 @@
        composed: true,
      }; // 触发事件的选项
      this.triggerEvent("changeCheckAll", myEventDetail, myEventOption);
      wx.hideLoading()
    },
    // 章节勾选
    checkResourceTitle(e) {
      wx.showLoading()
      const flag = e.detail.checked;
      // 这个时候checked的值是fals
      let checkResourceTitleList = [];
      const item = e.currentTarget.dataset.item;
      const parent = this.findParentById(this.properties.learnList, item.id);
      let childrenList;
      let childrenList = [];
      let flagNum;
      if (parent)
        childrenList = parent.children.filter((citem) => citem.id !== item.id);
      if (childrenList)
        flagNum = childrenList.findIndex((item) => item.checked == false);
      flagNum = childrenList.findIndex((item) => item.checked == false);
      let list = this.flattenTree([item]);
      list = list.filter(item => !item.disabled)
      let ids = [];
      list.forEach((item) => {
        if (
          item.sysType == "CmsFolder" ||
          (item.sysType == "CmsItem" && item.saleMethod.length)
        )
          ids.push(item.id);
        ids.push(item.id)
      });
      var myEventDetail = {
        ids,
@@ -434,26 +394,22 @@
        bubbles: true,
        composed: true,
      }; // 触发事件的选项
      // 去勾选点击目录的子项
      this.triggerEvent("changeListChecked", myEventDetail, myEventOption);
      if (flagNum == -1) {
      // 去勾选点击目录的父项
      if (flagNum == -1 && parent && !parent.disabled) {
        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,
        });
      }
      list.forEach((items) => {
        console.log(items.checked, "itesasd");
        if (!items.checked) {
          this.setData({
            cloudShoppingCart: items,
@@ -466,6 +422,7 @@
    },
    // 子项勾选
    checkResource(e) {
      wx.showLoading()
      const citem = e.currentTarget.dataset.item;
      const parent = e.currentTarget.dataset.parent;
      let outermost;
@@ -485,7 +442,7 @@
      };
      this.triggerEvent("changeResourceChecked", myEventDetail, myEventOption);
      // 通过判断,联通父级checked
      if (flag == -1) {
      if (flag == -1 && !parent.disabled) {
        let checkedState;
        citem.checked == false ? (checkedState = true) : (checkedState = false);
        var myEventDetail = {
@@ -511,7 +468,7 @@
        this.triggerEvent("changeListChecked", myEventDetail, myEventOption);
      }
      // 联通最外层级checked, 如果所有子项勾选, 且当前最外层未勾选, 则加上最外层的勾选
      if (!citem.checked && outermost) {
      if (!citem.checked && outermost && !outermost.disabled) {
        let allchildren = this.flattenTree([outermost]);
        allchildren = allchildren.filter(
          (item) =>
@@ -532,31 +489,12 @@
          this.triggerEvent("changeListChecked", myEventDetail, myEventOption);
        }
      }
      if (!citem.checked) {
        //选中的时候添加id
        let sonPurchaseSaleMethodId = this.data.sonPurchaseSaleMethodId || [];
        if (this.properties.paymentPage) {
          sonPurchaseSaleMethodId = [];
          this.properties.paymentPage = false;
        }
        if (!this.properties.paymentPage) {
          sonPurchaseSaleMethodId.push(citem.saleMethod[0].Id);
        }
        this.setData({
          sonPurchaseSaleMethodId: sonPurchaseSaleMethodId,
        });
      } else {
        // 取消选中的时候删除id
      }
    },
    // 加入购物车
    async onCloudShoppingCart(e) {
      let shoppingCartGetId = [];
      const item = e.currentTarget.dataset.item;
      console.log(item.isShopCar);
      const saleMethodId = e.currentTarget.dataset.item.saleMethod[0].Id;
      console.log(saleMethodId);
      let query = {
        start: 0,
        size: 999,
@@ -568,10 +506,8 @@
        shoppingCartGetId.push(item.saleMethod.id);
      });
      if (shoppingCartGetId.includes(saleMethodId)) {
        // this.data.updateShoppingCartHidden();
        // this.data.updateCloudLearning();
        wx.showToast({
          icon: "error",
          icon: "success",
          title: "已添加",
        });
        this.setData({
@@ -586,14 +522,20 @@
          }, ],
        };
        const addRes = await app.MG.store.addShoppingCart(query);
        console.log(addRes, "addRes");
        if (addRes) {
          wx.showToast({
            icon: "success",
            title: "添加成功",
          });
          item.isShopCar = false;
          this.triggerEvent("updateCloudLearning");
          var myEventDetail = {
            id: item.id,
          };
          var myEventOption = {
            bubbles: true,
            composed: true,
          };
          this.triggerEvent("updateCloudLearning", myEventDetail, myEventOption);
        }
      }
    },