From ffde6065dae620fbb349fa806d2cb881e90535d0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=82=B9=E6=99=AF=E7=AB=8B?= Date: Wed, 5 Apr 2017 14:18:08 +0800 Subject: [PATCH] =?UTF-8?q?[=E6=9B=B4=E6=96=B0]=E5=BE=AE=E4=BF=A1=E6=A8=A1?= =?UTF-8?q?=E5=9D=97=E5=BC=80=E5=8F=91=E4=B8=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- application/wechat/controller/Api.php | 68 +++++++++++------------- application/wechat/controller/Notify.php | 32 +++++++++++ extend/service/PayService.php | 2 +- 3 files changed, 63 insertions(+), 39 deletions(-) create mode 100644 application/wechat/controller/Notify.php diff --git a/application/wechat/controller/Api.php b/application/wechat/controller/Api.php index 102808cb6..bf050f122 100644 --- a/application/wechat/controller/Api.php +++ b/application/wechat/controller/Api.php @@ -45,11 +45,12 @@ class Api extends Controller { /* 实例接口对象 */ $this->wechat = &load_wechat('Receive'); /* 验证接口请求 */ - if ($this->wechat->valid() === FALSE) { - Log::error("微信被动接口验证失败,{$this->wechat->errMsg}[{$this->wechat->errCode}]"); - exit($this->wechat->errMsg); + if ($this->wechat->valid() === false) { + $msg = "微信消息验证失败,{$this->wechat->errMsg}[{$this->wechat->errCode}]"; + Log::error($msg); + exit($msg); } - /* 获取openid */ + /* 获取消息openid */ $this->openid = $this->wechat->getRev()->getRevFrom(); /* 同步粉丝数据 */ $this->_syncFans(true); @@ -65,7 +66,7 @@ class Api extends Controller { case WechatReceive::MSGTYPE_LOCATION: exit($this->_location()); default: - exit($this->_default()); + exit('success'); } } @@ -75,52 +76,51 @@ class Api extends Controller { * @param bool $default 是否启用默认模式 * @return string */ - protected function _keys($keys, $default = FALSE) { + private function _keys($keys, $default = false) { list($table, $field, $value) = explode('#', $keys . '##'); - $info = Db::table($table)->where('appid', $this->appid)->where($field, $value)->find(); - if ($info) { - # 转发给多客服 + $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(array('touser' => $this->openid, 'msgtype' => 'text', 'text' => array('content' => $info['content']))); + $this->wechat->sendCustomMessage(['touser' => $this->openid, 'msgtype' => 'text', 'text' => ['content' => $info['content']]]); return $this->wechat->transfer_customer_service()->reply(); } - # 无法给出回复时调用默认回复机制 + // 无法给出回复时调用默认回复机制 array_key_exists('status', $info) && empty($info['status']) && exit('success'); switch ($info['type']) { - case 'keys':/* 关键字 */ + case 'keys': /* 关键字 */ empty($info['content']) && empty($info['name']) && exit('success'); return $this->_keys('wechat_keys#keys#' . (empty($info['content']) ? $info['name'] : $info['content'])); - case 'text':/* 文本消息 */ + case 'text': /* 文本消息 */ empty($info['content']) && exit('success'); return $this->wechat->text($info['content'])->reply(); - case 'news':/* 图文消息 */ + case 'news': /* 图文消息 */ empty($info['news_id']) && exit('success'); return $this->_news($info['news_id']); - case 'music':/* 音频消息 */ + case 'music': /* 音频消息 */ (empty($info['music_url']) || empty($info['music_title']) || empty($info['music_desc'])) && exit('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(); - case 'voice':/* 语音消息 */ + case 'voice': /* 语音消息 */ empty($info['voice_url']) && exit('success'); $media_id = WechatService::uploadForeverMedia($info['voice_url'], 'voice')->reply(); empty($media_id) && exit('success'); return $this->wechat->voice($media_id)->reply(); - case 'image':/* 图文消息 */ + case 'image': /* 图文消息 */ empty($info['image_url']) && exit('success'); $media_id = WechatService::uploadForeverMedia($info['image_url'], 'image'); empty($media_id) && exit('success'); return $this->wechat->image($media_id)->reply(); - case 'video':/* 视频消息 */ + case 'video': /* 视频消息 */ (empty($info['video_url']) || empty($info['video_desc']) || empty($info['video_title'])) && exit('success'); - $data = array('title' => $info['video_title'], 'introduction' => $info['video_desc']); - $media_id = WechatService::uploadForeverMedia($info['video_url'], 'video', TRUE, $data); + $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(); } } - $default && exit('success'); - return $this->_keys('wechat_keys#keys#default', TRUE); + return $this->_keys('wechat_keys#keys#default', true); } @@ -130,7 +130,7 @@ class Api extends Controller { * @return bool|string */ protected function _news($news_id = 0) { - if (is_array($newsinfo = WechatService::getNewsById($news_id))) { + if (is_array($newsinfo = WechatService::getNewsById($news_id)) && !empty($newsinfo['articles'])) { $newsdata = array(); foreach ($newsinfo['articles'] as $vo) { $newsdata[] = [ @@ -151,23 +151,23 @@ 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); exit('success'); + /* 点击菜单 */ case 'click': - /* 点击链接 */ return $this->_keys($event['key']); + /* 扫码推事件 */ case 'scancode_push': case 'scancode_waitmsg': - /* 扫码推事件 */ $scanInfo = $this->wechat->getRev()->getRevScanInfo(); if (isset($scanInfo['ScanResult'])) { return $this->_keys($scanInfo['ScanResult']); @@ -186,7 +186,7 @@ class Api extends Controller { * @param string $key * @return mixed */ - protected function _spread($key) { + private function _spread($key) { // 检测推荐是否有效 $fans = Db::name('WechatFans')->where('id', $key)->find(); if (!is_array($fans) || !isset($fans['openid']) || $fans['openid'] === $this->openid) { @@ -204,22 +204,14 @@ class Api extends Controller { * 位置事情回复 * @return string */ - protected function _location() { + private function _location() { exit('success'); } /** * 图片事件处理 */ - protected function _image() { - exit('success'); - } - - /** - * 默认事件处理 - * @return string - */ - protected function _default() { + private function _image() { exit('success'); } diff --git a/application/wechat/controller/Notify.php b/application/wechat/controller/Notify.php new file mode 100644 index 000000000..25e9330a8 --- /dev/null +++ b/application/wechat/controller/Notify.php @@ -0,0 +1,32 @@ + + * @date 2017/04/05 14:02 + */ +class Notify extends Controller { + + public function index() { + + } + +} diff --git a/extend/service/PayService.php b/extend/service/PayService.php index d34f2b32d..41afd9b9a 100644 --- a/extend/service/PayService.php +++ b/extend/service/PayService.php @@ -125,7 +125,7 @@ class PayService { $prepayinfo = Db::name('WechatPayPrepayid')->where($where, $map)->find(); if (empty($prepayinfo) || empty($prepayinfo['prepayid'])) { $out_trade_no = DataService::createSequence(18, 'WXPAY-OUTER-NO'); - $prepayid = $pay->getPrepayId($openid, $title, $out_trade_no, $fee, url("@store/api/notify", '', true, true), $trade_type); + $prepayid = $pay->getPrepayId($openid, $title, $out_trade_no, $fee, url("@wechat/notify", '', true, true), $trade_type); if (empty($prepayid)) { Log::error("内部订单号{$order_no}生成预支付失败,{$pay->errMsg}"); return false;