阿里云9.9元云服务器限时购买

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

项目地址:https://github.com/HADB/GetWeixinCode

说明:微信项目很多,但是回调域名有限,经常使用,做个笔记。

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

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                         if (hash) {
35                             url = baseWithSearch + '#' + hash;
36                         } else {
37                             url = baseWithSearch;
38                         }
39                     }
40                     return url;
41                 },
42                 getUrlParams: function() {
43                     var pairs = location.search.substring(1).split('&');
44                     for (var i = 0; i < pairs.length; i++) {
45                         var pos = pairs[i].indexOf('=');
46                         if (pos === -1) {
47                             continue;
48                         }
49                         GWC.urlParams[pairs[i].substring(0, pos)] = decodeURIComponent(pairs[i].substring(pos + 1));
50                     }
51                 },
52                 doRedirect: function() {
53                     var code = GWC.urlParams['code'];
54                     var appId = GWC.urlParams['appid'];
55                     var scope = GWC.urlParams['scope'] || 'snsapi_base';
56                     var state = GWC.urlParams['state'];
57                     var device = GWC.urlParams['device'];
58                 
59                     var authUrl;
60                     
61                     var redirectUri;
62                     if (!code) {
63                         
64                         if (device == 'pc') {
65                             authUrl = 'https://open.weixin.qq.com/connect/qrconnect';//(电脑扫码登录)微信开放平台申请https://open.weixin.qq.com/
66                         } else {
67                             authUrl = 'https://open.weixin.qq.com/connect/oauth2/authorize#wechat_redirect';//(手机微信浏览器登录)微信公众号https://mp.weixin.qq.com/
68                         }
69                         //第一步,没有拿到code,跳转至微信授权页面获取code
70                         redirectUri = GWC.appendParams(authUrl, {
71                             'appid': appId,
72                             'redirect_uri': encodeURIComponent(location.href),
73                             'response_type': 'code',
74                             'scope': scope,
75                             'state': state,
76                             
77                         });
78                     } else {
79                         //第二步,从微信授权页面跳转回来,已经获取到了code,再次跳转到实际所需页面
80                         redirectUri = GWC.appendParams(GWC.urlParams['redirect_uri'], {
81                             'code': code,
82                             'state': state
83                         });
84                     }
85                     location.href = redirectUri;
86                 }
87             };
88             GWC.getUrlParams();
89             GWC.doRedirect();
90         </script>
91     </body>
92 
93 </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这个页面需要获取微信授权,那么你应该使用以下地址来获取授权:(device = pc 是电脑端扫码登录用到的,微信开放平台接口)

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

其他说明

  • 通过多一次的跳转,解决了微信限制回调域名只能设置一个的问题

  • 牺牲了一点用户体验,换来了项目部署的美感,不需要将各种项目都部署到一个域名下

  • 很多朋友问我怎么支持第三方微信平台,这个需要对不同的第三方平台的授权方式有所了解,熟悉他们的授权方式,请求参数等。如果他们是通过在网站入口处的URL上进行授权的,那么可以使用本项目,将入口的URL改成上述的方式,如果他们是在流程中的某些页面去获取授权,那么是没法改变他们的获取地址的,所以本项目就不适用了

posted @ 2018-03-22 09:45  HIIT  阅读(892)  评论(0编辑  收藏  举报
阿里云限时红包 最高 ¥ 2000 云产品通用红包,可叠加官网常规优惠使用