question-management.blade.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. <x-filament-panels::page>
  2. <div class="space-y-6">
  3. @php
  4. $questionsData = $this->questions;
  5. $metaData = $this->meta;
  6. $statisticsData = $this->statistics;
  7. @endphp
  8. {{-- 后台生成状态栏 - 仅在生成中显示 --}}
  9. @if($isGenerating && $currentTaskId)
  10. <div class="bg-blue-50 border-l-4 border-blue-400 p-4 rounded-r-lg animate-pulse">
  11. <div class="flex items-center">
  12. <div class="animate-spin rounded-full h-5 w-5 border-b-2 border-blue-600 mr-3"></div>
  13. <div class="flex-1">
  14. <p class="text-sm text-blue-800">
  15. <strong>正在后台生成题目...</strong>
  16. </p>
  17. <p class="text-xs text-blue-600 mt-1">
  18. 任务 ID: {{ $currentTaskId }} | AI生成完成后将自动刷新页面
  19. </p>
  20. </div>
  21. <button type="button" wire:click="$set('isGenerating', false)" class="text-blue-400 hover:text-blue-600">
  22. <svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
  23. <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M6 18L18 6M6 6l12 12"></path>
  24. </svg>
  25. </button>
  26. </div>
  27. </div>
  28. @endif
  29. <div class="flex justify-end">
  30. <button
  31. type="button"
  32. wire:click="$dispatch('ai-generate')"
  33. class="filament-button filament-button-size-sm filament-button-color-success filament-button-icon-start inline-flex items-center justify-center px-4 py-2 text-sm font-medium transition-colors border border-transparent rounded-lg focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-primary-500"
  34. >
  35. <svg class="w-4 h-4 mr-2" fill="none" stroke="currentColor" viewBox="0 0 24 24">
  36. <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 4v16m8-8H4"></path>
  37. </svg>
  38. 生成题目
  39. </button>
  40. </div>
  41. <div class="grid grid-cols-1 md:grid-cols-4 gap-4">
  42. <div class="bg-white p-4 rounded-lg border">
  43. <div class="text-sm text-gray-500">题目总数</div>
  44. <div class="text-2xl font-bold text-primary-600">{{ $statisticsData['total'] ?? 0 }}</div>
  45. </div>
  46. <div class="bg-white p-4 rounded-lg border">
  47. <div class="text-sm text-gray-500">基础难度 (≤0.4)</div>
  48. <div class="text-2xl font-bold text-green-600">
  49. @php
  50. $basicCount = 0;
  51. foreach ($statisticsData['by_difficulty'] ?? [] as $key => $value) {
  52. if ((float)$key <= 0.4) {
  53. $basicCount += $value;
  54. }
  55. }
  56. echo $basicCount;
  57. @endphp
  58. </div>
  59. </div>
  60. <div class="bg-white p-4 rounded-lg border">
  61. <div class="text-sm text-gray-500">中等难度 (0.4-0.7)</div>
  62. <div class="text-2xl font-bold text-yellow-600">
  63. @php
  64. $mediumCount = 0;
  65. foreach ($statisticsData['by_difficulty'] ?? [] as $key => $value) {
  66. if ((float)$key > 0.4 && (float)$key <= 0.7) {
  67. $mediumCount += $value;
  68. }
  69. }
  70. echo $mediumCount;
  71. @endphp
  72. </div>
  73. </div>
  74. <div class="bg-white p-4 rounded-lg border">
  75. <div class="text-sm text-gray-500">拔高难度 (>0.7)</div>
  76. <div class="text-2xl font-bold text-red-600">
  77. @php
  78. $advancedCount = 0;
  79. foreach ($statisticsData['by_difficulty'] ?? [] as $key => $value) {
  80. if ((float)$key > 0.7) {
  81. $advancedCount += $value;
  82. }
  83. }
  84. echo $advancedCount;
  85. @endphp
  86. </div>
  87. </div>
  88. </div>
  89. {{-- 调试信息 --}}
  90. @if(app()->environment('local'))
  91. <div class="bg-gray-100 p-4 rounded border text-xs">
  92. <div class="font-bold mb-2">统计数据调试:</div>
  93. <pre>{{ json_encode($statisticsData, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE) }}</pre>
  94. </div>
  95. @endif
  96. <div class="bg-white p-4 rounded-lg border">
  97. <div class="grid grid-cols-1 md:grid-cols-4 gap-4">
  98. <div>
  99. <label class="block text-sm font-medium text-gray-700 mb-2">搜索题目</label>
  100. <input type="text" wire:model.live.debounce.300ms="search" placeholder="输入关键词" class="w-full border rounded p-2">
  101. </div>
  102. <div>
  103. <label class="block text-sm font-medium text-gray-700 mb-2">知识点筛选</label>
  104. <input type="text" wire:model.live="selectedKpCode" placeholder="KP1001" class="w-full border rounded p-2">
  105. </div>
  106. <div>
  107. <label class="block text-sm font-medium text-gray-700 mb-2">难度筛选</label>
  108. <input type="text" wire:model.live="selectedDifficulty" placeholder="0.3/0.6/0.85" class="w-full border rounded p-2">
  109. </div>
  110. <div>
  111. <label class="block text-sm font-medium text-gray-700 mb-2">每页显示</label>
  112. <input type="number" wire:model.live="perPage" min="10" max="100" step="5" class="w-full border rounded p-2">
  113. </div>
  114. </div>
  115. </div>
  116. <div class="bg-white rounded-lg border overflow-hidden">
  117. <table class="min-w-full divide-y divide-gray-200">
  118. <thead class="bg-gray-50">
  119. <tr>
  120. <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase">题目编号</th>
  121. <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase">知识点</th>
  122. <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase">题干</th>
  123. <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase">难度</th>
  124. <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase">操作</th>
  125. </tr>
  126. </thead>
  127. <tbody class="bg-white divide-y divide-gray-200">
  128. @forelse($questionsData as $question)
  129. <tr class="hover:bg-gray-50">
  130. <td class="px-6 py-4 whitespace-nowrap">{{ $question['question_code'] ?? 'N/A' }}</td>
  131. <td class="px-6 py-4 whitespace-nowrap">{{ $question['kp_code'] ?? 'N/A' }}</td>
  132. <td class="px-6 py-4" style="word-wrap: break-word; white-space: normal; line-height: 1.8; max-width: 400px;">
  133. <x-math-render :content="\Illuminate\Support\Str::limit($question['stem'] ?? 'N/A', 150)" class="text-sm" />
  134. </td>
  135. <td class="px-6 py-4">
  136. @php
  137. $difficulty = $question['difficulty'] ?? null;
  138. $label = match (true) {
  139. !$difficulty => 'N/A',
  140. (float)$difficulty <= 0.4 => '基础',
  141. (float)$difficulty <= 0.7 => '中等',
  142. default => '拔高',
  143. };
  144. @endphp
  145. {{ $label }}
  146. @if(app()->environment('local'))
  147. <span class="text-xs text-gray-400">({{ $difficulty }})</span>
  148. @endif
  149. </td>
  150. <td class="px-6 py-4 whitespace-nowrap">
  151. <button wire:click="deleteQuestion('{{ $question['question_code'] }}')" class="text-red-600 hover:underline">删除</button>
  152. </td>
  153. </tr>
  154. @empty
  155. <tr><td colspan="5" class="px-6 py-12 text-center">暂无数据</td></tr>
  156. @endforelse
  157. </tbody>
  158. </table>
  159. @if(!empty($metaData) && ($metaData['total'] ?? 0) > 0)
  160. <div class="px-4 py-3 border-t border-gray-200 flex items-center justify-between">
  161. <div class="text-sm text-gray-700">共 {{ $metaData['total'] ?? 0 }} 条记录</div>
  162. <div class="flex items-center gap-2">
  163. <button wire:click="previousPage" @disabled($currentPage <= 1) class="px-3 py-1 border rounded">上一页</button>
  164. @foreach($this->getPages() as $page)
  165. <button wire:click="gotoPage({{ $page }})" class="px-3 py-1 border rounded {{ $page === $currentPage ? 'bg-blue-50 text-blue-700' : '' }}">{{ $page }}</button>
  166. @endforeach
  167. <button wire:click="nextPage" @disabled($currentPage >= ($metaData['total_pages'] ?? 1)) class="px-3 py-1 border rounded">下一页</button>
  168. </div>
  169. </div>
  170. @endif
  171. </div>
  172. @if($showGenerateModal)
  173. <div class="fixed inset-0 bg-black bg-opacity-50 z-50 flex items-center justify-center p-4">
  174. <div class="bg-white rounded-lg p-6 w-96 max-w-[28rem] shadow-xl">
  175. <h3 class="text-lg font-semibold mb-4">生成题目</h3>
  176. <div class="space-y-4">
  177. <div>
  178. <label class="block text-sm font-medium mb-2">知识点 <span class="text-red-500">*</span></label>
  179. <select wire:model.live="generateKpCode" class="w-full border rounded p-2">
  180. <option value="">选择知识点</option>
  181. @foreach($this->knowledgePointOptions as $code => $name)
  182. <option value="{{ $code }}">{{ $code }} - {{ $name }}</option>
  183. @endforeach
  184. </select>
  185. </div>
  186. @if(!empty($this->skillsOptions))
  187. <div>
  188. <div class="flex items-center justify-between mb-2">
  189. <label class="block text-sm font-medium">选择技能 <span class="text-red-500">*</span></label>
  190. <button type="button" class="text-sm text-blue-600 hover:underline" wire:click="toggleAllSkills">
  191. {{ count($selectedSkills) === count($this->skillsOptions) ? '取消全选' : '全选' }}
  192. </button>
  193. </div>
  194. <div class="max-h-48 overflow-y-auto border rounded p-3 space-y-1">
  195. @foreach($this->skillsOptions as $skill)
  196. <label class="flex items-center space-x-2">
  197. <input type="checkbox" value="{{ $skill['code'] }}" wire:model="selectedSkills" class="rounded border-gray-300">
  198. <span class="text-sm">
  199. <span class="font-medium">{{ $skill['code'] }}</span>
  200. <span class="text-gray-600 ml-2">{{ $skill['name'] }}</span>
  201. <span class="text-xs text-gray-400 ml-2">(权重: {{ $skill['weight'] ?? 1 }})</span>
  202. </span>
  203. </label>
  204. @endforeach
  205. </div>
  206. </div>
  207. @else
  208. <div class="text-sm text-gray-500 italic">
  209. 请先选择知识点以加载技能列表
  210. </div>
  211. @endif
  212. <div>
  213. <label class="block text-sm font-medium mb-2">题目数量</label>
  214. <input type="number" wire:model="questionCount" min="1" max="500" class="w-full border rounded p-2">
  215. </div>
  216. </div>
  217. <div class="flex justify-end gap-3 mt-6">
  218. <button type="button" wire:click="closeGenerateModal" class="px-4 py-2 border rounded" @disabled($isGenerating)>取消</button>
  219. <button
  220. type="button"
  221. wire:click="executeGenerate"
  222. wire:loading.attr="disabled"
  223. wire:loading.class="bg-yellow-500 cursor-not-allowed opacity-90"
  224. wire:loading.class.remove="bg-blue-600 hover:bg-blue-700"
  225. wire:target="executeGenerate"
  226. class="px-6 py-2 bg-blue-600 hover:bg-blue-700 rounded font-medium transition-all duration-200 flex items-center gap-2 text-white"
  227. >
  228. @if($isGenerating)
  229. <svg class="animate-spin h-4 w-4 text-white" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24">
  230. <circle class="opacity-25" cx="12" cy="12" r="10" stroke="currentColor" stroke-width="4"></circle>
  231. <path class="opacity-75" fill="currentColor" d="M4 12a8 8 0 018-8V0C5.373 0 0 5.373 0 12h4zm2 5.291A7.962 7.962 0 014 12H0c0 3.042 1.135 5.824 3 7.938l3-2.647z"></path>
  232. </svg>
  233. <span class="text-white font-semibold">生成中...</span>
  234. @else
  235. <svg class="w-4 h-4 text-white" fill="none" stroke="currentColor" viewBox="0 0 24 24">
  236. <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 4v16m8-8H4"></path>
  237. </svg>
  238. <span class="text-white font-semibold">开始生成</span>
  239. @endif
  240. </button>
  241. </div>
  242. </div>
  243. </div>
  244. @endif
  245. <script>
  246. document.addEventListener('livewire:init', () => {
  247. Livewire.on('ai-generate', () => {
  248. @this.call('openGenerateModal');
  249. });
  250. Livewire.on('refresh-page', () => {
  251. // 页面刷新事件
  252. // 触发数学公式重新渲染
  253. document.dispatchEvent(new Event('math:render'));
  254. });
  255. // 监听页面刷新事件
  256. Livewire.on('refresh-page', () => {
  257. console.log('[QuestionGen] 收到刷新页面事件');
  258. // 1秒后刷新页面,确保状态更新完成
  259. setTimeout(() => {
  260. console.log('[QuestionGen] 执行页面刷新');
  261. window.location.reload();
  262. }, 1000);
  263. });
  264. // ✅ 捕获回调参数,直接检查状态 - 避免盲目轮询
  265. Livewire.on('start-async-task-monitoring', () => {
  266. console.log('[QuestionGen] 开始监控任务状态');
  267. const taskId = @this.currentTaskId;
  268. if (!taskId) {
  269. console.error('[QuestionGen] 未找到任务ID');
  270. return;
  271. }
  272. window.currentTaskId = taskId;
  273. let checkCount = 0;
  274. const maxChecks = 5; // 最多检查5次
  275. function checkCallbackStatus() {
  276. checkCount++;
  277. console.log(`[QuestionGen] 检查回调 #${checkCount}/${maxChecks}`);
  278. // 直接调用 API 检查回调数据 - GET 请求无需 CSRF
  279. fetch(`/api/questions/callback/${taskId}`, {
  280. method: 'GET',
  281. headers: {
  282. 'X-Requested-With': 'XMLHttpRequest',
  283. 'Accept': 'application/json',
  284. }
  285. })
  286. .then(response => response.json())
  287. .then(data => {
  288. console.log('[QuestionGen] 回调数据:', data);
  289. // ✅ 如果有状态字段,说明回调已收到
  290. if (data.status) {
  291. if (data.status === 'completed') {
  292. console.log('[QuestionGen] ✅ 任务完成');
  293. @this.set('isGenerating', false);
  294. @this.set('currentTaskId', null);
  295. // 显示成功通知
  296. setTimeout(() => {
  297. window.location.reload();
  298. }, 1000);
  299. } else if (data.status === 'failed') {
  300. console.log('[QuestionGen] ❌ 任务失败');
  301. @this.set('isGenerating', false);
  302. @this.set('currentTaskId', null);
  303. }
  304. } else if (checkCount < maxChecks) {
  305. // 没收到回调,继续检查
  306. setTimeout(checkCallbackStatus, 3000);
  307. } else {
  308. // 达到最大检查次数,停止
  309. console.log('[QuestionGen] 检查超时,停止监控');
  310. @this.set('isGenerating', false);
  311. @this.set('currentTaskId', null);
  312. }
  313. })
  314. .catch(error => {
  315. console.error('[QuestionGen] 检查回调失败:', error);
  316. if (checkCount < maxChecks) {
  317. setTimeout(checkCallbackStatus, 3000);
  318. }
  319. });
  320. }
  321. // 立即检查一次
  322. checkCallbackStatus();
  323. // 15秒后强制停止
  324. setTimeout(() => {
  325. if (checkCount < maxChecks) {
  326. console.log('[QuestionGen] 强制停止监控');
  327. @this.set('isGenerating', false);
  328. @this.set('currentTaskId', null);
  329. }
  330. }, 15000);
  331. });
  332. // 监听强制关闭状态栏事件
  333. Livewire.on('force-close-status-bar', () => {
  334. console.log('[QuestionGen] 强制关闭状态栏');
  335. @this.set('isGenerating', false);
  336. @this.set('currentTaskId', null);
  337. });
  338. });
  339. </script>
  340. </div>
  341. @push('scripts')
  342. <script src="/js/math-render.js"></script>
  343. @endpush
  344. @push('styles')
  345. <link rel="stylesheet" href="/css/katex/katex.min.css">
  346. @endpush
  347. </x-filament-panels::page>