PbootCMS對(duì)接微信開(kāi)放平臺(tái)掃碼登錄
<?php
/**
* 微信公眾號(hào)配置
* ============================================================================
* 版權(quán)所有 云管家網(wǎng)絡(luò)科技有限公司
* 網(wǎng)站地址: https://www.kaifacn.com
* ============================================================================
*/
namespace coreasic;
use coreasicConfig;
class Wechat
{
protected $appid;
protected $secret;
public function __construct()
{
// 配置參數(shù)
$this->appid = Config::get('weixin_appid');
$this->secret = Config::get('weixin_secret');
}
// 獲取登錄二維碼
public function getqrcode()
{
// 回調(diào)地址
$redirect_uri= get_http_url().Url::home('Member/login');
// 該回調(diào)需要url編碼
$redirect_uri=urlencode($redirect_uri);
$url = "https://open.weixin.qq.com/connect/qrconnect?appid=".$this->appid."&redirect_uri=".$redirect_uri."&response_type=code&scope=snsapi_login&state=".session('access_token')."#wechat_redirect";
$result = get_url($url);
// 修改二維碼路徑
$result = str_replace("/connect/qrcode/", "https://open.weixin.qq.com/connect/qrcode/", $result);
// 簡(jiǎn)單優(yōu)化一下頁(yè)面樣式
$result = str_replace('<body>','<style>.impowerBox .qrcode{width: 220px;margin-top:0px;}.impowerBox .icon38_msg {width: 30px;height: 30px;margin-left: 27px;}</style><body>',$result);
$result = str_replace('<div>微信登錄</div>','',$result);
$result = str_replace('<p>“CMS開(kāi)發(fā)網(wǎng)”</p>','',$result);
$result = str_replace('<h4>掃描成功</h4>','<h4>掃描成功,請(qǐng)點(diǎn)擊授權(quán)!</h4>',$result);
$result = str_replace('<p>在微信中輕觸允許即可登錄</p>','',$result);
$result = str_replace('<p>你可再次掃描登錄,或關(guān)閉窗口</p>','',$result);
$result = str_replace('<p>使用微信掃一掃登錄</p>','<div><i></i>使用微信掃一掃登錄</div>',$result);
echo $result;
}
// 獲取登錄授權(quán)信息
public function getWxUser()
{
$code = get('code');
$state = get('state');
if ($code && $state && session('access_token') == $state)
{
$url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=".$this->appid."&secret=".$this->secret."&code=".$code."&grant_type=authorization_code";
$jsonResult = file_get_contents($url);
$result = json_decode($jsonResult, true);
$data = $this->getAuthUserInfo($result['access_token'],$result['openid']);
return $data;
}
location(Url::home('member/login'));
}
// 獲取用戶信息
private function getAuthUserInfo($token, $openid)
{
$url = "https://api.weixin.qq.com/sns/userinfo?access_token=".$token."&openid=".$openid."&lang=zh_CN";
$result = json_decode(get_url($url));
if (isset($result->errcode) && $result->errcode)
{
location(Url::home('member/login'));
}
return $result;
}
}
放在/core/basic里面,需自行處理登錄業(yè)務(wù)邏輯,本教程使用微信開(kāi)放平臺(tái)配置
版權(quán)聲明:
本站所有文章和圖片均來(lái)自用戶分享和網(wǎng)絡(luò)收集,文章和圖片版權(quán)歸原作者及原出處所有,僅供學(xué)習(xí)與參考,請(qǐng)勿用于商業(yè)用途,如果損害了您的權(quán)利,請(qǐng)聯(lián)系網(wǎng)站客服處理。