const api = require('../api/index')
function payMoney(data){
  return new Promise((resolve, reject) => { 
    api.weixinpay(data).then(res=>{
      console.log(res)
      if(res.code == 200){
        var obj = res.data.data
        wx.hideLoading()
        wx.requestPayment
            (
              {
                "timeStamp": `${obj.timestamp}`,
                "nonceStr": obj.nonceStr,
                "package": "prepay_id="+obj.prepayId,
                "signType":obj.signType,
                "paySign": obj.signature,
                "success":function(res){
                  // console.log(res)
                  // resolve(res); 
                },
                "fail":function(res){
                  // console.log(res)
                  resolve(res); 
                },
                "complete":function(res){
                  console.log(res)
                  resolve(res); 
                }
              }
            )
      }
    }).catch(error=>{
      reject(error || '未知错误'); 
    })
  })
}

// 导出请求方法  
module.exports = {  
  payMoney: (data) => payMoney(data)
};