File size: 6,159 Bytes
9eef12b
9dc5991
 
 
 
 
 
 
 
73b3231
9dc5991
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
73b3231
9dc5991
 
 
 
73b3231
9dc5991
 
 
 
 
 
 
 
 
 
 
 
 
73b3231
9dc5991
 
 
 
 
73b3231
9dc5991
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9eef12b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <title></title>
    </head>

    <body>
        <script>
            //alert("href: "+location.href);
            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
                       // alert(appId);
                        //alert("href: "+location.href);
                        var url = document.location.toString();
                        var arrUrl = url.split("?");
                        //let currentHost = "https://wx-1257268355.cos-website.ap-shanghai.myqcloud.com";//arrUrl[0];
                        //alert("currentHost: "+location.href);
                        redirectUri = GWC.appendParams(baseUrl, {
                            'appid': appId,
                            // 'redirect_uri': 'https://app-1257268355.cos-website.ap-shanghai.myqcloud.com',//location.href,
                           'redirect_uri': encodeURIComponent(location.href),
                           //  'redirect_uri': 'https://wx-1257268355.cos-website.ap-shanghai.myqcloud.com',
                            // 'redirect_uri': encodeURIComponent("http://wx-1257268355.cos-website.ap-shanghai.myqcloud.com/get-weixin-code.html"),
                            //'redirect_uri': encodeURIComponent(currentHost),
                            'response_type': 'code',
                            'scope': scope,
                            'state': state,
                        });
                    } else {
                        //第二步,从微信授权页面跳转回来,已经获取到了code,再次跳转到实际所需页面
                        //alert("p.83-code:"+code);
                        redirectUri = GWC.appendParams(GWC.urlParams['redirect_uri2'], {
                            'code': code,
                            'state': state
                        });
                    }
                    //alert("p.89-redirectUri:"+redirectUri);
                    console.log(redirectUri);
                    location.href = redirectUri;
                }
            };
            GWC.getUrlParams();
            GWC.doRedirect();

            // window.onload = function () {
            //     var appId = GWC.urlParams['appid'];
            //     var scope = GWC.urlParams['scope'] || 'snsapi_base';
            //     window.mplogin2.doLogin({
            //         appid: appId,                   // 必填,公众号appid,将以此 appid 名义进行请求
            //         scope: scope,                     // 必填,登录方式:snsapi_userinfo、snsapi_base// 必填,登录方式:snsapi_userinfo、snsapi_base
            //         // noback: true,                              // 选填,登录完不直接返回业务页面,执行自定义附加操作
            //         redirect: GWC.urlParams['redirect_uri2']             // 跳转的页面,默认是当前页面,如果你有自定义登录成功页,可以联动设置
            //     }).then(res => {
            //         // noback为 true 时,将不会返回业务页面,执行到这里
            //         // 此时需要自行控制跳转回业务页面
            //         console.log(res)
            //     })
            // }
        </script>
    </body>

</html>