From 08ed1243e76a5a6c3d0b8ba29ff7560e3775b635 Mon Sep 17 00:00:00 2001 From: Anyon Date: Fri, 25 Aug 2017 12:26:04 +0800 Subject: [PATCH] =?UTF-8?q?[=E6=9B=B4=E6=96=B0]=E4=BF=AE=E6=AD=A3BaseWecha?= =?UTF-8?q?t=E5=AF=B9=E5=BE=AE=E4=BF=A1=E5=AF=B9=E6=8E=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- extend/controller/BasicWechat.php | 98 +++++++++++++++++++++++++------ 1 file changed, 81 insertions(+), 17 deletions(-) diff --git a/extend/controller/BasicWechat.php b/extend/controller/BasicWechat.php index 6580c9906..b04d25fa6 100644 --- a/extend/controller/BasicWechat.php +++ b/extend/controller/BasicWechat.php @@ -14,7 +14,9 @@ namespace controller; +use service\WechatService; use think\Controller; +use think\Log; /** * 微信基础控制器 @@ -36,30 +38,92 @@ class BasicWechat extends Controller */ protected $fansinfo; + /** + * 当前访问网址 + * @var string + */ + protected $url; + + /** + * 是否默认开启网页授权 + * @var bool + */ + protected $checkAuth = true; + + /** + * 初始化方法 + */ + public function _initialize() + { + // 当前完整URL地址 + $this->url = $this->request->url(true); + // 网页授权,并获粉丝信息 + $this->assign('jsSign', load_wechat('script')->getJsSign($this->url)); + // 检查启用网页授权 + $this->checkAuth && $this->oAuth(); + } + /** * 微信网页授权 - * @param bool $mode 获取完整 + * @param bool $fullMode 获取完整 * @return string */ - protected function oauth($mode = true) + protected function oAuth($fullMode = true) { - $this->openid = session('openid'); - if (!empty($this->openid) && empty($mode)) { - return $this->openid; - } elseif (!empty($this->openid) && session('fansinfo')) { - $this->fansinfo = session('fansinfo'); - return $this->openid; + // 本地开发调试用户 openid + if (in_array($this->request->host(), ['127.0.0.1', 'localhost'])) { + session('openid', 'oBWB3wWVNujb-PJlmPmxC5CBTNF0'); } - list($sessionid, $location) = [session_id(), $this->request->url(true)]; - $result = load_wechat('wechat')->oauth($sessionid, $location, intval($mode)); - !empty($result['url']) && $this->redirect($result['url']); - if (!empty($result['openid'])) { - list($this->openid, $this->fansinfo) = [$result['openid'], $result['fans']]; - session('openid', $this->openid); - session('fansinfo', $this->fansinfo); - return $this->openid; + // 检查缓存中 openid 信息是否完整 + if (($this->openid = session('openid'))) { + if (!$fullMode) { + return $this->openid; + } + $this->fansinfo = WechatService::getFansInfo($this->openid); + if (!empty($this->fansinfo) && $this->fansinfo['expires_in'] > time()) { + $this->assign('fansinfo', $this->fansinfo); + return $this->openid; + } } - $this->error('网页授权失败,请稍候再试!'); + // 发起微信网页授权 + $wxoauth_url = $this->url; + if (!($redirect_url = $this->request->get('redirectcode', false, 'decode'))) { + $split = stripos($this->url, '?') === false ? '?' : '&'; + $wxoauth_url = "{$this->url}{$split}redirectcode=" . encode($this->url); + } + // 微信网页授权处理 + $wechat = &load_wechat('Oauth'); + if (!$this->request->get('code', false)) { + $this->redirect($wechat->getOauthRedirect($wxoauth_url, 'webOauth', 'snsapi_base')); + } + if (false === ($result = $wechat->getOauthAccessToken()) || empty($result['openid'])) { + Log::error("微信网页授权失败, {$wechat->errMsg}[{$wechat->errCode}]"); + $this->error("微信网页授权失败, {$wechat->errMsg}[{$wechat->errCode}]"); + } + session('openid', $this->openid = $result['openid']); + empty($fullMode) && $this->redirect($redirect_url); + // 微信粉丝信息处理 + $this->fansinfo = WechatService::getFansInfo($this->openid); + if (empty($this->fansinfo['expires_in']) || intval($this->fansinfo['expires_in']) < time()) { + /* 使用普通授权, 获取用户资料; 未关注时重新使用高级授权 */ + if ($result['scope'] === 'snsapi_base') : + $user = load_wechat('User')->getUserInfo($this->openid); + empty($user['subscribe']) && $this->redirect($wechat->getOauthRedirect($wxoauth_url, 'webOauth', 'snsapi_userinfo')); + /* 使用高级授权, 获取完整用户资料 */ + elseif ($result['scope'] === 'snsapi_userinfo') : + $user = $wechat->getOauthUserinfo($result['access_token'], $this->openid); + endif; + /* 授权结果处理, 更新粉丝信息 */ + if ((empty($user) || !array_key_exists('nickname', $user))) : + Log::error("微信网页授权获取用户信息失败, {$wechat->errMsg}[{$wechat->errCode}]"); + $this->error("微信网页授权获取用户信息失败, {$wechat->errMsg}[{$wechat->errCode}]"); + endif; + $user['expires_in'] = $result['expires_in'] + time() - 100; + $user['refresh_token'] = $result['refresh_token']; + $user['access_token'] = $result['access_token']; + WechatService::setFansInfo($user, $wechat->appid) or $this->error('微信网页授权用户保存失败!'); + } + $this->redirect($redirect_url); } }