微信服务号网页授权多个域名,支持未备案域名授权

1.首先我们需要准备一个已认证的服务号和一个已备案的域名

2.然后在你已备案的域名根目录(二级三级…目录都可以,不需要修改代码)下创建PHP索引index.php文件,代码如下:

<!DOCTYPE html>
<html lang="cn">

<head>
    <meta charset="UTF-8">
    <title>微信授权</title>
</head>

<body>
    <script>
        var GWC = {
            version: '1.2.0',
            urlParams: {},
            appendParams: function (url, params) {
                if (params) {
                    var baseWithSearch = url.split('#')[0];
                    var hash = url.split('#')[1];
                    for (var key in params) {
                        var attrValue = params[key];
                        if (attrValue !== undefined) {
                            var newParam = key + "=" + attrValue;
                            if (baseWithSearch.indexOf('?') > 0) {
                                var oldParamReg = new RegExp('^' + key + '=[-%.!~*\'\(\)\\w]*', 'g');
                                if (oldParamReg.test(baseWithSearch)) {
                                    baseWithSearch = baseWithSearch.replace(oldParamReg, newParam);
                                } else {
                                    baseWithSearch += "&" + newParam;
                                }
                            } else {
                                baseWithSearch += "?" + newParam;
                            }
                        }
                    }

                    if (hash) {
                        url = baseWithSearch + '#' + hash;
                    } else {
                        url = baseWithSearch;
                    }
                }
                return url;
            },
            getUrlParams: function () {
                var pairs = location.search.substring(1).split('&');
                for (var i = 0; i < pairs.length; i++) {
                    var pos = pairs[i].indexOf('=');
                    if (pos === -1) {
                        continue;
                    }
                    GWC.urlParams[pairs[i].substring(0, pos)] = decodeURIComponent(pairs[i].substring(pos + 1));
                }
            },
            doRedirect: function () {
                var code = GWC.urlParams['code'];
                var appId = GWC.urlParams['appid'];
                var scope = GWC.urlParams['scope'] || 'snsapi_base';
                var state = GWC.urlParams['state'];
                var isMp = GWC.urlParams['isMp']; //isMp为true时使用开放平台作授权登录,false为网页扫码登录
                var baseUrl;
                var redirectUri;

                if (!code) {
                    baseUrl = "https://open.weixin.qq.com/connect/oauth2/authorize#wechat_redirect";
                    if (scope == 'snsapi_login' && !isMp) {
                        baseUrl = "https://open.weixin.qq.com/connect/qrconnect";
                    }
                    //第一步,没有拿到code,跳转至微信授权页面获取code
                    redirectUri = GWC.appendParams(baseUrl, {
                        'appid': appId,
                        'redirect_uri': encodeURIComponent(location.href),
                        'response_type': 'code',
                        'scope': scope,
                        'state': encodeURIComponent(state),
                    });
                } else {
                    //第二步,从微信授权页面跳转回来,已经获取到了code,再次跳转到实际所需页面
                    redirectUri = GWC.appendParams(GWC.urlParams['redirect_uri'], {
                        'code': code,
                        'state': encodeURIComponent(state)
                    });
                }

                location.href = redirectUri;
            }
        };

        GWC.getUrlParams();
        GWC.doRedirect();
    </script>
</body>

</html>

 

3.然后进入你的微信服务号 – 设置 – 公众号设置 -功能设置,然后设置网页授权域名,填 你已备案的域名/weixin

4.再在需要授权的其他域名中(支持未备案域名),修改对应的授权接口,因为各个源码开发时接口对应的文件名各不相同,但回调域名都是微信官方的https://open.weixin.qq.com/connect/oauth2/authorize

这时候我们需要一款可以搜索文件内容的软件搜索你的源码,搜索“https://open.weixin.qq.com/connect/oauth2/authorize”替换为“https://你的域名/weixin”

协议头可以是http但最好还是去申请个SSL用https的协议头保险点

5.使用方法就是你哪个域名需要授权就修改哪个域名下的微信授权文件下的“https://open.weixin.qq.com/connect/oauth2/authorize”替换为“https://你已备案的域名/”即可

其实微信服务号已经增多了一个授权域名了,填2个够够的了对于那些做正规网站的,但对于没有备案的域名(你懂的)想要用微信授权登录的话,这个方法就大大的好了,我目前就在用

© 版权声明
THE END
喜欢就支持一下吧
点赞0 分享
评论 抢沙发

请登录后发表评论

    暂无评论内容