新增游戏管理系统:①game_configs表+模型(forGame/isEnabled/param静态方法) ②GameConfigSeeder初始化5款游戏参数 ③后台卡片式管理页(开关+参数表单) ④侧边栏菜单「游戏管理」
This commit is contained in:
@@ -0,0 +1,72 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* 文件功能:游戏配置后台管理控制器
|
||||
*
|
||||
* 管理员可在此页面统一管理所有娱乐游戏的开关状态和核心参数。
|
||||
* 每个游戏的参数说明通过前端渲染,后台只做通用 JSON 存储。
|
||||
*
|
||||
* @author ChatRoom Laravel
|
||||
*
|
||||
* @version 1.0.0
|
||||
*/
|
||||
|
||||
namespace App\Http\Controllers\Admin;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Models\GameConfig;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\RedirectResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\View\View;
|
||||
|
||||
class GameConfigController extends Controller
|
||||
{
|
||||
/**
|
||||
* 游戏管理总览页面。
|
||||
*/
|
||||
public function index(): View
|
||||
{
|
||||
$games = GameConfig::orderBy('id')->get();
|
||||
|
||||
return view('admin.game-configs.index', compact('games'));
|
||||
}
|
||||
|
||||
/**
|
||||
* 切换游戏开启/关闭状态。
|
||||
*/
|
||||
public function toggle(GameConfig $gameConfig): JsonResponse
|
||||
{
|
||||
$gameConfig->update(['enabled' => ! $gameConfig->enabled]);
|
||||
$gameConfig->clearCache();
|
||||
|
||||
return response()->json([
|
||||
'ok' => true,
|
||||
'enabled' => $gameConfig->enabled,
|
||||
'message' => $gameConfig->enabled
|
||||
? "「{$gameConfig->name}」已开启"
|
||||
: "「{$gameConfig->name}」已关闭",
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 保存游戏核心参数。
|
||||
*
|
||||
* 接收前端提交的 params JSON 对象并合并至现有配置。
|
||||
*/
|
||||
public function updateParams(Request $request, GameConfig $gameConfig): RedirectResponse
|
||||
{
|
||||
$request->validate([
|
||||
'params' => 'required|array',
|
||||
]);
|
||||
|
||||
// 合并参数,保留已有键,只更新传入的键
|
||||
$current = $gameConfig->params ?? [];
|
||||
$updated = array_merge($current, $request->input('params'));
|
||||
|
||||
$gameConfig->update(['params' => $updated]);
|
||||
$gameConfig->clearCache();
|
||||
|
||||
return back()->with('success', "「{$gameConfig->name}」参数已保存!");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,80 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* 文件功能:游戏配置模型
|
||||
*
|
||||
* 集中管理所有娱乐游戏的开关与参数,提供统一的读取接口。
|
||||
* 游戏服务层通过 GameConfig::forGame($key) 读取当前配置。
|
||||
*
|
||||
* @author ChatRoom Laravel
|
||||
*
|
||||
* @version 1.0.0
|
||||
*/
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
|
||||
class GameConfig extends Model
|
||||
{
|
||||
protected $fillable = [
|
||||
'game_key',
|
||||
'name',
|
||||
'icon',
|
||||
'description',
|
||||
'enabled',
|
||||
'params',
|
||||
];
|
||||
|
||||
/**
|
||||
* 属性类型转换。
|
||||
*/
|
||||
protected function casts(): array
|
||||
{
|
||||
return [
|
||||
'enabled' => 'boolean',
|
||||
'params' => 'array',
|
||||
];
|
||||
}
|
||||
|
||||
// ─── 静态工具方法 ─────────────────────────────────────────────────
|
||||
|
||||
/**
|
||||
* 根据游戏标识获取配置(带缓存)。
|
||||
*
|
||||
* @param string $key 游戏标识,如 'baccarat'
|
||||
*/
|
||||
public static function forGame(string $key): ?static
|
||||
{
|
||||
return Cache::remember("game_config:{$key}", 60, fn () => static::where('game_key', $key)->first());
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断游戏是否已开启。
|
||||
*/
|
||||
public static function isEnabled(string $key): bool
|
||||
{
|
||||
return (bool) static::forGame($key)?->enabled;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取指定游戏的某个参数值,不存在时返回默认值。
|
||||
*
|
||||
* @param string $key 游戏标识
|
||||
* @param string $param 参数名
|
||||
* @param mixed $default 默认值
|
||||
*/
|
||||
public static function param(string $key, string $param, mixed $default = null): mixed
|
||||
{
|
||||
return static::forGame($key)?->params[$param] ?? $default;
|
||||
}
|
||||
|
||||
/**
|
||||
* 清除游戏配置缓存。
|
||||
*/
|
||||
public function clearCache(): void
|
||||
{
|
||||
Cache::forget("game_config:{$this->game_key}");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user