支付宝、微信扫一扫

function sys(){
    // alert('扫一扫')
    //判断浏览器环境
    var userAgent = navigator.userAgent;
    if (userAgent.indexOf("MicroMessenger") > -1){
        
        // 获取配置信息
        // alert('appid'+wxappid)
        $.ajax({
            url: `/api/wechat/config/${wxappid}`,
            type: 'get',
            data: {url: encodeURIComponent(location.href.split('#')[0])},
            dataType: 'json',
            async: false,
            success: function(res){
                // alert('001')
                // alert(wx)
                wx.config({
                    debug: false,
                    appId: res.appId,
                    timestamp: res.timestamp,
                    nonceStr: res.nonceStr,
                    signature: res.signature,
                    jsApiList: ["scanQRCode"]
                });
                wx.ready(function () {
                    // alert('5')
                    wx.scanQRCode({
                        needResult: 1, // 默认为0,扫描结果由微信处理,1则直接返回扫描结果,
                        success: function (res) {
                            // alert('11111')
                            // alert(JSON.stringify(res))
                            // 扫码成功,跳转到二维码指定页面(res.resultStr为扫码返回的结果)
                            location.href = res.resultStr;
                        },
                        fail: function (res) {
                            // alert('44')
                            // 调用失败
                            if (res.errMsg.indexOf('function_not_exist') > 0 ) {
                                alert('当前版本过低,请进行升级');
                            }
                        },
                        cancel: function(res){
                            // 用户取消操作
                            alert('取消操作')
                        }
                    });
                });
            },
            error: function(res){
                // alert('222')
                // alert(JSON.stringify(res))
            }
        })



    } else if(userAgent.indexOf("AlipayClient") > -1){
        // alert('ZHIF')           
        ap.scan(function(res){
            // alert(JSON.stringify(res))
            // ap.alert(res.code);
            ap.redirectTo({url: res.code});
        });

    } else{
        // alert('a')
    }
}

猜你喜欢

转载自www.cnblogs.com/xhrr/p/11607645.html
今日推荐