前台流程是
1,給后台傳一個code(必傳)和后台需要的參數后台返回我參數
2,成功后我拉起支付吧這幾個返回參數在傳給微信端 然后監聽結果
// 用戶點擊我要支付按鈕后 wx.login({ success: function (res) { if (res.code) { //發起網絡請求 wx.request({ url: 這里是后台接口, //你需要給后台傳一個code method: 'GET', data: { code: res.code //請求wx.login成功后會返回一個code }, success: function (r) { // 成功后 后台返回參數 timeStamp,nonceStr,package,paySign,appid。(有的還會返回其他參數,左邊的這幾個參數是必須返回的) var _res = r.data.data // 太長了把r下面的data給_res這個變量 wx.requestPayment({ //調起支付 'timeStamp': _r.timeStamp, // 必填 時間戳從1970年1月1日00:00:00至今的秒數,即當前的 'nonceStr': _r.nonceStr, // 必填 隨機字符串,長度為32個字符以下。 'package': _r.package, // 必填 統一下單接口返回的 prepay_id 參數值 'signType': 'MD5', // 必填 簽名算法,暫支持 MD5 'paySign': _r.paySign, // 必填 簽名 'success': function (res) { // 接口調用成功的回調函數 console.log(res); }, 'fail': function (res) { // 接口調用失敗的回調函數 console.log('fail:' + JSON.stringify(res)); } }) }, fail: function (err) { console.log(err) } }) } else { console.log('獲取用戶登錄態失敗!' + res.errMsg) } } })
1 掃碼 2 // 允許從相機和相冊掃碼 3 wx.scanCode({ 4 success(res) { 5 console.log(res) 6 } 7 }) 8 // 只允許從相機掃碼 9 wx.scanCode({ 10 onlyFromCamera: true, 11 success(res) { 12 console.log(res) 13 } 14 })
// 轉載