关于微信授权域名一个月只能修改三次的解决方案!

因为最近公司对外出售的一款微信h5的棋牌游戏,因为游戏类的分享比较频繁所以经常造成 域名被封!  每次被封只能更换域名,但是微信 不知道那个傻逼产品经理想出的一个月只能修改三次的限制,导致被封三次后这个月都没法再次换域名了!

想到了一个曲线救国的办法,就是微信授权域名 和游戏网站域名分开!

首先新建一个网站(例如 http://one.com) 在网站根目录下 新建一个 zpl.html

文件里的代码如下

<!DOCTYPE html>
<html lang="en">
 
<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': state,
                    });
                } else {
                    //第二步,从微信授权页面跳转回来,已经获取到了code,再次跳转到实际所需页面
                    redirectUri = GWC.appendParams(GWC.urlParams['redirect_uri'], {
                        'code': code,
                        'state': state
                    });
                }
 
                location.href = redirectUri;
            }
        };
 
        GWC.getUrlParams();
        GWC.doRedirect();
    </script>
</body>
 
</html>

 

然后修改 现在网站(例如 http://two.com)的微信授权接口,默认会向  https://open.weixin.qq.com/connect/oauth2/authorize 这个地址请求

把这个地址换成  http://two.com/zpl.html 即可

然后往微信公众平台->接口权限->网页授权获取用户基本信息->修改,填写授权回调页面域名,例如one.com

庄朋龙
庄朋龙

一个爱生活的技术菜鸟

留下评论

您的电子邮箱地址不会被公开。 必填项已用 * 标注