question-generation.blade.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. <x-filament-panels::page>
  2. <!-- 数学公式渲染组件 -->
  3. <x-math-render />
  4. <div class="space-y-6">
  5. <!-- 后台生成状态栏 - 仅在生成中显示 -->
  6. @if($isGenerating && $currentTaskId)
  7. <div class="bg-blue-50 border-l-4 border-blue-400 p-4 rounded-r-lg animate-pulse">
  8. <div class="flex items-center">
  9. <div class="animate-spin rounded-full h-5 w-5 border-b-2 border-blue-600 mr-3"></div>
  10. <div class="flex-1">
  11. <p class="text-sm text-blue-800">
  12. <strong>正在后台生成题目...</strong>
  13. </p>
  14. <p class="text-xs text-blue-600 mt-1">
  15. 任务 ID: {{ $currentTaskId }} | AI生成完成后将自动刷新页面
  16. </p>
  17. </div>
  18. <button type="button" wire:click="$set('isGenerating', false)" class="text-blue-400 hover:text-blue-600">
  19. <svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
  20. <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M6 18L18 6M6 6l12 12"></path>
  21. </svg>
  22. </button>
  23. </div>
  24. </div>
  25. @endif
  26. <!-- 生成表单 -->
  27. <div class="bg-white p-6 rounded-lg border">
  28. <h2 class="text-lg font-semibold mb-4">生成配置</h2>
  29. <div class="space-y-4">
  30. <!-- 知识点选择 -->
  31. <div>
  32. <label class="block text-sm font-medium text-gray-700 mb-2">
  33. 知识点 <span class="text-red-500">*</span>
  34. </label>
  35. <select wire:model.live="generateKpCode" class="w-full border rounded p-2">
  36. <option value="">选择知识点</option>
  37. @foreach($this->knowledgePointOptions as $code => $name)
  38. <option value="{{ $code }}">{{ $code }} - {{ $name }}</option>
  39. @endforeach
  40. </select>
  41. </div>
  42. <!-- 技能选择 -->
  43. @if(!empty($this->skillsOptions))
  44. <div>
  45. <div class="flex items-center justify-between mb-2">
  46. <label class="block text-sm font-medium">
  47. 选择技能 <span class="text-red-500">*</span>
  48. <span class="text-xs text-gray-500 ml-2">({{ count($selectedSkills) }}/{{ count($this->skillsOptions) }} 已选)</span>
  49. </label>
  50. <button type="button"
  51. class="text-sm text-blue-600 hover:underline {{ count($selectedSkills) === count($this->skillsOptions) && count($this->skillsOptions) > 0 ? 'font-semibold' : '' }}"
  52. wire:click="toggleAllSkills">
  53. {{ count($selectedSkills) === count($this->skillsOptions) && count($this->skillsOptions) > 0 ? '取消全选' : '全选' }}
  54. </button>
  55. </div>
  56. <div class="max-h-64 overflow-y-auto border rounded p-3 grid grid-cols-2 gap-2">
  57. @foreach($this->skillsOptions as $skill)
  58. <label class="flex items-center space-x-2">
  59. <input type="checkbox"
  60. value="{{ $skill['code'] }}"
  61. @checked(in_array($skill['code'], $selectedSkills, true))
  62. wire:model="selectedSkills"
  63. class="rounded border-gray-300 text-primary focus:ring-primary">
  64. <span class="text-sm">
  65. <span class="font-medium">{{ $skill['code'] }}</span>
  66. <span class="text-gray-600 ml-2">{{ $skill['name'] }}</span>
  67. <span class="text-xs text-gray-400 ml-2">(权重: {{ $skill['weight'] ?? 1 }})</span>
  68. </span>
  69. </label>
  70. @endforeach
  71. </div>
  72. </div>
  73. @else
  74. <div class="bg-blue-50 border border-blue-200 rounded p-3">
  75. <div class="flex items-start">
  76. <svg class="w-5 h-5 text-blue-600 mt-0.5 mr-2" fill="currentColor" viewBox="0 0 20 20">
  77. <path fill-rule="evenodd" d="M18 10a8 8 0 11-16 0 8 8 0 0116 0zm-7-4a1 1 0 11-2 0 1 1 0 012 0zM9 9a1 1 0 000 2v3a1 1 0 001 1h1a1 1 0 100-2v-3a1 1 0 00-1-1H9z" clip-rule="evenodd"/>
  78. </svg>
  79. <div>
  80. <p class="text-sm text-blue-800 font-medium">
  81. 该知识点暂未关联技能
  82. </p>
  83. <p class="text-xs text-blue-700 mt-1">
  84. 将基于知识点本身生成题目,无需额外技能限制
  85. </p>
  86. </div>
  87. </div>
  88. </div>
  89. @endif
  90. <!-- 题目数量 -->
  91. <div>
  92. <label class="block text-sm font-medium text-gray-700 mb-2">题目数量</label>
  93. <input type="number" wire:model="questionCount" min="1" max="500" class="w-full border rounded p-2">
  94. </div>
  95. <!-- 生成按钮 -->
  96. <div class="flex justify-end pt-4">
  97. <button
  98. type="button"
  99. wire:click="executeGenerate"
  100. wire:loading.attr="disabled"
  101. wire:loading.class="bg-yellow-500 cursor-not-allowed opacity-90"
  102. wire:loading.class.remove="bg-blue-600 hover:bg-blue-700"
  103. wire:target="executeGenerate"
  104. 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"
  105. >
  106. @if($isGenerating)
  107. <svg class="animate-spin h-4 w-4 text-white" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24">
  108. <circle class="opacity-25" cx="12" cy="12" r="10" stroke="currentColor" stroke-width="4"></circle>
  109. <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>
  110. </svg>
  111. <span class="text-white font-semibold">生成中...</span>
  112. @else
  113. <svg class="w-4 h-4 text-white" fill="none" stroke="currentColor" viewBox="0 0 24 24">
  114. <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 4v16m8-8H4"></path>
  115. </svg>
  116. <span class="text-white font-semibold">开始生成</span>
  117. @endif
  118. </button>
  119. </div>
  120. </div>
  121. </div>
  122. </div>
  123. <script>
  124. document.addEventListener('livewire:init', () => {
  125. // ✅ 捕获回调参数,直接检查状态 - 避免盲目轮询
  126. Livewire.on('start-async-task-monitoring', () => {
  127. console.log('[QuestionGen] 开始监控任务状态');
  128. const taskId = @this.currentTaskId;
  129. if (!taskId) {
  130. console.error('[QuestionGen] 未找到任务ID');
  131. return;
  132. }
  133. window.currentTaskId = taskId;
  134. let checkCount = 0;
  135. const maxChecks = 5; // 最多检查5次
  136. function checkCallbackStatus() {
  137. checkCount++;
  138. console.log(`[QuestionGen] 检查回调 #${checkCount}/${maxChecks}`);
  139. // 直接调用 API 检查回调数据 - GET 请求无需 CSRF
  140. fetch(`/api/questions/callback/${taskId}`, {
  141. method: 'GET',
  142. headers: {
  143. 'X-Requested-With': 'XMLHttpRequest',
  144. 'Accept': 'application/json',
  145. }
  146. })
  147. .then(response => response.json())
  148. .then(data => {
  149. console.log('[QuestionGen] 回调数据:', data);
  150. // ✅ 如果有状态字段,说明回调已收到
  151. if (data.status) {
  152. if (data.status === 'completed') {
  153. console.log('[QuestionGen] ✅ 任务完成');
  154. @this.set('isGenerating', false);
  155. @this.set('currentTaskId', null);
  156. // 显示成功通知
  157. setTimeout(() => {
  158. window.location.reload();
  159. }, 1000);
  160. } else if (data.status === 'failed') {
  161. console.log('[QuestionGen] ❌ 任务失败');
  162. @this.set('isGenerating', false);
  163. @this.set('currentTaskId', null);
  164. }
  165. } else if (checkCount < maxChecks) {
  166. // 没收到回调,继续检查
  167. setTimeout(checkCallbackStatus, 3000);
  168. } else {
  169. // 达到最大检查次数,停止
  170. console.log('[QuestionGen] 检查超时,停止监控');
  171. @this.set('isGenerating', false);
  172. @this.set('currentTaskId', null);
  173. }
  174. })
  175. .catch(error => {
  176. console.error('[QuestionGen] 检查回调失败:', error);
  177. if (checkCount < maxChecks) {
  178. setTimeout(checkCallbackStatus, 3000);
  179. }
  180. });
  181. }
  182. // 立即检查一次
  183. checkCallbackStatus();
  184. // 15秒后强制停止
  185. setTimeout(() => {
  186. if (checkCount < maxChecks) {
  187. console.log('[QuestionGen] 强制停止监控');
  188. @this.set('isGenerating', false);
  189. @this.set('currentTaskId', null);
  190. }
  191. }, 15000);
  192. });
  193. // 监听强制关闭状态栏事件
  194. Livewire.on('force-close-status-bar', () => {
  195. console.log('[QuestionGen] 强制关闭状态栏');
  196. @this.set('isGenerating', false);
  197. @this.set('currentTaskId', null);
  198. });
  199. });
  200. </script>
  201. </x-filament-panels::page>