ChildPageDisplay.vue 12 KB

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