From 677b853f2082b4c72e30065fffe715fe1dd9f506 Mon Sep 17 00:00:00 2001 From: Summer Date: Fri, 20 Sep 2019 04:33:39 +0000 Subject: [PATCH] =?UTF-8?q?=E7=94=A8=E6=88=B7=E6=9C=80=E5=90=8E=E6=B4=BB?= =?UTF-8?q?=E8=B7=83=E6=97=B6=E9=97=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Console/Commands/SyncUserActivedAt.php | 18 ++++ app/Console/Kernel.php | 4 +- app/Http/Kernel.php | 4 + app/Http/Middleware/RecordLastActivedTime.php | 20 +++++ app/Models/Traits/LastActivedAtHelper.php | 84 +++++++++++++++++++ app/Models/User.php | 1 + ...036_add_last_actived_at_to_users_table.php | 22 +++++ resources/views/users/show.blade.php | 6 +- 8 files changed, 155 insertions(+), 4 deletions(-) create mode 100644 app/Console/Commands/SyncUserActivedAt.php create mode 100644 app/Http/Middleware/RecordLastActivedTime.php create mode 100644 app/Models/Traits/LastActivedAtHelper.php create mode 100644 database/migrations/2019_09_20_123036_add_last_actived_at_to_users_table.php diff --git a/app/Console/Commands/SyncUserActivedAt.php b/app/Console/Commands/SyncUserActivedAt.php new file mode 100644 index 000000000..4b181d705 --- /dev/null +++ b/app/Console/Commands/SyncUserActivedAt.php @@ -0,0 +1,18 @@ +syncUserActivedAt(); + $this->info("同步成功!"); + } +} diff --git a/app/Console/Kernel.php b/app/Console/Kernel.php index abcf92dff..e20b188f7 100644 --- a/app/Console/Kernel.php +++ b/app/Console/Kernel.php @@ -27,9 +27,11 @@ protected function schedule(Schedule $schedule) // $schedule->command('inspire') // ->hourly(); - // 一小时执行一次『活跃用户』数据生成的命令 $schedule->command('larabbs:calculate-active-user')->hourly(); + + // 每日零时执行一次 + $schedule->command('larabbs:sync-user-actived-at')->dailyAt('00:00'); } /** diff --git a/app/Http/Kernel.php b/app/Http/Kernel.php index eeaa58405..838955dc1 100644 --- a/app/Http/Kernel.php +++ b/app/Http/Kernel.php @@ -37,6 +37,10 @@ class Kernel extends HttpKernel \Illuminate\Routing\Middleware\SubstituteBindings::class, \App\Http\Middleware\EnsureEmailIsVerified::class, // <<--- 只需添加这一行 + + + // 记录用户最后活跃时间 + \App\Http\Middleware\RecordLastActivedTime::class, ], 'api' => [ diff --git a/app/Http/Middleware/RecordLastActivedTime.php b/app/Http/Middleware/RecordLastActivedTime.php new file mode 100644 index 000000000..711f5a18a --- /dev/null +++ b/app/Http/Middleware/RecordLastActivedTime.php @@ -0,0 +1,20 @@ +recordLastActivedAt(); + } + + return $next($request); + } +} diff --git a/app/Models/Traits/LastActivedAtHelper.php b/app/Models/Traits/LastActivedAtHelper.php new file mode 100644 index 000000000..222164803 --- /dev/null +++ b/app/Models/Traits/LastActivedAtHelper.php @@ -0,0 +1,84 @@ +getHashFromDateString(Carbon::now()->toDateString()); + + // 字段名称,如:user_1 + $field = $this->getHashField(); + + // 当前时间,如:2017-10-21 08:35:15 + $now = Carbon::now()->toDateTimeString(); + + // 数据写入 Redis ,字段已存在会被更新 + Redis::hSet($hash, $field, $now); + } + + public function syncUserActivedAt() + { + // 获取昨日的哈希表名称,如:larabbs_last_actived_at_2017-10-21 + $hash = $this->getHashFromDateString(Carbon::yesterday()->toDateString()); + + // 从 Redis 中获取所有哈希表里的数据 + $dates = Redis::hGetAll($hash); + + // 遍历,并同步到数据库中 + foreach ($dates as $user_id => $actived_at) { + // 会将 `user_1` 转换为 1 + $user_id = str_replace($this->field_prefix, '', $user_id); + + // 只有当用户存在时才更新到数据库中 + if ($user = $this->find($user_id)) { + $user->last_actived_at = $actived_at; + $user->save(); + } + } + + // 以数据库为中心的存储,既已同步,即可删除 + Redis::del($hash); + } + + public function getLastActivedAtAttribute($value) + { + // 获取今日对应的哈希表名称 + $hash = $this->getHashFromDateString(Carbon::now()->toDateString()); + + // 字段名称,如:user_1 + $field = $this->getHashField(); + + // 三元运算符,优先选择 Redis 的数据,否则使用数据库中 + $datetime = Redis::hGet($hash, $field) ? : $value; + + // 如果存在的话,返回时间对应的 Carbon 实体 + if ($datetime) { + return new Carbon($datetime); + } else { + // 否则使用用户注册时间 + return $this->created_at; + } + } + + public function getHashFromDateString($date) + { + // Redis 哈希表的命名,如:larabbs_last_actived_at_2017-10-21 + return $this->hash_prefix . $date; + } + + public function getHashField() + { + // 字段名称,如:user_1 + return $this->field_prefix . $this->id; + } +} diff --git a/app/Models/User.php b/app/Models/User.php index b151c12fa..4a72ed82b 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -15,6 +15,7 @@ class User extends Authenticatable implements MustVerifyEmailContract use HasRoles; use MustVerifyEmailTrait; use Traits\ActiveUserHelper; + use Traits\LastActivedAtHelper; use Notifiable { notify as protected laravelNotify; diff --git a/database/migrations/2019_09_20_123036_add_last_actived_at_to_users_table.php b/database/migrations/2019_09_20_123036_add_last_actived_at_to_users_table.php new file mode 100644 index 000000000..3d499119d --- /dev/null +++ b/database/migrations/2019_09_20_123036_add_last_actived_at_to_users_table.php @@ -0,0 +1,22 @@ +timestamp('last_actived_at')->nullable(); + }); + } + + public function down() + { + Schema::table('users', function (Blueprint $table) { + $table->dropColumn('last_actived_at'); + }); + } +} diff --git a/resources/views/users/show.blade.php b/resources/views/users/show.blade.php index d248d3435..a63cd3f18 100644 --- a/resources/views/users/show.blade.php +++ b/resources/views/users/show.blade.php @@ -10,11 +10,11 @@
{{ $user->name }}
-
个人简介
-

{{ $user->introduction }}

-
注册于

{{ $user->created_at->diffForHumans() }}

+
+
最后活跃
+

{{ $user->last_actived_at->diffForHumans() }}