From 67f70ae39a3bd5f88ca5e0bf40e0b5dea57c79ed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=82=B9=E6=99=AF=E7=AB=8B?= Date: Fri, 14 Apr 2017 15:02:51 +0800 Subject: [PATCH] =?UTF-8?q?[=E6=9B=B4=E6=96=B0]=E4=BF=AE=E6=94=B9=E5=BE=AE?= =?UTF-8?q?=E4=BF=A1=E6=94=AF=E6=8C=81=E6=A8=A1=E5=9D=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- application/common.php | 4 +- extend/controller/BasicAdmin.php | 34 ++++-------- extend/controller/BasicWechat.php | 89 +++++++++++++++---------------- extend/service/WechatService.php | 9 ++-- 4 files changed, 61 insertions(+), 75 deletions(-) diff --git a/application/common.php b/application/common.php index 7e3ef51ea..7ae6472ee 100644 --- a/application/common.php +++ b/application/common.php @@ -33,7 +33,7 @@ function p($data, $replace = false, $pathname = NULL) { /** * 获取微信操作对象 * @param string $type - * @return \Wechat\WechatReceive|\Wechat\WechatUser|\Wechat\WechatPay + * @return \Wechat\WechatReceive|\Wechat\WechatUser|\Wechat\WechatPay|\Wechat\WechatScript|\Wechat\WechatOauth */ function & load_wechat($type = '') { static $wechat = array(); @@ -58,7 +58,7 @@ function & load_wechat($type = '') { /** * 安全URL编码 - * @param array $data + * @param array|string $data * @return string */ function encode($data) { diff --git a/extend/controller/BasicAdmin.php b/extend/controller/BasicAdmin.php index 9b629fcd2..64c6ef1dd 100644 --- a/extend/controller/BasicAdmin.php +++ b/extend/controller/BasicAdmin.php @@ -56,33 +56,19 @@ class BasicAdmin extends Controller { * 后台权限控制初始化方法 */ public function _initialize() { - # 用户登录状态检查 - if (($this->checkLogin || $this->checkAuth) && !$this->_isLogin()) { + // 用户登录状态检查 + if (($this->checkLogin || $this->checkAuth) && !session('user')) { $this->redirect('@admin/login'); } - # 节点访问权限检查 - if ($this->checkLogin && $this->checkAuth) { - if (!auth(join('/', [$this->request->module(), $this->request->controller(), $this->request->action()]))) { - $this->error('抱歉,您没有访问该模块的权限!'); - } + list($module, $controller, $action) = [$this->request->module(), $this->request->controller(), $this->request->action()]; + // 节点访问权限检查 + if ($this->checkLogin && $this->checkAuth && !auth("{$module}/{$controller}/{$action}")) { + $this->error('抱歉,您没有访问该模块的权限!'); } - # 初始化赋值常用变量 - if ($this->request->isGet()) { - $class_uri = strtolower($this->request->module() . '/' . $this->request->controller()); - $this->assign('classuri', $class_uri); - } - } - - /** - * 判断用户是否登录 - * @return bool - */ - protected function _isLogin() { - $user = session('user'); - if (empty($user) || empty($user['id'])) { - return false; - } - return true; + // 初始化赋值常用变量 + $this->assign('_url_', $this->request->url(true)); + $this->assign('_uri_', strtolower("{$module}/{$controller}/{$action}")); + $this->assign('classuri', strtolower("{$module}/{$controller}")); } /** diff --git a/extend/controller/BasicWechat.php b/extend/controller/BasicWechat.php index b619d0e7c..a153d46a1 100644 --- a/extend/controller/BasicWechat.php +++ b/extend/controller/BasicWechat.php @@ -16,6 +16,7 @@ namespace controller; use service\WechatService; use think\Controller; +use think\Log; class BasicWechat extends Controller { @@ -35,7 +36,7 @@ class BasicWechat extends Controller { * 当前访问网址 * @var string */ - protected $current; + protected $url; /** * 是否默认开启网页授权 @@ -47,78 +48,74 @@ class BasicWechat extends Controller { * 初始化方法 */ public function _initialize() { - parent::_initialize(); - $this->current = ($this->request->isSsl() ? 'https' : 'http') . '://' . $this->request->host() . $this->request->url(); - /* 网页授权,并获粉丝信息 */ - if ($this->check_auth && $this->oAuth()) { - if ($this->request->isGet()) { - $this->assign('js_sign', load_wechat('script')->getJsSign($this->current)); - $this->assign('fansinfo', $this->fansinfo); - } + // 当前完整URL地址 + $this->url = $this->request->url(true); + // 网页授权,并获粉丝信息 + if ($this->check_auth && $this->_oAuth()) { + $this->assign('jsSign', load_wechat('script')->getJsSign($this->url)); + $this->assign('fansinfo', $this->fansinfo); } } /** - * 微信网页授权函数 - * @param bool $isfull + * 微信网页授权 + * @param bool $full 获取完整 * @return string */ - protected function oAuth($isfull = true) { - $host = $this->request->host(); - # 本地开发调试用户OPENID - if (in_array($host, ['127.0.0.1', 'localhost'])) { + protected function _oAuth($full = true) { + // 本地开发调试用户 openid + if (in_array($this->request->host(), ['127.0.0.1', 'localhost'])) { session('openid', 'o38gps1Unf64JOTdxNdd424lsEmM'); } - # 检查缓存中openid信息是否完整 - if (!!($this->openid = session('openid'))) { - if (!!($this->fansinfo = FansService::get($this->openid)) || !$isfull) { + // 检查缓存中 openid 信息是否完整 + if ($this->openid = session('openid')) { + if (($this->fansinfo = WechatService::getFansInfo($this->openid)) || !$full) { return $this->openid; } } - # 发起微信网页授权 - $wxoauth_url = $this->current; - if (!($redirect_url = $this->request->get('redirecturl', false, 'decode'))) { - $params = $this->request->param(); - $params['redirecturl'] = encode($wxoauth_url); - $wxoauth_url = url($this->request->baseUrl(), '', false, true) . '?' . http_build_query($params); + // 发起微信网页授权 + $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)) { - exit(redirect($wechat->getOauthRedirect($wxoauth_url, 'webOauth', 'snsapi_base'))->send()); + $this->redirect($wechat->getOauthRedirect($wxoauth_url, 'webOauth', 'snsapi_base')); } if (FALSE === ($result = $wechat->getOauthAccessToken()) || empty($result['openid'])) { - Log::error("微信授权失败 [ {$wechat->errMsg} ]"); - exit('网页授权失败,请稍候再试!'); + Log::error("微信网页授权失败,{$wechat->errMsg}[{$wechat->errCode}]"); + exit("微信网页授权失败,{$wechat->errMsg}[{$wechat->errCode}]"); } session('openid', $this->openid = $result['openid']); + !$full && $this->redirect($redirect_url); + // 微信粉丝信息处理 $this->fansinfo = WechatService::getFansInfo($this->openid); - # 微信粉丝信息处理 if (empty($this->fansinfo['expires_in']) || $this->fansinfo['expires_in'] < time()) { - switch ($result['scope']) { - case 'snsapi_base': /* 普通授权,获取用户资料;未关注时重新使用高级授权 */ - $user = load_wechat('User')->getUserInfo($this->openid); - if ($isfull && empty($user['subscribe'])) { - exit(redirect($wechat->getOauthRedirect($wxoauth_url, 'webOauth', 'snsapi_userinfo'))->send()); - } - break; - case 'snsapi_userinfo': /* 高级授权,获取用户资料 */ - $user = $wechat->getOauthUserinfo($result['access_token'], $this->openid); - break; + /* 使用普通授权,获取用户资料;未关注时重新使用高级授权 */ + if ($result['scope'] === 'snsapi_base') { + $user = load_wechat('User')->getUserInfo($this->openid); + if ($full && 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); } - if ($isfull && (empty($user) || !array_key_exists('nickname', $user))) { - exit("微信授权失败 [{$wechat->errMsg}]!"); + if ($full && (empty($user) || !array_key_exists('nickname', $user))) { + Log::error("微信网页授权获取用户信息失败,{$wechat->errMsg}[{$wechat->errCode}]"); + exit("微信网页授权获取用户信息失败,{$wechat->errMsg}[{$wechat->errCode}]"); } /* 更新粉丝信息 */ $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) && exit('微信授权失败 [ save userinfo faild ]'); - $this->fansinfo = WechatService::getFansInfo($this->openid); + if (!WechatService::setFansInfo($user, $wechat->appid)) { + exit('微信网页授权获取用户信息保存失败'); + } } - empty($this->fansinfo) && exit('获取微信用户信息失败!'); - !!$redirect_url && exit(redirect($redirect_url)->send()); - return $this->openid; + $this->redirect($redirect_url); } } diff --git a/extend/service/WechatService.php b/extend/service/WechatService.php index a8d6f30a0..aba065934 100644 --- a/extend/service/WechatService.php +++ b/extend/service/WechatService.php @@ -138,11 +138,14 @@ class WechatService { /** * 读取粉丝信息 - * @param string $openid + * @param string $openid 微信用户openid + * @param string $appid 公众号appid * @return array|false */ - public static function getFansInfo($openid) { - return Db::name('WechatFans')->where('openid', $openid)->find(); + public static function getFansInfo($openid, $appid = null) { + $map = ['openid' => $openid]; + is_string($map['appid']) && $map['appid'] = $appid; + return Db::name('WechatFans')->where($map)->find(); } /**