- 登錄 微信開放平台
1.簡要引導
-
當應用創建通過后,必須還要滿足接口權限的獲取,會有工作人員主動聯系,一般一天就能完成
2.綁定公眾賬號/小程序
為了保證同一個開發賬號下對應微信用戶的 UnionID 綁定使用,需要在下面的列表中綁定對應的公眾號/服務號,文檔中介紹一般要滿足微信支付功能
3.授權獲取 access_token
時序圖
三、代碼實現
- 其實,主要的時間都花費在了前期的申請操作上,而真正的代碼實現卻是極為簡單,以下是我的實現方式,敬請指摘
1、公共文件配置
- 習慣主要的配置信息同意放在了配置文件中,‘\Application\Common\Conf\config.php'。
1
2
3
4
5
6
|
'WEIXIN_LOGIN'
=>
array
(
// 微信開放平台 使用微信帳號登錄App或者網站 配置信息
'OPEN_APPID'
=>
'wxbd961b2a6b7b2963'
,
//應用 AppID
'OPEN_APPSECRET'
=>
'e6xxxxxxxxxxxxxxxxxxxxe90'
,
//應用 AppSecret
),
|
2.核心代碼
- 具體代碼,請參考路徑 “zmPro\Application\Home\Controller\LoginController.class.php”
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
|
public
function
wxIndex(){
//--微信登錄-----生成唯一隨機串防CSRF攻擊
$state
= md5(uniqid(rand(), TRUE));
$_SESSION
[
"wx_state"
] =
$state
;
//存到SESSION
$callback
= urlencode(
$this
->callBackUrl);
'https://https://blog.csdn.net/u011415782/article/details/open.weixin.qq.com/connect/qrconnect?appid=APPID&redirect_uri=REDIRECT_URI&response_type=code&scope=SCOPE&state=STATE#wechat_redirect'
;
$wxurl
=
"https://https://blog.csdn.net/u011415782/article/details/open.weixin.qq.com/connect/qrconnect?appid="
.
$this
->appID.
"&redirect_uri="
.
$callback
.
"&response_type=code&scope=snsapi_login&state="
.
$state
.
"#wechat_redirect"
;
header(
"Location: $wxurl"
);
}
public
function
wxBack(){
if
(
$_GET
[
'state'
]!=
$_SESSION
[
"wx_state"
]){
echo
'sorry,網絡請求失敗...'
;
exit
(
"5001"
);
}
$url
=
'https://api.weixin.qq.com/sns/oauth2/access_token?appid='
.
$this
->appID.
'&secret='
.
$this
->appSecret.
'&code='
.
$_GET
[
'code'
].
'&grant_type=authorization_code'
;
$arr
= curl_get_contents(
$url
);
//得到 access_token 與 openid
$url
=
'https://api.weixin.qq.com/sns/userinfo?access_token='
.
$arr
[
'access_token'
].
'&openid='
.
$arr
[
'openid'
].
'&lang=zh_CN'
;
$user_info
= curl_get_contents(
$url
);
$this
->dealWithWxLogin(
$user_info
);
}
/**
* 根據微信授權用戶的信息 進行下一步的梳理
* @param $user_info
*/
public
function
dealWithWxLogin(
$user_info
){
//TODO 數據處理
var_dump(
$user_info
);
die
;
}
|
3.前端顯示
- 根據官方文檔的介紹,既可以直接訪問授權掃碼界面,也可以進行自定義設計
- 估計本人腦抽,嵌套登錄掃碼的功能整了半天也沒實現,在此只好使用默認跳轉。
- 掃描登錄成功后進行頁面跳轉