question-management-simple.blade.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  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. <div class="bg-white p-4 rounded-lg border">
  90. <div class="grid grid-cols-1 md:grid-cols-4 gap-4">
  91. <div>
  92. <label class="block text-sm font-medium text-gray-700 mb-2">搜索题目</label>
  93. <input type="text" wire:model.live.debounce.300ms="search" placeholder="输入关键词" class="w-full border rounded p-2">
  94. </div>
  95. <div>
  96. <label class="block text-sm font-medium text-gray-700 mb-2">知识点筛选</label>
  97. <input type="text" wire:model.live="selectedKpCode" placeholder="KP1001" class="w-full border rounded p-2">
  98. </div>
  99. <div>
  100. <label class="block text-sm font-medium text-gray-700 mb-2">难度筛选</label>
  101. <input type="text" wire:model.live="selectedDifficulty" placeholder="0.3/0.6/0.85" class="w-full border rounded p-2">
  102. </div>
  103. <div>
  104. <label class="block text-sm font-medium text-gray-700 mb-2">每页显示</label>
  105. <input type="number" wire:model.live="perPage" min="10" max="100" step="5" class="w-full border rounded p-2">
  106. </div>
  107. </div>
  108. </div>
  109. <div class="bg-white rounded-lg border overflow-hidden">
  110. <table class="min-w-full divide-y divide-gray-200">
  111. <thead class="bg-gray-50">
  112. <tr>
  113. <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase">题目编号</th>
  114. <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase">知识点</th>
  115. <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase">题干</th>
  116. <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase">难度</th>
  117. <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase">操作</th>
  118. </tr>
  119. </thead>
  120. <tbody class="bg-white divide-y divide-gray-200">
  121. @forelse($questionsData as $question)
  122. <tr class="hover:bg-gray-50">
  123. <td class="px-6 py-4 whitespace-nowrap">{{ $question['question_code'] ?? 'N/A' }}</td>
  124. <td class="px-6 py-4 whitespace-nowrap">{{ $question['kp_code'] ?? 'N/A' }}</td>
  125. <td class="px-6 py-4" style="word-wrap: break-word; white-space: normal; line-height: 1.8; max-width: 400px;">
  126. <span class="text-sm">
  127. @math($question['stem'] ?? 'N/A')
  128. </span>
  129. </td>
  130. <td class="px-6 py-4">
  131. @php
  132. $difficulty = $question['difficulty'] ?? null;
  133. $label = match (true) {
  134. !$difficulty => 'N/A',
  135. (float)$difficulty <= 0.4 => '基础',
  136. (float)$difficulty <= 0.7 => '中等',
  137. default => '拔高',
  138. };
  139. @endphp
  140. {{ $label }}
  141. @if(app()->environment('local'))
  142. <span class="text-xs text-gray-400">({{ $difficulty }})</span>
  143. @endif
  144. </td>
  145. <td class="px-6 py-4 whitespace-nowrap">
  146. <button wire:click="deleteQuestion('{{ $question['question_code'] }}')" class="text-red-600 hover:underline">删除</button>
  147. </td>
  148. </tr>
  149. @empty
  150. <tr><td colspan="5" class="px-6 py-12 text-center">暂无数据</td></tr>
  151. @endforelse
  152. </tbody>
  153. </table>
  154. @if(!empty($metaData) && ($metaData['total'] ?? 0) > 0)
  155. <div class="px-4 py-3 border-t border-gray-200 flex items-center justify-between">
  156. <div class="text-sm text-gray-700">共 {{ $metaData['total'] ?? 0 }} 条记录</div>
  157. <div class="flex items-center gap-2">
  158. <button wire:click="previousPage" @disabled($currentPage <= 1) class="px-3 py-1 border rounded">上一页</button>
  159. @foreach($this->getPages() as $page)
  160. <button wire:click="gotoPage({{ $page }})" class="px-3 py-1 border rounded {{ $page === $currentPage ? 'bg-blue-50 text-blue-700' : '' }}">{{ $page }}</button>
  161. @endforeach
  162. <button wire:click="nextPage" @disabled($currentPage >= ($metaData['total_pages'] ?? 1)) class="px-3 py-1 border rounded">下一页</button>
  163. </div>
  164. </div>
  165. @endif
  166. </div>
  167. @if($showGenerateModal)
  168. <div class="fixed inset-0 bg-black bg-opacity-50 z-50 flex items-center justify-center p-4">
  169. <div class="bg-white rounded-lg p-6 w-96 max-w-[28rem] shadow-xl">
  170. <h3 class="text-lg font-semibold mb-4">生成题目</h3>
  171. <div class="space-y-4">
  172. <div>
  173. <label class="block text-sm font-medium mb-2">知识点 <span class="text-red-500">*</span></label>
  174. <select wire:model.live="generateKpCode" class="w-full border rounded p-2">
  175. <option value="">选择知识点</option>
  176. @foreach($this->knowledgePointOptions as $code => $name)
  177. <option value="{{ $code }}">{{ $code }} - {{ $name }}</option>
  178. @endforeach
  179. </select>
  180. </div>
  181. @if(!empty($this->skillsOptions))
  182. <div>
  183. <div class="flex items-center justify-between mb-2">
  184. <label class="block text-sm font-medium">选择技能 <span class="text-red-500">*</span></label>
  185. <button type="button" class="text-sm text-blue-600 hover:underline" wire:click="toggleAllSkills">
  186. {{ count($selectedSkills) === count($this->skillsOptions) ? '取消全选' : '全选' }}
  187. </button>
  188. </div>
  189. <div class="max-h-48 overflow-y-auto border rounded p-3 space-y-1">
  190. @foreach($this->skillsOptions as $skill)
  191. <label class="flex items-center space-x-2">
  192. <input type="checkbox" value="{{ $skill['code'] }}" wire:model="selectedSkills" class="rounded border-gray-300">
  193. <span class="text-sm">
  194. <span class="font-medium">{{ $skill['code'] }}</span>
  195. <span class="text-gray-600 ml-2">{{ $skill['name'] }}</span>
  196. <span class="text-xs text-gray-400 ml-2">(权重: {{ $skill['weight'] ?? 1 }})</span>
  197. </span>
  198. </label>
  199. @endforeach
  200. </div>
  201. </div>
  202. @else
  203. <div class="text-sm text-gray-500 italic">
  204. 请先选择知识点以加载技能列表
  205. </div>
  206. @endif
  207. <div>
  208. <label class="block text-sm font-medium mb-2">题目数量</label>
  209. <input type="number" wire:model="questionCount" min="1" max="500" class="w-full border rounded p-2">
  210. </div>
  211. </div>
  212. <div class="flex justify-end gap-3 mt-6">
  213. <button type="button" wire:click="closeGenerateModal" class="px-4 py-2 border rounded" @disabled($isGenerating)>取消</button>
  214. <button
  215. type="button"
  216. wire:click="executeGenerate"
  217. wire:loading.attr="disabled"
  218. wire:loading.class="bg-yellow-500 cursor-not-allowed opacity-90"
  219. wire:loading.class.remove="bg-blue-600 hover:bg-blue-700"
  220. wire:target="executeGenerate"
  221. 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"
  222. >
  223. @if($isGenerating)
  224. <svg class="animate-spin h-4 w-4 text-white" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24">
  225. <circle class="opacity-25" cx="12" cy="12" r="10" stroke="currentColor" stroke-width="4"></circle>
  226. <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>
  227. </svg>
  228. <span class="text-white font-semibold">生成中...</span>
  229. @else
  230. <svg class="w-4 h-4 text-white" fill="none" stroke="currentColor" viewBox="0 0 24 24">
  231. <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 4v16m8-8H4"></path>
  232. </svg>
  233. <span class="text-white font-semibold">开始生成</span>
  234. @endif
  235. </button>
  236. </div>
  237. </div>
  238. </div>
  239. @endif
  240. <script>
  241. document.addEventListener('livewire:init', () => {
  242. Livewire.on('ai-generate', () => {
  243. @this.call('openGenerateModal');
  244. });
  245. Livewire.on('refresh-page', () => {
  246. // 页面刷新事件
  247. // 触发数学公式重新渲染
  248. document.dispatchEvent(new Event('math:render'));
  249. });
  250. // 监听页面刷新事件
  251. Livewire.on('refresh-page', () => {
  252. console.log('[QuestionGen] 收到刷新页面事件');
  253. // 1秒后刷新页面,确保状态更新完成
  254. setTimeout(() => {
  255. console.log('[QuestionGen] 执行页面刷新');
  256. window.location.reload();
  257. }, 1000);
  258. });
  259. // ✅ 捕获回调参数,直接检查状态 - 避免盲目轮询
  260. Livewire.on('start-async-task-monitoring', () => {
  261. console.log('[QuestionGen] 开始监控任务状态');
  262. const taskId = @this.currentTaskId;
  263. if (!taskId) {
  264. console.error('[QuestionGen] 未找到任务ID');
  265. return;
  266. }
  267. window.currentTaskId = taskId;
  268. let checkCount = 0;
  269. const maxChecks = 5; // 最多检查5次
  270. function checkCallbackStatus() {
  271. checkCount++;
  272. console.log(`[QuestionGen] 检查回调 #${checkCount}/${maxChecks}`);
  273. // 直接调用 API 检查回调数据 - GET 请求无需 CSRF
  274. fetch(`/api/questions/callback/${taskId}`, {
  275. method: 'GET',
  276. headers: {
  277. 'X-Requested-With': 'XMLHttpRequest',
  278. 'Accept': 'application/json',
  279. }
  280. })
  281. .then(response => response.json())
  282. .then(data => {
  283. console.log('[QuestionGen] 回调数据:', data);
  284. // ✅ 如果有状态字段,说明回调已收到
  285. if (data.status) {
  286. if (data.status === 'completed') {
  287. console.log('[QuestionGen] ✅ 任务完成');
  288. @this.set('isGenerating', false);
  289. @this.set('currentTaskId', null);
  290. // 显示成功通知
  291. setTimeout(() => {
  292. window.location.reload();
  293. }, 1000);
  294. } else if (data.status === 'failed') {
  295. console.log('[QuestionGen] ❌ 任务失败');
  296. @this.set('isGenerating', false);
  297. @this.set('currentTaskId', null);
  298. }
  299. } else if (checkCount < maxChecks) {
  300. // 没收到回调,继续检查
  301. setTimeout(checkCallbackStatus, 3000);
  302. } else {
  303. // 达到最大检查次数,停止
  304. console.log('[QuestionGen] 检查超时,停止监控');
  305. @this.set('isGenerating', false);
  306. @this.set('currentTaskId', null);
  307. }
  308. })
  309. .catch(error => {
  310. console.error('[QuestionGen] 检查回调失败:', error);
  311. if (checkCount < maxChecks) {
  312. setTimeout(checkCallbackStatus, 3000);
  313. }
  314. });
  315. }
  316. // 立即检查一次
  317. checkCallbackStatus();
  318. // 15秒后强制停止
  319. setTimeout(() => {
  320. if (checkCount < maxChecks) {
  321. console.log('[QuestionGen] 强制停止监控');
  322. @this.set('isGenerating', false);
  323. @this.set('currentTaskId', null);
  324. }
  325. }, 15000);
  326. });
  327. // 监听强制关闭状态栏事件
  328. Livewire.on('force-close-status-bar', () => {
  329. console.log('[QuestionGen] 强制关闭状态栏');
  330. @this.set('isGenerating', false);
  331. @this.set('currentTaskId', null);
  332. });
  333. });
  334. </script>
  335. </div>
  336. <x-math-render />
  337. </x-filament-panels::page>