解决微信公众号网页授权只能设置一个回调域名的问题

使用场景:

一个公众号(服务号),下面部署了多个项目,每个项目都部署在不一样的服务器上面,多个域名。

 

需求:

XX工作室公众号:  电商首页、微信订购两个项目部署

 

页面代码:get-weixin-code.html

 1 <!DOCTYPE html>
 2 <html lang="en">
 3 
 4     <head>
 5         <meta charset="UTF-8">
 6         <title>微信登录</title>
 7     </head>
 8 
 9     <body>
10         <script>
11             var GWC = {
12                 version: '1.1.1',
13                 urlParams: {},
14                 appendParams: function(url, params) {
15                     if (params) {
16                         var baseWithSearch = url.split('#')[0];
17                         var hash = url.split('#')[1];
18                         for (var key in params) {
19                             var attrValue = params[key];
20                             if (attrValue !== undefined) {
21                                 var newParam = key + "=" + attrValue;
22                                 if (baseWithSearch.indexOf('?') > 0) {
23                                     var oldParamReg = new RegExp('^' + key + '=[-%.!~*\'\(\)\\w]*', 'g');
24                                     if (oldParamReg.test(baseWithSearch)) {
25                                         baseWithSearch = baseWithSearch.replace(oldParamReg, newParam);
26                                     } else {
27                                         baseWithSearch += "&" + newParam;
28                                     }
29                                 } else {
30                                     baseWithSearch += "?" + newParam;
31                                 }
32                             }
33                         }
34 
35                         if (hash) {
36                             url = baseWithSearch + '#' + hash;
37                         } else {
38                             url = baseWithSearch;
39                         }
40                     }
41                     return url;
42                 },
43                 getUrlParams: function() {
44                     var pairs = location.search.substring(1).split('&');
45                     for (var i = 0; i < pairs.length; i++) {
46                         var pos = pairs[i].indexOf('=');
47                         if (pos === -1) {
48                             continue;
49                         }
50                         GWC.urlParams[pairs[i].substring(0, pos)] = decodeURIComponent(pairs[i].substring(pos + 1));
51                     }
52                 },
53                 doRedirect: function() {
54                     var code = GWC.urlParams['code'];
55                     var appId = GWC.urlParams['appid'];
56                     var scope = GWC.urlParams['scope'] || 'snsapi_base';
57                     var state = GWC.urlParams['state'];
58                     var redirectUri;
59 
60                     if (!code) {
61                         //第一步,没有拿到code,跳转至微信授权页面获取code
62                         redirectUri = GWC.appendParams('https://open.weixin.qq.com/connect/oauth2/authorize#wechat_redirect', {
63                             'appid': appId,
64                             'redirect_uri': encodeURIComponent(location.href),
65                             'response_type': 'code',
66                             'scope': scope,
67                             'state': state,
68                         });
69                     } else {
70                         //第二步,从微信授权页面跳转回来,已经获取到了code,再次跳转到实际所需页面
71                         redirectUri = GWC.appendParams(GWC.urlParams['redirect_uri'], {
72                             'code': code,
73                             'state': state
74                         });
75                     }
76 
77                     location.href = redirectUri;
78                 }
79             };
80 
81             GWC.getUrlParams();
82             GWC.doRedirect();
83         </script>
84     </body>
85 
86 </html>

 

 

 

使用方法

  1. 部署get-weixin-code.html至你的微信授权回调域名的目录下

  2. 使用方式类似于直接通过微信回调的方式,只是将回调地址改成了get-weixin-code.html所在的地址,另外省去了response_type参数(因为它只能为code)以及#wechat_redirect(它是固定的),它们会在get-weixin-code.html里面自己加上

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

详细示例

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

  2. www.abc.com域名下部署get-weixin-code.html,不一定是根目录,例如:http://www.abc.com/xxx/get-weixin-code.html

  3. 假设你的http://www.xyz.com/hello-world.html这个页面需要获取微信授权,那么你应该使用以下地址来获取授权:http://www.abc.com/xxx/get-weixin-code.html?appid=XXXX&scope=snsapi_base&state=hello-world&redirect_uri=http%3A%2F%2Fwww.xyz.com%2Fhello-world.html

  4. 这样最终就会跳转到这样一个地址:http://www.xyz.com/hello-world.html?code=XXXXXXXXXXXXXXXXX&state=hello-world,从而你就拿到了授权code以及自定义的state参数了

 

 

eg:

公众号回调地址:wxcsh.edsl.com.cn

项目地址:wxcs78.edsl.com.cn

通过loginByWeixin.do接口可以取到对应公众号的code,从而获取openId

http://wxcsh.edsl.com.cn/get-weixin-code.html?appid=wxa63f33a8579fdf36&scope=snsapi_base&state=hello-world&redirect_uri=http://wxcs78.edsl.com.cn/user/loginByWeixin.do

 

posted @ 2017-07-21 10:55  ChiRain  阅读(6564)  评论(0编辑  收藏  举报