功能:新增用户积分流水系统
- 新建 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
This commit is contained in:
@@ -3,6 +3,7 @@
|
||||
/**
|
||||
* 文件功能:全局风云排行榜控制器
|
||||
* 各种维度(等级、经验、交友币、魅力)的前20名抓取与缓存展示。
|
||||
* 新增今日榜:显示今天经验成长、今日金币获得、今日魅力增长最多的用户。
|
||||
*
|
||||
* @author ChatRoom Laravel
|
||||
*
|
||||
@@ -12,26 +13,33 @@
|
||||
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
|
||||
{
|
||||
// 缓存 15 分钟,防止每秒几百个人看排行榜把数据库扫死
|
||||
// 选用 remember 则在过期时自动执行闭包查询并重置缓存
|
||||
$ttl = 60 * 15;
|
||||
|
||||
// 管理员等级阈值,排行榜中隐藏管理员
|
||||
$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')
|
||||
@@ -76,6 +84,36 @@ class LeaderboardController extends Controller
|
||||
->get();
|
||||
});
|
||||
|
||||
return view('leaderboard.index', compact('topLevels', 'topExp', 'topWealth', 'topCharm'));
|
||||
// ── 今日榜(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'));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user