payResult.js 2.53 KB
import {
  shopmallApi
} from '~/api/api'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    count: 5,
    payStatus: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    console.log(options,123355);
    wx.p.wxApi.showLoading('加载中')
    if (options && options.orderCode) {
      this.checkPay(options.orderCode)
      this.query = JSON.parse(options.query)
      this.ids = JSON.parse(options.ids)
      this.orderCode = options.orderCode
      this.buyType = options.buyType
    }
  },
  // 查询支付状态
  checkPay(orderCode) {
    this.inval = setInterval(() => {
      this.checkOrderDetail(orderCode)
    }, 1000)
  },
  countdown() {
    this.timer = setInterval(() => {
      let { count } = this.data
      if (count == 0) {
        clearInterval(this.timer)
        this.toOrder()
        return
      }
      this.setData({
        count: --count
      })
    }, 1 * 1000)
  },
  pay() {
    let _this = this
    wx.p.wxApi.showLoading('加载中')
    shopmallApi
      .createOrder(this.query)
      .then(data => {
        if (data.code == -1) {
          wx.hideLoading()
          wx.showToast({
            title: data.message,
            icon: 'error'
          })
          return
        }
        if(this.buyType!='pay'){
          this.removeCar(this.ids)
        }
        let subOrderCode = data.subOrderDTOS[0].subOrderCode
        wx.hideLoading()
        wx.navigateToMiniProgram({
          appId: 'wx06d1198e751435d1',
          path: `packagesPayment/pages/payment/index?orderCode=${subOrderCode}`,
          envVersion: 'trial',
          success(res) {
            _this.checkPay(_this.orderCode)
          }
        })
      })
  },
  // 查询订单详情
  checkOrderDetail(orderCode) {
    shopmallApi.orderDetailInfo({ orderCode }).then(res => {
      if (res && res.payStatus && res.payStatus > 1) {
        clearInterval(this.inval)
        this.countdown()
        wx.hideLoading();
        this.setData({
          payStatus: res.payStatus == 2 ? 1 : 2,
          giveIntegral: res.giveIntegral
        })
      }
    })
  },
  // 支付前移除购物车
  removeCar(ids) {
    shopmallApi
      .removeBuuCar({
        ids
      })
      .then(data => { })
  },
  toCar() {
    wx.$router.reLaunch('/packageMall/pages/shoppingCart/shoppingCart')
  },
  toOrder() {
    wx.$router.reLaunch('/packageMine/pages/order/index/index')
  },
  onUnload(){
    clearInterval(this.timer)
    clearInterval(this.inval)
  },
  onHide() {
    clearInterval(this.timer)
    clearInterval(this.inval)
  },

})