注意:这里留意this的指向问题,
/**
* 扫码事件
*/
scanCodeEvent: function(){
var that = this;
wx.scanCode({
onlyFromCamera: true,// 只允许从相机扫码
success(res){
console.log(res.result,'666');
// console.log("扫码成功:"+JSON.stringify(res))
// 扫码成功后 在此处理接下来的逻辑
//以下方法可以分别拿到二维码的type和id;
// const q = decodeURIComponent(query.q)
const q = decodeURIComponent(res.result)
function parseUrl(url, key) {
let arr = url.split('?')
let querys = arr[1];
let params = querys.split('&');
for (let i = 0 ; i < params.length; i++) {
let vars = params[i];
let arr2 = vars.split('=');
if (arr2[0] == key) {
return arr2[1];
}
}
return '';
}
that.qrcodeId = parseUrl(q, 'id')
function parseType(url,type) {
let arr2 = url.split('?')
let querys2 = arr2[1];
let params2 = querys2.split('&');
for(let j = 0;j < params2.length;j++){
let vars2 = params2[j];
let arr3 = vars2.split('=');
if(arr3[0] == type) {
return arr3[1];
}
}
return ''
}
that.type = parseType(q,'type')
console.log(that.qrcodeId ,'this.qrcodeId ');
console.log(that.type ,'this.type ');