| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458 | <template>	<view class="page" :style="{'min-height':h+'px', 'padding-top':mt+'px'}">		<cus-header title='对报告提问' bgColor="#FFFFFF"></cus-header>		<div class="dialogs container" ref="messageContainer">			<div class="d_pdf">				<div class="dp_box">					<div class="dq_text">{{ fileName||'' }}</div>					<div class="dp_type adfac">						<image src="@/static/pdf.png"></image>						<span>PDF</span>					</div>				</div>			</div>			<div v-for="(item,index) in dialogList" :key="index">				<div class="d_question">					<div class="dq_text">{{ item.question }}</div>				</div>				<div class="d_answer">					<div class="da_top adfac">						<image src="@/static/logo.png"></image>						<text>创衡汇教练AI智能体</text>					</div>					<div class="da_content">						<template v-if="item.think">							<view class="dac_think adfac">								<image src="https://transcend.ringzle.com/xiaozhi-app/profile/2025/07/07/13c5dd16-2032-464a-8b1c-2722d201cfe2.gif"></image>								<text>正在思考中</text>							</view>						</template>						<template v-else>							<u-parse :content="item.answer"></u-parse>						</template>						<div class="dc_btns adfacjb" v-if="item.answer">							<div class="db_l">								<image :src="item.copy?require('@/static/copy_active.png'):require('@/static/copy.png')" @tap="toCopy(item,index)"></image>								<image :src="item.upvote?require('@/static/upvote_active.png'):require('@/static/upvote.png')" @tap="toUpvote(item,index)"></image>								<image :src="item.comment?require('@/static/comment_active.png'):require('@/static/comment.png')" @tap="toComment(item,index)"></image>							</div>							<div class="db_r">								<!-- <image :src="item.share?require('@/static/share_active.png'):require('@/static/share.png')" @tap="toShare(item,index)"></image> -->							</div>						</div>					</div>				</div>			</div>		</div>		<div class="ask_box" :style="{'bottom':fixBottom+'px'}">			<div class="ask">				<div class="a_inp">					<div class="ai_l">						<u-textarea v-model="question" placeholder="请输入您的问题" autoHeight :showConfirmBar="false" :adjustPosition="false" maxlength="999999"></u-textarea>					</div>					<div class="ai_r">						<image @tap="sendQuestion" src="https://transcend.ringzle.com/xiaozhi-app/profile/2025/06/16/2429556b-54b7-4878-97c0-6b440b546ee4.png"></image>					</div>				</div>			</div>		</div>		<u-modal :show="commentShow" title="评论" @confirm="commentConfirm" @cancel="commentCancel" @close="commentCancel" :showCancelButton="true">			<u-textarea v-model="content" placeholder="对于我们的回答您是否不满意,您有更好的答案建议吗?请在此输入."></u-textarea>		</u-modal>	</view></template><script>	var timer = null;	let requestTask = null;	import { BaseApi } from '../http/baseApi.js'	import * as TextEncoding from "text-encoding-shim";	export default {		data(){			return {				pdfUrl:'',				fileName:'',				retryCount: 3, // 最大重试次数				currentRetry: 0, // 当前重试次数				isRequesting: false, // 请求状态锁				question:'',				dialogList:[],				streamingResponse:'',				receivedData:'',				windex:0,				commentShow:false,				content:'',				cindex:'',				fixBottom: 0			}		},		onLoad(option) {			this.pdfUrl = option.pdfUrl;			this.fileName = option.fileName;		},		onReady() {		    uni.onKeyboardHeightChange(res => {		      this.fixBottom = res.height||0;		    });		},		onUnload() {		    uni.offKeyboardHeightChange();			this.fixBottom = 0;		},		methods:{			// 封装带重试机制的请求方法			async sendRequestWithRetry() {				if (this.isRequesting) return;				this.isRequesting = true;				this.currentRetry = 0;				try {					await this._executeRequest();				} catch (error) {					  this.$showToast('请求失败,请稍后重试')				} finally {					this.isRequesting = false;				}			},			// 实际执行请求的方法			_executeRequest() {				return new Promise((resolve, reject) => {					requestTask = uni.request({						url: `${BaseApi}/core/chat/sendChatMessageStream`,						method: 'POST',						timeout: 10000,						data:{ 							query: this.question,							identity:'被教练者',							report:this.pdfUrl						},						header: {						  'Content-Type': 'application/json',						  'token': uni.getStorageSync('token') || ''						},						enableChunked: true, // 启用流式接收						responseType:'text',						success: (res) => {							if (res.statusCode === 200) {								this._handleSuccess(res.data);								resolve();							} else {								this._handleError(`状态码异常: ${res.statusCode}`, resolve, reject);							}						},						fail: (err) => {							this._handleError(err.errMsg, resolve, reject);						},						complete: (com) => {							console.log('请求完成',com)						}					});										requestTask.onChunkReceived(async (res) => {						const uint8Array = new Uint8Array(res.data);						const decoder = new TextEncoding.TextDecoder("utf-8");						const decodedString = decoder.decode(uint8Array);						try {							let newtext = decodedString.replaceAll('data:','').replaceAll(':keepAlive','');							let ntArr = newtext.split('\n\n');							if(ntArr.length){								ntArr.forEach(n=>{									if(!n.trim()) return									let nj = JSON.parse(n);									if(nj.event=='message'){										let answer = this.dialogList[this.dialogList.length-1].answer+nj.answer?.replace(/(\r\n|\n|\r)+/g, '<br>');										this.$set(this.dialogList[this.dialogList.length-1],'answer',answer);										this.$set(this.dialogList[this.dialogList.length-1],'id',nj.id);										this.$set(this.dialogList[this.dialogList.length-1],'task_id',nj.task_id);										this.$set(this.dialogList[this.dialogList.length-1],'message_id',nj.message_id);										this.$set(this.dialogList[this.dialogList.length-1],'conversation_id',nj.conversation_id);										this.$set(this.dialogList[this.dialogList.length-1],'think',false);									}								})								setTimeout(()=>{									this.scrollToBottom();								},100)							}						} catch (e) {							console.error('解析失败', e, '原始数据:', decodedString);						}					});				});			},			// 成功处理			_handleSuccess(data) {				if (data) {				  this.streamingResponse += data;				}				this.currentRetry = 0; // 重置重试计数器			},			// 错误处理			_handleError(errorMsg, resolve, reject) {				if (this._shouldRetry(errorMsg)) {					this.currentRetry++;					setTimeout(() => {						this._executeRequest().then(resolve).catch(reject);					}, this._getRetryDelay());				} else {					reject(errorMsg);				}			},			// 判断是否需要重试			_shouldRetry(errorMsg) {				const retryableErrors = [					'timeout', 					'request:fail', 					'Network Error'				];				return this.currentRetry < this.retryCount && retryableErrors.some(e => errorMsg.includes(e));			},			// 获取指数退避延迟时间			_getRetryDelay() {				return Math.min(1000 * Math.pow(2, this.currentRetry), 10000);			},			sendQuestion(){				if(!this.question) return this.$showToast('请输入您的问题');				let qa = {					question:JSON.parse(JSON.stringify(this.question)),					answer:'',					copy:false,					upvote:false,					comment:'',					share:false,					think:true				}				this.dialogList = [...this.dialogList,...[qa]];				this.$nextTick(()=>{					this.scrollToBottom();					this.sendRequestWithRetry();					this.question = '';				})			},			// 滚动到底部			scrollToBottom() {				this.$nextTick(()=>{					this.$nextTick(()=>{						uni.pageScrollTo({							scrollTop:99999,							duration:300						})					})				})			},			toCopy(item,index){				uni.setClipboardData({					data:item.answer,					success: (res) => {						this.$showToast('复制成功');					},					fail: (err) => {						this.$showToast('复制失败');					}				})			},			toUpvote(item,index){				this.$set(this.dialogList[index],'upvote',!this.dialogList[index].upvote);			},			toComment(item,index){				this.cindex = index;				this.commentShow = true;			},			toShare(item,index){				this.$set(this.dialogList[index],'share',!this.dialogList[index].share);			},			commentConfirm(){				this.$set(this.dialogList[this.cindex],'comment',this.content);				this.commentShow = false;			},			commentCancel(){				this.content = '';				this.commentShow = false;			},		}	}</script><style scoped lang="scss">	::v-deep .u-textarea{		border: none !important;		padding: 0 !important;	}	::v-deep .u-textarea textarea{		min-height: 64rpx !important;	}		.page{		background: linear-gradient( 227deg, #EEEFF8 0%, #F6ECF4 100%, #F6ECF4 100%);		padding: 0 30rpx 200rpx;		box-sizing: border-box;				.welcome{			margin-top: 259rpx;			padding: 0 34rpx;			image{				width: 88rpx;				height: 87rpx;				margin-left: 20rpx;			}			p{				font-family: PingFang-SC, PingFang-SC;				font-weight: bold;				font-size: 36rpx;				color: #252525;				line-height: 36rpx;				margin-top: 36rpx;				&.tip{					font-family: PingFangSC, PingFang SC;					font-weight: 400;					font-size: 26rpx;					color: #646464;					line-height: 40rpx;					margin-top: 20rpx;				}			}		}			.dialogs{			width: 100%;			padding-top: 34rpx;			box-sizing: border-box;			overflow-y: auto;			.d_answer{				margin-top: 40rpx;				&.init{					margin-top: 0;				}				.da_top{					image{						width: 48rpx;						height: 48rpx;					}					text{						font-family: PingFang-SC, PingFang-SC;						font-weight: bold;						font-size: 30rpx;						color: #505050;						line-height: 48rpx;						margin-left: 20rpx;					}				}				.da_content{					padding: 30rpx 32rpx;					margin-top: 20rpx;					background: #FFFFFF;					border-radius: 4rpx 24rpx 24rpx 24rpx;					.dac_think{						image{							width: 40rpx;							height: 40rpx;						}						text{							font-size: 30rpx;							margin-left: 10rpx;						}					}					.dc_btns{						margin-top: 44rpx;						image{							width: 48rpx;							height: 48rpx;						}						.db_l{							image{								margin-right: 40rpx;							}						}					}				}			}			.d_question{				margin-top: 40rpx;				display: flex;				justify-content: flex-end;				.dq_text{					background: #833478;					border-radius: 24rpx 4rpx 24rpx 24rpx;					font-family: PingFangSC, PingFang SC;					font-weight: 400;					font-size: 30rpx;					color: #FFFFFF;					line-height: 48rpx;					// text-align: right;					padding: 26rpx 30rpx;				}			}						.d_pdf{				margin-top: 20rpx;				display: flex;				justify-content: flex-end;				.dp_box{					width: 364rpx;					background: #FFFFFF;					border-radius: 16rpx;					border: 1rpx solid #F0F2F8;					padding: 27rpx 21rpx;					.dq_text{						background: #FFFFFF;						font-family: PingFang-SC, PingFang-SC;						font-weight: bold;						font-size: 26rpx;						color: #252525;						line-height: 37rpx;					}					.dp_type{						margin-top: 15rpx;						image{							width: 25rpx;							height: 30rpx;						}						span{							font-family: PingFang-SC, PingFang-SC;							font-weight: bold;							font-size: 24rpx;							color: #999999;							line-height: 33rpx;							margin-left: 5rpx;						}					}				}			}		}			.ask_box{			width: 100%;			min-height: 176rpx;			background: linear-gradient( 227deg, #EEEFF8 0%, #F6ECF4 100%, #F6ECF4 100%);			padding: 0 30rpx 60rpx;			position: fixed;			left: 0;			box-sizing: border-box;		}			.ask{			min-height: 116rpx;			background: #FFFFFF;			border-radius: 24rpx;			border: 2rpx solid #F0F2F8;			padding: 24rpx;			box-sizing: border-box;			.a_inp{				display: flex;				align-items: flex-end;				max-height: 300rpx;				overflow-y: auto;				.ai_l{					width: calc(100% - 64rpx);					padding-right: 20rpx;					box-sizing: border-box;				}				.ai_r{					width: 64rpx;					image{						width: 64rpx;						height: 64rpx;					}				}			}		}	}</style>
 |