Files
chatroom/app/Http/Controllers/LeaderboardController.php
lkddi 0c5e218aa8 功能:新增用户积分流水系统
- 新建 user_currency_logs 流水表 (Migration)
- App\Enums\CurrencySource 来源枚举(可扩展)
- App\Models\UserCurrencyLog 流水模型
- App\Services\UserCurrencyService 统一积分变更服务
- FishingController:抛竿/收竿接入流水记录
- AutoSaveExp:自动存点接入流水记录
- Admin/UserManagerController:管理员调整接入流水记录
- LeaderboardController:新增今日三榜(经验/金币/魅力)+ 个人流水日志页
- Admin/CurrencyStatsController:后台活动统计页
- views:新增个人日志页、后台统计页;排行榜新增今日榜数据传递
- routes:新增个人日志路由 /my/currency-logs、后台路由 /admin/currency-stats
2026-02-28 12:49:26 +08:00

120 lines
4.4 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?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',
'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'));
}
}