問題

在實際的需求中,我們常常遇到多個部署在不同域名下的業(yè)務(wù),想使用同一個「微信服務(wù)號」進(jìn)行微信網(wǎng)頁授權(quán)。但是微信的網(wǎng)頁授權(quán)只能設(shè)置兩個個回調(diào)地址" />

国产成人精品无码青草_亚洲国产美女精品久久久久∴_欧美人与鲁交大毛片免费_国产果冻豆传媒麻婆精东

15158846557 在線咨詢 在線咨詢
15158846557 在線咨詢
所在位置: 首頁 > 營銷資訊 > 網(wǎng)站運(yùn)營 > 解決微信網(wǎng)頁授權(quán)域名只能授權(quán)兩個

解決微信網(wǎng)頁授權(quán)域名只能授權(quán)兩個

時間:2022-08-06 19:42:01 | 來源:網(wǎng)站運(yùn)營

時間:2022-08-06 19:42:01 來源:網(wǎng)站運(yùn)營

轉(zhuǎn)載請注明:藏羚骸的博客~解決微信網(wǎng)頁授權(quán)域名只能授權(quán)兩個.

問題

在實際的需求中,我們常常遇到多個部署在不同域名下的業(yè)務(wù),想使用同一個「微信服務(wù)號」進(jìn)行微信網(wǎng)頁授權(quán)。但是微信的網(wǎng)頁授權(quán)只能設(shè)置兩個個回調(diào)地址。為了滿足業(yè)務(wù)需求,有人想到給每個業(yè)務(wù)部門配置一個微信服務(wù)號,但是這樣的成本太高昂。每個服務(wù)號需要定期繳納年檢費(fèi)用,而且每一個網(wǎng)頁授權(quán)都需要配置相應(yīng)后端。

那么,如何解決多個域名使用同一個微信服務(wù)號進(jìn)行網(wǎng)頁授權(quán)呢?

解決辦法

制作微信回調(diào)中轉(zhuǎn)器,通過授權(quán)給中轉(zhuǎn)器實現(xiàn),多域名分發(fā)跳轉(zhuǎn)

中轉(zhuǎn)域名 http://transfer.example.com/ 下的php

<?phpnamespace app/index/controller;use app/BaseController;use think/facade/View;use think/facade/Db;use think/facade/Cache;class Wechat extends BaseController{ public $arrayurl=['http://aa.example.com/rep','http://bb.example.com/index/wechat/rep']; public $arraytype=['infoh5']; public $APPID = "xxx"; public $SECRET = "xxx"; //授權(quán)登錄 public function actionScopeRedirectUriAppid(){ $tourl=input('tourl'); if(empty($tourl)||!in_array($tourl,$this->arrayurl)){ exit('非法路徑'); } $type=input('type'); if(empty($type)||!in_array($type,$this->arraytype)){ exit('非法操作'); } $REDIRECT_URI= 'http://'.$_SERVER['HTTP_HOST'].'/index/wechat/actionWeixinOpenidCallback?tourl='.$tourl.'&&type='.$type; $scope='snsapi_userinfo'; //手動授權(quán)snsapi_userinfo 靜默授權(quán)snsapi_base $url='https://open.weixin.qq.com/connect/oauth2/authorize?appid='.$this->APPID.'&redirect_uri='.urlencode($REDIRECT_URI).'&response_type=code&scope='.$scope.'&state=scene#wechat_redirect'; header("Location:{$url}"); die(); } ##微信回調(diào),獲取openid public function actionWeixinOpenidCallback(){ $tourl=input('tourl'); if(empty($tourl)||!in_array($tourl,$this->arrayurl)){ exit('非法路徑'); } $type=input('type'); if(empty($type)||!in_array($type,$this->arraytype)){ exit('非法操作'); } if($code= input('code')){ //h5的access_token $url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid={$this->APPID}&secret={$this->SECRET}&code={$code}&grant_type=authorization_code"; $UserOpenidArr = json_decode($this->dogetCurl($url),1); if(empty($UserOpenidArr ['openid']) ) { print_r( $UserOpenidArr ); die("授權(quán)失敗"); } $openid=$UserOpenidArr ['openid']; session('openid',$openid); /think/facade/Session::save(); session('access_token',$UserOpenidArr['access_token']); /think/facade/Session::save(); $openid=session('openid'); $access_token=session('access_token'); $token=$this -> getAccessToken(); if($type=="infoh5"){ //跳轉(zhuǎn)h5獲取用戶信息 header("Location:http://transfer.example.com/index/wechat/actionWeixinOpenidGetUserInfo?openid={$openid}&&access_token={$access_token}&&tourl={$tourl}" ); } die(); }else{ die("微信授權(quán)失敗"); } } //h5獲取用戶信息 public function actionWeixinOpenidGetUserInfo(){ $tourl=input('tourl'); if(empty($tourl)||!in_array($tourl,$this->arrayurl)){ exit('非法路徑'); } $openid= input('openid'); $access_token=input('access_token'); $res=$this->getapktj("https://api.weixin.qq.com/sns/userinfo?access_token={$access_token}&openid={$openid}&lang=zh_CN"); $res['nickname']=preg_replace('/[/x{10000}-/x{10FFFF}]/u', '', $res['nickname']); $res['access_token']=$access_token; $res['token']=$this ->getAccessToken(); $res=http_build_query($res); header("Location:{$tourl}?$res"); die(); } //跨服務(wù)器高級用法 public function getapktj($url){ $method ="GET"; $curl = curl_init(); curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method); curl_setopt($curl, CURLOPT_URL, $url); curl_setopt($curl, CURLOPT_FAILONERROR, false); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); curl_setopt($curl, CURLOPT_HEADER, false); $ret = curl_exec($curl); $all=json_decode($ret,true); return $all; //返回數(shù)組 } public function getAccessToken(){ if(!Cache::get("token")){ //公眾號的access_token $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={$this->APPID}&secret={$this->SECRET}"; $UserOpenidArrcgi = json_decode($this->dogetCurl($url),1); $token=$UserOpenidArrcgi['access_token']; Cache::set('token',$token,4800); } $accessToken = Cache::get("token"); return $accessToken; } }回調(diào)目標(biāo)域名http://aa.example.com 下的php下的方法

public function checklogin(){if(!session('openid')||!session('access_token')||!Cache::get("token")){ header("Location:http://rep.zlhdsg.com/index/wechat/actionScopeRedirectUriAppid?tourl=http://sg.zlhdsg.com/index/wechat/rep&&type=infoh5"); die(); }}public function rep(){ $res=input("get."); $id=Db::table("users")->where("openid",$res['openid'])->value("id"); if($id){ $res1=Db::table("users")->where('id',$id)->update(['nickname'=>$res['nickname'],'headimgurl'=>$res['headimgurl'],'updated_at'=>date("Y-m-d H:i:s")]); }else{ $id=$res1=Db::table("users")->insertGetId(['openid'=>$res['openid'],'nickname'=>$res['nickname'],'headimgurl'=>$res['headimgurl'],'created_at'=>date("Y-m-d H:i:s"),'updated_at'=>date("Y-m-d H:i:s")]); } session('openid',$res['openid']); /think/facade/Session::save(); session('access_token',$res['access_token']); /think/facade/Session::save(); Cache::set('token',$res['token'],4800); if($res1){ $this->redirect(url("index/index")); }else{ echo "error"; } }轉(zhuǎn)載請注明:藏羚骸的博客~解決微信網(wǎng)頁授權(quán)域名只能授權(quán)兩個.

關(guān)鍵詞:授權(quán),解決

74
73
25
news

版權(quán)所有? 億企邦 1997-2025 保留一切法律許可權(quán)利。

為了最佳展示效果,本站不支持IE9及以下版本的瀏覽器,建議您使用谷歌Chrome瀏覽器。 點擊下載Chrome瀏覽器
關(guān)閉