litian
2024-04-17 d4071d14eae980dd8fac6019f8b86f3b90465c56
packageBookService/pages/bookServices/detail/buyResource/index.js
@@ -16,13 +16,16 @@
    methodId: [],
    paymentPage: false,
    superior: false,
    checkAllState: false,
    checkAllState: true,
    productLinkPath: '',
    refCode: ''
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.getBookInfo(options.bookId);
    const systInfo = wx.getSystemInfoSync();
    const menu = wx.getMenuButtonBoundingClientRect(); // 胶囊信息
    const navBarHeight =
@@ -31,15 +34,11 @@
      barHeight: systInfo.statusBarHeight,
      navBarHeight: navBarHeight,
      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);
  },
  /**
@@ -148,7 +147,6 @@
                loading: false,
                openLearnids: result,
              });
              console.log("教学资源", list);
            }
          } else {
            // 无数据
@@ -228,12 +226,12 @@
    });
    data.datas.cmsDatas[0].datas.forEach((item) => {
      item.checked = false;
      item.isbuy = this.resourceIsBuy(item);
      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,
@@ -271,12 +269,17 @@
        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,
      });
      this.getResourceData({
        productLinkPath: this.data.productLinkPath,
        refCode: this.data.refCode,
      });
    });
  },
  // 获取已购买商品
  getShoppingCartProductGet() {
@@ -303,6 +306,22 @@
      const isSHow = this.data.buyIdList.some(
        (item) => item == data.saleMethod[0].Id
      );
      return !isSHow;
    } else {
      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;
@@ -380,25 +399,15 @@
    let item = [];
    let selectAllId = [];
    const child = this.selectComponent("#tree");
    console.log(
      child.data.superiorPurchaseSaleMethodId,
      "superiorPurchaseSaleMethodId"
    );
    console.log(child.data.sonPurchaseSaleMethodId, "child.data");
    // console.log(child.data.selectAll, 'selectAll');
    item = child.data.superiorPurchaseSaleMethodId;
    const sonItem = child.data.sonPurchaseSaleMethodId;
    let selectAll = child.data.selectAll;
    if (selectAll) {
      // console.log(selectAll);
      selectAll.forEach((item) => {
        if (item.saleMethod != 0) {
          // console.log(item, 'item');
          selectAllId.push(item.saleMethod[0].Id);
        }
      });
      console.log(selectAllId);
      app.MG.store
        .initOrder({
          requests: selectAllId.map((id) => ({
@@ -409,8 +418,7 @@
        .then((res) => {
          if (res) {
            wx.navigateTo({
              url:
                "/pages/cart/paymentPage/index?orderNumber=" + res.orderNumber,
              url: "/pages/cart/paymentPage/index?orderNumber=" + res.orderNumber,
            });
            selectAllId = [];
            child.setData({
@@ -420,15 +428,12 @@
        });
    }
    if (item) {
      console.log(item, "sadsdasd");
      // 拿到id
      item.forEach((items) => {
        if (items.saleMethod != 0) {
          saleMethodIds.push(items.saleMethod[0].Id);
        }
      });
      console.log(saleMethodIds, "saleMethodIds");
      app.MG.store
        .initOrder({
          requests: saleMethodIds.map((id) => ({
@@ -439,8 +444,7 @@
        .then((res) => {
          if (res) {
            wx.navigateTo({
              url:
                "/pages/cart/paymentPage/index?orderNumber=" + res.orderNumber,
              url: "/pages/cart/paymentPage/index?orderNumber=" + res.orderNumber,
            });
            child.setData({
@@ -460,8 +464,7 @@
        .then((res) => {
          if (res) {
            wx.navigateTo({
              url:
                "/pages/cart/paymentPage/index?orderNumber=" + res.orderNumber,
              url: "/pages/cart/paymentPage/index?orderNumber=" + res.orderNumber,
            });
            this.setData({
              paymentPage: true,
@@ -473,4 +476,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
    })
  }
});