feat(ai): ai剧本页添加历史信息获取功能和重新生成功能

This commit is contained in:
fhysy 2025-03-21 15:35:00 +08:00
parent 9de2ef3734
commit 19f29666a7
4 changed files with 95 additions and 20 deletions

View File

@ -5,6 +5,8 @@ export default{
productApi:require("./module/productApi.js").default, productApi:require("./module/productApi.js").default,
// 订单,模块 // 订单,模块
orderApi:require("./module/orderApi.js").default, orderApi:require("./module/orderApi.js").default,
// ai模块
aiApi:require("./module/aiApi.js").default,
/* /*
* 登录相关 * 登录相关
*/ */

View File

@ -0,0 +1,14 @@
import {request} from '../request'
export default {
// 获取商品列表
getAiHistory(businessId){
return new Promise((resolve, reject) => {
request.get(`/ai/chat/history/${businessId}`)
.then((res) =>{
resolve(res);
}).catch(err =>{
reject(err);
})
})
}
}

View File

@ -22,16 +22,26 @@
<text class="message-time">{{ message.time }}</text> <text class="message-time">{{ message.time }}</text>
</view> </view>
<!-- 在每条AI消息后显示确认按钮除了系统消息 --> <!-- 在每条AI消息后显示确认按钮除了系统消息 -->
<view v-if="message.role === 'assistant' && message.endStatus" class="accept-button-container"> <view v-if="message.role === 'assistant' && message.endStatus && index !== 0" class="accept-button-container">
<u-button <u-button
type="success" type="success"
size="mini" size="mini"
@click="acceptScript(message.content)" @click="acceptScript(message.content)"
:custom-style="{marginTop: '10px'}" :custom-style="{marginTop: '10px'}"
> >
<u-icon name="checkmark" size="14" class="button-icon"></u-icon> <u-icon name="checkmark" size="20" class="button-icon"></u-icon>
确认使用此剧本 使用此剧本
</u-button> </u-button>
<u-button
v-if="index === findLastAssistantIndex()"
type="primary"
size="mini"
@click="resetScript(index)"
:custom-style="{marginTop: '10px',marginLeft: '10px'}"
>
<u-icon name="reload" size="20" class="button-icon"></u-icon>
重新生成
</u-button>
</view> </view>
</block> </block>
@ -120,7 +130,7 @@ export default {
needScrollToBottom: true, needScrollToBottom: true,
// //
scrollTimer: null, scrollTimer: null,
token:'' token:'',
}; };
}, },
onLoad(options) { onLoad(options) {
@ -137,6 +147,7 @@ export default {
// Markdown // Markdown
this.renderAllMarkdown(); this.renderAllMarkdown();
this.getAiChatHistory();
}, },
onReady() { onReady() {
// //
@ -168,6 +179,55 @@ export default {
title: 'AI 剧本助手' title: 'AI 剧本助手'
}); });
}, },
async getAiChatHistory(){
console.log("this.$api",this.$api)
let res = await this.$api.aiApi.getAiHistory(this.orderId)
console.log('当前获取历史记录', res);
if(res.code == 200){
let list = res.data || [];
this.messages = list.map((item) => {
return {
role: item.messageType,
content: item.message,
renderedContent: renderMarkdown(item.message),
time: item.createTime,
endStatus: true
}
});
if (list.length > 0) {
//
this.$nextTick(()=>{
setTimeout(()=>this.throttledScrollToBottom(),1000)
})
}
}else{
uni.showToast({
title: res.msg,
icon: 'error'
});
}
},
findLastAssistantIndex() {
//
for (let i = this.messages.length - 1; i >= 0; i--) {
if (this.messages[i].role === 'assistant') {
return i; // assistant
}
}
return -1; // -1
},
resetScript(index) {
if(this.messages[index-1] && this.messages[index-1].content && this.messages[index-1].role === 'user' ){
this.inputMessage = this.messages[index-1].content;
this.sendMessage()
}else{
uni.showToast({
title: '重新生成失败',
icon: 'error'
});
}
},
// Markdown // Markdown
renderAllMarkdown() { renderAllMarkdown() {
@ -205,7 +265,7 @@ export default {
this.loading = true; this.loading = true;
// //
this.throttledScrollToBottom(); this.throttledScrollToBottom()
try { try {
// API // API
@ -224,7 +284,7 @@ export default {
// H5使fetch API // H5使fetch API
if (this.isH5) { if (this.isH5) {
await this.fetchStreamResponse(apiMessages); await this.fetchStreamResponse(userInput);
} else { } else {
// H5使 // H5使
await this.fetchNormalResponse(apiMessages); await this.fetchNormalResponse(apiMessages);
@ -255,7 +315,7 @@ export default {
endStatus: false endStatus: false
}); });
const response = await fetch(`${config.baseUrl}/ai/generateStream`, { const response = await fetch(`${config.baseUrl}/ai/chatStream`, {
method: 'POST', method: 'POST',
headers: { headers: {
'Authorization': `Bearer ${this.token}`, 'Authorization': `Bearer ${this.token}`,
@ -263,8 +323,9 @@ export default {
'Content-Type': 'application/json' 'Content-Type': 'application/json'
}, },
redirect: 'follow', redirect: 'follow',
body: JSON.stringify(apiMessages) body: JSON.stringify({ businessId:this.orderId, message:apiMessages })
}); });
// console.log("response",response)
if (!response.ok) { if (!response.ok) {
throw new Error(`API请求失败: ${response.status}`); throw new Error(`API请求失败: ${response.status}`);
@ -444,19 +505,18 @@ export default {
title: '已确认使用此剧本', title: '已确认使用此剧本',
icon: 'success' icon: 'success'
}); });
// Return to previous page // pageB
uni.navigateBack({ uni.navigateBack({
delta: 1, // Number of pages to go back, 1 means return to previous page delta: 1, // 1
success: function() { success: function() {
// Get page stack //
const pages = getCurrentPages(); const pages = getCurrentPages();
// Previous page instance // pageA
const prevPage = pages[pages.length - 1]; const prevPage = pages[pages.length - 2];
// Call method on previous page console.log("prevPage",prevPage)
if (prevPage && typeof prevPage.getAiDramaTxt === 'function') { //
prevPage.getAiDramaTxt(scriptContent); prevPage.getAiDramaTxt(scriptContent);
}
} }
}); });
}, },

View File

@ -138,7 +138,7 @@
}, },
openAiDrama(){ openAiDrama(){
uni.navigateTo({ uni.navigateTo({
url:'./AiDrama' url:'./AiDrama?orderId=' + this.orderItemId
}) })
}, },
getDisabledStatus(){ getDisabledStatus(){
@ -203,7 +203,6 @@
fileName:'', fileName:'',
} }
} }
}, },
async getMaterialDictList(){ async getMaterialDictList(){
let res = await this.$api.orderApi.getProductMaterialDictList({ pageNum: 1, pageSize: 100, productId: this.productId }) let res = await this.$api.orderApi.getProductMaterialDictList({ pageNum: 1, pageSize: 100, productId: this.productId })