微信公众号 网页域名只能设置2个的解决方法(中转站) 记录哈

//新建一个html页面 把这个页面放在服务器上 域名 例如:http://wx.abc.com/get-weixin-code.html
<!DOCTYPE html>
<html lang="en">
 
    <head>
        <meta charset="UTF-8">
    </head>
 
    <body>
        <script>
            var GWC = {
                version: '1.1.1',
                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>
//在需要跳转的地方

```cpp
location.href = 'http://wx.abc.com/get-weixin-code.html?appid=微信公众号appId&scope=XXX&state=XXX&redirect_uri=' + encodeURIComponent(回调地址) 
公众号上需要设置:![在这里插入图片描述](https://img-blog.csdnimg.cn/69d7591df53643b8b6f80bbdf340dc05.png)


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值