|
|
|
@ -45,9 +45,12 @@ |
|
|
|
mode="aspectFit"></image> |
|
|
|
|
|
|
|
<view class="card-text"> |
|
|
|
<text class="card-paragraph"> |
|
|
|
|
|
|
|
<rich-text :nodes="renderMarkdown(answerContent)"></rich-text> |
|
|
|
|
|
|
|
<!-- <text class="card-paragraph"> |
|
|
|
{{answerContent}} |
|
|
|
</text> |
|
|
|
</text> --> |
|
|
|
</view> |
|
|
|
</view> |
|
|
|
|
|
|
|
@ -65,6 +68,8 @@ |
|
|
|
import { |
|
|
|
getAnswerApi |
|
|
|
} from "../../api/customerServicePlatform/customerServicePlatform"; |
|
|
|
import marked from "marked"; // 引入 marked 库 |
|
|
|
|
|
|
|
export default { |
|
|
|
data() { |
|
|
|
return { |
|
|
|
@ -90,6 +95,29 @@ |
|
|
|
} |
|
|
|
}, |
|
|
|
methods: { |
|
|
|
renderMarkdown(content) { |
|
|
|
const renderer = new marked.Renderer(); |
|
|
|
// renderer.heading = function (text, level) { |
|
|
|
// return `<p>${text}</p>`; |
|
|
|
// }; |
|
|
|
// 设置 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,7 +126,7 @@ |
|
|
|
} catch (e) { |
|
|
|
conversationId = ''; |
|
|
|
} |
|
|
|
|
|
|
|
try { |
|
|
|
const res = await getAnswerApi({ |
|
|
|
question: this.questionTitle, |
|
|
|
conversationId: conversationId, |
|
|
|
@ -108,18 +136,29 @@ |
|
|
|
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(); |
|
|
|
// 流式输出逻辑 |
|
|
|
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 = '获取回答失败,请重试'; |
|
|
|
} |
|
|
|
} catch { |
|
|
|
this.answerContent = '获取回答失败,请重试'; |
|
|
|
} |
|
|
|
}, |
|
|
|
async sleepTime() { |
|
|
|
sleepTime() { |
|
|
|
const ms = Math.floor(Math.random() * (300 - 30 + 1)) + 30; |
|
|
|
return new Promise(resolve => setTimeout(resolve, ms)); |
|
|
|
return ms; |
|
|
|
}, |
|
|
|
|
|
|
|
toRegistration() { |
|
|
|
|