在做微信開發的時候,會遇到這樣的場景:一個公眾號,會有多個業務:官網、論壇、商城等等
網頁授權是只能一個域名,那么問題來了?這怎么搞?
答案就是: 做一個中轉服務!
域名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'; }
目前是兩個業務域名。如果還有其他業務,可以按照上述例子新增!