diff --git a/api/customerServicePlatform/customerServicePlatform.js b/api/customerServicePlatform/customerServicePlatform.js index 0f20be0..0345ab0 100644 --- a/api/customerServicePlatform/customerServicePlatform.js +++ b/api/customerServicePlatform/customerServicePlatform.js @@ -14,7 +14,7 @@ export const uploadImageApi = (data) => { export const getAnswerApi = (data) => { return http({ method: 'POST', - url: 'http://pbb6edde.natappfree.cc' +'/api/customer/askQuestion', + url: '/api/customer/askQuestion', data }) } @@ -23,7 +23,7 @@ export const getAnswerApi = (data) => { export const getQuestionApi = (data) => { return http({ method: 'GET', - url: 'http://pbb6edde.natappfree.cc' +'/api/customer/getQuestion', + url: '/api/customer/getQuestion', }) } diff --git a/pages/customerServicePlatform/questionDetail.vue b/pages/customerServicePlatform/questionDetail.vue index cedeecb..2bbba38 100644 --- a/pages/customerServicePlatform/questionDetail.vue +++ b/pages/customerServicePlatform/questionDetail.vue @@ -45,9 +45,12 @@ mode="aspectFit"> - + + + + @@ -65,6 +68,8 @@ import { getAnswerApi } from "../../api/customerServicePlatform/customerServicePlatform"; + import marked from "marked"; // 引入 marked 库 + export default { data() { return { @@ -72,7 +77,7 @@ iSMT: 0, questionTitle: '', answerContent: '正在思考...', - showLoginRegister:false, + showLoginRegister: false, }; }, mounted() { @@ -83,13 +88,36 @@ if (options.question) { this.questionTitle = decodeURIComponent(options.question); if (this.questionTitle.includes("如何注册")) { - this.showLoginRegister = true + this.showLoginRegister = true } else { - this.showLoginRegister = false + this.showLoginRegister = false } } }, methods: { + renderMarkdown(content) { + const renderer = new marked.Renderer(); + // renderer.heading = function (text, level) { + // return `

${text}

`; + // }; + // 设置 marked 选项 + marked.setOptions({ + renderer: renderer, + highlight: null, // 如果需要代码高亮,可以设置适当的函数 + langPrefix: "language-", + pedantic: false, + gfm: true, + breaks: false, + sanitize: false, + smartLists: true, + smartypants: false, + xhtml: false, + }); + if (!content) return ""; + let renderedContent = marked.parse(content); + renderedContent = renderedContent.replace(/\*/g, ''); + return renderedContent; + }, async getAnswerContent() { let conversationId = ''; try { @@ -98,29 +126,40 @@ } catch (e) { conversationId = ''; } - - const res = await getAnswerApi({ - question: this.questionTitle, - conversationId: conversationId, - }) - console.log(res) - - if (res.code == 200) { - uni.setStorageSync('conversationId', res.data.conversationId); - const answer = res.data.answer - this.answerContent = ''; - for (let i = 0; i < answer.length; i++) { - this.answerContent += answer[i]; - await this.sleepTime(); + try { + const res = await getAnswerApi({ + question: this.questionTitle, + conversationId: conversationId, + }) + console.log(res) + + if (res.code == 200) { + uni.setStorageSync('conversationId', res.data.conversationId); + const answer = res.data.answer + // 流式输出逻辑 + let currentIndex = 0; + const answerLength = answer.length; + + // 每隔一定时间显示一部分内容 + this.interval = setInterval(() => { + this.answerContent = answer.slice(0, currentIndex); + currentIndex++; + + if (currentIndex > answerLength) { + clearInterval(this.interval); + } + }, Math.floor(Math.random() * (150 - 30 + 1)) + 30); + } else { + this.answerContent = '获取回答失败,请重试'; } - } else { + } catch { this.answerContent = '获取回答失败,请重试'; } }, - async sleepTime() { - const ms = Math.floor(Math.random() * (300 - 30 + 1)) + 30; - return new Promise(resolve => setTimeout(resolve, ms)); - }, + sleepTime() { + const ms = Math.floor(Math.random() * (300 - 30 + 1)) + 30; + return ms; + }, toRegistration() { uni.redirectTo({