天天看点

公众号无限对接

准备工具:

认证公众号,备案域名,服务器一台,配置随意,访问不卡就可以

服务器安装宝塔,添加一个站点,站点内不需要什么页面,宝塔默认的就可以

放入get-weixin-code.html文件,这个文件不需要设置什么,放入公众号验证的txt文本

公众号设置ip白名单,业务域名,授权域名,这个和正常对接是一样的

假设配置无限对接的站点为A,需要对接的站点为B

那么A的配置如下简单配置

公众号无限对接

下面是get-weixin-code.html 的内容

<!DOCTYPE html>
<html >

<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>
           

以xxx游戏为例子:

正常搭建xxx或者其他平台,填入appid和AppSecret

如 $appid=”wxd12adf8177ff9e40″;

$key=”0be823266e76c095bfd2af70f885dc75″;

原获取公众号登陆地址为 https://open.weixin.qq.com/connect/oauth2/authorize?appid=

现对接至A实现无限对接地址为 http://xx.xxx.cn/get-weixin-code.html?appid=

其实这里意思呢简单明了,官方的就不多解释了,就是验证你所对接的公众号的appid和AppSecret

而我们配置的A站点中的get-weixin-code.html文件同样可以实现这个效果

只需要把官方的地址更改为我们的A站点就可以了。

这个所起到的效果就是防封,突破一个公众号只能授权俩个站点的限制,真正达到了无限授权的目的。