wxpay.js 1.71 KB
import orderService from './orderService.js'
import router from '../../router/index.js'

// const api = new orderService();
/**
 * orderCode 订单号
 * fromWhere 支付来源页面  0:确认订单页面,1:订单列表,2:订单详情
 */
export const prePay = function (productId, orderCode, fromWhere, isStore) {
  let api = new orderService()
  wx.showLoading({
    title: '',
  })
  let openID = '';
  try {
    openID = wx.getStorageSync('openid')
  } catch (e) {

  }

  if (openID) {
    api.prePay(orderCode, openID, () => wx.hideLoading())
      .then(data => {
        if (data) {
          callWxPay(data, productId, orderCode, fromWhere, isStore)
        }
      })
      .catch(error => {
        console.log(error)
      })
  } else {
    //提示重新登录
  }
}

function callWxPay(data, productId, orderCode, fromWhere ,isStore) {
  let that = this;
  // let orderCode = ;
  wx.requestPayment({
    ...data,
    success: function (res) {
      console.log('orderCode:' + orderCode + '==productID:' + productId);
      let params = {
        orderCode: orderCode,
        product_id: productId,
        isStore
      }
      router.go('orderSuccess', params);
    },
    fail: function (res) {
      console.log(res)
      let params = {
        orderCode: orderCode
      }
      if (res.errMsg == 'requestPayment:fail cancel') {
        if (fromWhere == 0) {
          router.go('orderDetail', params);
        }

      } else {
        wx.showModal({
          content: res.errMsg,
          showCancel: false,
          confirmText: "确定",
          complete: function (res) {
            if (fromWhere == 0) {
              router.go('orderDetail', params);
            }
          }
        })
      }
    }
  })
}