重构猜谜活动并统一聊天室答题通知

This commit is contained in:
pllx
2026-04-29 13:35:20 +08:00
parent 192259f0a4
commit fe3e74b5f8
34 changed files with 3369 additions and 1833 deletions
@@ -3,6 +3,11 @@
@section('title', '游戏管理')
@section('content')
@php
$riddleTypeOptions = \App\Models\Riddle::typeOptions();
$availableRooms = \App\Models\Room::orderBy('id')->get();
@endphp
<div class="space-y-6">
{{-- 页头 --}}
@@ -82,7 +87,8 @@
{{-- 参数配置区域 --}}
<div class="p-5">
<form action="{{ route('admin.game-configs.params', $game) }}" method="POST">
<form action="{{ route('admin.game-configs.params', $game) }}" method="POST"
@if ($game->game_key === 'idiom') data-idiom-config-form @endif>
@csrf
@php
@@ -95,53 +101,140 @@
$paramKeys = array_values(array_filter($paramKeys, fn ($key) => ! in_array($key, $hiddenLegacyKeys, true)));
@endphp
<div class="grid grid-cols-2 md:grid-cols-3 lg:grid-cols-4 gap-4">
@foreach ($paramKeys as $paramKey)
@php
$paramValue = $params[$paramKey] ?? ($labels[$paramKey]['default'] ?? '');
if ($game->game_key === 'mystery_box') {
$legacyFallbackMap = [
'normal_reward_min' => 'min_reward',
'normal_reward_max' => 'max_reward',
'rare_reward_min' => 'rare_min_reward',
'rare_reward_max' => 'rare_max_reward',
];
if (($paramValue === '' || $paramValue === null) && isset($legacyFallbackMap[$paramKey])) {
$paramValue = $params[$legacyFallbackMap[$paramKey]] ?? $paramValue;
}
}
@endphp
@php $meta = $labels[$paramKey] ?? ['label' => $paramKey, 'type' => 'number', 'unit' => ''] @endphp
<div>
<label class="block text-xs font-bold text-gray-600 mb-1">
{{ $meta['label'] }}
@if ($meta['unit'])
<span class="font-normal text-gray-400">{{ $meta['unit'] }}</span>
@endif
</label>
@if ($meta['type'] === 'boolean')
<select name="params[{{ $paramKey }}]"
class="w-full border border-gray-300 rounded-lg p-2 text-sm focus:border-indigo-400">
<option value="1" {{ $paramValue ? 'selected' : '' }}></option>
<option value="0" {{ !$paramValue ? 'selected' : '' }}></option>
</select>
@elseif ($meta['type'] === 'array')
<input type="text" name="params[{{ $paramKey }}]"
value="{{ implode(',', (array) $paramValue) }}"
class="w-full border border-gray-300 rounded-lg p-2 text-sm focus:border-indigo-400"
placeholder="多个值用逗号分隔">
@else
<input type="{{ $meta['type'] }}" name="params[{{ $paramKey }}]"
value="{{ $paramValue }}" step="{{ $meta['step'] ?? 1 }}"
min="{{ $meta['min'] ?? 0 }}"
class="w-full border border-gray-300 rounded-lg p-2 text-sm focus:border-indigo-400">
@endif
@if ($game->game_key === 'idiom')
@php
$sharedConfig = gameRiddleSharedConfig($params);
$checkedRoomIds = collect($sharedConfig['room_ids'])->map(fn ($roomId) => (int) $roomId)->all();
@endphp
<div class="space-y-4">
<div class="rounded-xl border border-indigo-100 bg-indigo-50/60 p-4 text-xs leading-6 text-indigo-700">
猜成语与脑筋急转弯共用同一套奖励、过期时间、自动出题间隔与参与房间范围配置。
手动出题时再单独选择题型即可。
</div>
@endforeach
</div>
<div class="rounded-2xl border border-slate-200 bg-slate-50/70 p-4" data-idiom-config-card>
<div class="mb-4 flex flex-wrap items-center justify-between gap-3">
<div>
<div class="text-sm font-bold text-slate-800">猜谜活动公共设置</div>
<div class="text-xs text-slate-500">以下参数会同时作用于猜成语与脑筋急转弯。</div>
</div>
<div class="flex flex-wrap items-center gap-2">
@foreach ($riddleTypeOptions as $typeLabel)
<span class="rounded-full bg-white px-3 py-1 text-xs font-semibold text-slate-500 shadow-sm">{{ $typeLabel }}</span>
@endforeach
</div>
</div>
<div class="grid grid-cols-1 gap-4 md:grid-cols-2 xl:grid-cols-3">
<div>
<label class="mb-1 block text-xs font-bold text-gray-600">答对奖励金币</label>
<input type="number" name="params[reward_gold]"
value="{{ old('params.reward_gold', $sharedConfig['reward_gold']) }}"
min="0"
class="w-full rounded-lg border border-gray-300 p-2 text-sm focus:border-indigo-400">
</div>
<div>
<label class="mb-1 block text-xs font-bold text-gray-600">答对奖励经验</label>
<input type="number" name="params[reward_exp]"
value="{{ old('params.reward_exp', $sharedConfig['reward_exp']) }}"
min="0"
class="w-full rounded-lg border border-gray-300 p-2 text-sm focus:border-indigo-400">
</div>
<div>
<label class="mb-1 block text-xs font-bold text-gray-600">题目过期时间</label>
<input type="number" name="params[expire_minutes]"
value="{{ old('params.expire_minutes', $sharedConfig['expire_minutes']) }}"
min="0"
class="w-full rounded-lg border border-gray-300 p-2 text-sm focus:border-indigo-400">
<p class="mt-1 text-xs text-gray-400">分钟,0 表示不过期。</p>
</div>
<div>
<label class="mb-1 block text-xs font-bold text-gray-600">自动出题间隔</label>
<input type="number" name="params[auto_start_interval]"
value="{{ old('params.auto_start_interval', $sharedConfig['auto_start_interval']) }}"
min="0"
class="w-full rounded-lg border border-gray-300 p-2 text-sm focus:border-indigo-400">
<p class="mt-1 text-xs text-gray-400">分钟,0 表示仅手动出题。</p>
</div>
<div>
<label class="mb-1 block text-xs font-bold text-gray-600">参与房间模式</label>
<select name="params[room_scope_mode]"
data-idiom-room-mode
class="w-full rounded-lg border border-gray-300 p-2 text-sm focus:border-indigo-400">
<option value="all" @selected($sharedConfig['room_mode'] === 'all')>全部房间</option>
<option value="single" @selected($sharedConfig['room_mode'] === 'single')>单选房间</option>
<option value="multiple" @selected($sharedConfig['room_mode'] === 'multiple')>多选房间</option>
</select>
</div>
<div class="xl:col-span-2">
<label class="mb-2 block text-xs font-bold text-gray-600" data-idiom-room-label>参与房间</label>
<div class="grid grid-cols-2 gap-2 rounded-xl border border-dashed border-slate-200 bg-white p-3 lg:grid-cols-3">
@foreach ($availableRooms as $room)
<label class="flex items-center gap-2 rounded-lg border border-slate-100 px-3 py-2 text-sm text-slate-600">
<input type="checkbox"
name="params[room_ids][]"
value="{{ $room->id }}"
@checked(in_array((int) $room->id, $checkedRoomIds, true))
data-idiom-room-checkbox
class="rounded border-slate-300 text-indigo-600">
<span>#{{ $room->id }} {{ $room->name }}</span>
</label>
@endforeach
</div>
<p class="mt-2 text-xs text-gray-400">单选模式下只保留一个房间,多选模式可同时勾选多个房间。</p>
</div>
</div>
</div>
</div>
@else
<div class="grid grid-cols-2 gap-4 md:grid-cols-3 lg:grid-cols-4">
@foreach ($paramKeys as $paramKey)
@php
$paramValue = $params[$paramKey] ?? ($labels[$paramKey]['default'] ?? '');
if ($game->game_key === 'mystery_box') {
$legacyFallbackMap = [
'normal_reward_min' => 'min_reward',
'normal_reward_max' => 'max_reward',
'rare_reward_min' => 'rare_min_reward',
'rare_reward_max' => 'rare_max_reward',
];
if (($paramValue === '' || $paramValue === null) && isset($legacyFallbackMap[$paramKey])) {
$paramValue = $params[$legacyFallbackMap[$paramKey]] ?? $paramValue;
}
}
@endphp
@php $meta = $labels[$paramKey] ?? ['label' => $paramKey, 'type' => 'number', 'unit' => ''] @endphp
<div>
<label class="mb-1 block text-xs font-bold text-gray-600">
{{ $meta['label'] }}
@if ($meta['unit'])
<span class="font-normal text-gray-400">{{ $meta['unit'] }}</span>
@endif
</label>
@if ($meta['type'] === 'boolean')
<select name="params[{{ $paramKey }}]"
class="w-full rounded-lg border border-gray-300 p-2 text-sm focus:border-indigo-400">
<option value="1" {{ $paramValue ? 'selected' : '' }}></option>
<option value="0" {{ !$paramValue ? 'selected' : '' }}></option>
</select>
@elseif ($meta['type'] === 'array')
<input type="text" name="params[{{ $paramKey }}]"
value="{{ implode(',', (array) $paramValue) }}"
class="w-full rounded-lg border border-gray-300 p-2 text-sm focus:border-indigo-400"
placeholder="多个值用逗号分隔">
@else
<input type="{{ $meta['type'] }}" name="params[{{ $paramKey }}]"
value="{{ $paramValue }}" step="{{ $meta['step'] ?? 1 }}"
min="{{ $meta['min'] ?? 0 }}"
class="w-full rounded-lg border border-gray-300 p-2 text-sm focus:border-indigo-400">
@endif
</div>
@endforeach
</div>
@endif
<div class="mt-4 flex items-center gap-3">
<button type="submit"
@@ -152,6 +245,32 @@
</div>
</form>
@if ($game->game_key === 'idiom')
<div class="mt-4 border-t border-gray-100 pt-4">
<div class="mb-3 text-xs font-bold text-gray-600">🧩 手动出题</div>
<div class="flex flex-wrap items-center gap-3">
<select id="idiom-manual-room"
class="rounded-lg border border-gray-300 px-3 py-2 text-sm text-slate-700">
@foreach ($availableRooms as $room)
<option value="{{ $room->id }}">#{{ $room->id }} {{ $room->name }}</option>
@endforeach
</select>
<select id="idiom-manual-type"
class="rounded-lg border border-gray-300 px-3 py-2 text-sm text-slate-700">
@foreach ($riddleTypeOptions as $typeKey => $typeLabel)
<option value="{{ $typeKey }}">{{ $typeLabel }}</option>
@endforeach
</select>
<button type="button" id="idiom-manual-start-btn"
data-idiom-start-url="{{ route('riddle-quiz.start') }}"
class="rounded-lg bg-gradient-to-r from-purple-600 to-indigo-600 px-4 py-2 text-sm font-bold text-white transition hover:opacity-90">
立即出题
</button>
<span class="text-xs text-gray-400">先选房间,再选题型,后台会按对应题型配置发题。</span>
</div>
</div>
@endif
{{-- 神秘箱子:手动投放区域 --}}
@if ($game->game_key === 'mystery_box')
<div class="mt-4 pt-4 border-t border-gray-100">
@@ -416,4 +535,136 @@
default => [],
};
}
/**
* 解析猜谜活动公共配置,并兼容旧版题型拆分配置。
*
* @return array{reward_gold:int,reward_exp:int,expire_minutes:int,auto_start_interval:int,room_mode:string,room_ids:array<int, int>}
*/
function gameRiddleSharedConfig(array $params): array
{
$fallbackTypeConfig = collect((array) ($params['type_configs'] ?? []))
->first(fn ($typeConfig) => is_array($typeConfig) && $typeConfig !== [], []);
$roomMode = (string) ($params['room_scope_mode'] ?? ($fallbackTypeConfig['room_mode'] ?? 'single'));
if (! in_array($roomMode, ['all', 'single', 'multiple'], true)) {
$roomMode = 'single';
}
return [
'reward_gold' => max(0, (int) ($params['reward_gold'] ?? ($fallbackTypeConfig['reward_gold'] ?? 50))),
'reward_exp' => max(0, (int) ($params['reward_exp'] ?? ($fallbackTypeConfig['reward_exp'] ?? 30))),
'expire_minutes' => max(0, (int) ($params['expire_minutes'] ?? ($fallbackTypeConfig['expire_minutes'] ?? 5))),
'auto_start_interval' => max(0, (int) ($params['auto_start_interval'] ?? ($fallbackTypeConfig['auto_start_interval'] ?? 0))),
'room_mode' => $roomMode,
'room_ids' => collect((array) ($params['room_ids'] ?? ($fallbackTypeConfig['room_ids'] ?? [])))
->map(fn ($roomId) => (int) $roomId)
->filter(fn ($roomId) => $roomId > 0)
->unique()
->values()
->all(),
];
}
@endphp
@push('scripts')
<script>
document.addEventListener('DOMContentLoaded', function () {
function showAdminAlert(message, title = '提示', icon = '️') {
if (window.adminDialog?.alert) {
window.adminDialog.alert(message, title, icon);
return;
}
window.alert(message);
}
document.querySelectorAll('[data-idiom-config-form]').forEach(function (form) {
form.addEventListener('submit', function (event) {
let hasValidationError = false;
const modeSelect = this.querySelector('[data-idiom-room-mode]');
const roomCheckboxes = Array.from(this.querySelectorAll('[data-idiom-room-checkbox]'));
const checkedRooms = roomCheckboxes.filter(function (checkbox) {
return checkbox.checked;
});
if (modeSelect) {
if (modeSelect.value === 'single' && checkedRooms.length > 1) {
showAdminAlert('猜谜活动处于单选房间模式时,只能勾选一个房间。', '房间选择有误', '⚠️');
hasValidationError = true;
}
if (modeSelect.value === 'single' && checkedRooms.length === 0) {
const firstRoomCheckbox = roomCheckboxes[0];
if (firstRoomCheckbox) {
firstRoomCheckbox.checked = true;
}
}
if (modeSelect.value === 'multiple' && checkedRooms.length === 0) {
showAdminAlert('猜谜活动处于多选房间模式时,请至少选择一个房间。', '房间选择有误', '⚠️');
hasValidationError = true;
}
}
if (hasValidationError) {
event.preventDefault();
}
});
});
const manualStartButton = document.getElementById('idiom-manual-start-btn');
if (!manualStartButton) {
return;
}
manualStartButton.addEventListener('click', function () {
const startUrl = this.getAttribute('data-idiom-start-url') || '';
const roomId = Number.parseInt(document.getElementById('idiom-manual-room')?.value || '0', 10);
const quizType = document.getElementById('idiom-manual-type')?.value || '';
if (!startUrl || roomId <= 0 || !quizType) {
showAdminAlert('请先选择房间和题型。', '手动出题', '⚠️');
return;
}
const button = this;
button.disabled = true;
button.textContent = '出题中...';
fetch(startUrl, {
method: 'POST',
headers: {
'X-CSRF-TOKEN': document.querySelector('meta[name="csrf-token"]')?.content ?? '',
'Content-Type': 'application/json',
'Accept': 'application/json',
},
body: JSON.stringify({
room_id: roomId,
quiz_type: quizType,
}),
})
.then(function (response) {
return response.json();
})
.then(function (response) {
if (response.status === 'success') {
showAdminAlert('题目已发送到目标房间。', '手动出题成功', '✅');
return;
}
showAdminAlert(response.message || '出题失败', '手动出题失败', '❌');
})
.catch(function () {
showAdminAlert('网络错误,出题失败', '手动出题失败', '🌐');
})
.finally(function () {
button.disabled = false;
button.textContent = '立即出题';
});
});
});
</script>
@endpush
@@ -1,338 +0,0 @@
@extends('admin.layouts.app')
@section('title', '猜成语题库管理')
@section('content')
@php require resource_path('views/admin/partials/list-theme.php'); @endphp
@php
$idiomPayload = $idioms->mapWithKeys(
fn($item) => [
(string) $item->id => [
'id' => $item->id,
'answer' => $item->answer,
'hint' => $item->hint,
'sort' => $item->sort,
'is_active' => (bool) $item->is_active,
'update_url' => route('admin.idioms.update', $item->id),
'toggle_url' => route('admin.idioms.toggle', $item->id),
],
],
);
$idiomConfig = \App\Models\GameConfig::forGame('idiom');
$idiomParams = $idiomConfig?->params ?? [];
@endphp
<script type="application/json" id="admin-idioms-data">@json($idiomPayload)</script>
<div class="{{ $adminListPageClass }}">
{{-- 页头 --}}
<div class="{{ $adminListHeaderCardClass }}">
<div>
<h2 class="{{ $adminListHeaderTitleClass }}">🧩 猜成语题库管理</h2>
<p class="{{ $adminListHeaderSubtitleClass }}">
管理猜成语游戏的题目库,共 <strong class="text-indigo-600">{{ $idioms->count() }}</strong> 条题目
</p>
</div>
</div>
{{-- 游戏参数 + 出题 --}}
<div class="{{ $adminListCardClass }}">
<div class="{{ $adminListSectionHeadClass }}">
<h3 class="{{ $adminListSectionTitleClass }}">⚙️ 游戏参数</h3>
</div>
<form action="{{ route('admin.idioms.settings.save') }}" method="POST" class="p-5">
@csrf
<div class="grid grid-cols-1 md:grid-cols-3 gap-4">
<div>
<label class="{{ $adminListFilterLabelClass }}">答对奖励金币</label>
<input type="number" name="reward_gold"
value="{{ old('reward_gold', $idiomParams['reward_gold'] ?? 50) }}" min="0"
class="w-full {{ $adminListFilterInputClass }}">
</div>
<div>
<label class="{{ $adminListFilterLabelClass }}">答对奖励经验</label>
<input type="number" name="reward_exp"
value="{{ old('reward_exp', $idiomParams['reward_exp'] ?? 30) }}" min="0"
class="w-full {{ $adminListFilterInputClass }}">
</div>
<div>
<label class="{{ $adminListFilterLabelClass }}">自动出题间隔(分钟)</label>
<input type="number" name="auto_start_interval"
value="{{ old('auto_start_interval', $idiomParams['auto_start_interval'] ?? 0) }}" min="0"
class="w-full {{ $adminListFilterInputClass }}">
<p class="text-xs text-gray-400 mt-1">0=仅手动出题</p>
</div>
<div>
<label class="{{ $adminListFilterLabelClass }}">题目过期时间(分钟)</label>
<input type="number" name="expire_minutes"
value="{{ old('expire_minutes', $idiomParams['expire_minutes'] ?? 5) }}" min="0"
class="w-full {{ $adminListFilterInputClass }}">
<p class="text-xs text-gray-400 mt-1">0=不过期;大于 0 时超时会自动公布答案并结束回合</p>
</div>
</div>
<div class="mt-4 flex items-center gap-4">
<button type="submit" class="{{ $adminListPrimaryButtonClass }}">
💾 保存参数
</button>
<span class="text-sm text-gray-400">|</span>
<label class="text-sm text-gray-600">选择房间:</label>
<select id="idiom-start-room" class="border border-gray-300 rounded-lg px-3 py-1.5 text-sm">
@foreach (\App\Models\Room::orderBy('id')->get() as $room)
<option value="{{ $room->id }}">{{ $room->name }}</option>
@endforeach
</select>
<button type="button" id="idiom-start-btn"
class="inline-flex items-center gap-1.5 px-4 py-2 bg-gradient-to-r from-purple-600 to-indigo-600 text-white text-sm font-bold rounded-lg hover:opacity-90 transition">
🧩 出题
</button>
</div>
</form>
</div>
{{-- 题目列表 --}}
<div class="{{ $adminListCardClass }}">
<div class="{{ $adminListTableWrapClass }}">
<table class="{{ $adminListTableClass }}">
<thead class="{{ $adminListTableHeadRowClass }}">
<tr>
<th class="{{ $adminListTableHeadCellClass }}">排序</th>
<th class="{{ $adminListTableHeadCellClass }}">成语答案</th>
<th class="{{ $adminListTableHeadCellClass }} w-2/5">谜语提示</th>
<th class="{{ $adminListTableHeadCellClass }} text-center">状态</th>
<th class="{{ $adminListTableHeadCellClass }} text-right">操作</th>
</tr>
</thead>
<tbody class="{{ $adminListTableBodyClass }}">
@foreach ($idioms as $item)
<tr id="row-{{ $item->id }}" class="{{ $adminListTableRowClass }} {{ $item->is_active ? '' : 'opacity-50' }}">
<td class="px-4 py-3 {{ $adminListSecondaryTextClass }}">{{ $item->sort }}</td>
<td class="px-4 py-3 font-bold {{ $adminListPrimaryTextClass }}">{{ $item->answer }}</td>
<td class="px-4 py-3 {{ $adminListBodyTextClass }} text-sm">{{ $item->hint }}</td>
<td class="px-4 py-3 text-center">
<button type="button" data-idiom-toggle-id="{{ $item->id }}"
id="toggle-{{ $item->id }}"
class="{{ $adminListBadgeBaseClass }} px-2 py-1 transition
{{ $item->is_active ? 'border-emerald-200 bg-emerald-100 text-emerald-700 hover:bg-emerald-200' : 'border-gray-200 bg-gray-100 text-gray-500 hover:bg-gray-200' }}">
{{ $item->is_active ? '启用' : '禁用' }}
</button>
</td>
<td class="px-4 py-3 text-right">
<button type="button" data-idiom-edit-id="{{ $item->id }}"
class="{{ $adminListActionButtonClass }} bg-indigo-50 text-indigo-700 hover:bg-indigo-100 mr-1">
编辑
</button>
<form action="{{ route('admin.idioms.destroy', $item->id) }}" method="POST"
class="inline" data-idiom-delete-confirm="确定删除题目「{{ $item->answer }}」?">
@csrf @method('DELETE')
<button type="submit"
class="{{ $adminListActionButtonClass }} bg-red-50 text-red-600 hover:bg-red-100">
删除
</button>
</form>
</td>
</tr>
@endforeach
</tbody>
</table>
</div>
</div>
{{-- 新增题目卡片 --}}
<div class="{{ $adminListCardClass }}">
<div class="{{ $adminListSectionHeadClass }}">
<h3 class="{{ $adminListSectionTitleClass }}"> 新增成语题目</h3>
</div>
<form action="{{ route('admin.idioms.store') }}" method="POST" class="p-5">
@csrf
<div class="grid grid-cols-1 md:grid-cols-2 gap-4">
<div>
<label class="{{ $adminListFilterLabelClass }}">成语答案</label>
<input type="text" name="answer" value="{{ old('answer') }}" placeholder="画蛇添足" required
class="w-full {{ $adminListFilterInputClass }}">
</div>
<div>
<label class="{{ $adminListFilterLabelClass }}">排序</label>
<input type="number" name="sort" value="{{ old('sort', 0) }}" min="0"
class="w-full {{ $adminListFilterInputClass }}">
</div>
<div class="md:col-span-2">
<label class="{{ $adminListFilterLabelClass }}">谜语提示</label>
<input type="text" name="hint" value="{{ old('hint') }}" placeholder="🧩 四人比赛画蛇,最慢的那个反而多此一举。猜一成语" required
class="w-full {{ $adminListFilterInputClass }}">
</div>
</div>
<div class="mt-4 flex items-center gap-4">
<button type="submit" class="{{ $adminListPrimaryButtonClass }}">
💾 添加题目
</button>
<label class="flex items-center gap-2 text-sm text-gray-600 cursor-pointer">
<input type="checkbox" name="is_active" value="1" checked class="rounded">
立即启用
</label>
</div>
</form>
</div>
</div>
{{-- 编辑弹窗 --}}
<div id="edit-modal" class="hidden fixed inset-0 bg-black/40 z-50 flex items-center justify-center p-4">
<div class="bg-white rounded-xl w-full max-w-lg shadow-2xl">
<div class="p-5 border-b border-gray-100 flex justify-between items-center">
<h3 class="font-bold text-gray-800">✏️ 编辑成语题目</h3>
<button type="button" data-idiom-edit-close class="text-gray-400 hover:text-gray-600 text-xl"></button>
</div>
<form id="edit-form" method="POST" class="p-5">
@csrf @method('PUT')
<div class="grid grid-cols-1 md:grid-cols-2 gap-4">
<div>
<label class="block text-xs font-bold text-gray-600 mb-1">成语答案</label>
<input type="text" name="answer" id="edit-answer" required
class="w-full border border-gray-300 rounded-lg p-2 text-sm">
</div>
<div>
<label class="block text-xs font-bold text-gray-600 mb-1">排序</label>
<input type="number" name="sort" id="edit-sort" min="0"
class="w-full border border-gray-300 rounded-lg p-2 text-sm">
</div>
<div class="md:col-span-2">
<label class="block text-xs font-bold text-gray-600 mb-1">谜语提示</label>
<input type="text" name="hint" id="edit-hint" required
class="w-full border border-gray-300 rounded-lg p-2 text-sm">
</div>
<div class="md:col-span-2">
<label class="flex items-center gap-2 text-sm cursor-pointer">
<input type="checkbox" name="is_active" id="edit-is-active" value="1" class="rounded">
启用此题目
</label>
</div>
</div>
<div class="mt-5 flex gap-3">
<button type="submit" class="{{ $adminListPrimaryButtonClass }}">
💾 保存修改
</button>
<button type="button" data-idiom-edit-close
class="{{ $adminListSecondaryButtonClass }}">
取消
</button>
</div>
</form>
</div>
</div>
@endsection
{{-- 前端编辑/切换交互脚本 --}}
@push('scripts')
<script>
document.addEventListener('DOMContentLoaded', function () {
const idiomsDataEl = document.getElementById('admin-idioms-data');
if (!idiomsDataEl) return;
const idiomsData = JSON.parse(idiomsDataEl.textContent || '{}');
// ── 打开编辑弹窗 ──
document.querySelectorAll('[data-idiom-edit-id]').forEach(btn => {
btn.addEventListener('click', function () {
const id = this.dataset.idiomEditId;
const data = idiomsData[id];
if (!data) return;
document.getElementById('edit-answer').value = data.answer;
document.getElementById('edit-hint').value = data.hint;
document.getElementById('edit-sort').value = data.sort;
document.getElementById('edit-is-active').checked = data.is_active;
document.getElementById('edit-form').action = data.update_url;
document.getElementById('edit-modal').classList.remove('hidden');
});
});
// ── 关闭编辑弹窗 ──
document.querySelectorAll('[data-idiom-edit-close]').forEach(btn => {
btn.addEventListener('click', function () {
document.getElementById('edit-modal').classList.add('hidden');
});
});
// ── 切换启用/禁用(AJAX) ──
document.querySelectorAll('[data-idiom-toggle-id]').forEach(btn => {
btn.addEventListener('click', function () {
const id = this.dataset.idiomToggleId;
const data = idiomsData[id];
if (!data) return;
fetch(data.toggle_url, {
method: 'POST',
headers: {
'X-CSRF-TOKEN': document.querySelector('meta[name="csrf-token"]')?.content ?? '',
'Accept': 'application/json',
},
})
.then(r => r.json())
.then(res => {
if (res.ok) {
const row = document.getElementById('row-' + id);
if (row) row.style.opacity = res.is_active ? '1' : '0.5';
const btn = document.getElementById('toggle-' + id);
if (btn) {
btn.textContent = res.is_active ? '启用' : '禁用';
btn.className = (res.is_active
? 'border-emerald-200 bg-emerald-100 text-emerald-700 hover:bg-emerald-200'
: 'border-gray-200 bg-gray-100 text-gray-500 hover:bg-gray-200')
+ ' px-2 py-1 transition rounded-full text-xs font-semibold border';
}
}
})
.catch(() => alert('操作失败'));
});
});
// ── 删除确认 ──
document.querySelectorAll('[data-idiom-delete-confirm]').forEach(form => {
form.addEventListener('submit', function (e) {
if (!confirm(this.dataset.idiomDeleteConfirm)) {
e.preventDefault();
}
});
});
// ── 出题按钮 ──
const startBtn = document.getElementById('idiom-start-btn');
if (startBtn) {
startBtn.addEventListener('click', function () {
const roomSelect = document.getElementById('idiom-start-room');
const roomId = roomSelect?.value;
if (!roomId) return;
const btn = this;
btn.disabled = true;
btn.textContent = '出题中...';
fetch('/idiom-quiz/start', {
method: 'POST',
headers: {
'X-CSRF-TOKEN': document.querySelector('meta[name="csrf-token"]')?.content ?? '',
'Content-Type': 'application/json',
'Accept': 'application/json',
},
body: JSON.stringify({ room_id: parseInt(roomId, 10) }),
})
.then(r => r.json())
.then(data => {
if (data.status === 'success') {
alert('✅ 出题成功!提示已发送到聊天室。');
} else {
alert(data.message || '出题失败');
}
})
.catch(() => alert('网络错误,出题失败'))
.finally(() => {
btn.disabled = false;
btn.textContent = '🧩 出题';
});
});
}
});
</script>
@endpush
+3 -3
View File
@@ -100,9 +100,9 @@
class="block px-4 py-3 rounded-md transition {{ request()->routeIs('admin.fishing.*') ? 'bg-indigo-600 font-bold' : 'hover:bg-white/10' }}">
🎣 钓鱼事件
</a>
<a href="{{ route('admin.idioms.index') }}"
class="block px-4 py-3 rounded-md transition {{ request()->routeIs('admin.idioms.*') ? 'bg-indigo-600 font-bold' : 'hover:bg-white/10' }}">
🧩 成语题库
<a href="{{ route('admin.riddles.index') }}"
class="block px-4 py-3 rounded-md transition {{ request()->routeIs('admin.riddles.*') || request()->routeIs('admin.idioms.*') ? 'bg-indigo-600 font-bold' : 'hover:bg-white/10' }}">
🧩 谜活动题库
</a>
<a href="{{ route('admin.departments.index') }}"
class="block px-4 py-3 rounded-md transition {{ request()->routeIs('admin.departments.*') ? 'bg-indigo-600 font-bold' : 'hover:bg-white/10' }}">
@@ -0,0 +1,304 @@
@extends('admin.layouts.app')
@section('title', '猜谜活动题库管理')
@section('content')
@php require resource_path('views/admin/partials/list-theme.php'); @endphp
@php
$quizTypes = $typeOptions;
$idiomPayload = $idioms->mapWithKeys(
fn ($item) => [
(string) $item->id => [
'id' => $item->id,
'type' => $item->type,
'answer' => $item->answer,
'hint' => $item->hint,
'sort' => $item->sort,
'is_active' => (bool) $item->is_active,
'update_url' => route('admin.riddles.update', $item->id),
],
],
);
@endphp
<script type="application/json" id="admin-idioms-data">@json($idiomPayload)</script>
<div class="{{ $adminListPageClass }}">
<div class="{{ $adminListHeaderCardClass }}">
<div>
<h2 class="{{ $adminListHeaderTitleClass }}">🧩 猜谜活动题库管理</h2>
<p class="{{ $adminListHeaderSubtitleClass }}">
这里只管理题目本身;奖励、自动出题、参与房间和手动开题请前往
<a href="{{ route('admin.game-configs.index') }}" class="font-semibold text-indigo-600 hover:text-indigo-500">游戏管理</a>
页面操作。
</p>
</div>
</div>
<div class="{{ $adminListCardClass }}">
<div class="{{ $adminListSectionHeadClass }}">
<div>
<h3 class="{{ $adminListSectionTitleClass }}">🔎 题库筛选</h3>
<p class="mt-1 text-xs text-slate-500">支持按题型和关键词快速定位题目。</p>
</div>
</div>
<form action="{{ route('admin.riddles.index') }}" method="GET" class="p-5">
<div class="grid grid-cols-1 gap-4 md:grid-cols-3">
<div>
<label class="{{ $adminListFilterLabelClass }}">题型</label>
<select name="type" class="w-full {{ $adminListFilterInputClass }}">
<option value="">全部题型</option>
@foreach ($quizTypes as $quizType => $quizLabel)
<option value="{{ $quizType }}" @selected($selectedType === $quizType)>{{ $quizLabel }}</option>
@endforeach
</select>
</div>
<div>
<label class="{{ $adminListFilterLabelClass }}">关键词</label>
<input type="text" name="keyword" value="{{ $keyword }}" placeholder="匹配答案或题面" class="w-full {{ $adminListFilterInputClass }}">
</div>
<div class="flex items-end gap-3">
<button type="submit" class="{{ $adminListPrimaryButtonClass }}">筛选</button>
<a href="{{ route('admin.riddles.index') }}" class="{{ $adminListSecondaryButtonClass }}">重置</a>
</div>
</div>
<div class="mt-4 flex flex-wrap gap-2 text-xs text-slate-500">
@foreach ($quizTypes as $quizType => $quizLabel)
<span class="rounded-full bg-slate-100 px-3 py-1">
{{ $quizLabel }}{{ $typeStats[$quizType] ?? 0 }}
</span>
@endforeach
</div>
</form>
</div>
<div class="{{ $adminListCardClass }}">
<div class="{{ $adminListSectionHeadClass }}">
<h3 class="{{ $adminListSectionTitleClass }}">📚 题目列表</h3>
</div>
<div class="overflow-x-auto">
<table class="min-w-full divide-y divide-slate-200 text-sm">
<thead class="bg-slate-50 text-slate-600">
<tr>
<th class="px-4 py-3 text-left font-semibold">ID</th>
<th class="px-4 py-3 text-left font-semibold">题型</th>
<th class="px-4 py-3 text-left font-semibold">标准答案</th>
<th class="px-4 py-3 text-left font-semibold">题面 / 提示</th>
<th class="px-4 py-3 text-left font-semibold">排序</th>
<th class="px-4 py-3 text-left font-semibold">状态</th>
<th class="px-4 py-3 text-right font-semibold">操作</th>
</tr>
</thead>
<tbody class="divide-y divide-slate-100 bg-white">
@forelse ($idioms as $item)
<tr class="hover:bg-slate-50/80">
<td class="px-4 py-3 text-slate-500">#{{ $item->id }}</td>
<td class="px-4 py-3">
<span class="rounded-full px-2.5 py-1 text-xs font-semibold {{ $item->type === \App\Models\Riddle::TYPE_BRAIN_TEASER ? 'bg-amber-100 text-amber-700' : 'bg-indigo-100 text-indigo-700' }}">
{{ \App\Models\Riddle::labelForType($item->type) }}
</span>
</td>
<td class="px-4 py-3 font-semibold text-slate-800">{{ $item->answer }}</td>
<td class="px-4 py-3 text-slate-600">{{ $item->hint }}</td>
<td class="px-4 py-3 text-slate-500">{{ $item->sort }}</td>
<td class="px-4 py-3">
<button type="button"
data-idiom-toggle-url="{{ route('admin.riddles.toggle', $item->id) }}"
class="rounded-full px-3 py-1 text-xs font-semibold transition {{ $item->is_active ? 'bg-emerald-100 text-emerald-700 hover:bg-emerald-200' : 'bg-slate-200 text-slate-500 hover:bg-slate-300' }}">
{{ $item->is_active ? '已启用' : '已禁用' }}
</button>
</td>
<td class="px-4 py-3 text-right">
<button type="button" data-idiom-edit-id="{{ $item->id }}" class="{{ $adminListActionButtonClass }} bg-indigo-50 text-indigo-700 hover:bg-indigo-100">
编辑
</button>
<form action="{{ route('admin.riddles.destroy', $item->id) }}" method="POST" class="inline" data-idiom-delete-confirm="确定删除题目「{{ $item->answer }}」?">
@csrf
@method('DELETE')
<input type="hidden" name="redirect_type" value="{{ $selectedType }}">
<input type="hidden" name="redirect_keyword" value="{{ $keyword }}">
<button type="submit" class="{{ $adminListActionButtonClass }} bg-red-50 text-red-600 hover:bg-red-100">
删除
</button>
</form>
</td>
</tr>
@empty
<tr>
<td colspan="7" class="px-4 py-8 text-center text-sm text-slate-400">当前筛选条件下暂无题目。</td>
</tr>
@endforelse
</tbody>
</table>
</div>
</div>
<div class="{{ $adminListCardClass }}">
<div class="{{ $adminListSectionHeadClass }}">
<h3 class="{{ $adminListSectionTitleClass }}"> 新增题目</h3>
</div>
<form action="{{ route('admin.riddles.store') }}" method="POST" class="p-5">
@csrf
<input type="hidden" name="redirect_type" value="{{ $selectedType }}">
<input type="hidden" name="redirect_keyword" value="{{ $keyword }}">
<div class="grid grid-cols-1 gap-4 md:grid-cols-2">
<div>
<label class="{{ $adminListFilterLabelClass }}">题型</label>
<select name="type" class="w-full {{ $adminListFilterInputClass }}">
@foreach ($quizTypes as $quizType => $quizLabel)
<option value="{{ $quizType }}" @selected(old('type', $selectedType ?: \App\Models\Riddle::TYPE_IDIOM) === $quizType)>{{ $quizLabel }}</option>
@endforeach
</select>
</div>
<div>
<label class="{{ $adminListFilterLabelClass }}">排序</label>
<input type="number" name="sort" min="0" value="{{ old('sort', 0) }}" class="w-full {{ $adminListFilterInputClass }}">
</div>
<div>
<label class="{{ $adminListFilterLabelClass }}">标准答案</label>
<input type="text" name="answer" value="{{ old('answer') }}" required placeholder="请输入标准答案" class="w-full {{ $adminListFilterInputClass }}">
</div>
<div class="md:col-span-2">
<label class="{{ $adminListFilterLabelClass }}">题面 / 提示</label>
<input type="text" name="hint" value="{{ old('hint') }}" required placeholder="请输入题面或提示文案" class="w-full {{ $adminListFilterInputClass }}">
</div>
</div>
<div class="mt-4 flex items-center gap-4">
<button type="submit" class="{{ $adminListPrimaryButtonClass }}">💾 添加题目</button>
<label class="flex cursor-pointer items-center gap-2 text-sm text-slate-600">
<input type="checkbox" name="is_active" value="1" checked class="rounded border-slate-300">
立即启用
</label>
</div>
</form>
</div>
</div>
<div id="edit-modal" class="fixed inset-0 z-50 hidden items-center justify-center bg-black/40 p-4">
<div class="w-full max-w-lg rounded-xl bg-white shadow-2xl">
<div class="flex items-center justify-between border-b border-slate-100 p-5">
<h3 class="text-base font-bold text-slate-800">✏️ 编辑题目</h3>
<button type="button" data-idiom-edit-close class="text-xl text-slate-400 hover:text-slate-600"></button>
</div>
<form id="edit-form" method="POST" class="p-5">
@csrf
@method('PUT')
<input type="hidden" name="redirect_type" value="{{ $selectedType }}">
<input type="hidden" name="redirect_keyword" value="{{ $keyword }}">
<div class="grid grid-cols-1 gap-4 md:grid-cols-2">
<div>
<label class="mb-1 block text-xs font-bold text-slate-600">题型</label>
<select name="type" id="edit-type" class="w-full rounded-lg border border-slate-300 p-2 text-sm">
@foreach ($quizTypes as $quizType => $quizLabel)
<option value="{{ $quizType }}">{{ $quizLabel }}</option>
@endforeach
</select>
</div>
<div>
<label class="mb-1 block text-xs font-bold text-slate-600">排序</label>
<input type="number" name="sort" id="edit-sort" min="0" class="w-full rounded-lg border border-slate-300 p-2 text-sm">
</div>
<div class="md:col-span-2">
<label class="mb-1 block text-xs font-bold text-slate-600">标准答案</label>
<input type="text" name="answer" id="edit-answer" required class="w-full rounded-lg border border-slate-300 p-2 text-sm">
</div>
<div class="md:col-span-2">
<label class="mb-1 block text-xs font-bold text-slate-600">题面 / 提示</label>
<input type="text" name="hint" id="edit-hint" required class="w-full rounded-lg border border-slate-300 p-2 text-sm">
</div>
<div class="md:col-span-2">
<label class="flex cursor-pointer items-center gap-2 text-sm text-slate-600">
<input type="checkbox" name="is_active" id="edit-is-active" value="1" class="rounded border-slate-300">
启用此题目
</label>
</div>
</div>
<div class="mt-5 flex gap-3">
<button type="submit" class="{{ $adminListPrimaryButtonClass }}">💾 保存修改</button>
<button type="button" data-idiom-edit-close class="{{ $adminListSecondaryButtonClass }}">取消</button>
</div>
</form>
</div>
</div>
@endsection
@push('scripts')
<script>
document.addEventListener('DOMContentLoaded', function () {
const idiomsDataEl = document.getElementById('admin-idioms-data');
const editModal = document.getElementById('edit-modal');
const editForm = document.getElementById('edit-form');
if (!idiomsDataEl || !editModal || !editForm) {
return;
}
const idiomsData = JSON.parse(idiomsDataEl.textContent || '{}');
document.querySelectorAll('[data-idiom-toggle-url]').forEach((button) => {
button.addEventListener('click', async function () {
const url = this.getAttribute('data-idiom-toggle-url');
if (!url) {
return;
}
const response = await fetch(url, {
method: 'POST',
headers: {
'X-CSRF-TOKEN': '{{ csrf_token() }}',
'X-Requested-With': 'XMLHttpRequest',
'Accept': 'application/json',
},
});
const result = await response.json();
if (!result.ok) {
alert(result.message || '状态切换失败。');
return;
}
window.location.reload();
});
});
document.querySelectorAll('[data-idiom-delete-confirm]').forEach((form) => {
form.addEventListener('submit', function (event) {
const message = this.getAttribute('data-idiom-delete-confirm') || '确定删除这条题目吗?';
if (!window.confirm(message)) {
event.preventDefault();
}
});
});
document.querySelectorAll('[data-idiom-edit-id]').forEach((button) => {
button.addEventListener('click', function () {
const idiomId = this.getAttribute('data-idiom-edit-id') || '';
const payload = idiomsData[idiomId];
if (!payload) {
return;
}
document.getElementById('edit-type').value = payload.type;
document.getElementById('edit-answer').value = payload.answer;
document.getElementById('edit-hint').value = payload.hint;
document.getElementById('edit-sort').value = payload.sort;
document.getElementById('edit-is-active').checked = Boolean(payload.is_active);
editForm.action = payload.update_url;
editModal.classList.remove('hidden');
editModal.classList.add('flex');
});
});
document.querySelectorAll('[data-idiom-edit-close]').forEach((button) => {
button.addEventListener('click', function () {
editModal.classList.add('hidden');
editModal.classList.remove('flex');
});
});
});
</script>
@endpush