ChildPageDisplay.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  1. <template>
  2. <div class="child-page-display">
  3. <div v-if="loading" class="loading-overlay">
  4. <div class="loading-spinner"></div>
  5. <span class="loading-text">{{ lang.ssAiWait }}</span>
  6. </div>
  7. <div class="child-page-header">
  8. <div class="header-left">
  9. <span class="badge">{{ lang.ssWaitSelection }}</span>
  10. <span class="hint">{{ formatHint(lang.ssWaitSelectionHint, toolsArray.length) }}</span>
  11. </div>
  12. <div class="header-right">
  13. <button v-for="(tool, index) in toolsArray" :key="tool.tool_id" class="candidate-tag"
  14. :class="{ active: selectedToolIndex === index }" @click="selectTool(index)">
  15. {{ lang.ssCandidate }} {{ fillDigit(index + 1, 2) }} {{ tool.tool_name }}
  16. </button>
  17. </div>
  18. </div>
  19. <div class="child-page-body" v-if="toolsJson?.url === toolsArray[selectedToolIndex]?.url">
  20. <div class="question-area">
  21. <iframe
  22. ref="iframeRef"
  23. :src="toolsJson?.url + '&isSet=2'"
  24. frameborder="0"></iframe>
  25. </div>
  26. <div class="side-panel" v-if="!toolsJson.isSet">
  27. <button class="side-btn primary" @click="saveTool(toolsJson.url || '', currentToolType, selectedToolIndex)">{{ lang.ssKeep }}</button>
  28. <button class="side-btn secondary" @click="discardTool(selectedToolIndex)">{{ lang.ssDiscard }}</button>
  29. <div class="divider"></div>
  30. <button class="side-btn link" @click="keepAllTools">{{ lang.ssKeepAll }}</button>
  31. </div>
  32. </div>
  33. </div>
  34. </template>
  35. <script lang="ts" setup>
  36. import { ref, onMounted, watch, computed } from 'vue'
  37. import { storeToRefs } from 'pinia'
  38. import { useMainStore } from '@/store'
  39. import { fillDigit } from '@/utils/common'
  40. import { lang } from '@/main'
  41. import { useSlidesStore } from '@/store/slides'
  42. import { chat_no_stream, chat_no_stream2 } from '@/tools/aiChat'
  43. const slidesStore = useSlidesStore()
  44. const mainStore = useMainStore()
  45. const { childPageToolsArray, userid, courseTitle, childPageIndex } = storeToRefs(mainStore)
  46. const { setChildPageState } = mainStore
  47. const { currentSlide, slideIndex } = storeToRefs(slidesStore)
  48. const toolsArray = childPageToolsArray.value
  49. const selectedToolIndex = ref(0)
  50. const agentid2 = ref('f86aa63c-b7b7-4d03-9b37-b59f116d36f3')// 生成内容
  51. const loading = ref(true)
  52. const iframeRef = ref<HTMLIFrameElement>()
  53. const toolsJson = computed(() => {
  54. return toolsArray[selectedToolIndex.value] || {}
  55. })
  56. const formatHint = (str: string, count: number) => {
  57. return str.replace('{0}', String(count))
  58. }
  59. const selectTool = (index: number) => {
  60. selectedToolIndex.value = index
  61. if (iframeRef.value) {
  62. iframeRef.value.contentWindow?.location.reload()
  63. }
  64. console.log('👶 选中工具:', toolsArray[index])
  65. }
  66. onMounted(async () => {
  67. console.log('👶 带选页打开,toolsArray:', toolsArray)
  68. const toolsToCreate = toolsArray.filter(t => !t.url)
  69. if (toolsToCreate.length === 0) {
  70. loading.value = false
  71. return
  72. }
  73. const createPromises = toolsToCreate.map((tool) => {
  74. const originalIndex = toolsArray.findIndex(t => t.tool_id === tool.tool_id)
  75. return createTool(toolsArray, tool, originalIndex)
  76. })
  77. await Promise.all(createPromises)
  78. loading.value = false
  79. })
  80. watch(childPageToolsArray, async (newVal) => {
  81. console.log('👶 带选页 toolsArray 更新:', newVal)
  82. const toolsToCreate = newVal.filter(t => !t.url)
  83. if (toolsToCreate.length === 0) {
  84. loading.value = false
  85. return
  86. }
  87. loading.value = true
  88. const createPromises = toolsToCreate.map((tool) => {
  89. const originalIndex = newVal.findIndex(t => t.tool_id === tool.tool_id)
  90. return createTool(newVal, tool, originalIndex)
  91. })
  92. await Promise.all(createPromises)
  93. loading.value = false
  94. })
  95. const createTool = async (array: any[], tool: any, index: number) => {
  96. let action = ''
  97. let type = 0
  98. if (tool.tool_id === 'choice') {
  99. action = lang.ssAiChatQuickAction1
  100. type = 45
  101. }
  102. else if (tool.tool_id === 'qa') {
  103. action = lang.ssAiChatQuickAction2
  104. type = 15
  105. }
  106. else if (tool.tool_id === 'poll') {
  107. action = lang.ssAiChatQuickAction6
  108. type = 78
  109. }
  110. else if (tool.tool_id === 'photo') {
  111. action = lang.ssAiChatQuickAction7
  112. type = 79
  113. }
  114. const textContents = currentSlide.value?.elements
  115. .filter((element: any) => element.type === 'text' || (element.type === 'shape' && element.text && element.text.content))
  116. .map((textElement: any) => {
  117. if (textElement.type === 'shape') {
  118. const tempElement = document.createElement('div')
  119. tempElement.innerHTML = textElement.text.content
  120. return tempElement.textContent || tempElement.innerText || ''
  121. }
  122. const tempElement = document.createElement('div')
  123. tempElement.innerHTML = textElement.content
  124. return tempElement.textContent || tempElement.innerText || ''
  125. })
  126. .filter(content => content.trim() !== '') || []
  127. console.log('textContents', textContents)
  128. const prompt = `
  129. #课程标题(course_title): ${courseTitle.value || ''}
  130. #当前页面内容(current_page_content): ${textContents.length > 0 ? textContents.join('\n') : '无文本内容'}
  131. #query: ${action}
  132. `
  133. const result = chat_no_stream(prompt, agentid2.value, userid.value || '', lang.lang)
  134. console.log(result)
  135. const content = await result.promise
  136. console.log('👶 创建工具:', content)
  137. let jsonFormat = ''
  138. if (tool.tool_id === 'choice') {
  139. jsonFormat = `{"testCount":1,"testTitle":"","testJson":[{"id":"7de1fdb4-bec3-4324-8986-4623f838e3d7","type":"2","teststitle":"1+1?","checkList":["1","2","3"],"timuList":[],"answer":[1],"userAnswer":[],"explanation":"解析"}]}`
  140. }
  141. else if (tool.tool_id === 'qa') {
  142. jsonFormat = `{"answerQ":"问题","answer":"","fileList":[],"imageList":[],"evaluationCriteria":"评价标准"}`
  143. }
  144. else if (tool.tool_id === 'poll') {
  145. jsonFormat = `{"testCount":1,"testTitle":"","testJson":[{"id":"7de1fdb4-bec3-4324-8986-4623f838e3d7","type":"2","teststitle":"1+1?","checkList":["1","2","3"],"timuList":[],"answer":[],"userAnswer":[]}]}`
  146. }
  147. else if (tool.tool_id === 'photo') {
  148. jsonFormat = `{"answerQ":"问题","answer":"","fileList":[]}`
  149. }
  150. const res = await chat_no_stream2([{
  151. role: 'user',
  152. content: `这是用户输入的内容:"${content}",根据用户输入的内容,生成json。输出一个json格式的回复,格式如下:${jsonFormat}。`,
  153. }], { type: 'json_object' })
  154. console.log(`${tool.tool_name}`, JSON.parse(res))
  155. const pageId = await setPageId(type, res)
  156. const baseUrl = setUrl()
  157. const url = `${baseUrl}/pbl-teacher-table/dist/workPage.html#/setWorkPage?id=${pageId}&type=${type}`
  158. array[index].url = url
  159. setTool()
  160. }
  161. const setTool = () => {
  162. const slide = currentSlide.value
  163. slidesStore.updateSlide({ toolsArray: toolsArray }, slide.id)
  164. console.log(slide)
  165. // 判断所有工具是否都已保留
  166. const allToolsSet = toolsArray.every(t => t.isSet)
  167. if (allToolsSet) {
  168. const slide = currentSlide.value
  169. slidesStore.updateSlide({ toolsArray: [] }, slide.id)
  170. setChildPageState(false, [], slideIndex.value || 0)
  171. }
  172. }
  173. const setUrl = () => {
  174. let url = 'https://beta.pbl.cocorobo.cn'
  175. if (window.location.href.includes('beta')) {
  176. url = 'https://beta.pbl.cocorobo.cn/'
  177. }
  178. else if (lang.lang === 'cn') {
  179. url = 'https://pbl.cocorobo.cn/'
  180. }
  181. else if (lang.lang === 'hk') {
  182. url = 'https://pbl.cocorobo.hk/'
  183. }
  184. else if (lang.lang === 'en') {
  185. url = 'https://pbl.cocorobo.com/'
  186. }
  187. return url
  188. }
  189. import { getWorkPageId } from '@/services/course'
  190. const setPageId = async (tool: any, json: any) => {
  191. const res = await getWorkPageId({
  192. userid: userid.value || '',
  193. type: tool,
  194. json: json
  195. })
  196. return res[0][0].id
  197. }
  198. import useCreateElement from '@/hooks/useCreateElement'
  199. import useSlideHandler from '@/hooks/useSlideHandler'
  200. const { createSlide } = useSlideHandler()
  201. const { createFrameElement } = useCreateElement()
  202. // 切换页面
  203. const changeSlideIndex = (index: number) => {
  204. mainStore.setActiveElementIdList([])
  205. if (slideIndex.value === index) return
  206. slidesStore.updateSlideIndex(index)
  207. }
  208. // 保留工具
  209. const saveTool = (url: string, type: any, index: number) => {
  210. createSlide()
  211. createFrameElement(url, type)
  212. changeSlideIndex(childPageIndex.value)
  213. toolsArray[index].isSet = true
  214. setTool()
  215. }
  216. // 放弃工具
  217. const discardTool = (index: number) => {
  218. toolsArray[index].isSet = true
  219. setTool()
  220. }
  221. // 保留所有工具
  222. const keepAllTools = () => {
  223. toolsArray.forEach(tool => {
  224. if (!tool.isSet) {
  225. createSlide()
  226. createFrameElement(tool.url, getToolType(tool.tool_id))
  227. tool.isSet = true
  228. }
  229. })
  230. changeSlideIndex(childPageIndex.value)
  231. setTool()
  232. }
  233. // 判断工具type
  234. const toolTypeMap: Record<string, number> = {
  235. 'choice': 45,
  236. 'qa': 15,
  237. 'poll': 78,
  238. 'photo': 79
  239. }
  240. const currentToolType = computed(() => {
  241. const toolId = toolsArray[selectedToolIndex.value]?.tool_id
  242. return toolId ? toolTypeMap[toolId] || 0 : 0
  243. })
  244. const getToolType = (toolId: string) => {
  245. return toolTypeMap[toolId] || 0
  246. }
  247. </script>
  248. <style lang="scss" scoped>
  249. .child-page-display {
  250. width: 100%;
  251. height: 100%;
  252. display: flex;
  253. flex-direction: column;
  254. background-color: #fffbeb;
  255. position: absolute;
  256. top: 0;
  257. left: 0;
  258. border-radius: 3px;
  259. overflow: hidden;
  260. }
  261. .child-page-header {
  262. display: flex;
  263. justify-content: space-between;
  264. align-items: center;
  265. padding: 7px 20px;
  266. background: linear-gradient(180deg, #fcd34d 0%, #f59e0b 100%);
  267. .header-left {
  268. display: flex;
  269. align-items: center;
  270. gap: 10px;
  271. }
  272. .badge {
  273. background-color: #d97706;
  274. color: white;
  275. padding: 4px 12px;
  276. border-radius: 4px;
  277. font-size: 12px;
  278. font-weight: 600;
  279. }
  280. .hint {
  281. color: #78350f;
  282. font-size: 14px;
  283. }
  284. .header-right {
  285. display: flex;
  286. gap: 8px;
  287. background: #d97706;
  288. padding: 5px 10px;
  289. border-radius: 30px;
  290. }
  291. .candidate-tag {
  292. background-color: #d97706;
  293. color: white;
  294. padding: 4px 12px;
  295. border-radius: 4px;
  296. font-size: 12px;
  297. font-weight: 500;
  298. border: none;
  299. cursor: pointer;
  300. transition: all 0.2s;
  301. border-radius: 30px;
  302. &:hover {
  303. background-color: #b45309;
  304. }
  305. &.active {
  306. background-color: white;
  307. color: #d97706;
  308. }
  309. }
  310. }
  311. .child-page-body {
  312. flex: 1;
  313. display: flex;
  314. padding: 20px;
  315. gap: 20px;
  316. overflow: auto;
  317. }
  318. .question-area {
  319. flex: 1;
  320. background-color: white;
  321. border: 2px solid #f59e0b;
  322. border-radius: 8px;
  323. padding: 20px;
  324. box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1);
  325. iframe {
  326. width: 100%;
  327. height: 100%;
  328. border: none;
  329. }
  330. }
  331. .loading-overlay {
  332. position: absolute;
  333. top: 0;
  334. left: 0;
  335. right: 0;
  336. bottom: 0;
  337. background-color: rgba(255, 251, 235, 0.95);
  338. display: flex;
  339. flex-direction: column;
  340. justify-content: center;
  341. align-items: center;
  342. gap: 16px;
  343. z-index: 100;
  344. }
  345. .loading-spinner {
  346. width: 48px;
  347. height: 48px;
  348. border: 4px solid #f59e0b;
  349. border-top-color: transparent;
  350. border-radius: 50%;
  351. animation: spin 1s linear infinite;
  352. }
  353. .loading-text {
  354. color: #d97706;
  355. font-size: 16px;
  356. font-weight: 500;
  357. }
  358. @keyframes spin {
  359. to {
  360. transform: rotate(360deg);
  361. }
  362. }
  363. .side-panel {
  364. width: 90px;
  365. display: flex;
  366. flex-direction: column;
  367. gap: 12px;
  368. .side-btn {
  369. padding: 12px 20px;
  370. border: none;
  371. border-radius: 8px;
  372. font-size: 14px;
  373. font-weight: 500;
  374. cursor: pointer;
  375. transition: all 0.2s;
  376. &.primary {
  377. background-color: #f59e0b;
  378. color: white;
  379. &:hover {
  380. background-color: #d97706;
  381. }
  382. }
  383. &.secondary {
  384. background-color: white;
  385. color: #6b7280;
  386. border: 1px solid #e5e7eb;
  387. &:hover {
  388. border-color: #f59e0b;
  389. color: #f59e0b;
  390. }
  391. }
  392. &.link {
  393. background-color: transparent;
  394. color: #f59e0b;
  395. text-align: left;
  396. padding-left: 0;
  397. &:hover {
  398. text-decoration: underline;
  399. }
  400. }
  401. }
  402. .divider {
  403. height: 1px;
  404. background-color: #e5e7eb;
  405. margin: 4px 0;
  406. }
  407. }
  408. </style>