From d1e0c21a9a7ec4e176f0fb30d77649afe12ead3d Mon Sep 17 00:00:00 2001 From: fthvgb1 Date: Sat, 2 Jun 2018 18:21:53 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B4=BB=E8=B7=83=E7=9A=84=E7=94=A8=E6=88=B7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Console/Commands/CalculateActiveUser.php | 44 +++++++++ app/Console/Kernel.php | 1 + app/Http/Controllers/CategoriesController.php | 6 +- app/Http/Controllers/TopicsController.php | 6 +- app/Models/Traits/ActiveUserHelper.php | 96 +++++++++++++++++++ app/Models/User.php | 2 + public/css/app.css | 19 ++++ resources/assets/sass/app.scss | 22 +++++ resources/views/topics/_sidebar.blade.php | 21 +++- storage/administrator_settings/site.json | 2 +- 10 files changed, 213 insertions(+), 6 deletions(-) create mode 100644 app/Console/Commands/CalculateActiveUser.php create mode 100644 app/Models/Traits/ActiveUserHelper.php diff --git a/app/Console/Commands/CalculateActiveUser.php b/app/Console/Commands/CalculateActiveUser.php new file mode 100644 index 0000000..547d556 --- /dev/null +++ b/app/Console/Commands/CalculateActiveUser.php @@ -0,0 +1,44 @@ +info('开始计算!'); + $user->calculateAndCacheActiveUsers(); + $this->info('生成成功!'); + } +} diff --git a/app/Console/Kernel.php b/app/Console/Kernel.php index 72fc593..f9e4410 100644 --- a/app/Console/Kernel.php +++ b/app/Console/Kernel.php @@ -26,6 +26,7 @@ class Kernel extends ConsoleKernel { // $schedule->command('inspire') // ->hourly(); + $schedule->command('larabbs:calculate-active-user')->hourly(); } /** diff --git a/app/Http/Controllers/CategoriesController.php b/app/Http/Controllers/CategoriesController.php index 861894c..1d4e35b 100644 --- a/app/Http/Controllers/CategoriesController.php +++ b/app/Http/Controllers/CategoriesController.php @@ -4,13 +4,15 @@ namespace App\Http\Controllers; use App\Models\Category; use App\Models\Topic; +use App\Models\User; use Illuminate\Http\Request; class CategoriesController extends Controller { - public function show(Category $category, Request $request) + public function show(Category $category, Request $request, User $user) { $topics = Topic::where('category_id', $category->id)->withOrder($request->order)->with('user', 'category')->paginate(); - return view('topics.index', compact('topics', 'category')); + $active_users = $user->getActiveUsers(); + return view('topics.index', compact('topics', 'category', 'active_users')); } } diff --git a/app/Http/Controllers/TopicsController.php b/app/Http/Controllers/TopicsController.php index f7d58d1..8ed2077 100644 --- a/app/Http/Controllers/TopicsController.php +++ b/app/Http/Controllers/TopicsController.php @@ -5,6 +5,7 @@ namespace App\Http\Controllers; use App\Http\Requests\TopicRequest; use App\Models\Category; use App\Models\Topic; +use App\Models\User; use App\Tools\ImageUploadTool; use Illuminate\Http\Request; use Illuminate\Support\Facades\Auth; @@ -22,10 +23,11 @@ class TopicsController extends Controller return view('topics.create_and_edit', compact('topic', 'categories')); } - public function index(Request $request, Topic $topic) + public function index(Request $request, Topic $topic, User $user) { + $active_users = $user->getActiveUsers(); $topics = $topic->with('user', 'category')->withOrder($request->order)->paginate(); - return view('topics.index', compact('topics')); + return view('topics.index', compact('topics', 'active_users')); } public function show(Topic $topic, Request $request) diff --git a/app/Models/Traits/ActiveUserHelper.php b/app/Models/Traits/ActiveUserHelper.php new file mode 100644 index 0000000..488e607 --- /dev/null +++ b/app/Models/Traits/ActiveUserHelper.php @@ -0,0 +1,96 @@ +cache_key, $this->cache_expire_in_minutes, function () { + return $this->calculateActiveUsers(); + }); + } + + public function calculateActiveUsers() + { + $this->calculateTopicScore(); + $this->calculateReplyScore(); + $users = array_sort($this->users, function ($user) { + return $user['score']; + }); + $users = array_reverse($users, true); + $users = array_slice($users, 0, $this->user_number, true); + $active_users = collect(); + foreach ($users as $user_id => $user) { + $user = $this->find($user_id); + if ($user) { + $active_users->push($user); + } + } + return $active_users; + } + + private function calculateTopicScore() + { + // 从话题数据表里取出限定时间范围($pass_days)内,有发表过话题的用户 + // 并且同时取出用户此段时间内发布话题的数量 + $topic_users = Topic::query()->select(\DB::raw('user_id, count(*) as topic_count')) + ->where('created_at', '>=', Carbon::now()->subDays($this->pass_days)) + ->groupBy('user_id') + ->get(); + // 根据话题数量计算得分 + foreach ($topic_users as $value) { + $this->users[$value->user_id]['score'] = $value->topic_count * $this->topic_weight; + } + } + + protected function calculateReplyScore() + { + $reply_users = Reply::query() + ->select(\DB::raw('user_id,count(*) as reply_count')) + ->where('created_at', '>=', Carbon::now()->subDays($this->pass_days)) + ->groupBy('user_id') + ->get(); + foreach ($reply_users as $value) { + $reply_score = $value->reply_count * $this->reply_weight; + if (isset($this->users[$value->user_id])) { + $this->users[$value->user_id]['score'] += $reply_score; + } else { + $this->users[$value->user_id]['score'] = $reply_score; + } + } + } + + public function calculateAndCacheActiveUsers() + { + $active_users = $this->calculateActiveUsers(); + $this->cacheActiveUsers($active_users); + } + + private function cacheActiveUsers($active_users) + { + Cache::put($this->cache_key, $active_users, $this->cache_expire_in_minutes); + } +} \ No newline at end of file diff --git a/app/Models/User.php b/app/Models/User.php index 16dfc99..c6ef919 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -2,6 +2,7 @@ namespace App\Models; +use App\Models\Traits\ActiveUserHelper; use Illuminate\Foundation\Auth\User as Authenticatable; use Illuminate\Notifications\Notifiable; use Illuminate\Support\Facades\Auth; @@ -13,6 +14,7 @@ class User extends Authenticatable notify as protected laravelNotify; } use HasRoles; + use ActiveUserHelper; /** * The attributes that are mass assignable. diff --git a/public/css/app.css b/public/css/app.css index ec8d177..36890ce 100644 --- a/public/css/app.css +++ b/public/css/app.css @@ -8457,6 +8457,25 @@ body { border-bottom: 1px solid #efefef; } +/** + sidebar + */ + +.sidebar .panel-body { + padding: 12px; +} + +.sidebar hr { + margin-top: 8px; + margin-bottom: 10px; +} + +.sidebar .active-users a.media { + color: #797878; + line-height: 26px; + font-size: 0.9em; +} + /* Topic Index Page */ .topics-index-page .topic-list .nav > li > a, diff --git a/resources/assets/sass/app.scss b/resources/assets/sass/app.scss index 2f593c8..5fff1da 100644 --- a/resources/assets/sass/app.scss +++ b/resources/assets/sass/app.scss @@ -112,6 +112,28 @@ body { } +/** + sidebar + */ +.sidebar { + .panel-body { + padding: 12px; + } + + hr { + margin-top: 8px; + margin-bottom: 10px; + } + + .active-users { + a.media { + color: #797878; + line-height: 26px; + font-size: 0.9em; + } + } +} + /* Topic Index Page */ .topics-index-page, .categories-show-page { .topic-list { diff --git a/resources/views/topics/_sidebar.blade.php b/resources/views/topics/_sidebar.blade.php index 968a897..8d47409 100644 --- a/resources/views/topics/_sidebar.blade.php +++ b/resources/views/topics/_sidebar.blade.php @@ -5,4 +5,23 @@ 新建帖子 - \ No newline at end of file + +@if(count($active_users)) +
+
+
活跃用户
+
+ @foreach($active_users as $active_user) + +
+ header +
+
+ {{$active_user->name}} +
+
+ @endforeach +
+
+@endif \ No newline at end of file diff --git a/storage/administrator_settings/site.json b/storage/administrator_settings/site.json index 743506a..ac8206e 100644 --- a/storage/administrator_settings/site.json +++ b/storage/administrator_settings/site.json @@ -1,6 +1,6 @@ { "site_name": "larabbs - Powered by LaraBBS", - "contact_email": "fthvgb1@163.com", + "contact_email": "fthvgb2@163.com", "seo_description": "laravel\u642d\u5efa\u7684bbs", "seo_keyword": "laravel,bbs,laravel\u793e\u533a" } \ No newline at end of file