解决微信OAuth2.0网页授权回调域名只能设置一个的问题

372 查看

1、部署get-weixin-code.html至你的微信授权回调域名的目录下
例如http://wx.abc.com/get-weixin-...

2、在其他页面的使用方式如下,类似于直接通过微信回调的方式,只是将请求地址改成了get-weixin-code.html的地址,另外省 去了response_type参数(因为它只能为code)以及#wechat_redirect的hash
它们会在get-weixin-code.html里面去加上location.href = 'http://wx.abc.com/get-weixin-...' + encodeURIComponent(location.href);

3、get-weixin-code.html页面从微信那里拿到code之后会重新跳转回调用的页面,并且在url后面带上code

附上在CI框架中实现代码示例:

public function GetOpenid()
{
    if (!isset($_GET['code']))
    {
        //触发微信返回code码
     
        $redirect_uri = urlencode(site_url('wap/login/GetOpenid'));
     
        $url = site_url('wap/common/get_weixin_code').'?appid='.APPID.'&scope=snsapi_userinfo&state=STATE&redirect_uri='.$redirect_uri;
     
        //请求公共的地址
     
        redirect($url);
     
        exit();
     
    } else {
     
        //获取code码,以获取openid
     
        $code = $_GET['code'];
     
        $url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=".APPID."&secret=".APPSECRET."&code={$code}&grant_type=authorization_code";
     
        $result = weixinCurl($url); //curl请求微信获取access_token接口
     
        print_r($result);
     
    }
}

公共统一代码如下:get_weixin_code.php

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>微信登陆</title>
</head>
<body>
<script>
    function getUrlParams(key) {
        var args = {};
        var pairs = location.search.substring(1).split('&');
        for (var i = 0; i < pairs.length; i++) {
            var pos = pairs[i].indexOf('=');
            if (pos === -1) {
                continue;
            }
            args[pairs[i].substring(0, pos)] = decodeURIComponent(pairs[i].substring(pos + 1));
        }
        return args[key];
    }
    function appendParams(params, url) {
        var baseWithSearch = url.split('#')[0];
        var hash = url.split('#')[1];
        for (var i = 0; i < params.length; i++) {
            if (params[i].value !== undefined) {
                var newParam = params[i].key + "=" + params[i].value;
                if (baseWithSearch.indexOf('?') > 0) {
                    var oldParamReg = new RegExp(params[i].key + '=[-\\w]{0,40}', '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;
    }
    var code = getUrlParams('code');
    var appId = getUrlParams('appid');
    var scope = getUrlParams('scope') || 'snsapi_base';
    var state = getUrlParams('state');
    var redirectUrl;
    if (!code) {
        redirectUrl = appendParams([{
            key: 'appid',
            value: appId
    }, {
            key: 'redirect_uri',
            value: encodeURIComponent(location.href)
        }, {
            key: 'response_type',
            value: 'code'
        }, {
            key: 'scope',
            value: scope
        }, {
            key: 'state',
            value: state
        }], 'https://open.weixin.qq.com/connect/oauth2/authorize#wechat_redirect');
            } else {
        redirectUrl = appendParams([{key: 'code', value: code},{
            key: 'state',
            value: state
        }], getUrlParams('redirect_uri'));
    }
    location.href = redirectUrl;
</script>
</body>
</html>