question-generation.blade.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. <div>
  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>
  97. <div class="flex items-center justify-between mb-2">
  98. <label class="block text-sm font-medium text-gray-700">
  99. 提示词模板
  100. </label>
  101. <a href="{{ route('filament.admin.pages.prompt-management') }}"
  102. class="text-xs text-blue-600 hover:underline"
  103. target="_blank">
  104. 去提示词管理
  105. </a>
  106. </div>
  107. <select wire:model="promptTemplate" class="w-full border rounded p-2">
  108. <option value="">使用默认模板</option>
  109. @foreach($this->promptOptions as $name => $label)
  110. <option value="{{ $name }}">{{ $label }}</option>
  111. @endforeach
  112. </select>
  113. <p class="text-xs text-gray-500 mt-1">仅展示激活的“题目生成”模板,后台调整后可即时生效。</p>
  114. </div>
  115. <!-- 生成按钮 -->
  116. <div class="flex justify-end pt-4">
  117. <button
  118. type="button"
  119. onclick="handleGenerateWithRedirect()"
  120. wire:click="executeGenerate"
  121. wire:loading.attr="disabled"
  122. wire:loading.class="bg-yellow-500 cursor-not-allowed opacity-90"
  123. wire:loading.class.remove="bg-blue-600 hover:bg-blue-700"
  124. wire:target="executeGenerate"
  125. 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"
  126. id="generate-button"
  127. >
  128. @if($isGenerating)
  129. <svg class="animate-spin h-4 w-4 text-white" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24">
  130. <circle class="opacity-25" cx="12" cy="12" r="10" stroke="currentColor" stroke-width="4"></circle>
  131. <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>
  132. </svg>
  133. <span class="text-white font-semibold">生成中...</span>
  134. @else
  135. <svg class="w-4 h-4 text-white" fill="none" stroke="currentColor" viewBox="0 0 24 24">
  136. <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 4v16m8-8H4"></path>
  137. </svg>
  138. <span class="text-white font-semibold">开始生成</span>
  139. @endif
  140. </button>
  141. </div>
  142. </div>
  143. </div>
  144. </div>
  145. <script>
  146. // 全局跳转函数(备用方案)
  147. function redirectAfterGeneration(url, taskId) {
  148. console.log('[QuestionGen-Global] 收到跳转请求:', url, taskId);
  149. if (taskId && taskId !== 'unknown') {
  150. alert(`✅ 任务已启动\n任务 ID: ${taskId}\n正在跳转到题库管理页面...`);
  151. }
  152. if (url) {
  153. console.log('[QuestionGen-Global] 准备跳转到:', url);
  154. setTimeout(() => {
  155. console.log('[QuestionGen-Global] 执行页面跳转...');
  156. window.location.href = url;
  157. }, 500);
  158. }
  159. }
  160. // 直接处理生成并跳转的函数
  161. function handleGenerateWithRedirect() {
  162. console.log('[QuestionGen] 开始处理生成和跳转');
  163. // 禁用按钮防止重复点击
  164. const button = document.getElementById('generate-button');
  165. if (button) {
  166. button.disabled = true;
  167. button.innerHTML = '<svg class="animate-spin h-4 w-4 text-white" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24"><circle class="opacity-25" cx="12" cy="12" r="10" stroke="currentColor" stroke-width="4"></circle><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></svg><span class="text-white font-semibold">生成中...</span>';
  168. }
  169. // 0.5秒后自动跳转(不管Livewire是否成功)
  170. setTimeout(() => {
  171. console.log('[QuestionGen] 0.5秒后自动跳转到题库管理页面');
  172. // 直接跳转,不传递task_id
  173. window.location.href = '/admin/question-management';
  174. }, 500);
  175. }
  176. // 将函数暴露到全局作用域
  177. window.redirectAfterGeneration = redirectAfterGeneration;
  178. document.addEventListener('livewire:init', () => {
  179. console.log('[QuestionGen] Livewire已初始化,开始绑定事件监听器');
  180. // ✅ 监听跳转事件,生成成功后立即跳转
  181. Livewire.on('redirect-now', (event) => {
  182. console.log('[QuestionGen] ✅ 收到JavaScript跳转事件:', event);
  183. console.log('[QuestionGen] URL:', event.url, 'TaskID:', event.taskId);
  184. console.log('[QuestionGen] 事件类型:', typeof event, '事件详情:', JSON.stringify(event));
  185. if (event.url && event.taskId) {
  186. console.log('[QuestionGen] ✅ 事件参数验证通过,准备跳转');
  187. redirectAfterGeneration(event.url, event.taskId);
  188. } else {
  189. console.error('[QuestionGen] ❌ 事件参数不完整:', {
  190. 'url': event.url,
  191. 'taskId': event.taskId,
  192. 'event': event
  193. });
  194. }
  195. });
  196. console.log('[QuestionGen] 事件监听器绑定完成');
  197. // 添加全局调试函数
  198. window.testRedirect = function() {
  199. console.log('[QuestionGen] 手动测试跳转');
  200. redirectAfterGeneration('/admin/question-management', 'test-task-id');
  201. };
  202. console.log('[QuestionGen] 调试函数已设置,可使用 testRedirect() 手动测试跳转');
  203. // ✅ 捕获回调参数,直接检查状态 - 避免盲目轮询
  204. Livewire.on('start-async-task-monitoring', () => {
  205. console.log('[QuestionGen] 开始监控任务状态');
  206. const taskId = @this.currentTaskId;
  207. if (!taskId) {
  208. console.error('[QuestionGen] 未找到任务ID');
  209. return;
  210. }
  211. window.currentTaskId = taskId;
  212. let checkCount = 0;
  213. const maxChecks = 5; // 最多检查5次
  214. function checkCallbackStatus() {
  215. checkCount++;
  216. console.log(`[QuestionGen] 检查回调 #${checkCount}/${maxChecks}`);
  217. // 直接调用 API 检查回调数据 - GET 请求无需 CSRF
  218. fetch(`/api/questions/callback/${taskId}`, {
  219. method: 'GET',
  220. headers: {
  221. 'X-Requested-With': 'XMLHttpRequest',
  222. 'Accept': 'application/json',
  223. }
  224. })
  225. .then(response => response.json())
  226. .then(data => {
  227. console.log('[QuestionGen] 回调数据:', data);
  228. // ✅ 如果有状态字段,说明回调已收到
  229. if (data.status) {
  230. if (data.status === 'completed') {
  231. console.log('[QuestionGen] ✅ 任务完成');
  232. @this.set('isGenerating', false);
  233. @this.set('currentTaskId', null);
  234. // 显示成功通知
  235. setTimeout(() => {
  236. window.location.reload();
  237. }, 1000);
  238. } else if (data.status === 'failed') {
  239. console.log('[QuestionGen] ❌ 任务失败');
  240. @this.set('isGenerating', false);
  241. @this.set('currentTaskId', null);
  242. }
  243. } else if (checkCount < maxChecks) {
  244. // 没收到回调,继续检查
  245. setTimeout(checkCallbackStatus, 3000);
  246. } else {
  247. // 达到最大检查次数,停止
  248. console.log('[QuestionGen] 检查超时,停止监控');
  249. @this.set('isGenerating', false);
  250. @this.set('currentTaskId', null);
  251. }
  252. })
  253. .catch(error => {
  254. console.error('[QuestionGen] 检查回调失败:', error);
  255. if (checkCount < maxChecks) {
  256. setTimeout(checkCallbackStatus, 3000);
  257. }
  258. });
  259. }
  260. // 立即检查一次
  261. checkCallbackStatus();
  262. // 15秒后强制停止
  263. setTimeout(() => {
  264. if (checkCount < maxChecks) {
  265. console.log('[QuestionGen] 强制停止监控');
  266. @this.set('isGenerating', false);
  267. @this.set('currentTaskId', null);
  268. }
  269. }, 15000);
  270. });
  271. // 监听强制关闭状态栏事件
  272. Livewire.on('force-close-status-bar', () => {
  273. console.log('[QuestionGen] 强制关闭状态栏');
  274. @this.set('isGenerating', false);
  275. @this.set('currentTaskId', null);
  276. });
  277. });
  278. </script>
  279. </div>