跳至主要內容

调用扫一扫

chanchaw大约 1 分钟languagewechat

依赖

使用 jquery 进行测试要用到两个依赖 js 文件:jquery.min.js 和 jweixin-1.6.0.js 后者是微信官方提供的工具 js

实现

	
// html 模板页面调用本方法 reqConfigs
function reqConfigs(){
    let that = this;
	// 后端项目名称是 wxbe,对应控制器注解是 wechat
	// 具体请求是 getWxJsApiConfigs,其后跟随当前页面的URL
    const url_configs = 'https://www.jzy.world/wxbe/wechat/getWxJsApiConfigs/' + getCurPageUrl() ;
    $.ajax({
        type : "GET",
        contentType: "application/json;charset=UTF-8",
        url : url_configs,
        success : function(res){// 返回的数据是配置对象构成的数组,取下标2
            that.configs = res.data;// 全局保存配置对象数组
            that.config = res.data[2];
			console.log('后端响应来的数据数组是:');
			console.log(res);
			console.log('序号2的元素被应用::');
			console.log(res.data[2]);
            that.applyConfigs(res.data[2]);
        },
        error : function(e){//请求失败,包含具体的错误信息
            alert(e.status + ':' + e.responseText);
        }})
}
		
			
function applyConfigs(conf){
    let self = this;
	console.log('即将应用的配置是:',conf);
    wx.config({
        debug:false,
        appId:conf.appId,
        timestamp:conf.timestamp,
        nonceStr:conf.nonceStr,
        signature:conf.signature,
        jsApiList:['checkJsApi','scanQRCode']
    });

	console.log('开始调用扫描');
    wx.ready(function(){
        self.wxScan();
    })
}

function wxScan(){// 调用扫一扫
    let that = this;
    wx.scanQRCode({
        needResult: 1,
        scanType: ["qrCode","barCode"],
        success: function(res){
			console.log('成功获取扫描结果:');
			console.log(res);
			alert(res);
        }
    });
}

// 获取当前页面路径
function getCurPageUrl(){
    return window.location.href;
}

后端

参考后端项目 https://gitee.com/chanchaw/flux_transopen in new window 的 wxbe 分支的 “扫一扫测试完成”提交点的代码

源码

测试通过的源码项目在 https://gitee.com/chanchaw/wechat_scan_jqueryopen in new window