安装 weixin-js-sdk
npm install weixin-js-sdk --save
mian.js中引入'weixin-js-sdk'
import wx from 'weixin-js-sdk'
Vue.prototype.$wx = wx
页面中配置wx.config
onLoad() {
this.getCofig();
},
methods: {
getCofig() {
let url = '';
url = window.location.href.split('?')[0]; //url是当前页面的域名
console.log(window.location.href,url)
this.$api.getData(`sys/getSignature?signatureUrl=${url}`).then(res=>{
if(res.success){
const data = res.result
Vue.prototype.$wx.config({
debug: false, // 开启调试模式,
appId: 'wwxxxxxxx', // 必填,企业号的唯一标识
timestamp: data.timesTamp, // 必填,生成签名的时间戳
nonceStr: data.noncestr, // 必填,生成签名的随机串
signature: data.signature, // 必填,签名
jsApiList: ['scanQRCode', 'checkJsApi'], // 必填,需要使用的JS接口列表
});
Vue.prototype.$wx.ready(() => {
console.log('配置完成,扫码前准备完成')
})
Vue.prototype.$wx.error(function (res) {
console.log('出错了:' + res.errMsg); //wx.config配置错误,会弹出窗口哪里错误,然后根据微信文档查询即可。
});
}
})
},
}
使用扫码功能
Vue.prototype.$wx.scanQRCode({
needResult: 1,
success: function(res) {
console.log(res.resultStr)
}
});