yiming
2024-04-01 f72f8966ba4e1715e8df82d5cf1c7e912b6da719
packageBookService/pages/bookServices/detail/buyResource/index.js
@@ -12,12 +12,18 @@
    openLearnids: [],
    bookId: '',
    loading: false,
    shoppingList: [],
    shoppingCartGetId: [],
    methodId: [],
    paymentPage: false,
    superior: false
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    const systInfo = wx.getSystemInfoSync();
    const menu = wx.getMenuButtonBoundingClientRect(); // 胶囊信息
    const navBarHeight = (menu.top - systInfo.statusBarHeight) * 2 + menu.height; // 导航栏高度
@@ -28,7 +34,12 @@
    });
    this.getResourceClass()
    console.log(options);
    this.getResourceData({ productLinkPath: options.productLinkPath, refCode: options.refCode })
    this.getResourceData({
      productLinkPath: options.productLinkPath,
      refCode: options.refCode
    })
    this.getShoppingCartProductGet()
    this.getBookInfo(options.bookId)
  },
  /**
@@ -123,7 +134,8 @@
        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.checked = false,
                item.checkAll = true
            })
            const list = await this.getAllResource(res.datas.cmsDatas[0].datas)
            if (type.refCode == 'jsek_teachingResources') {
@@ -133,15 +145,17 @@
              })
              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
              })
              const list = await this.getAllResource(res.datas.cmsDatas[0].datas)
              this.findChildIds(list, result)
              this.setData({
                learn: list,
                loading: false
                loading: false,
                openLearnids: result
              })
              this.findChildIds(this.data.learn, this.data.openLearnids = [])
              console.log('教学资源', list);
            }
          } else {
            // 无数据
@@ -218,15 +232,103 @@
      }
    })
    data.datas.cmsDatas[0].datas.forEach(item => {
      if (this.data.tabValue == 'jsek_teachingResources') {
        item.checked = false
      } else if (this.data.tabValue == 'jsek_cloudLearning') {
        item.checked = false
        item.isbuy = this.resourceIsBuy(item)
        item.isShopCar = this.isShoppingCart(item)
      }
      item.checked = false
      item.isbuy = this.resourceIsBuy(item)
      item.isShopCar = this.isShoppingCart(item)
    })
    return data.datas.cmsDatas[0].datas
  },
  getBookInfo(id) {
    this.setData({
      "mockData'.id": 0,
      "mockData.price": 0,
      "mockData.count": 0
    })
    const query = {
      path: '*',
      queryType: '*',
      productId: id,
      favoriteTypes: 'FavoriteBookCity',
      itemFields: {
        'SysType=': 'CmsFolder',
      },
      coverSize: {
        height: 300,
      },
      fields: {
        seriesName: [],
        author: [],
        isbn: [],
        publicationDate: [],
        bookClassification: [],
        paperPrice: [],
        JDLink: [],
        tmallLink: [],
        dangdangLink: [],
        weidianLink: [],
        content: [],
        authorIntroduction: [],
        isApplyBook: [],
        isSell: [],
        pdf: [],
        protectedEpub: [],
        probationPage: [], //pdf试读页数
        freeEpubPage: [], //epub试读百分比
      },
    };
    app.MG.store.getProductDetail(query).then(async (res) => {
      this.setData({
        bookDetail: res.datas,
        buyIdList: res.datas.purchasedSaleMethodIdList
      });
    });
  },
  // 获取已购买商品
  getShoppingCartProductGet() {
    let query = {
      start: 0,
      size: 999,
      filterList: [],
      searchList: []
    }
    app.MG.store.getShoppingCartProductList(query).then((res) => {
      const list = []
      res.datas.forEach((item) => {
        list.push(item.saleMethod.id)
      })
      this.setData({
        shoppingList: res.datas,
        shoppingCartGetId: list
      })
    })
  },
  // 判断资源是否购买
  resourceIsBuy(data) {
    if (data.saleMethod && data.saleMethod.length) {
      const isSHow = this.data.buyIdList.some((item) => item == data.saleMethod[0].Id)
      return !isSHow
    } else {
      return false
    }
  },
  // 判断资源加入购物车按钮是否显示
  isShoppingCart(data) {
    if (data.saleMethod && data.saleMethod.length) {
      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
    }
  },
  // 获取展开项
  findChildIds(data, result) {
@@ -234,12 +336,12 @@
    for (let i = 0; i < data.length; i++) {
      if (index < 3) {
        const item = data[i]
        if (item.childrenFolderCount > 0) {
        if (item.children && item.children.length > 0) {
          result.push(item.id)
          for (let j = 0; j < item.children.length; j++) {
            if (index < 3) {
              const childrenItme = item.children[j]
              if (item.childrenCount > 0) {
              if (item.children && item.children.length > 0) {
                result.push(childrenItme.id)
                index += 1
              }
@@ -247,7 +349,7 @@
              break
            }
          }
        } else if (item.childrenCount > 0) {
        } else if (item.children && item.children.length > 0) {
          result.push(item.id)
          index += 1
        }
@@ -267,4 +369,102 @@
      })
    })
  },
  checkAll() {
    const child = this.selectComponent('#tree')
    child.checkAll()
  },
  //购买按钮
  batchPurchase() {
    let saleMethodIds = []
    let requests = []
    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 => ({
          saleMethodId: id,
          count: 1,
        })),
      }).then(res => {
        if (res) {
          wx.navigateTo({
            url: '/pages/cart/paymentPage/index?orderNumber=' + res.orderNumber,
          })
          selectAllId = []
          child.setData({
            selectAll: []
          })
        }
      })
    }
    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 => ({
          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数据
          })
        }
      })
    }
  },
})