From d8c5a3d0a2492302024fe6aedb64da6d3f78bee7 Mon Sep 17 00:00:00 2001 From: koogua Date: Fri, 21 Oct 2022 17:55:31 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E9=80=9A=E7=9F=A5=E8=AE=A1?= =?UTF-8?q?=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Models/Notification.php | 12 ++++++++++++ app/Models/User.php | 7 +++++++ .../Logic/User/Console/NotifyStats.php | 12 +----------- db/migrations/20221021035953.php | 19 +++++++++++++++++++ public/static/home/js/common.js | 4 ++-- 5 files changed, 41 insertions(+), 13 deletions(-) diff --git a/app/Models/Notification.php b/app/Models/Notification.php index 675472a8..8f589960 100644 --- a/app/Models/Notification.php +++ b/app/Models/Notification.php @@ -198,4 +198,16 @@ class Notification extends Model } } + public function afterCreate() + { + /** + * @var $user User + */ + $user = User::findFirst($this->receiver_id); + + $user->notice_count += 1; + + $user->update(); + } + } diff --git a/app/Models/User.php b/app/Models/User.php index 298c8958..05ba6c31 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -154,6 +154,13 @@ class User extends Model */ public $favorite_count = 0; + /** + * 通知数 + * + * @var int + */ + public $notice_count = 0; + /** * 会员期限 * diff --git a/app/Services/Logic/User/Console/NotifyStats.php b/app/Services/Logic/User/Console/NotifyStats.php index 0d7a2483..23df3d54 100644 --- a/app/Services/Logic/User/Console/NotifyStats.php +++ b/app/Services/Logic/User/Console/NotifyStats.php @@ -7,7 +7,6 @@ namespace App\Services\Logic\User\Console; -use App\Repos\User as UserRepo; use App\Services\Logic\Service as LogicService; class NotifyStats extends LogicService @@ -17,16 +16,7 @@ class NotifyStats extends LogicService { $user = $this->getLoginUser(); - $noticeCount = $this->getNoticeCount($user->id); - - return ['notice_count' => $noticeCount]; - } - - protected function getNoticeCount($userId) - { - $userRepo = new UserRepo(); - - return $userRepo->countUnreadNotifications($userId); + return ['notice_count' => $user->notice_count]; } } diff --git a/db/migrations/20221021035953.php b/db/migrations/20221021035953.php index 4c7c79a1..f9498200 100644 --- a/db/migrations/20221021035953.php +++ b/db/migrations/20221021035953.php @@ -15,6 +15,7 @@ final class V20221021035953 extends AbstractMigration { $this->alterPageTable(); $this->alterHelpTable(); + $this->alterUserTable(); } protected function alterPageTable() @@ -53,4 +54,22 @@ final class V20221021035953 extends AbstractMigration $table->save(); } + protected function alterUserTable() + { + $table = $this->table('kg_user'); + + if ($table->hasColumn('notice_count') == false) { + $table->addColumn('notice_count', 'integer', [ + 'null' => false, + 'default' => '0', + 'limit' => MysqlAdapter::INT_REGULAR, + 'signed' => false, + 'comment' => '通知数', + 'after' => 'favorite_count', + ]); + } + + $table->save(); + } + } diff --git a/public/static/home/js/common.js b/public/static/home/js/common.js index bdceab89..67d84753 100644 --- a/public/static/home/js/common.js +++ b/public/static/home/js/common.js @@ -48,7 +48,7 @@ layui.use(['jquery', 'form', 'element', 'layer', 'helper'], function () { $token.attr('content', res.token); } }); - }, 300000); + }, 600000); if (window.user.id > 0) { setInterval(function () { @@ -60,7 +60,7 @@ layui.use(['jquery', 'form', 'element', 'layer', 'helper'], function () { $notifyDot.removeClass('layui-badge-dot'); } }); - }, 30000); + }, 60000); setInterval(function () { $.post('/uc/online'); }, 60000);