问题描述
- 你是否遇到过这各情况,在H5开发时,要用到微信授权,可同一个服务号只能填写两个域名,这样每开发一个应用就要换域名。
- 用vue开发时,因用到微信授权,每次都要打包上传到服务器上,才可以调试
解决问题思路
- 遇到这样的问题太棘手了。有没有这样一种方法,在微信服务器各我前端请求中间再加上一个中转服务器,我要微信制授权,就都去找中转服务器,再由中转服务器统一去微信要授权,得到授权后,转发到请求前端
- 之前的流程
- 加上中转服务器
- 按照上边的思路
- 首先我要搭建中转服务器
- 在中转服务器上处理前端和微信发来的数据请求
解决方法
oauth.php
<?php
//微信授权带code回调
$url = $_GET['redirect_uri'];
unset($_GET['redirect_uri']);
if( strpos($url,'?') === false ){
$url .= '?' . http_build_query($_GET);
}else{
$url .= '&' . http_build_query($_GET);
}
//跳转
//header("Location:".$url);
//var_dump($url);
echo "
<script>
location.href='{$url}';
</script>
";
- 在你的服务器上配置一个站点,内容就放oauth.php这个文件
比如我的站点是oauth.abcde.cn - 在微信授权接口那填写的地址是:http://oauth.abcde.cn/oauth.php
同时要把微信验证文件放到站点中 - 要授权页面代码(js)
function get_oauth(){
var AppID = 'your AppID ';
var redirect_uri = location.origin + location.pathname + location.hash
//加上中间件的回调
redirect_uri = "http://oauth.abcde.cn/oauth.php?redirect_uri=" + redirect_uri;
//编码
redirect_uri = encodeURIComponent(redirect_uri);
location.href="https://open.weixin.qq.com/connect/oauth2/authorize?appid="+AppID+"&redirect_uri="+redirect_uri+"&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect";
}
以上就是一个公众号多个网页授权方法