From 6c76ab26ce231b9d3d34d224648758338a7941e6 Mon Sep 17 00:00:00 2001 From: Anyon Date: Thu, 25 May 2017 10:43:35 +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=B6=88=E6=81=AF=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- application/wechat/controller/Api.php | 72 ++++++++++++--------------- 1 file changed, 31 insertions(+), 41 deletions(-) diff --git a/application/wechat/controller/Api.php b/application/wechat/controller/Api.php index 69d3cca4b..f81b6c780 100644 --- a/application/wechat/controller/Api.php +++ b/application/wechat/controller/Api.php @@ -16,10 +16,10 @@ namespace app\wechat\controller; use service\DataService; use service\WechatService; -use Wechat\WechatReceive; use think\Controller; -use think\Log; use think\Db; +use think\Log; +use Wechat\WechatReceive; /** * 微信接口控制器 @@ -35,14 +35,12 @@ class Api extends Controller { */ protected $openid; - /** * 微信消息对象 * @var WechatReceive */ protected $wechat; - /** * 微信消息接口 * @return string @@ -59,7 +57,7 @@ class Api extends Controller { // 获取消息来源用户OPENID $this->openid = $this->wechat->getRev()->getRevFrom(); // 获取并同步粉丝信息到数据库 - $this->_syncFans(true); + $this->_updateFansInfo(true); // 分别执行对应类型的操作 switch ($this->wechat->getRev()->getRevType()) { case WechatReceive::MSGTYPE_TEXT: @@ -77,40 +75,37 @@ class Api extends Controller { /** * 关键字处理 - * @param string $keys 关键字(常规或规格关键字) - * @param bool $isDefaultMode 是否启用默认模式 + * @param string $keys + * @param bool $isForce * @return string */ - private function _keys($keys, $isDefaultMode = false) { + private function _keys($keys, $isForce = 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(false, true); - } - // 无法给出回复时调用默认回复机制 + if (is_array($info = Db::name($table)->where($field, $value)->find()) && isset($info['type'])) { + // 数据状态检查 if (array_key_exists('status', $info) && empty($info['status'])) { return 'success'; } switch ($info['type']) { - case 'keys': /* 关键字 */ + case 'customservice': // 多客服 + $this->wechat->sendCustomMessage(['touser' => $this->openid, 'msgtype' => 'text', 'text' => ['content' => $info['content']]]); + return $this->wechat->transfer_customer_service()->reply(false, true); + case 'keys': // 关键字 if (empty($info['content']) && empty($info['name'])) { return 'success'; } return $this->_keys('wechat_keys#keys#' . (empty($info['content']) ? $info['name'] : $info['content'])); - case 'text': /* 文本消息 */ + case 'text': // 文本消息 if (empty($info['content']) && empty($info['name'])) { return 'success'; } return $this->wechat->text($info['content'])->reply(false, true); - case 'news': /* 图文消息 */ + case 'news': // 图文消息 if (empty($info['news_id'])) { return 'success'; } return $this->_news($info['news_id']); - case 'music': /* 音频消息 */ + case 'music': // 音频消息 if (empty($info['music_url']) || empty($info['music_title']) || empty($info['music_desc'])) { return 'success'; } @@ -119,7 +114,7 @@ class Api extends Controller { 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': /* 语音消息 */ + case 'voice': // 语音消息 if (empty($info['voice_url'])) { return 'success'; } @@ -128,7 +123,7 @@ class Api extends Controller { return 'success'; } return $this->wechat->voice($media_id)->reply(false, true); - case 'image': /* 图文消息 */ + case 'image': // 图文消息 if (empty($info['image_url'])) { return 'success'; } @@ -137,7 +132,7 @@ class Api extends Controller { return 'success'; } return $this->wechat->image($media_id)->reply(false, true); - case 'video': /* 视频消息 */ + case 'video': // 视频消息 if (empty($info['video_url']) || empty($info['video_desc']) || empty($info['video_title'])) { return 'success'; } @@ -146,7 +141,7 @@ class Api extends Controller { return $this->wechat->video($media_id, $info['video_title'], $info['video_desc'])->reply(false, true); } } - if ($isDefaultMode) { + if ($isForce) { return 'success'; } return $this->_keys('wechat_keys#keys#default', true); @@ -179,23 +174,17 @@ class Api extends Controller { protected function _event() { $event = $this->wechat->getRevEvent(); switch (strtolower($event['event'])) { - /* 粉丝关注事件 */ - case 'subscribe': - $this->_syncFans(true); - if (!empty($event['key']) && stripos($event['key'], 'qrscene_') !== false) { - $this->_spread(preg_replace('|^.*?(\d+).*?$|', '$1', $event['key'])); - } - return $this->_keys('wechat_keys#keys#subscribe'); - /* 粉丝取消关注 */ - case 'unsubscribe': - $this->_syncFans(false); + case 'subscribe': // 粉丝关注事件 + $this->_updateFansInfo(true); + $this->_spread($event['key']); + return $this->_keys('wechat_keys#keys#subscribe', true); + case 'unsubscribe':// 粉丝取消关注 + $this->_updateFansInfo(false); return 'success'; - /* 点击菜单事件 */ - case 'click': + case 'click': // 点击菜单事件 return $this->_keys($event['key']); - /* 扫码推事件 */ case 'scancode_push': - case 'scancode_waitmsg': + case 'scancode_waitmsg': // 扫码推事件 $scanInfo = $this->wechat->getRev()->getRevScanInfo(); if (isset($scanInfo['ScanResult'])) { return $this->_keys($scanInfo['ScanResult']); @@ -212,10 +201,11 @@ class Api extends Controller { /** * 推荐好友扫码关注 - * @param string $key + * @param string $event * @return mixed */ - private function _spread($key) { + private function _spread($event) { + $key = preg_replace('|^.*?(\d+).*?$|', '$1', "{$event}"); // 检测推荐是否有效 $fans = Db::name('WechatFans')->where('id', $key)->find(); if (!is_array($fans) || !isset($fans['openid']) || $fans['openid'] === $this->openid) { @@ -246,7 +236,7 @@ class Api extends Controller { * 同步粉丝状态 * @param bool $subscribe 关注状态 */ - protected function _syncFans($subscribe = true) { + protected function _updateFansInfo($subscribe = true) { if ($subscribe) { $fans = WechatService::getFansInfo($this->openid); if (empty($fans) || empty($fans['subscribe'])) {