闫增涛
2024-03-19 33615d230f898d8dc48c3baed0570f51aef26330
packageBookService/pages/bookServices/detail/index.js
@@ -1,6 +1,7 @@
// pages/bookServices/detail/index.js
import Message from 'tdesign-miniprogram/message/index.js';
import Toast from 'tdesign-miniprogram/toast';
import tool from '../../../../assets/js/toolClass'
const app = getApp();
Page({
  /**
@@ -49,7 +50,8 @@
      useCount: 0, // 组件已使用次数
      residue: 0, // 组卷剩余次数
      mockList: [] // 用户组卷提交信息
    }
    },
    flag: true
  },
@@ -1115,6 +1117,88 @@
          })
        }
      })
  },
  // 购买组卷
  async buyMock() {
    let res
    if (!this.data.mockData.id) return wx.showToast({
      icon: "error",
      title: '暂无组卷',
      mask: true
    })
    // 构建订单查询对象
    try {
      let query = {
        remarks: '组卷',
        requests: [
          {
            saleMethodId: this.data.mockData.id,
            count: 1
          }
        ]
      }
      if (this.data.flag) {
        this.setData({
          flag: false
        })
        res = await app.MG.store.initOrder(query)
  }
      if (res.orderNumber && this.data.mockData.price) {
        wx.navigateTo({
          url: `/pages/cart/paymentPage/index?orderNumber=${res.orderNumber}`,
        })
      } else {
        // 0元直接领取
        // 确认订单
        const confirmOrderRes = await app.MG.store.confirmOrder({ orderNum: res.orderNumber })
        if (confirmOrderRes) {
          wx.showToast({
            icon: "success",
            title: '领取成功',
          })
          this.setData({
            flag: true
          })
          await this.recordBuyMock() // 记录购买时间
        }
      }
    } catch (error) {
      console.log(error)
    }
  },
  // 记录购买组卷时间
  recordBuyMock() {
    const num = this.data.mockData.mockList.length
    const id = tool.uuid(8)
    const createDate = new Date().getTime()
    const mockList = this.data.mockData.mockList
    mockList.push({ id, createDate, name: `第${num + 1}套试卷`, state: '0' })
    this.setData({
      'mockData.mockList': mockList
    })
    // 记录购买时间,并设状态state为1,表示刚购买完成组卷
    app.MG.identity.setUserKey({
      setKeyRequests: [
        {
          domain: 'mockData',
          key: this.data.bookDetail.id + '',
          value: JSON.stringify(this.data.mockData.mockList)
        }
      ]
    })
  },
  // 底部购买按钮
  buyBtn() {
    if (this.data.tabValue == 'brief') {
      this.buyBook()
    }
    else if (this.data.tabValue == 'jsek_cloudLearning') {
      console.log('云学习');
    } else if (this.data.tabValue == 'questionBank') {
      this.buyMock()
    } else {
      console.log(this.data.tabValue);
    }
  }
})