Browse Source

markdown格式数据转换

maziyang/feature-20251025172218-智能客服中台
maziyang 3 weeks ago
parent
commit
24ca157d47
  1. 4
      api/customerServicePlatform/customerServicePlatform.js
  2. 57
      pages/customerServicePlatform/questionDetail.vue

4
api/customerServicePlatform/customerServicePlatform.js

@ -14,7 +14,7 @@ export const uploadImageApi = (data) => {
export const getAnswerApi = (data) => { export const getAnswerApi = (data) => {
return http({ return http({
method: 'POST', method: 'POST',
url: 'http://pbb6edde.natappfree.cc' +'/api/customer/askQuestion',
url: '/api/customer/askQuestion',
data data
}) })
} }
@ -23,7 +23,7 @@ export const getAnswerApi = (data) => {
export const getQuestionApi = (data) => { export const getQuestionApi = (data) => {
return http({ return http({
method: 'GET', method: 'GET',
url: 'http://pbb6edde.natappfree.cc' +'/api/customer/getQuestion',
url: '/api/customer/getQuestion',
}) })
} }

57
pages/customerServicePlatform/questionDetail.vue

@ -45,9 +45,12 @@
mode="aspectFit"></image> mode="aspectFit"></image>
<view class="card-text"> <view class="card-text">
<text class="card-paragraph">
<rich-text :nodes="renderMarkdown(answerContent)"></rich-text>
<!-- <text class="card-paragraph">
{{answerContent}} {{answerContent}}
</text>
</text> -->
</view> </view>
</view> </view>
@ -65,6 +68,8 @@
import { import {
getAnswerApi getAnswerApi
} from "../../api/customerServicePlatform/customerServicePlatform"; } from "../../api/customerServicePlatform/customerServicePlatform";
import marked from "marked"; // marked
export default { export default {
data() { data() {
return { return {
@ -90,6 +95,29 @@
} }
}, },
methods: { 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() { async getAnswerContent() {
let conversationId = ''; let conversationId = '';
try { try {
@ -98,7 +126,7 @@
} catch (e) { } catch (e) {
conversationId = ''; conversationId = '';
} }
try {
const res = await getAnswerApi({ const res = await getAnswerApi({
question: this.questionTitle, question: this.questionTitle,
conversationId: conversationId, conversationId: conversationId,
@ -108,18 +136,29 @@
if (res.code == 200) { if (res.code == 200) {
uni.setStorageSync('conversationId', res.data.conversationId); uni.setStorageSync('conversationId', res.data.conversationId);
const answer = res.data.answer 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 { } else {
this.answerContent = '获取回答失败,请重试'; this.answerContent = '获取回答失败,请重试';
} }
} catch {
this.answerContent = '获取回答失败,请重试';
}
}, },
async sleepTime() {
sleepTime() {
const ms = Math.floor(Math.random() * (300 - 30 + 1)) + 30; const ms = Math.floor(Math.random() * (300 - 30 + 1)) + 30;
return new Promise(resolve => setTimeout(resolve, ms));
return ms;
}, },
toRegistration() { toRegistration() {

Loading…
Cancel
Save