小云微站微信回调解决方案

小云微站微信回调解决方案,所需的文件代码,在本文尾部并将get-weixin-code.html上传到你授权域名根目录

然后修改服务器配置文件,添加proxy_redirect一行

参考代码

upstream weizhan {
server 127.0.0.1:3006;
}
server {
listen 80;
server_name app.yunxunwl.com;//你的微站域名
location /m/dist/{
proxy_pass http://weizhan/dist/;
expires 365d;
}
location /m/ {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_pass http://weizhan/;
# proxy_redirect off;
proxy_redirect
https://open.weixin.qq.com/connect/oauth2/authorize
https://www.yunxunwl.com/get-weixin-code.html
;
proxy_http_version 1.1;
proxy_set_header Connection "";
client_max_body_size 200m;
client_body_buffer_size 1024k;
proxy_connect_timeout 172800;
proxy_read_timeout 5m;
proxy_send_timeout 172800;
proxy_buffer_size 64k;
proxy_buffers 32 64k;
proxy_busy_buffers_size 128k;
proxy_temp_file_write_size 128k;
}
location / {
rewrite / http://$host/m/ redirect;
}}

proxy_redirect这里的 https://www.yunxunwl.com/get-weixin-code.html

改成你的域名/get-weixin-code.html即可

然后重启nginx,便解决了微信授权域名只有一个的问题

以下是get-weixin-code.html文件代码:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>微信登录</title>
</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 redirectUri;

if (!code) {
//第一步,没有拿到code,跳转至微信授权页面获取code
redirectUri = GWC.appendParams('https://open.weixin.qq.com/connect/oauth2/authorize#wechat_redirect', {
'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>

转载请注明出处:https://www.onexin.net/xiaoyun-callback/

随机文章:

1、用yum安装Memcache到Centos 6.3 更新版本32/64位上
https://www.onexin.net/yum-install-memcache-to-centos-6-3-3264/

2、SPL迭代器接口(二)—Iterator Interface
https://www.onexin.net/spl-iterator-interface/

3、Regex正则表达式参数详解
https://www.onexin.net/regex-rules-match/

4、[个人潜能3B]专注的力量
https://www.onexin.net/personal-potentials-3b-focus-on-the-strength-of/

5、Memcache安装配置优化篇
https://www.onexin.net/memcache-installation-and-configuration-optimization-articles/

Leave a Reply