微信下一句话扫码接口调用方法

步骤:

1.重定向到地址://996315.com/api/scan/?redirect_uri=扫码结果接收页URL
2.在接收页里获取qrresult参数结果

一般用法:

<a href="//996315.com/api/scan/?redirect_uri=修改成你要接收扫码结果页面的url地址">Scan</a>
<script type="text/javascript">
if (location.href.indexOf("qrresult=")>-1) alert(decodeURIComponent(location.href.split("qrresult=")[1]));
</script>

如果要接收扫码结果的页面就是当前页面,那么可以直接用location.href来指定。encodeURIComponent作用是兼容url有参数的情况,尤其是含#的情况,不用encodeURIComponent编码下的话会出现莫名其妙的错误。以下是完整代码。

<a href="javascript:window.open('//996315.com/api/scan/?redirect_uri=' + encodeURIComponent(location.href), '_self');">Scan</a>
<script type="text/javascript">
if (location.href.indexOf("qrresult=")>-1) alert(decodeURIComponent(location.href.split("qrresult=")[1]));
</script>

建议写成下面这样,对qrresult参数用专门的获取参数的js函数来获取。这样更稳定。

<a href="javascript:window.open('//996315.com/api/scan/?redirect_uri=' + encodeURIComponent(location.href), '_self');">Scan</a>
<script>
var qr=GetQueryString("qrresult");
if(qr){
      
      
    alert(qr); //放入表单输入框或者提交到后端,具体根据自己业务做相应处理
    //window.open(qr, "_self"); //打开二维码中的网址
}
 
function GetQueryString(name){
      
      
    var reg = new RegExp("\\b"+ name +"=([^&]*)");
    var r = location.href.match(reg);
    if (r!=null) return decodeURIComponent(r[1]);
}
</script>

注意:

如果用window.open唤起扫码接口请注意一定要加上"_self"参数,这个表示使用当前窗口打开扫码,如果不加表示新窗口打开,俗称弹窗,这个在2022年后的苹果手机系统已经完全禁止了弹窗,不加"_self"会发现无效,在我们平常的js代码中要用到window.open也应当一律加上"_self",以便兼容新的iOS系统。

猜你喜欢

转载自blog.csdn.net/sysdzw/article/details/130299857