知識(shí)
不管是網(wǎng)站,軟件還是小程序,都要直接或間接能為您產(chǎn)生價(jià)值,我們?cè)谧非笃湟曈X(jué)表現(xiàn)的同時(shí),更側(cè)重于功能的便捷,營(yíng)銷的便利,運(yùn)營(yíng)的高效,讓網(wǎng)站成為營(yíng)銷工具,讓軟件能切實(shí)提升企業(yè)內(nèi)部管理水平和效率。優(yōu)秀的程序?yàn)楹笃谏?jí)提供便捷的支持!
您當(dāng)前位置>首頁(yè) » 新聞資訊 » 公眾號(hào)相關(guān) >
公眾號(hào)H5授權(quán)登錄原理
發(fā)表時(shí)間:2020-10-27
發(fā)布人:葵宇科技
瀏覽次數(shù):49
公眾號(hào)H5授權(quán)登錄
在微信公眾號(hào)里獲取微信用戶信息分兩種方式
注意:以下所有操作,都需要再公眾號(hào)配置網(wǎng)頁(yè)授權(quán)配置域名授權(quán)
1.以snsapi_base為scope發(fā)起的網(wǎng)頁(yè)授權(quán)
這種方式是靜默獲取,對(duì)用戶來(lái)說(shuō)是無(wú)感的,看不到任何變化
使用場(chǎng)景,只能獲取到openid,調(diào)用微信支付使用,無(wú)法獲取頭像昵稱等數(shù)據(jù)
$wxurl = https://open.weixin.qq.com/connect/oauth2/authorize?appid=你的APPID&redirect_uri={$url}&response_type=code&scope=snsapi_base&state=STATE&connect_redirect=1#wechat_redirect
注意參數(shù)說(shuō)明:
appid=你的APPID
{$url}回調(diào)的url地址,url需要用urlencode編碼
scope=snsapi_base 靜默授權(quán)類型
使用說(shuō)明
$wxurl 這個(gè)鏈接地址是固定的拼接方式,微信官網(wǎng)有文檔點(diǎn)擊我獲取
把拿到的這個(gè)連接給前端,前端通過(guò)讓用戶觸發(fā)這個(gè)鏈接,跳轉(zhuǎn)獲取微信的code參數(shù),或者自己通過(guò)代碼觸發(fā)跳轉(zhuǎn)
微信跳轉(zhuǎn)后得到的鏈接 https://domain.com/index/index/wxlogin?code=051pYGkl2bQ3S54TUxll2ttJ6Z2pYGk0&state=STATE
后面多了code參數(shù)和state參數(shù),如果跳轉(zhuǎn)的是后端,后端直接獲取code就可以,如果跳轉(zhuǎn)的是前端, 前端截取url,拿到code傳給后端
$wxurl必須在微信里打開(kāi)才能獲取到code,開(kāi)發(fā)調(diào)試請(qǐng)使用微信開(kāi)發(fā)者工具,獲取結(jié)果如下
{"access_token":"38_gUjz8OA3RUbAiGSEB-o68bSAyI8rGVwOXmNepBHXKdcCEiNotFb3e9CTzFTxsFt21PxwXT3l9qUsL3gNBcsg8A","expires_in":7200,"refresh_token":"38_7aZk0NQHhLOO5e3j-uqVl5na48CMvuoedJ90c3lDmby8mOEsBHcsfT4SPtQJTMpMdx8GKFFR7sXFj6Nuu8h21A","openid":"obBHk04gFkeKsRVDowMfXn-LLYpU","scope":"snsapi_base"}
實(shí)現(xiàn)截圖
實(shí)現(xiàn)代碼
//后臺(tái)代碼 我用的是tp5
public function wxlogin()
{
$appid = '';
$appsecret = '';
$input = input();
if(!empty($input['code'])){
$url = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid='.$appid.'&secret='.$appsecret.'&code='.$input['code'].'&grant_type=authorization_code';
$client = new Client(); //GuzzleHttp\Client 模擬http 請(qǐng)求,如果不會(huì)用 直接換成curl函數(shù)
$result = $client->get($url)->getBody()->getContents();
echo $result;
//die();
$api_data = json_decode($result, true);
//$api_data 里就是我們要獲取的openid
}
$url = urlencode('https://d-fangfei.bigchun.com/index/index/wxlogin');
$this->assign('url', $url);
return $this->view->fetch();
}
前端代碼
<p>
<a href="https://open.weixin.qq.com/connect/oauth2/authorize?appid=你的APPID&redirect_uri={$url}&response_type=code&scope=snsapi_base&state=STATE&connect_redirect=1#wechat_redirect">
獲取靜默授權(quán)獲取code
</a>
</p>
2.以snsapi_userinfo為scope發(fā)起的網(wǎng)頁(yè)授權(quán)
這種方式是授權(quán)獲取,會(huì)彈出讓用戶確認(rèn)獲取資料按鈕
使用場(chǎng)景,可以獲取用戶openid,頭像,昵稱,性別等資料
$wxurl = https://open.weixin.qq.com/connect/oauth2/authorize?appid=APPID&redirect_uri={$url}&response_type=code&scope=snsapi_userinfo&state=STATE&connect_redirect=1#wechat_redirect
注意參數(shù)說(shuō)明:
appid=你的APPID
{$url}回調(diào)的url地址,url需要用urlencode編碼
scope = snsapi_userinfo
靜默授權(quán)類型
使用說(shuō)明
$wxurl 這個(gè)鏈接地址是固定的拼接方式,微信官網(wǎng)有文檔點(diǎn)擊我獲取
把拿到的這個(gè)連接給前端,前端通過(guò)讓用戶觸發(fā)這個(gè)鏈接,跳轉(zhuǎn)獲取微信的code參數(shù),或者自己通過(guò)代碼觸發(fā)跳轉(zhuǎn)
微信跳轉(zhuǎn)后得到的鏈接 https://domain.com/index/index/wxlogin?code=051pYGkl2bQ3S54TUxll2ttJ6Z2pYGk0&state=STATE
后面多了code參數(shù)和state參數(shù),如果跳轉(zhuǎn)的是后端,后端直接獲取code就可以,如果跳轉(zhuǎn)的是前端, 前端截取url,拿到code傳給后端
實(shí)現(xiàn)過(guò)程截圖
$wxurl必須在微信里打開(kāi)才能獲取到code,開(kāi)發(fā)調(diào)試請(qǐng)使用微信開(kāi)發(fā)者工具,獲取結(jié)果如下:
{"openid":"obBHk04gFkeKsRVDowMfXn-LLYpU","nickname":"D永濤","sex":1,"language":"zh_CN","city":"深圳","province":"廣東","country":"中國(guó)","headimgurl":"https:\/\/thirdwx.qlogo.cn\/mmopen\/vi_32\/DYAIOgq83eremdfYmic4MpxiadSdloCicKKNOdMZCIIQdwmiaPluD8NDVxXN5axqc9kmVGxVoFo88UkEy9GPKpicQUw\/132","privilege":[],"unionid":"oKzvn1ZU_Ne_4or8VLwhtZxlJOJU"}
實(shí)現(xiàn)截圖
1.觸發(fā)截圖
2.拼接上code后得到的結(jié)果
實(shí)現(xiàn)代碼
//后臺(tái)實(shí)現(xiàn)代碼
public function wxlogin()
{
$appid = '';
$appsecret = '';
$input = input();
if(!empty($input['code'])){
$url = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid='.$appid.'&secret='.$appsecret.'&code='.$input['code'].'&grant_type=authorization_code';//微信接口地址,參考接口文檔
$client = new Client(); //GuzzleHttp\Client 模擬http 請(qǐng)求,如果不會(huì)用 直接換成curl函數(shù)
$result = $client->get($url)->getBody()->getContents();
$api_data = json_decode($result, true);
if(!empty($api_data['openid'])){
$url = 'https://api.weixin.qq.com/sns/userinfo?access_token='.$api_data['access_token'].'&openid='.$api_data['openid'].'&lang=zh_CN';//微信接口地址,參考接口文檔
$client = new Client();
$result = $client->get($url)->getBody()->getContents();
$userinfo = json_decode($result, true);
print_r($userinfo);
}
}
$url = urlencode('https://d-fangfei.bigchun.com/index/index/wxlogin');
$this->assign('url', $url);
return $this->view->fetch();
}
前端實(shí)現(xiàn)代碼
<p>
<a href="https://open.weixin.qq.com/connect/oauth2/authorize?appid=你的APPID&redirect_uri={$url}&response_type=code&scope=snsapi_userinfo&state=STATE&connect_redirect=1#wechat_redirect">
微信授權(quán)登錄
</a>
</p>
有問(wèn)題可以加裙問(wèn)我,我是群主721200119
相關(guān)案例查看更多
相關(guān)閱讀
- 小程序技術(shù)
- 云南軟件定制
- web前端
- 生成海報(bào)
- 汽車回收系統(tǒng)
- 企業(yè)網(wǎng)站
- 小程序生成海報(bào)
- 云南網(wǎng)站建設(shè)方法
- 網(wǎng)絡(luò)公司
- 昆明網(wǎng)站建設(shè)公司
- 昆明網(wǎng)站制作
- 云南網(wǎng)站建設(shè)方案 doc
- 云南網(wǎng)絡(luò)營(yíng)銷
- 云南做軟件
- 微信小程序
- 政府網(wǎng)站建設(shè)服務(wù)
- 云南省住房建設(shè)廳網(wǎng)站
- 電商網(wǎng)站建設(shè)
- 網(wǎng)站維護(hù)
- 昆明小程序開(kāi)發(fā)聯(lián)系方式
- 云南網(wǎng)站建設(shè)百度
- typescript
- 云南etc小程序
- 云南省建設(shè)廳網(wǎng)站
- 報(bào)廢車拆解系統(tǒng)
- 網(wǎng)站建設(shè)開(kāi)發(fā)
- 百度人工排名
- 云南網(wǎng)站建設(shè)開(kāi)發(fā)
- 表單
- 南通小程序制作公司