[更新]修改微信支持模块

This commit is contained in:
邹景立 2017-04-14 15:02:51 +08:00
parent b0eddc416c
commit 67f70ae39a
4 changed files with 61 additions and 75 deletions

View File

@ -33,7 +33,7 @@ function p($data, $replace = false, $pathname = NULL) {
/** /**
* 获取微信操作对象 * 获取微信操作对象
* @param string $type * @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 = '') { function & load_wechat($type = '') {
static $wechat = array(); static $wechat = array();
@ -58,7 +58,7 @@ function & load_wechat($type = '') {
/** /**
* 安全URL编码 * 安全URL编码
* @param array $data * @param array|string $data
* @return string * @return string
*/ */
function encode($data) { function encode($data) {

View File

@ -56,33 +56,19 @@ class BasicAdmin extends Controller {
* 后台权限控制初始化方法 * 后台权限控制初始化方法
*/ */
public function _initialize() { public function _initialize() {
# 用户登录状态检查 // 用户登录状态检查
if (($this->checkLogin || $this->checkAuth) && !$this->_isLogin()) { if (($this->checkLogin || $this->checkAuth) && !session('user')) {
$this->redirect('@admin/login'); $this->redirect('@admin/login');
} }
# 节点访问权限检查 list($module, $controller, $action) = [$this->request->module(), $this->request->controller(), $this->request->action()];
if ($this->checkLogin && $this->checkAuth) { // 节点访问权限检查
if (!auth(join('/', [$this->request->module(), $this->request->controller(), $this->request->action()]))) { if ($this->checkLogin && $this->checkAuth && !auth("{$module}/{$controller}/{$action}")) {
$this->error('抱歉,您没有访问该模块的权限!'); $this->error('抱歉,您没有访问该模块的权限!');
}
} }
# 初始化赋值常用变量 // 初始化赋值常用变量
if ($this->request->isGet()) { $this->assign('_url_', $this->request->url(true));
$class_uri = strtolower($this->request->module() . '/' . $this->request->controller()); $this->assign('_uri_', strtolower("{$module}/{$controller}/{$action}"));
$this->assign('classuri', $class_uri); $this->assign('classuri', strtolower("{$module}/{$controller}"));
}
}
/**
* 判断用户是否登录
* @return bool
*/
protected function _isLogin() {
$user = session('user');
if (empty($user) || empty($user['id'])) {
return false;
}
return true;
} }
/** /**

View File

@ -16,6 +16,7 @@ namespace controller;
use service\WechatService; use service\WechatService;
use think\Controller; use think\Controller;
use think\Log;
class BasicWechat extends Controller { class BasicWechat extends Controller {
@ -35,7 +36,7 @@ class BasicWechat extends Controller {
* 当前访问网址 * 当前访问网址
* @var string * @var string
*/ */
protected $current; protected $url;
/** /**
* 是否默认开启网页授权 * 是否默认开启网页授权
@ -47,78 +48,74 @@ class BasicWechat extends Controller {
* 初始化方法 * 初始化方法
*/ */
public function _initialize() { public function _initialize() {
parent::_initialize(); // 当前完整URL地址
$this->current = ($this->request->isSsl() ? 'https' : 'http') . '://' . $this->request->host() . $this->request->url(); $this->url = $this->request->url(true);
/* 网页授权,并获粉丝信息 */ // 网页授权,并获粉丝信息
if ($this->check_auth && $this->oAuth()) { if ($this->check_auth && $this->_oAuth()) {
if ($this->request->isGet()) { $this->assign('jsSign', load_wechat('script')->getJsSign($this->url));
$this->assign('js_sign', load_wechat('script')->getJsSign($this->current)); $this->assign('fansinfo', $this->fansinfo);
$this->assign('fansinfo', $this->fansinfo);
}
} }
} }
/** /**
* 微信网页授权函数 * 微信网页授权
* @param bool $isfull * @param bool $full 获取完整
* @return string * @return string
*/ */
protected function oAuth($isfull = true) { protected function _oAuth($full = true) {
$host = $this->request->host(); // 本地开发调试用户 openid
# 本地开发调试用户OPENID if (in_array($this->request->host(), ['127.0.0.1', 'localhost'])) {
if (in_array($host, ['127.0.0.1', 'localhost'])) {
session('openid', 'o38gps1Unf64JOTdxNdd424lsEmM'); session('openid', 'o38gps1Unf64JOTdxNdd424lsEmM');
} }
# 检查缓存中openid信息是否完整 // 检查缓存中 openid 信息是否完整
if (!!($this->openid = session('openid'))) { if ($this->openid = session('openid')) {
if (!!($this->fansinfo = FansService::get($this->openid)) || !$isfull) { if (($this->fansinfo = WechatService::getFansInfo($this->openid)) || !$full) {
return $this->openid; return $this->openid;
} }
} }
# 发起微信网页授权 // 发起微信网页授权
$wxoauth_url = $this->current; $wxoauth_url = $this->url;
if (!($redirect_url = $this->request->get('redirecturl', false, 'decode'))) { if (!($redirect_url = $this->request->get('redirectcode', false, 'decode'))) {
$params = $this->request->param(); $split = stripos($this->url, '?') === false ? '?' : '&';
$params['redirecturl'] = encode($wxoauth_url); $wxoauth_url = "{$this->url}{$split}redirectcode=" . encode($this->url);
$wxoauth_url = url($this->request->baseUrl(), '', false, true) . '?' . http_build_query($params);
} }
$wechat = &load_wechat('Oauth'); $wechat = &load_wechat('Oauth');
# 微信网页授权处理 // 微信网页授权处理
if (!$this->request->get('code', false)) { 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'])) { if (FALSE === ($result = $wechat->getOauthAccessToken()) || empty($result['openid'])) {
Log::error("微信授权失败 [ {$wechat->errMsg} ]"); Log::error("微信网页授权失败,{$wechat->errMsg}[{$wechat->errCode}]");
exit('网页授权失败,请稍候再试!'); exit("微信网页授权失败,{$wechat->errMsg}[{$wechat->errCode}]");
} }
session('openid', $this->openid = $result['openid']); session('openid', $this->openid = $result['openid']);
!$full && $this->redirect($redirect_url);
// 微信粉丝信息处理
$this->fansinfo = WechatService::getFansInfo($this->openid); $this->fansinfo = WechatService::getFansInfo($this->openid);
# 微信粉丝信息处理
if (empty($this->fansinfo['expires_in']) || $this->fansinfo['expires_in'] < time()) { if (empty($this->fansinfo['expires_in']) || $this->fansinfo['expires_in'] < time()) {
switch ($result['scope']) { /* 使用普通授权,获取用户资料;未关注时重新使用高级授权 */
case 'snsapi_base': /* 普通授权,获取用户资料;未关注时重新使用高级授权 */ if ($result['scope'] === 'snsapi_base') {
$user = load_wechat('User')->getUserInfo($this->openid); $user = load_wechat('User')->getUserInfo($this->openid);
if ($isfull && empty($user['subscribe'])) { if ($full && empty($user['subscribe'])) {
exit(redirect($wechat->getOauthRedirect($wxoauth_url, 'webOauth', 'snsapi_userinfo'))->send()); $this->redirect($wechat->getOauthRedirect($wxoauth_url, 'webOauth', 'snsapi_userinfo'));
} }
break; } /* 使用高级授权,获取用户资料 */
case 'snsapi_userinfo': /* 高级授权,获取用户资料 */ elseif ($result['scope'] === 'snsapi_userinfo') {
$user = $wechat->getOauthUserinfo($result['access_token'], $this->openid); $user = $wechat->getOauthUserinfo($result['access_token'], $this->openid);
break;
} }
if ($isfull && (empty($user) || !array_key_exists('nickname', $user))) { if ($full && (empty($user) || !array_key_exists('nickname', $user))) {
exit("微信授权失败 [{$wechat->errMsg}]!"); Log::error("微信网页授权获取用户信息失败,{$wechat->errMsg}[{$wechat->errCode}]");
exit("微信网页授权获取用户信息失败,{$wechat->errMsg}[{$wechat->errCode}]");
} }
/* 更新粉丝信息 */ /* 更新粉丝信息 */
$user['expires_in'] = $result['expires_in'] + time() - 100; $user['expires_in'] = $result['expires_in'] + time() - 100;
$user['refresh_token'] = $result['refresh_token']; $user['refresh_token'] = $result['refresh_token'];
$user['access_token'] = $result['access_token']; $user['access_token'] = $result['access_token'];
!WechatService::setFansInfo($user, $wechat->appid) && exit('微信授权失败 [ save userinfo faild ]'); if (!WechatService::setFansInfo($user, $wechat->appid)) {
$this->fansinfo = WechatService::getFansInfo($this->openid); exit('微信网页授权获取用户信息保存失败');
}
} }
empty($this->fansinfo) && exit('获取微信用户信息失败!'); $this->redirect($redirect_url);
!!$redirect_url && exit(redirect($redirect_url)->send());
return $this->openid;
} }
} }

View File

@ -138,11 +138,14 @@ class WechatService {
/** /**
* 读取粉丝信息 * 读取粉丝信息
* @param string $openid * @param string $openid 微信用户openid
* @param string $appid 公众号appid
* @return array|false * @return array|false
*/ */
public static function getFansInfo($openid) { public static function getFansInfo($openid, $appid = null) {
return Db::name('WechatFans')->where('openid', $openid)->find(); $map = ['openid' => $openid];
is_string($map['appid']) && $map['appid'] = $appid;
return Db::name('WechatFans')->where($map)->find();
} }
/** /**