在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 問答/HTML5  HTML/ 微信開發(fā)調(diào)用js-sdk

微信開發(fā)調(diào)用js-sdk

我現(xiàn)在需要點(diǎn)擊一個(gè)按鈕調(diào)取微信的支付接口
1 首先引入js文件<script src=" http://res.wx.qq.com/open/js/...;></script>
2 配置config,發(fā)起ajax請(qǐng)求,從后端獲取config所需要的參數(shù),然后執(zhí)行wx.ready()和wx.error()

$.ajax({
    url:url,
    method:'GET',
    ...略,
    success:function(data) {
        wx.config({

    debug: true, // 開啟調(diào)試模式,調(diào)用的所有api的返回值會(huì)在客戶端alert出來,若要查看傳入的參數(shù),可以在pc端打開,參數(shù)信息會(huì)通過log打出,僅在pc端時(shí)才會(huì)打印。

    appId: data.appId, // 必填,公眾號(hào)的唯一標(biāo)識(shí)

    timestamp: data.timestamp, // 必填,生成簽名的時(shí)間戳

    nonceStr: data.nonceStr, // 必填,生成簽名的隨機(jī)串

    signature: data.signature,// 必填,簽名,見附錄1

    jsApiList: ["chooseWXPay"] // 支付接口 必填,需要使用的JS接口列表
});

wx.ready(function(){
        wx.checkJsApi({
            jsApiList: ['chooseImage'], 
            success: function(res) {
               console.log(res)
            }
        });
    });
    
     wx.error(function(res){
        console.log(res)
    });
    }
})

3 這里有個(gè)用vue寫的頁面,我需要點(diǎn)擊button的時(shí)候進(jìn)行支付,或者隨便干些什么比如分享到朋友圈

<button @click="pay"></button>
我這里的js需要怎么寫? 這樣寫對(duì)嗎?
methods:{
  pay:function() {
   wx.chooseWXPay({

    timestamp: 0, // 支付簽名時(shí)間戳,注意微信jssdk中的所有使用timestamp字段均為小寫。但最新版的支付后臺(tái)生成簽名使用的timeStamp字段名需大寫其中的S字符

    nonceStr: '', // 支付簽名隨機(jī)串,不長于 32 位

    package: '', // 統(tǒng)一支付接口返回的prepay_id參數(shù)值,提交格式如:prepay_id=***)

    signType: '', // 簽名方式,默認(rèn)為'SHA1',使用新版支付需傳入'MD5'

    paySign: '', // 支付簽名

    success: function (res) {

        // 支付成功后的回調(diào)函數(shù)

    }

});
 }
}
回答
編輯回答
瘋浪

最好是點(diǎn)擊按鈕之前先調(diào)用jssdk的配置信息,將后端返回的內(nèi)容存到data里面,你點(diǎn)按鈕,直接使用data里面存的配置信息就行了。
大概是這樣的。

data: {
          config: ''
       },
created: function () {
       this.getConfig();
      },
methods: {
  //獲取配置信息
 getConfig: function () {
     var self = this;
     $.ajax({
         url: url,
         method: 'GET',
         success: function (data) {
           self.config = data;
               wx.config({
               debug: true,
               appId: data.appId, 
               timestamp: data.timestamp, 
               nonceStr: data.nonceStr,
              signature: data.signature, 
              jsApiList: ["chooseWXPay"] 
           });
        }
   })

},

 //點(diǎn)按鈕
   pay: function () {
       var self=this;
       wx.ready(function () {
           wx.chooseWXPay({
              timestamp: self.timestamp, 
              nonceStr: self.nonceStr
              package: self.package, 
              signType: self.signType, 
                paySign: self.paySign, 
               success: function (res) {
                                    // 支付成功后的回調(diào)函數(shù)
                     }
              });
         });
          wx.error(function (res) {
                   console.log(res)
          });
        }
   })
2018年6月18日 10:26