点击扫码按钮拉取微信扫码
// 扫码添加设备
goAddEquipment() {
const wx = window.wx
let _this = this
// wx.ready(function() {
wx.checkJsApi({
jsApiList: ['scanQRCode'], // 需要检测的JS接口列表,所有JS接口列表见附录2,
success: function(res) {
console.log(res)
// 以键值对的形式返回,可用的api值true,不可用为false
// 如:{"checkResult":{"chooseImage":true},"errMsg":"checkJsApi:ok"}
wx.scanQRCode({
needResult: 1, // 默认为0,扫描结果由微信处理,1则直接返回扫描结果,
scanType: ["qrCode", "barCode"], // 可以指定扫二维码还是一维码,默认二者都有
success: function(res) {
console.log(res)
var code = res.resultStr // 当needResult 为 1 时,扫码返回的结果
// alert(code)
_this._initDeviceStatus(code)
// '设备重复绑定'
},
fail: function(res) {
console.log(res)
// _this._initWeixinAuth()
}
})
}
// })
})
},
不需要的可省略以下两步
// 判断是否有设备
_initDeviceStatus(code) {
this.$http.get(`/device?device_code=${code}`).then(res => {
if (res.errcode === 0 && res.data?.id) {
return this.$http.post('/device/is_bind', {
device_code: code
})
// this.confirm(code)
} else {
this.$toast('无效的设备二维码')
}
}).then(result => {
if (result.errcode === 0 && !result.data) {
this.confirm(code)
} else {
this.$toast('设备重复绑定')
}
})
}
confirm(code) {
this.$store.commit("bindEquipment/INIT_STATE")
this.$router.push({
path: '/game',
query: {
dcode: code
}
})
},