【微信开发】网页授权多域名解决方案

在做微信开发的时候,会遇到这样的场景:一个公众号,会有多个业务:官网、论坛、商城等等

网页授权是只能一个域名,那么问题来了?这怎么搞?

 

答案就是: 做一个中转服务!

域名1: www.test.com

域名2: bbs.test.com

 

这时候,再解析一个二级域名:code.test.com 作为中转授权域名

并在微信公众平台后台网页授权域名地方填写这个 中转域名

 

www.test.com   授权代码改为:

header("location:http://code.test.com/code.php?ask_type=www");

bbs.test.com  授权代码改为:

header("location:http://code.test.com/code.php?ask_type=bbs");

 

code.test.com 域名新建三个文件

【微信开发】网页授权多域名解决方案
  • code_php : 发起授权文件
  • www.php: 返回接收code并跳转www.test.com文件
  • bbs.php : 返回接收code并跳转 bbs.test.com文件

code_php 文件代码:

if(isset($_GET[‘ask_type‘]) && !empty($_GET[‘ask_type‘])){ $ask_type = $_GET[‘ask_type‘]; if($ask_type == ‘bbs‘){ $action = "bbs.php"; }elseif($ask_type == ‘www‘){ $action = "www.php"; } //发起授权 $appid = ""; $redirect_url = "http://code.test.com/".$action; $code_url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=".$appid."&redirect_uri=".urlencode($redirect_url)."&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect"; header("location: ".$code_url); die;}else{ echo "nononono";}

 

bbs.php文件代码:

if(isset($_GET[‘code‘]) && !empty($_GET[‘code‘])){ $code = $_GET[‘code‘]; $bbs = "http://bbs.test.com/"; header("location:".$bbs."?code=".$code);}else{ echo ‘nonono‘;}

 

www.php 文件代码:

 

if(isset($_GET[‘code‘]) && !empty($_GET[‘code‘])){ $code = $_GET[‘code‘]; $bbs = "http://www.test.com/"; header("location:".$bbs."?code=".$code);}else{ echo ‘nonono‘;}

 

目前是两个业务域名。如果还有其他业务,可以按照上述例子新增!

 

相关文章