From 3b92468c06182bfbbbb38c8f6b573bd5d779f832 Mon Sep 17 00:00:00 2001 From: xiaochong0302 Date: Tue, 29 Dec 2020 15:19:29 +0800 Subject: [PATCH] =?UTF-8?q?=E8=B0=83=E6=95=B4=E5=85=AC=E4=BC=97=E5=8F=B7?= =?UTF-8?q?=E6=A8=A1=E6=9D=BF=E6=B6=88=E6=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Console/Tasks/CleanLogTask.php | 136 +++++++++++++++++--- app/Console/Tasks/NoticeTask.php | 2 + app/Http/Admin/Services/AuthNode.php | 2 +- app/Http/Admin/Views/setting/sms.volt | 2 +- app/Http/Admin/Views/setting/wechat_oa.volt | 12 +- app/Listeners/Pay.php | 10 +- app/Services/LiveNotify.php | 46 +++++++ app/Services/Logic/Notice/ConsultReply.php | 2 + app/Services/Logic/Notice/OrderFinish.php | 2 + app/Services/Logic/Notice/RefundFinish.php | 6 +- app/Services/Logic/Refund/RefundCreate.php | 6 +- app/Services/Wechat/Notice/AccountLogin.php | 8 +- app/Services/Wechat/Notice/ConsultReply.php | 4 +- app/Services/Wechat/Notice/LiveBegin.php | 8 +- app/Services/Wechat/Notice/OrderFinish.php | 6 +- app/Services/Wechat/Notice/RefundFinish.php | 7 +- 16 files changed, 214 insertions(+), 45 deletions(-) diff --git a/app/Console/Tasks/CleanLogTask.php b/app/Console/Tasks/CleanLogTask.php index d4d532c7..725dfa6c 100644 --- a/app/Console/Tasks/CleanLogTask.php +++ b/app/Console/Tasks/CleanLogTask.php @@ -5,6 +5,8 @@ namespace App\Console\Tasks; class CleanLogTask extends Task { + protected $whitelist = []; + public function mainAction() { $this->cleanCommonLog(); @@ -24,6 +26,7 @@ class CleanLogTask extends Task $this->cleanOrderLog(); $this->cleanRefundLog(); $this->cleanNoticeLog(); + $this->cleanOtherLog(); } /** @@ -31,7 +34,11 @@ class CleanLogTask extends Task */ protected function cleanCommonLog() { - $this->cleanLog('common', 7); + $type = 'common'; + + $this->cleanLog($type, 7); + + $this->whitelist[] = $type; } /** @@ -39,7 +46,11 @@ class CleanLogTask extends Task */ protected function cleanHttpLog() { - $this->cleanLog('http', 7); + $type = 'http'; + + $this->cleanLog($type, 7); + + $this->whitelist[] = $type; } /** @@ -47,7 +58,11 @@ class CleanLogTask extends Task */ protected function cleanConsoleLog() { - $this->cleanLog('console', 7); + $type = 'console'; + + $this->cleanLog($type, 7); + + $this->whitelist[] = $type; } /** @@ -55,7 +70,11 @@ class CleanLogTask extends Task */ protected function cleanSqlLog() { - $this->cleanLog('sql', 3); + $type = 'sql'; + + $this->cleanLog($type, 3); + + $this->whitelist[] = $type; } /** @@ -63,7 +82,11 @@ class CleanLogTask extends Task */ protected function cleanListenLog() { - $this->cleanLog('listen', 7); + $type = 'listen'; + + $this->cleanLog($type, 7); + + $this->whitelist[] = $type; } /** @@ -71,7 +94,11 @@ class CleanLogTask extends Task */ protected function cleanCaptchaLog() { - $this->cleanLog('captcha', 7); + $type = 'captcha'; + + $this->cleanLog($type, 7); + + $this->whitelist[] = $type; } /** @@ -79,7 +106,11 @@ class CleanLogTask extends Task */ protected function cleanVodLog() { - $this->cleanLog('vod', 7); + $type = 'vod'; + + $this->cleanLog($type, 7); + + $this->whitelist[] = $type; } /** @@ -87,7 +118,11 @@ class CleanLogTask extends Task */ protected function cleanLiveLog() { - $this->cleanLog('live', 7); + $type = 'live'; + + $this->cleanLog($type, 7); + + $this->whitelist[] = $type; } /** @@ -95,7 +130,11 @@ class CleanLogTask extends Task */ protected function cleanStorageLog() { - $this->cleanLog('storage', 7); + $type = 'storage'; + + $this->cleanLog($type, 7); + + $this->whitelist[] = $type; } /** @@ -103,7 +142,11 @@ class CleanLogTask extends Task */ protected function cleanSmsLog() { - $this->cleanLog('sms', 7); + $type = 'sms'; + + $this->cleanLog($type, 7); + + $this->whitelist[] = $type; } /** @@ -111,7 +154,11 @@ class CleanLogTask extends Task */ protected function cleanMailLog() { - $this->cleanLog('mail', 7); + $type = 'mail'; + + $this->cleanLog($type, 7); + + $this->whitelist[] = $type; } /** @@ -119,7 +166,11 @@ class CleanLogTask extends Task */ protected function cleanWechatLog() { - $this->cleanLog('wechat', 7); + $type = 'wechat'; + + $this->cleanLog($type, 7); + + $this->whitelist[] = $type; } /** @@ -127,7 +178,11 @@ class CleanLogTask extends Task */ protected function cleanAlipayLog() { - $this->cleanLog('alipay', 30); + $type = 'alipay'; + + $this->cleanLog($type, 30); + + $this->whitelist[] = $type; } /** @@ -135,7 +190,11 @@ class CleanLogTask extends Task */ protected function cleanWxpayLog() { - $this->cleanLog('wxpay', 30); + $type = 'wxpay'; + + $this->cleanLog($type, 30); + + $this->whitelist[] = $type; } /** @@ -143,7 +202,11 @@ class CleanLogTask extends Task */ protected function cleanOrderLog() { - $this->cleanLog('order', 30); + $type = 'order'; + + $this->cleanLog($type, 30); + + $this->whitelist[] = $type; } /** @@ -151,7 +214,11 @@ class CleanLogTask extends Task */ protected function cleanRefundLog() { - $this->cleanLog('refund', 30); + $type = 'refund'; + + $this->cleanLog($type, 30); + + $this->whitelist[] = $type; } /** @@ -159,7 +226,42 @@ class CleanLogTask extends Task */ protected function cleanNoticeLog() { - $this->cleanLog('notice', 7); + $type = 'notice'; + + $this->cleanLog($type, 7); + + $this->whitelist[] = $type; + } + + /** + * 清理其它日志 + * + * @param int $keepDays + * @return mixed + */ + protected function cleanOtherLog($keepDays = 7) + { + $files = glob(log_path() . "/*.log"); + + if (!$files) return false; + + foreach ($files as $file) { + $name = str_replace(log_path() . '/', '', $file); + $type = substr($name, 0, -15); + $date = substr($name, -14, 10); + $today = date('Y-m-d'); + if (in_array($type, $this->whitelist)) { + continue; + } + if (strtotime($today) - strtotime($date) >= $keepDays * 86400) { + $deleted = unlink($file); + if ($deleted) { + echo "delete {$file} success" . PHP_EOL; + } else { + echo "delete {$file} failed" . PHP_EOL; + } + } + } } /** diff --git a/app/Console/Tasks/NoticeTask.php b/app/Console/Tasks/NoticeTask.php index c4a385d4..06841554 100644 --- a/app/Console/Tasks/NoticeTask.php +++ b/app/Console/Tasks/NoticeTask.php @@ -64,6 +64,8 @@ class NoticeTask extends Task $task->update(); $logger->info('Notice Process Exception ' . kg_json_encode([ + 'file' => $e->getFile(), + 'line' => $e->getLine(), 'code' => $e->getCode(), 'message' => $e->getMessage(), 'task' => $task->toArray(), diff --git a/app/Http/Admin/Services/AuthNode.php b/app/Http/Admin/Services/AuthNode.php index db2ec7d8..fb828d9c 100644 --- a/app/Http/Admin/Services/AuthNode.php +++ b/app/Http/Admin/Services/AuthNode.php @@ -758,7 +758,7 @@ class AuthNode extends Service ], [ 'id' => '5-1-13', - 'title' => '微信公众平台', + 'title' => '微信平台', 'type' => 'menu', 'route' => 'admin.setting.wechat', ], diff --git a/app/Http/Admin/Views/setting/sms.volt b/app/Http/Admin/Views/setting/sms.volt index 8601b0a2..05a2acda 100644 --- a/app/Http/Admin/Views/setting/sms.volt +++ b/app/Http/Admin/Views/setting/sms.volt @@ -70,7 +70,7 @@ 复制 - 回复通知 + 咨询通知 复制 diff --git a/app/Http/Admin/Views/setting/wechat_oa.volt b/app/Http/Admin/Views/setting/wechat_oa.volt index efc70be5..97702aa3 100644 --- a/app/Http/Admin/Views/setting/wechat_oa.volt +++ b/app/Http/Admin/Views/setting/wechat_oa.volt @@ -54,7 +54,7 @@ - + @@ -66,27 +66,27 @@ - + - + - + - + - + diff --git a/app/Listeners/Pay.php b/app/Listeners/Pay.php index 2ee624a8..ac5b7d88 100644 --- a/app/Listeners/Pay.php +++ b/app/Listeners/Pay.php @@ -73,11 +73,11 @@ class Pay extends Listener 'message' => $e->getMessage(), ])); - $this->logger->debug('Event: {event}, Source: {source}, Data: {data}', [ - 'event' => $event->getType(), - 'source' => get_class($source), - 'data' => kg_json_encode($trade), - ]); + $this->logger->debug('After Pay Event Info ' . kg_json_encode([ + 'event' => $event->getType(), + 'source' => get_class($source), + 'data' => kg_json_encode($trade), + ])); throw new \RuntimeException('sys.trans_rollback'); } diff --git a/app/Services/LiveNotify.php b/app/Services/LiveNotify.php index edf7982d..82fcc768 100644 --- a/app/Services/LiveNotify.php +++ b/app/Services/LiveNotify.php @@ -2,21 +2,39 @@ namespace App\Services; +use App\Caches\CourseChapterList as CatalogCache; use App\Models\Chapter as ChapterModel; use App\Models\ChapterLive as ChapterLiveModel; use App\Repos\Chapter as ChapterRepo; use App\Repos\CourseUser as CourseUserRepo; use App\Services\Logic\Notice\LiveBegin as LiveBeginNotice; +use Phalcon\Logger\Adapter\File as FileLogger; class LiveNotify extends Service { + /** + * @var FileLogger + */ + protected $logger; + + public function __construct() + { + $this->logger = $this->getLogger('live'); + } + public function handle() { $time = $this->request->getPost('t', 'int'); $sign = $this->request->getPost('sign', 'string'); $action = $this->request->getQuery('action', 'string'); + $this->logger->debug('Received Live Notify Data: ' . kg_json_encode([ + 't' => $time, + 'sign' => $sign, + 'action' => $action, + ])); + if (!$this->checkSign($sign, $time)) { return false; } @@ -53,6 +71,8 @@ class LiveNotify extends Service $chapter = $this->getChapter($streamId); + $this->logger->debug("Chapter:{$chapter->id} Stream Begin"); + if (!$chapter) return false; $attrs = $chapter->attrs; @@ -65,6 +85,8 @@ class LiveNotify extends Service $chapterLive->update(['status' => ChapterLiveModel::STATUS_ACTIVE]); + $this->rebuildCatalogCache($chapter); + $this->handleStreamBeginNotice($chapter); return true; @@ -79,6 +101,8 @@ class LiveNotify extends Service $chapter = $this->getChapter($streamId); + $this->logger->info("Chapter:{$chapter->id} Stream End"); + if (!$chapter) return false; $attrs = $chapter->attrs; @@ -91,6 +115,8 @@ class LiveNotify extends Service $chapterLive->update(['status' => ChapterLiveModel::STATUS_INACTIVE]); + $this->rebuildCatalogCache($chapter); + return true; } @@ -120,6 +146,19 @@ class LiveNotify extends Service protected function handleStreamBeginNotice(ChapterModel $chapter) { + /** + * 防止发送多次通知 + */ + $cache = $this->getCache(); + + $keyName = "live_notify:{$chapter->id}"; + + if ($cache->get($keyName)) { + return; + } + + $cache->save($keyName, time(), 86400); + $courseUserRepo = new CourseUserRepo(); $courseUsers = $courseUserRepo->findByCourseId($chapter->course_id); @@ -135,6 +174,13 @@ class LiveNotify extends Service } } + protected function rebuildCatalogCache(ChapterModel $chapter) + { + $cache = new CatalogCache(); + + $cache->rebuild($chapter->course_id); + } + protected function getChapter($streamName) { $id = ChapterLiveModel::parseFromStreamName($streamName); diff --git a/app/Services/Logic/Notice/ConsultReply.php b/app/Services/Logic/Notice/ConsultReply.php index cdd3c34a..3b77f7c1 100644 --- a/app/Services/Logic/Notice/ConsultReply.php +++ b/app/Services/Logic/Notice/ConsultReply.php @@ -46,6 +46,8 @@ class ConsultReply extends LogicService 'id' => $consult->id, 'question' => $consult->question, 'answer' => $consult->answer, + 'create_time' => $consult->create_time, + 'reply_time' => $consult->reply_time, ], 'course' => [ 'id' => $course->id, diff --git a/app/Services/Logic/Notice/OrderFinish.php b/app/Services/Logic/Notice/OrderFinish.php index 5b70e132..206f231b 100644 --- a/app/Services/Logic/Notice/OrderFinish.php +++ b/app/Services/Logic/Notice/OrderFinish.php @@ -35,6 +35,8 @@ class OrderFinish extends LogicService 'sn' => $order->sn, 'subject' => $order->subject, 'amount' => $order->amount, + 'create_time' => $order->create_time, + 'update_time' => $order->update_time, ], ]; diff --git a/app/Services/Logic/Notice/RefundFinish.php b/app/Services/Logic/Notice/RefundFinish.php index 468f5539..833cd6dc 100644 --- a/app/Services/Logic/Notice/RefundFinish.php +++ b/app/Services/Logic/Notice/RefundFinish.php @@ -31,10 +31,12 @@ class RefundFinish extends LogicService 'id' => $user->id, 'name' => $user->name, ], - 'order' => [ + 'refund' => [ 'sn' => $refund->sn, 'subject' => $refund->subject, 'amount' => $refund->amount, + 'create_time' => $refund->create_time, + 'update_time' => $refund->update_time, ], ]; @@ -66,7 +68,7 @@ class RefundFinish extends LogicService $task->item_id = $refund->id; $task->item_info = $itemInfo; - $task->item_type = TaskModel::TYPE_NOTICE_ORDER_FINISH; + $task->item_type = TaskModel::TYPE_NOTICE_REFUND_FINISH; $task->priority = TaskModel::PRIORITY_MIDDLE; $task->status = TaskModel::STATUS_PENDING; diff --git a/app/Services/Logic/Refund/RefundCreate.php b/app/Services/Logic/Refund/RefundCreate.php index bb67fcc9..a3332928 100644 --- a/app/Services/Logic/Refund/RefundCreate.php +++ b/app/Services/Logic/Refund/RefundCreate.php @@ -66,7 +66,11 @@ class RefundCreate extends Service * 设定延迟,给取消退款一个调解机会 */ $itemInfo = [ - 'refund' => $refund->toArray(), + 'refund' => [ + 'id' => $refund->id, + 'order_id' => $refund->order_id, + 'trade_id' => $refund->trade_id, + ], 'deadline' => time() + 3600 * 24 * 2, ]; diff --git a/app/Services/Wechat/Notice/AccountLogin.php b/app/Services/Wechat/Notice/AccountLogin.php index b4e901e7..4bf761bc 100644 --- a/app/Services/Wechat/Notice/AccountLogin.php +++ b/app/Services/Wechat/Notice/AccountLogin.php @@ -26,13 +26,17 @@ class AccountLogin extends WechatNotice $params['login_region']['city'], ]); - $loginTime = date('Y-m-d H:i:s', $params['login_time']); + $loginTime = date('Y-m-d H:i', $params['login_time']); + $loginUser = $params['user']['name']; + $loginIp = $params['login_ip']; $params = [ 'first' => $first, 'remark' => $remark, - 'keyword1' => $loginRegion, + 'keyword1' => $loginUser, 'keyword2' => $loginTime, + 'keyword3' => $loginRegion, + 'keyword4' => $loginIp, ]; $templateId = $this->getTemplateId($this->templateCode); diff --git a/app/Services/Wechat/Notice/ConsultReply.php b/app/Services/Wechat/Notice/ConsultReply.php index f33db7f9..41b7562f 100644 --- a/app/Services/Wechat/Notice/ConsultReply.php +++ b/app/Services/Wechat/Notice/ConsultReply.php @@ -24,7 +24,9 @@ class ConsultReply extends WechatNotice $params = [ 'first' => $first, 'remark' => $remark, - 'keyword1' => $params['course']['title'], + 'keyword1' => kg_substr($params['consult']['question'], 0, 50), + 'keyword2' => date('Y-m-d H:i', $params['consult']['create_time']), + 'keyword3' => kg_substr($params['consult']['answer'], 0, 50), ]; $templateId = $this->getTemplateId($this->templateCode); diff --git a/app/Services/Wechat/Notice/LiveBegin.php b/app/Services/Wechat/Notice/LiveBegin.php index 83077f4d..96ab8250 100644 --- a/app/Services/Wechat/Notice/LiveBegin.php +++ b/app/Services/Wechat/Notice/LiveBegin.php @@ -18,14 +18,16 @@ class LiveBegin extends WechatNotice public function handle(WechatSubscribeModel $subscribe, array $params) { $first = '你参与的课程直播就要开始了!'; - $remark = '如果没能参与直播,记得观看直播录像哦!'; + + $startTime = date('H:i', $params['live']['start_time']); + + $remark = sprintf('课程直播:%s 即将在 %s 开始, 千万不要错过哦!', $params['chapter']['title'], $startTime); $params = [ 'first' => $first, 'remark' => $remark, 'keyword1' => $params['course']['title'], - 'keyword2' => $params['chapter']['title'], - 'keyword3' => date('Y-m-d H:i', $params['live']['start_time']), + 'keyword2' => $params['user']['name'], ]; $templateId = $this->getTemplateId($this->templateCode); diff --git a/app/Services/Wechat/Notice/OrderFinish.php b/app/Services/Wechat/Notice/OrderFinish.php index c21ea2a9..c4179b39 100644 --- a/app/Services/Wechat/Notice/OrderFinish.php +++ b/app/Services/Wechat/Notice/OrderFinish.php @@ -24,9 +24,9 @@ class OrderFinish extends WechatNotice $params = [ 'first' => $first, 'remark' => $remark, - 'keyword1' => $params['order']['subject'], - 'keyword2' => $params['order']['sn'], - 'keyword3' => $params['order']['amount'], + 'keyword1' => sprintf('%s元', $params['order']['amount']), + 'keyword2' => $params['order']['subject'], + 'keyword3' => date('Y-m-d H:i', $params['order']['update_time']), ]; $templateId = $this->getTemplateId($this->templateCode); diff --git a/app/Services/Wechat/Notice/RefundFinish.php b/app/Services/Wechat/Notice/RefundFinish.php index 6b2030fe..5d63b220 100644 --- a/app/Services/Wechat/Notice/RefundFinish.php +++ b/app/Services/Wechat/Notice/RefundFinish.php @@ -23,9 +23,10 @@ class RefundFinish extends WechatNotice $params = [ 'first' => $first, 'remark' => $remark, - 'keyword1' => $params['refund']['subject'], - 'keyword2' => $params['refund']['sn'], - 'keyword3' => $params['refund']['amount'], + 'keyword1' => $params['refund']['sn'], + 'keyword2' => $params['refund']['subject'], + 'keyword3' => sprintf('%s元', $params['refund']['amount']), + 'keyword4' => date('Y-m-d H:i', $params['refund']['update_time']), ]; $templateId = $this->getTemplateId($this->templateCode);
登录提醒登录成功通知
订单通知购买成功提醒
退款通知退款成功通知
直播提醒课程直播提醒
回复通知咨询结果通知