闫增涛
2024-09-18 c96612e8a63ecef9590be0f8b5199277ec94e5b9
packageBookService/pages/bookServices/detail/buyResource/index.js
@@ -19,8 +19,8 @@
    checkAllState: true,
    productLinkPath: '',
    refCode: '',
    haider: false,
    naData: false
    naData: false,
    bookRefCode: []
  },
  /**
@@ -39,7 +39,6 @@
      productLinkPath: options.productLinkPath,
      refCode: options.refCode
    });
    this.getResourceClass();
    this.getShoppingCartProductGet();
  },
@@ -77,6 +76,7 @@
   * 用户点击右上角分享
   */
  onShareAppMessage() {},
  onShareTimeline() {},
  changeCheckAll(e) {
    this.setData({
      checkAllState: e.detail.value,
@@ -125,9 +125,9 @@
  },
  // 扁平化数据转换tree
  getTreeList(rootList, pathLength, newArr, path) {
  getTreeList(rootList, newArr, path) {
    for (const item of rootList) {
      if ((pathLength - item.productLinkPath.length >= 0 && pathLength - item.productLinkPath.length <= 3) && item.productLinkPath.includes(path)) {
      if (path == item.productLinkPath.replace('\\' + item.id, '')) {
        if (item.sysType == 'CmsItem') {
          if (item.selectType == 'webpage') {
            item.disabled = true
@@ -140,19 +140,16 @@
            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)
        }
        // 在插入过程中对数据进行排序
        newArr = this.insertAndSortObjectsByProductLinkPath(newArr, item)
        // 插进数组后 从原数据中删除
        rootList = rootList.filter(nitem => nitem.id != item.id)
      }
    }
    //给数组里面再添加一个children的空数组
    for (const i of newArr) {
      i.children = []
      this.getTreeList(rootList, pathLength + 6, i.children, i.productLinkPath)
      this.getTreeList(rootList, i.children, i.productLinkPath)
      if (i.children.length == 0) {
        delete i.children
      }
@@ -179,8 +176,7 @@
  // 排序数组 按照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)));
    let insertIndex = array.findIndex(obj => newObj.id < obj.id);
    // 如果没有找到合适的位置,则放在数组末尾  
    if (insertIndex === -1) {
      insertIndex = array.length;
@@ -190,16 +186,10 @@
    // 测试6 正式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
      if (a.id < b.id) return -1;
      if (a.id > b.id) return 1;
      return 0;
    });
    // 返回更新后的数组  
    return array;
  },
@@ -216,10 +206,44 @@
        flattenedArray.push(...childrenArray);
      }
    });
    return flattenedArray.filter((item) => item.sysType == "CmsItem");
    return flattenedArray;
  },
  // 教学资源 云学习 获取
  // 递归树结构,如果父级有销售方式(有效期),去掉所有子集的销售方式
  clearTreeSealmethod(clearSaleMethod, tree) {
    for (let item of tree) {
      const saleData = item.saleMethod &&
        item.saleMethod.length ?
        item.saleMethod.find((citem) => citem.SaleType == 'Normal') : null
      if (
        ((saleData && new Date(saleData.EndDate).getTime() >= new Date().getTime() && new Date(saleData.BeginDate).getTime() <= new Date().getTime()) ||
          clearSaleMethod) && item.children && item.children.length
      ) {
        item.children.forEach((ditem) => (ditem.saleMethod = []))
      }
      if (item.children && item.children.length)
        this.clearTreeSealmethod(
          saleData && new Date(saleData.EndDate).getTime() >= new Date().getTime() && new Date(saleData.BeginDate).getTime() <= new Date().getTime(),
          item.children
        )
    }
    return tree
  },
  // 勾选禁用
  handleTreeCheck(tree) {
    for (const item of tree) {
      const saleData = item.saleMethod &&
        item.saleMethod.length ?
        item.saleMethod.find((citem) => citem.SaleType == 'Normal') : undefined
      if (saleData && new Date(saleData.EndDate).getTime() >= new Date().getTime() && new Date(saleData.BeginDate).getTime() <= new Date().getTime()) {
        item.disabled = false
      } else {
        item.disabled = true
      }
      if (item.children && item.children.length) this.handleTreeCheck(item.children)
    }
    return tree
  },
  //  获取云学习
  getResourceDataList(type) {
    this.setData({
      loading: true,
@@ -255,236 +279,48 @@
        return this.setData({
          noResources: true,
          loading: false,
          haider: true,
        });
      }
      let list = []
      // 测试 6位// 正式 5位//  测试调用传20,内部7  正式调用传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
        //   })
      res.datas.cmsDatas[0].datas.forEach((item) => {
        item.checked = false;
        // if (item.sysType == 'CmsItem') {
        //   item.isbuy = this.resourceIsBuy(item);
        //   item.isShopCar = this.isShoppingCart(item);
        // }
        // 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,
          });
        }
      }
      });
      // 显示 需要购买的和需要免费领取的
      this.getTreeList(res.datas.cmsDatas[0].datas, list, query.cmsPath)
      list = this.ensureTreeConsistency(list)
      list = this.changeResourceChildren(list)
      list = this.clearTreeSealmethod(false, list)
      list = this.handleTreeCheck(list)
      this.handleTreeData(list).forEach(item => {
        item.isbuy = this.resourceIsBuy(item);
        item.isShopCar = this.isShoppingCart(item);
      })
      console.log('修改后云', list);
      let result = [];
      let arr = res.datas.cmsDatas[0].datas.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,
      });
    })
  },
  //  获取教学资源  云学习  云测试
  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,
            haider: true,
          });
        }
        //教学资源 云学习
        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,
                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,
                haider: true
              });
            }
            console.log(list, '云學習');
          } 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,
            haider: true
          });
        } 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",
        // 资源类型,试读文件,是否允许下载等参数
        selectType: [],
        freeFile: [],
        file: [],
        protectedFile: [],
        resourcesClassification: [],
        isDownload: [],
        jsek_resourceBrief: [],
        jsek_link: [],
        accessType: [],
        learnSelectType: [],
      },
      pading: {
        start: 0,
        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;
          }
        }
        if (item.file) {
          if (item.fileMap[item.file].protectType == "Private")
            item.disabled = true;
        }
      }
    });
    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,
      "mockData.price": 0,
      "mockData.count": 0,
    });
    const query = {
      path: "*",
      queryType: "*",
@@ -522,10 +358,6 @@
        bookDetail: res.datas,
        buyIdList: res.datas.purchasedSaleMethodIdList,
      });
      // this.getResourceData({
      //   productLinkPath: this.data.productLinkPath,
      //   refCode: this.data.refCode,
      // });
      this.getResourceDataList({
        productLinkPath: this.data.productLinkPath,
        refCode: this.data.refCode,
@@ -555,48 +387,73 @@
  // 判断资源是否购买
  resourceIsBuy(data) {
    if (data.saleMethod && data.saleMethod.length) {
      const isSHow = this.data.buyIdList.some(
        (item) => item == data.saleMethod[0].Id
      const saleData = data.saleMethod.find((item) => item.SaleType == 'Normal')
      if (saleData.State == 'Disable') return false
      const isShow = this.data.buyIdList.some(
        (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;
    }
  },
  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;
      const saleData = data.saleMethod.find((item) => item.SaleType == 'Normal')
      // 先判断是否免费
      if (saleData.Price <= 0) return false
      // 再判断是否购买
      const isBuy = this.data.buyIdList.some(
        (item) => item == data.saleMethod[0].Id
      );
      const isBuy = this.data.buyIdList.some((item) => item == saleData.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 == saleData.Id)
        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
          }
        }
      }
      // if (data.saleMethod[0].Price <= 0) return false;
      // // 再判断是否购买
      // const isBuy = this.data.buyIdList.some(
      //   (item) => item == data.saleMethod[0].Id
      // );
      // if (isBuy) {
      //   // 购买了
      //   return false;
      // } else {
      //   // 判断是否加入购物车
      //   const isSHow = this.data.shoppingCartGetId.some(
      //     (item) => item == data.saleMethod[0].Id
      //   );
      //   return !isSHow;
      // }
    } else {
      return false;
    }
@@ -629,17 +486,6 @@
      }
    }
  },
  // 获取资源所属分类
  getResourceClass() {
    let query = {
      refCodes: ["resourcesClassification"],
    };
    app.MG.store.getProductTypeField(query).then((res) => {
      this.setData({
        resourceClassList: JSON.parse(res[0].config).option,
      });
    });
  },
  checkAll() {
    const child = this.selectComponent("#tree");
    child.checkAll();
@@ -652,87 +498,90 @@
        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,
            });
    // 修改后方法
    const checkData = this.handleTreeData(this.data.learn).filter(item => item.checked == true)
    console.log('选中', checkData);
    // 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.length) {
    //   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数据
            });
          }
        });
    }
    //         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数据
    //         });
    //       }
    //     });
    // }
  },
  // 章节勾选方法
@@ -787,5 +636,26 @@
    this.setData({
      learn: updated
    })
  },
  upDateShopCar(id, tree) {
    for (let item of tree) {
      if (item.id == id) {
        item.isShopCar = false
      } else {
        if (item.children && item.children.length) this.upDateShopCar(id, item.children)
      }
    }
    return tree
  },
  // 更新购物车图标
  updateCloudLearning(e) {
    const {
      id
    } = e.detail
    console.log(id);
    const list = this.upDateShopCar(id, this.data.learn)
    this.setData({
      learn: list
    })
  }
});