vue 调用微信支付方法

pay(){
let data ={
  order_id:this.order_id,
  wechatpay_type:this.wechatpay_type,
  merchant_id:localStorage.merchant_id,
  authentication_token:this.token,
  client_token:this.client_token
}

this.$fetch(this.Wxpay, data)
.then(res=>{
  if(res.code==200){
    if (res.data.wechatpay_type == "wxpay") {
    // 公众号支付
    this.callWxPay(res.data.pay_param);
  } else if (res.data.wechatpay_type == "wxpay_h5_wap") {
    this.callWxPayH5(res.data.pay_param.mweb_url);
  } else if (res.data.wechatpay_type == "wxpay_app") {
    this.callWxPayAPP(res.data.pay_param);
  } else {
    Toast('无效的支付类型');
  }
  }else{
    Toast(res.error)
  }
  },err=>{
    alert(JSON.stringify(err))
  })
},
 
jsApiCall(params) {
  let that = this
  WeixinJSBridge.invoke(
    'getBrandWCPayRequest', {
      'appId': params.appId,
      'timeStamp':params.timeStamp,
      'nonceStr': params.nonceStr,
      'package': params.package,
      'signType': params.signType,
      'paySign': params.paySign
    },
    function (res) {
      if (res.err_msg === 'get_brand_wcpay_request:ok') {
        Toast('微信支付成功')
        that.$router.replace({name:'fullOrder',query:{id:'2'}})
      } else if (res.err_msg === 'get_brand_wcpay_request:cancel') {
        Toast('用户取消支付')
        that.$router.replace({name:'fullOrder',query:{id:'1'}})
      } else if (res.err_msg === 'get_brand_wcpay_request:fail') {
        Toast('网络异常,请重试')
      }
    }
  );
},
 
callWxPay(params) {
  if (typeof WeixinJSBridge == "undefined"){
  if( document.addEventListener ){
    document.addEventListener('WeixinJSBridgeReady', this.jsApiCall(params), false);
  }else if (document.attachEvent){
    document.attachEvent('WeixinJSBridgeReady', this.jsApiCall(params));
    document.attachEvent('onWeixinJSBridgeReady', this.jsApiCall(params));
    }
  }else{
    this.jsApiCall(params);
  }
},
 
callWxPayH5(mweb_url) {
  location.href = mweb_url;
},
 
callWxPayAPP(params) {
  let dsBridge = require("dsbridge");
  dsBridge.call("requestWeChatPay", params, function (data) {
    alert(data);
  })
},
 
wechatpaytype(){
  if(this.isWeiXin()){
    this.wechatpay_type='wxpay'
  }else{
    this.wechatpay_type='wxpay_h5_wap'
  }
},
 
isWeiXin(){    //判断是否微信平台
  var ua = window.navigator.userAgent.toLowerCase();
    if (ua.match(/MicroMessenger/i) == 'micromessenger'){
    return true;
  } else {
    return false;
  }
}
posted @ 2018-05-08 16:46  我以为你不在乎  阅读(14736)  评论(0编辑  收藏  举报