From 4c26baf713ee7e2f37371d5ade536785ab4dec92 Mon Sep 17 00:00:00 2001 From: Anyon Date: Wed, 24 May 2017 14:23:26 +0800 Subject: [PATCH] =?UTF-8?q?[=E6=9B=B4=E6=96=B0]=E5=8E=BB=E9=99=A4=E9=A1=B9?= =?UTF-8?q?=E7=9B=AE=E4=B8=ADexit=E4=B8=AD=E6=96=AD=EF=BC=8C=E5=AE=8C?= =?UTF-8?q?=E5=85=A8=E5=88=87=E5=90=88ThinkPHP=E8=A7=84=E5=88=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- application/admin/controller/Auth.php | 2 +- application/admin/controller/Log.php | 2 +- application/admin/controller/Menu.php | 2 +- application/admin/controller/Plugs.php | 1 - application/admin/controller/User.php | 2 +- application/wechat/controller/Api.php | 119 ++++++++++++++++--------- application/wechat/controller/Keys.php | 6 +- application/wechat/controller/Menu.php | 10 +-- extend/controller/BasicAdmin.php | 56 ++++++------ extend/controller/BasicWechat.php | 6 +- extend/service/ToolsService.php | 1 - 11 files changed, 118 insertions(+), 89 deletions(-) diff --git a/application/admin/controller/Auth.php b/application/admin/controller/Auth.php index 0bda0a129..59c992922 100644 --- a/application/admin/controller/Auth.php +++ b/application/admin/controller/Auth.php @@ -40,7 +40,7 @@ class Auth extends BasicAdmin { */ public function index() { $this->title = '系统权限管理'; - parent::_list($this->table); + return parent::_list($this->table); } /** diff --git a/application/admin/controller/Log.php b/application/admin/controller/Log.php index 20e949193..197239ecd 100644 --- a/application/admin/controller/Log.php +++ b/application/admin/controller/Log.php @@ -46,7 +46,7 @@ class Log extends BasicAdmin { $db->where($key, 'like', "%{$get[$key]}%"); } } - parent::_list($db); + return parent::_list($db); } /** diff --git a/application/admin/controller/Menu.php b/application/admin/controller/Menu.php index dc71fcbe1..74b69e7df 100644 --- a/application/admin/controller/Menu.php +++ b/application/admin/controller/Menu.php @@ -41,7 +41,7 @@ class Menu extends BasicAdmin { public function index() { $this->title = '系统菜单管理'; $db = Db::name($this->table)->order('sort asc,id asc'); - parent::_list($db, false); + return parent::_list($db, false); } /** diff --git a/application/admin/controller/Plugs.php b/application/admin/controller/Plugs.php index 6987e1555..37b0c08b1 100644 --- a/application/admin/controller/Plugs.php +++ b/application/admin/controller/Plugs.php @@ -118,7 +118,6 @@ class Plugs extends BasicAdmin { * @return string */ protected function _getQiniuToken($key) { - empty($key) && exit('param error'); $accessKey = sysconf('storage_qiniu_access_key'); $secretKey = sysconf('storage_qiniu_secret_key'); $bucket = sysconf('storage_qiniu_bucket'); diff --git a/application/admin/controller/User.php b/application/admin/controller/User.php index 300f4cbf9..a03f839d6 100644 --- a/application/admin/controller/User.php +++ b/application/admin/controller/User.php @@ -50,7 +50,7 @@ class User extends BasicAdmin { } } // 实例化并显示 - parent::_list($db); + return parent::_list($db); } /** diff --git a/application/wechat/controller/Api.php b/application/wechat/controller/Api.php index 050247517..c669a7b1a 100644 --- a/application/wechat/controller/Api.php +++ b/application/wechat/controller/Api.php @@ -18,8 +18,8 @@ use service\DataService; use service\WechatService; use Wechat\WechatReceive; use think\Controller; -use think\Db; use think\Log; +use think\Db; /** * 微信接口控制器 @@ -29,97 +29,127 @@ use think\Log; */ class Api extends Controller { - /** - * 微信消息对象 - * @var WechatReceive - */ - protected $wechat; - /** * 微信openid * @var string */ protected $openid; + + /** + * 微信消息对象 + * @var WechatReceive + */ + protected $wechat; + + + /** + * 微信消息接口 + * @return string + */ public function index() { - /* 实例接口对象 */ + // 实例接口对象 $this->wechat = &load_wechat('Receive'); - /* 验证接口请求 */ + // 验证接口请求 if ($this->wechat->valid() === false) { $msg = "{$this->wechat->errMsg}[{$this->wechat->errCode}]"; Log::error($msg); - exit($msg); + return $msg; } - /* 获取消息openid */ + // 获取消息来源用户OPENID $this->openid = $this->wechat->getRev()->getRevFrom(); - /* 同步粉丝数据 */ + // 获取并同步粉丝信息到数据库 $this->_syncFans(true); - /* 分别执行对应类型的操作 */ + // 分别执行对应类型的操作 switch ($this->wechat->getRev()->getRevType()) { case WechatReceive::MSGTYPE_TEXT: $keys = $this->wechat->getRevContent(); - exit($this->_keys("wechat_keys#keys#{$keys}")); + return $this->_keys("wechat_keys#keys#{$keys}"); case WechatReceive::MSGTYPE_EVENT: - exit($this->_event()); + return $this->_event(); case WechatReceive::MSGTYPE_IMAGE: - exit($this->_image()); + return $this->_image(); case WechatReceive::MSGTYPE_LOCATION: - exit($this->_location()); + return $this->_location(); default: - exit('success'); + return 'success'; } } /** * 关键字处理 * @param string $keys 关键字(常规或规格关键字) - * @param bool $default 是否启用默认模式 + * @param bool $isDefaultMode 是否启用默认模式 * @return string */ - private function _keys($keys, $default = false) { + private function _keys($keys, $isDefaultMode = false) { list($table, $field, $value) = explode('#', $keys . '##'); $info = Db::name($table)->where($field, $value)->find(); if ($info && is_array($info) && isset($info['type'])) { // 转发给多客服 if (!empty($info['type']) && $info['type'] === 'customservice') { $this->wechat->sendCustomMessage(['touser' => $this->openid, 'msgtype' => 'text', 'text' => ['content' => $info['content']]]); - return $this->wechat->transfer_customer_service()->reply(); + return $this->wechat->transfer_customer_service()->reply(false, true); } // 无法给出回复时调用默认回复机制 - array_key_exists('status', $info) && empty($info['status']) && exit('success'); + if (array_key_exists('status', $info) && empty($info['status'])) { + return 'success'; + } switch ($info['type']) { case 'keys': /* 关键字 */ - empty($info['content']) && empty($info['name']) && exit('success'); + if (empty($info['content']) && empty($info['name'])) { + return 'success'; + } return $this->_keys('wechat_keys#keys#' . (empty($info['content']) ? $info['name'] : $info['content'])); case 'text': /* 文本消息 */ - empty($info['content']) && exit('success'); - return $this->wechat->text($info['content'])->reply(); + if (empty($info['content']) && empty($info['name'])) { + return 'success'; + } + return $this->wechat->text($info['content'])->reply(false, true); case 'news': /* 图文消息 */ - empty($info['news_id']) && exit('success'); + if (empty($info['news_id'])) { + return 'success'; + } return $this->_news($info['news_id']); case 'music': /* 音频消息 */ - (empty($info['music_url']) || empty($info['music_title']) || empty($info['music_desc'])) && exit('success'); + if (empty($info['music_url']) || empty($info['music_title']) || empty($info['music_desc'])) { + return 'success'; + } $media_id = empty($info['music_image']) ? '' : WechatService::uploadForeverMedia($info['music_image'], 'image'); - empty($media_id) && exit('success'); - return $this->wechat->music($info['music_title'], $info['music_desc'], $info['music_url'], $info['music_url'], $media_id)->reply(); + if (empty($media_id)) { + return 'success'; + } + return $this->wechat->music($info['music_title'], $info['music_desc'], $info['music_url'], $info['music_url'], $media_id)->reply(false, true); case 'voice': /* 语音消息 */ - empty($info['voice_url']) && exit('success'); + if (empty($info['voice_url'])) { + return 'success'; + } $media_id = WechatService::uploadForeverMedia($info['voice_url'], 'voice'); - empty($media_id) && exit('success'); - return $this->wechat->voice($media_id)->reply(); + if (empty($media_id)) { + return 'success'; + } + return $this->wechat->voice($media_id)->reply(false, true); case 'image': /* 图文消息 */ - empty($info['image_url']) && exit('success'); + if (empty($info['image_url'])) { + return 'success'; + } $media_id = WechatService::uploadForeverMedia($info['image_url'], 'image'); - empty($media_id) && exit('success'); - return $this->wechat->image($media_id)->reply(); + if (empty($media_id)) { + return 'success'; + } + return $this->wechat->image($media_id)->reply(false, true); case 'video': /* 视频消息 */ - (empty($info['video_url']) || empty($info['video_desc']) || empty($info['video_title'])) && exit('success'); + if (empty($info['video_url']) || empty($info['video_desc']) || empty($info['video_title'])) { + return 'success'; + } $data = ['title' => $info['video_title'], 'introduction' => $info['video_desc']]; $media_id = WechatService::uploadForeverMedia($info['video_url'], 'video', true, $data); - return $this->wechat->video($media_id, $info['video_title'], $info['video_desc'])->reply(); + return $this->wechat->video($media_id, $info['video_title'], $info['video_desc'])->reply(false, true); } } - $default && exit('success'); + if ($isDefaultMode) { + return 'success'; + } return $this->_keys('wechat_keys#keys#default', true); } @@ -141,7 +171,7 @@ class Api extends Controller { } return $this->wechat->news($newsdata)->reply(); } - exit('success'); + return 'success'; } /** @@ -160,7 +190,7 @@ class Api extends Controller { /* 粉丝取消关注 */ case 'unsubscribe': $this->_syncFans(false); - exit('success'); + return 'success'; /* 点击菜单事件 */ case 'click': return $this->_keys($event['key']); @@ -171,13 +201,14 @@ class Api extends Controller { if (isset($scanInfo['ScanResult'])) { return $this->_keys($scanInfo['ScanResult']); } - exit('success'); + return 'success'; case 'scan': if (!empty($event['key'])) { return $this->_spread($event['key']); } - exit('success'); + return 'success'; } + return 'success'; } /** @@ -202,14 +233,14 @@ class Api extends Controller { * @return string */ private function _location() { - exit('success'); + return 'success'; } /** * 图片事件处理 */ private function _image() { - exit('success'); + return 'success'; } /** diff --git a/application/wechat/controller/Keys.php b/application/wechat/controller/Keys.php index 91197c93d..049229fd7 100644 --- a/application/wechat/controller/Keys.php +++ b/application/wechat/controller/Keys.php @@ -38,7 +38,7 @@ class Keys extends BasicAdmin { public function index() { $this->assign('title', '微信关键字'); $db = Db::name($this->table)->where('keys', 'not in', ['subscribe', 'default']); - $this->_list($db); + return $this->_list($db); } /** @@ -57,7 +57,7 @@ class Keys extends BasicAdmin { * @return string */ public function add() { - $this->assign('title', '添加关键字规则'); + $this->title = '添加关键字规则'; return $this->_form($this->table, 'form'); } @@ -66,7 +66,7 @@ class Keys extends BasicAdmin { * @return string */ public function edit() { - $this->assign('title', '编辑关键字规则'); + $this->title = '编辑关键字规则'; return $this->_form($this->table, 'form'); } diff --git a/application/wechat/controller/Menu.php b/application/wechat/controller/Menu.php index 069a371fc..359884836 100644 --- a/application/wechat/controller/Menu.php +++ b/application/wechat/controller/Menu.php @@ -59,7 +59,7 @@ class Menu extends BasicAdmin { * 显示列表操作 */ public function index() { - parent::_list(Db::name($this->table), false, true); + return parent::_list(Db::name($this->table), false, true); } /** @@ -116,10 +116,10 @@ class Menu extends BasicAdmin { */ protected function _push() { $result = Db::name($this->table) - ->field('id,index,pindex,name,type,content') - ->where('status', '1') - ->order('sort ASC,id ASC') - ->select(); + ->field('id,index,pindex,name,type,content') + ->where('status', '1') + ->order('sort ASC,id ASC') + ->select(); foreach ($result as &$row) { empty($row['content']) && $row['content'] = uniqid(); switch ($row['type']) { diff --git a/extend/controller/BasicAdmin.php b/extend/controller/BasicAdmin.php index 512d6882c..55dac4eb0 100644 --- a/extend/controller/BasicAdmin.php +++ b/extend/controller/BasicAdmin.php @@ -68,52 +68,52 @@ class BasicAdmin extends Controller { $data = array_merge($this->request->post(), $data); if (false !== $this->_callback('_form_filter', $data)) { $result = DataService::save($db, $data, $pk, $where); - (false !== $this->_callback('_form_result', $result)) && ($result !== false ? $this->success('恭喜, 数据保存成功!', '') : $this->error('数据保存失败, 请稍候再试!')); + if (false === $this->_callback('_form_result', $result)) { + return $result; + } + if ($result !== false) { + $this->success('恭喜, 数据保存成功!', ''); + } + $this->error('数据保存失败, 请稍候再试!'); } - return $result; } // GET请求, 获取并显示表单页面 - $vo = ($pkValue !== null) ? array_merge((array) $db->where($pk, $pkValue)->where($where)->find(), $data) : $data; - if (false !== $this->_callback('_form_filter', $vo)) { - return $this->fetch($tplFile, ['title' => $this->title, 'vo' => $vo]); + $vo = ($pkValue !== null) ? array_merge((array)$db->where($pk, $pkValue)->where($where)->find(), $data) : $data; + if (false === $this->_callback('_form_filter', $vo)) { + return $vo; } - return $vo; + empty($this->title) or $this->assign('title', $this->title); + return $this->fetch($tplFile, ['vo' => $vo]); } /** * 列表集成处理方法 - * @param Query $db 数据库查询对象 - * @param bool $is_page 是启用分页 - * @param bool $is_display 是否直接输出显示 + * @param Query $dbQuery 数据库查询对象 + * @param bool $isPage 是启用分页 + * @param bool $isDisplay 是否直接输出显示 * @param bool $total 总记录数 * @return array|string */ - protected function _list($db = null, $is_page = true, $is_display = true, $total = false) { - if (is_null($db)) { - $db = Db::name($this->table); - } elseif (is_string($db)) { - $db = Db::name($db); - } - # 列表排序默认处理 + protected function _list($dbQuery = null, $isPage = true, $isDisplay = true, $total = false) { + $db = is_null($dbQuery) ? Db::name($this->table) : (is_string($dbQuery) ? Db::name($dbQuery) : $dbQuery); + // 列表排序默认处理 if ($this->request->isPost() && $this->request->post('action') === 'resort') { $data = $this->request->post(); unset($data['action']); foreach ($data as $key => &$value) { - if (false === $db->where('id', intval(ltrim($key, '_')))->update(['sort' => $value])) { - $this->error('列表排序失败,请稍候再试!'); + if (false === $db->where('id', intval(ltrim($key, '_')))->setField('sort', $value)) { + $this->error('列表排序失败, 请稍候再试'); } } - $this->success('列表排序成功,正在刷新列表!', ''); + $this->success('列表排序成功, 正在刷新列表', ''); } - # 列表显示 + // 列表数据查询与显示 $result = array(); - # 默认排序 - $options = $db->getOptions(); - if (empty($options['order'])) { + if (null === $db->getOptions('order')) { $fields = $db->getTableFields(['table' => $db->getTable()]); in_array('sort', $fields) && $db->order('sort asc'); } - if ($is_page) { + if ($isPage) { $row_page = $this->request->get('rows', cookie('rows'), 'intval'); cookie('rows', $row_page >= 10 ? $row_page : 20); $page = $db->paginate($row_page, $total, ['query' => $this->request->get()]); @@ -122,18 +122,18 @@ class BasicAdmin extends Controller { } else { $result['list'] = $db->select(); } - if ($this->_callback('_data_filter', $result['list']) === false) { + if (false === $this->_callback('_data_filter', $result['list']) || !$isDisplay) { return $result; } !empty($this->title) && $this->assign('title', $this->title); - $is_display && exit($this->fetch('', $result)); - return $result; + return $this->fetch('', $result); + } /** * 当前对象回调成员方法 * @param string $method - * @param array $data + * @param array|bool $data * @return bool */ protected function _callback($method, &$data) { diff --git a/extend/controller/BasicWechat.php b/extend/controller/BasicWechat.php index 652498a53..552daec66 100644 --- a/extend/controller/BasicWechat.php +++ b/extend/controller/BasicWechat.php @@ -95,7 +95,7 @@ class BasicWechat extends Controller { } if (FALSE === ($result = $wechat->getOauthAccessToken()) || empty($result['openid'])) { Log::error("微信网页授权失败, {$wechat->errMsg}[{$wechat->errCode}]"); - exit("微信网页授权失败, {$wechat->errMsg}[{$wechat->errCode}]"); + $this->error("微信网页授权失败, {$wechat->errMsg}[{$wechat->errCode}]"); } session('openid', $this->openid = $result['openid']); empty($fullMode) && $this->redirect($redirect_url); @@ -113,12 +113,12 @@ class BasicWechat extends Controller { /* 授权结果处理, 更新粉丝信息 */ if ((empty($user) || !array_key_exists('nickname', $user))) : Log::error("微信网页授权获取用户信息失败, {$wechat->errMsg}[{$wechat->errCode}]"); - exit("微信网页授权获取用户信息失败, {$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 exit('微信网页授权用户保存失败!'); + WechatService::setFansInfo($user, $wechat->appid) or $this->error('微信网页授权用户保存失败!'); } $this->redirect($redirect_url); } diff --git a/extend/service/ToolsService.php b/extend/service/ToolsService.php index 137a973f0..5322854fc 100644 --- a/extend/service/ToolsService.php +++ b/extend/service/ToolsService.php @@ -37,7 +37,6 @@ class ToolsService { header('Content-Length: 0', true); header('status: 204'); header('HTTP/1.0 204 No Content'); - exit; } }