我的项目使用的是vue 开发的,因此首先安装 weixin-js-sdk
具体步骤如下:
一、安装weixin-js-sdk
npm i weixin-js-sdk --save
二、在使用的页面引入 ,或者全局引入
import wx from 'weixin-js-sdk'
三、 获取wx.config 所需要的数据,同时添加所需要使用的js-sdk的方法
getConfig({
url: location.href.split('#')[0]
}).then(res => {
// console.log(res)
wx.config({
debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。
appId: res.data.appId, // 必填,公众号的唯一标识
timestamp: res.data.timestamp, // 必填,生成签名的时间戳
nonceStr: res.data.nonceStr, // 必填,生成签名的随机串
signature: res.data.signature, // 必填,签名
jsApiList: ['checkJsApi', 'scanQRCode'] // 必填,需要使用的JS接口列表
})
})
四、使用
scan() {
var _this = this
wx.ready(function() {
wx.checkJsApi({
jsApiList: ['scanQRCode'],
success: function(res) {
wx.scanQRCode({
needResult: 1, // 默认为0,扫描结果由微信处理,1则直接返回扫描结果,
scanType: ['qrCode', 'barCode'], // 可以指定扫二维码还是一维码,默认二者都有
success: function(res) {
var result = res.resultStr // 当needResult 为 1 时,扫码返回的结果
alert('扫描结果' + result)
// _this.deviceCode = res.resultStr
}
})
}
})
})
}