141 lines
5.3 KiB
PHP
141 lines
5.3 KiB
PHP
<?php
|
||
|
||
/**
|
||
* 文件功能:全局风云排行榜控制器
|
||
* 各种维度(等级、经验、交友币、魅力)的前20名抓取与缓存展示。
|
||
* 新增今日榜:显示今天经验成长、今日金币获得、今日魅力增长最多的用户。
|
||
*
|
||
* @author ChatRoom Laravel
|
||
*
|
||
* @version 1.0.0
|
||
*/
|
||
|
||
namespace App\Http\Controllers;
|
||
|
||
use App\Models\User;
|
||
use App\Services\UserCurrencyService;
|
||
use Illuminate\Support\Facades\Cache;
|
||
use Illuminate\View\View;
|
||
|
||
class LeaderboardController extends Controller
|
||
{
|
||
/**
|
||
* 注入积分统计服务(用于今日榜单数据查询)
|
||
*/
|
||
public function __construct(
|
||
private readonly UserCurrencyService $currencyService,
|
||
) {}
|
||
|
||
/**
|
||
* 渲染排行榜主视角(包含累计榜 + 今日榜)
|
||
*/
|
||
public function index(): View
|
||
{
|
||
// 管理员等级阈值,排行榜中隐藏管理员
|
||
$superLevel = (int) \App\Models\Sysparam::getValue('superlevel', '100');
|
||
|
||
// 排行榜显示人数(后台可配置)
|
||
$topN = (int) \App\Models\Sysparam::getValue('leaderboard_limit', '20');
|
||
|
||
// ── 累计榜(15分钟缓存)──────────────────────────────
|
||
$ttl = 60 * 15;
|
||
|
||
// 1. 境界榜 (以 user_level 为尊)
|
||
$topLevels = Cache::remember('leaderboard:top_levels', $ttl, function () use ($superLevel, $topN) {
|
||
return User::select('id', 'username', 'usersf', 'user_level', 'sex')
|
||
->where('user_level', '>', 0)
|
||
->where('user_level', '<', $superLevel)
|
||
->orderByDesc('user_level')
|
||
->orderBy('id')
|
||
->limit($topN)
|
||
->get();
|
||
});
|
||
|
||
// 2. 修为榜 (以 exp_num 为尊)
|
||
$topExp = Cache::remember('leaderboard:top_exp', $ttl, function () use ($superLevel, $topN) {
|
||
return User::select('id', 'username', 'usersf', 'exp_num', 'sex', 'user_level')
|
||
->where('exp_num', '>', 0)
|
||
->where('user_level', '<', $superLevel)
|
||
->orderByDesc('exp_num')
|
||
->orderBy('id')
|
||
->limit($topN)
|
||
->get();
|
||
});
|
||
|
||
// 3. 财富榜 (以 jjb-交友币 为尊)
|
||
$topWealth = Cache::remember('leaderboard:top_wealth', $ttl, function () use ($superLevel, $topN) {
|
||
return User::select('id', 'username', 'usersf', 'jjb', 'sex', 'user_level')
|
||
->where('jjb', '>', 0)
|
||
->where('user_level', '<', $superLevel)
|
||
->orderByDesc('jjb')
|
||
->orderBy('id')
|
||
->limit($topN)
|
||
->get();
|
||
});
|
||
|
||
// 4. 魅力榜 (以 meili 为尊)
|
||
$topCharm = Cache::remember('leaderboard:top_charm', $ttl, function () use ($superLevel, $topN) {
|
||
return User::select('id', 'username', 'usersf', 'meili', 'sex', 'user_level')
|
||
->where('meili', '>', 0)
|
||
->where('user_level', '<', $superLevel)
|
||
->orderByDesc('meili')
|
||
->orderBy('id')
|
||
->limit($topN)
|
||
->get();
|
||
});
|
||
|
||
// ── 今日榜(5分钟缓存,数据来自 user_currency_logs 流水表)──
|
||
$todayTtl = 60 * 5;
|
||
$today = today()->toDateString();
|
||
|
||
$todayExp = Cache::remember("leaderboard:today_exp:{$today}", $todayTtl,
|
||
fn () => $this->currencyService->todayLeaderboard('exp', $topN, $today)
|
||
);
|
||
$todayGold = Cache::remember("leaderboard:today_gold:{$today}", $todayTtl,
|
||
fn () => $this->currencyService->todayLeaderboard('gold', $topN, $today)
|
||
);
|
||
$todayCharm = Cache::remember("leaderboard:today_charm:{$today}", $todayTtl,
|
||
fn () => $this->currencyService->todayLeaderboard('charm', $topN, $today)
|
||
);
|
||
|
||
return view('leaderboard.index', compact(
|
||
'topLevels', 'topExp', 'topWealth', 'topCharm',
|
||
));
|
||
}
|
||
|
||
/**
|
||
* 今日风云榜独立页(经验/金币/魅力今日排行)
|
||
*/
|
||
public function todayIndex(): View
|
||
{
|
||
$todayTtl = 60 * 5;
|
||
$today = today()->toDateString();
|
||
$topN = (int) \App\Models\Sysparam::getValue('leaderboard_limit', '20');
|
||
|
||
$todayExp = Cache::remember("leaderboard:today_exp:{$today}", $todayTtl,
|
||
fn () => $this->currencyService->todayLeaderboard('exp', $topN, $today)
|
||
);
|
||
$todayGold = Cache::remember("leaderboard:today_gold:{$today}", $todayTtl,
|
||
fn () => $this->currencyService->todayLeaderboard('gold', $topN, $today)
|
||
);
|
||
$todayCharm = Cache::remember("leaderboard:today_charm:{$today}", $todayTtl,
|
||
fn () => $this->currencyService->todayLeaderboard('charm', $topN, $today)
|
||
);
|
||
|
||
return view('leaderboard.today', compact('todayExp', 'todayGold', 'todayCharm'));
|
||
}
|
||
|
||
/**
|
||
* 用户个人流水日志页(查询自己的经验/金币/魅力操作历史)
|
||
*/
|
||
public function myLogs(): View
|
||
{
|
||
$user = auth()->user();
|
||
$currency = request('currency');
|
||
$days = (int) request('days', 7);
|
||
$logs = $this->currencyService->userLogs($user->id, $currency ?: null, $days);
|
||
|
||
return view('leaderboard.my-logs', compact('logs', 'user', 'currency', 'days'));
|
||
}
|
||
}
|