chat.vue 23.4 KB
Newer Older
DCloud_JSON's avatar
DCloud_JSON 已提交
1 2 3 4
<template>
	<view class="page">
		<view class="container">
			<view v-if="isWidescreen" class="header">uni-ai-chat</view>
DCloud_JSON's avatar
1.0.12  
DCloud_JSON 已提交
5
			<text class="noData" v-if="msgLength === 0">没有对话记录</text>
DCloud_JSON's avatar
DCloud_JSON 已提交
6
			<scroll-view :scroll-into-view="scrollIntoView" scroll-y="true" class="msg-list" :enable-flex="true">
DCloud_JSON's avatar
1.0.16  
DCloud_JSON 已提交
7
				<uni-ai-msg ref="msg" v-for="(msgIndex,index) in msgLength" :key="index" :msgIndex="index"
DCloud_JSON's avatar
1.0.17  
DCloud_JSON 已提交
8
					:show-cursor="index == msgLength - 1 && msgLength%2 === 0 && sseIndex" :isLastMsg="index == msgLength - 1"></uni-ai-msg>
DCloud_JSON's avatar
1.0.12  
DCloud_JSON 已提交
9
				<view class="tip-ai-ing" v-if="msgLength && msgLength%2 !== 0">
DCloud_JSON's avatar
DCloud_JSON 已提交
10 11
					<text>uni-ai正在思考中...</text>
					<view v-if="NODE_ENV == 'development' && !enableStream">
DCloud_JSON's avatar
1.0.16  
DCloud_JSON 已提交
12 13
						如需提速,请开通<uni-link class="uni-link" href="https://uniapp.dcloud.net.cn/uniCloud/uni-ai-chat.html"
							text="[流式响应]"></uni-link>
DCloud_JSON's avatar
DCloud_JSON 已提交
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
					</view>
				</view>
				<view id="last-msg-item"></view>
			</scroll-view>

			<view class="foot-box">
				<view class="menu" v-if="isWidescreen">
					<view class="trash menu-item">
						<image @click="clear" src="@/static/remove.png" mode="heightFix"></image>
					</view>
				</view>

				<view class="foot-box-content">
					<view v-if="!isWidescreen" class="trash">
						<uni-icons @click="clear" type="trash" size="24" color="#888"></uni-icons>
DCloud_JSON's avatar
1.0.17  
DCloud_JSON 已提交
29 30
					</view>
					<view class="textarea-box" @click="focus = true">
DCloud_JSON's avatar
1.0.12  
DCloud_JSON 已提交
31
						<textarea v-model="content" :cursor-spacing="15" class="textarea" :auto-height="!isWidescreen"
DCloud_JSON's avatar
1.0.18  
DCloud_JSON 已提交
32
							:disabled="inputBoxDisabled" :placeholder="placeholderText" :maxlength="-1" :focus="focus" @blur="focus = false"
DCloud_JSON's avatar
DCloud_JSON 已提交
33 34 35 36
							placeholder-class="input-placeholder"></textarea>
					</view>
					<view class="send-btn-box">
						<text v-if="isWidescreen" class="send-btn-tip">↵ 发送 / shift + ↵ 换行</text>
DCloud_JSON's avatar
1.0.12  
DCloud_JSON 已提交
37 38
						<button @click="beforeSendMsg" :disabled="inputBoxDisabled || !content" class="send"
							type="primary">发送</button>
DCloud_JSON's avatar
DCloud_JSON 已提交
39 40 41
					</view>
				</view>
			</view>
DCloud_JSON's avatar
1.0.12  
DCloud_JSON 已提交
42
		</view>
DCloud_JSON's avatar
DCloud_JSON 已提交
43 44 45 46 47
	</view>
</template>

<script>
	// 引入配置文件
DCloud_JSON's avatar
1.0.12  
DCloud_JSON 已提交
48
	import config from '@/config.js';
DCloud_JSON's avatar
1.0.16  
DCloud_JSON 已提交
49 50 51 52

	import {
		msgList
	} from '@/pages/chat/msgList.js';
DCloud_JSON's avatar
1.0.12  
DCloud_JSON 已提交
53

DCloud_JSON's avatar
DCloud_JSON 已提交
54 55 56 57 58 59 60 61
	// 获取广告id
	const {
		adpid
	} = config
	// 初始化sse通道
	let sseChannel = false;

	// 是否通过回调,当用户点击清空后应当跳过前一次请求的回调
DCloud_JSON's avatar
1.0.12  
DCloud_JSON 已提交
62 63 64
	let skip_callback = false;

	// 键盘的shift键是否被按下
DCloud_JSON's avatar
DCloud_JSON 已提交
65
	let shiftKeyPressed = false
DCloud_JSON's avatar
1.0.16  
DCloud_JSON 已提交
66
	export default {
DCloud_JSON's avatar
DCloud_JSON 已提交
67 68 69 70
		data() {
			return {
				// 使聊天窗口滚动到指定元素id的值
				scrollIntoView: "",
DCloud_JSON's avatar
1.0.12  
DCloud_JSON 已提交
71
				// 消息长度(个数)
DCloud_JSON's avatar
1.0.16  
DCloud_JSON 已提交
72
				msgLength: 0,
DCloud_JSON's avatar
DCloud_JSON 已提交
73 74 75 76 77 78 79 80 81 82 83
				// 消息列表数据
				msgList: [],
				// 输入框的消息内容
				content: "",
				// 记录流式响应次数
				sseIndex: 0,
				// 是否启用流式响应模式
				enableStream: true,
				// 当前屏幕是否为宽屏
				isWidescreen: false,
				// 广告位id
DCloud_JSON's avatar
1.0.12  
DCloud_JSON 已提交
84 85
				adpid,
				focus: false
DCloud_JSON's avatar
DCloud_JSON 已提交
86
			}
DCloud_JSON's avatar
1.0.16  
DCloud_JSON 已提交
87
		},
DCloud_JSON's avatar
DCloud_JSON 已提交
88 89 90 91 92 93 94 95
		computed: {
			// 输入框是否禁用
			inputBoxDisabled() {
				// 如果正在等待流式响应,则禁用输入框
				if (this.sseIndex !== 0) {
					return true
				}
				// 如果消息列表长度为奇数,则禁用输入框
DCloud_JSON's avatar
1.0.12  
DCloud_JSON 已提交
96
				return !!(this.msgLength && this.msgLength % 2 !== 0)
DCloud_JSON's avatar
DCloud_JSON 已提交
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
			},
			// 输入框占位符文本
			placeholderText() {
				// 如果输入框被禁用,则显示“uni-ai正在回复中”
				if (this.inputBoxDisabled) {
					return 'uni-ai正在回复中'
				} else {
					// #ifdef H5
					// 如果屏幕宽度大于960,则显示“请输入内容,ctrl + enter 发送”,否则显示“请输入要发给uni-ai的内容”
					return window.innerWidth > 960 ? '请输入内容,ctrl + enter 发送' : '请输入要发给uni-ai的内容'
					// #endif
					return '请输入要发给uni-ai的内容'
				}
			},
			// 获取当前环境
			NODE_ENV() {
				return process.env.NODE_ENV
			}
		},
		// 监听msgList变化,将其存储到本地缓存中
DCloud_JSON's avatar
1.0.12  
DCloud_JSON 已提交
117 118 119 120 121 122 123 124
		watch: {
			// #ifdef H5
			inputBoxDisabled(val) {
				this.$nextTick(() => {
					this.focus = !val
					// console.log('this.focus', this.focus);
				})
			},
DCloud_JSON's avatar
1.0.16  
DCloud_JSON 已提交
125
			// #endif
DCloud_JSON's avatar
DCloud_JSON 已提交
126
			msgList: {
DCloud_JSON's avatar
1.0.16  
DCloud_JSON 已提交
127 128 129 130 131 132 133 134
				handler(msgList) {
					let msgLength = msgList.length
					if (msgLength != this.msgLength) {
						this.msgLength = msgLength
						this.$nextTick(() => {
							this.updateLastMsg(msgList[msgLength - 1])
						})
					}
DCloud_JSON's avatar
DCloud_JSON 已提交
135
					// 将msgList存储到本地缓存中
DCloud_JSON's avatar
1.0.16  
DCloud_JSON 已提交
136 137 138 139
					uni.setStorage({
						"key": "uni-ai-msg",
						"data": msgList
					})
DCloud_JSON's avatar
DCloud_JSON 已提交
140 141 142 143
				},
				// 深度监听msgList变化
				deep: true
			}
DCloud_JSON's avatar
1.0.17  
DCloud_JSON 已提交
144 145 146 147 148 149 150 151 152 153
		},
		beforeMount() {
			// #ifdef H5
			// 监听屏幕宽度变化,判断是否为宽屏 并设置isWidescreen的值
			uni.createMediaQueryObserver(this).observe({
				minWidth: 650,
			}, matches => {
				this.isWidescreen = matches;
			})
			// #endif
DCloud_JSON's avatar
DCloud_JSON 已提交
154
		},
DCloud_JSON's avatar
1.0.16  
DCloud_JSON 已提交
155 156
		async mounted() {

DCloud_JSON's avatar
DCloud_JSON 已提交
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
			// 如果存在广告位id且用户token未过期
			if (this.adpid && uniCloud.getCurrentUserInfo().tokenExpired > Date.now()) {
				// 查询当前用户的积分
				// 获取数据库对象
				let db = uniCloud.databaseForJQL();
				// 获取uni-id-users集合
				let res = await db.collection("uni-id-users")
					// 查询条件
					.where({
						// 当前用户id
						"_id": uniCloud.getCurrentUserInfo().uid
					})
					// 返回score字段
					.field('score')
					// 执行查询
					.get()
				// 输出当前用户积分
				console.log('当前用户有多少积分:', res.data[0] && res.data[0].score);
DCloud_JSON's avatar
1.0.12  
DCloud_JSON 已提交
175
			}
DCloud_JSON's avatar
DCloud_JSON 已提交
176 177 178 179 180 181 182

			// for (let i = 0; i < 15; i++) {
			// 	this.msgList.push({
			// 		isAi: i % 2 == true,
			// 		content: "1-" + i
			// 	})
			// }
DCloud_JSON's avatar
1.0.16  
DCloud_JSON 已提交
183 184 185 186 187 188 189 190


			let _msgList = uni.getStorageSync('uni-ai-msg') || [];
			if (_msgList.length) {
				msgList.push(..._msgList)
			}

			this.msgList = msgList
DCloud_JSON's avatar
DCloud_JSON 已提交
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209

			// 如果上一次对话中 最后一条消息ai未回复。则一启动就自动重发。
			let length = this.msgList.length
			if (length) {
				let lastMsg = this.msgList[length - 1]
				if (!lastMsg.isAi) {
					this.retriesSendMsg()
				}
			}


			// this.msgList.pop()
			// console.log('this.msgList', this.msgList);

			// 在dom渲染完毕后 使聊天窗口滚动到最后一条消息
			this.$nextTick(() => {
				this.showLastMsg()
			})

DCloud_JSON's avatar
1.0.18  
DCloud_JSON 已提交
210
			// #ifdef H5
DCloud_JSON's avatar
1.0.12  
DCloud_JSON 已提交
211 212 213 214 215 216 217 218 219 220 221 222
			//获得消息输入框对象
			let adjunctKeydown = false
			const textareaDom = document.querySelector('.textarea-box textarea');
			if (textareaDom) {
				//键盘按下时
				textareaDom.onkeydown = e => {
					// console.log('onkeydown', e.keyCode)
					if ([16, 17, 18, 93].includes(e.keyCode)) {
						//按下了shift ctrl alt windows键
						adjunctKeydown = true;
					}
					if (e.keyCode == 13 && !adjunctKeydown) {
DCloud_JSON's avatar
1.0.18  
DCloud_JSON 已提交
223 224 225 226 227
						e.preventDefault()
						// 执行发送
						setTimeout(()=>{
							this.beforeSendMsg();
						},300)
DCloud_JSON's avatar
1.0.12  
DCloud_JSON 已提交
228 229 230 231 232 233 234 235 236 237
					}
				};
				textareaDom.onkeyup = e => {
					//松开adjunct键
					if ([16, 17, 18, 93].includes(e.keyCode)) {
						adjunctKeydown = false;
					}
				};
			}
			// #endif
DCloud_JSON's avatar
DCloud_JSON 已提交
238
		},
DCloud_JSON's avatar
1.0.12  
DCloud_JSON 已提交
239
		methods: {
DCloud_JSON's avatar
DCloud_JSON 已提交
240 241 242 243 244 245 246 247 248 249 250 251 252 253
			// 此(惰性)函数,检查是否开通uni-push;决定是否启用enableStream
			async checkIsOpenPush() {
				try {
					// 获取推送客户端id
					await uni.getPushClientId()
					// 如果获取成功,则将checkIsOpenPush函数重写为一个空函数
					this.checkIsOpenPush = () => {}
				} catch (err) {
					// 如果获取失败,则将enableStream设置为false
					this.enableStream = false
				}
			},
			// 更新最后一条消息
			updateLastMsg(param) {
DCloud_JSON's avatar
1.0.18  
DCloud_JSON 已提交
254
				let length = this.msgList.length
DCloud_JSON's avatar
DCloud_JSON 已提交
255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272
				if (length === 0) {
					return
				}
				let lastMsg = this.msgList[length - 1]

				// 如果param是函数,则将最后一条消息作为参数传入该函数
				if (typeof param == 'function') {
					let callback = param;
					callback(lastMsg)
				} else {
					// 否则,将参数解构为data和cover两个变量
					const [data, cover = false] = arguments
					if (cover) {
						lastMsg = data
					} else {
						lastMsg = Object.assign(lastMsg, data)
					}
				}
DCloud_JSON's avatar
1.0.16  
DCloud_JSON 已提交
273
				this.msgList.splice(length - 1, 1, lastMsg)
DCloud_JSON's avatar
DCloud_JSON 已提交
274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404
			},
			// 广告关闭事件
			onAdClose(e) {
				console.log('onAdClose e.detail.isEnded', e.detail.isEnded);
				if (e.detail.isEnded) {
					//5次轮训查结果
					let i = 0;
					uni.showLoading({
						mask: true
					})
					let myIntive = setInterval(async e => {
						i++;
						// 获取云数据库实例
						const db = uniCloud.database();
						// 获取uni-id-users集合
						let res = await db.collection("uni-id-users")
							// 查询条件为_id等于当前用户id
							.where('"_id" == $cloudEnv_uid')
							// 只返回score字段
							.field('score')
							// 执行查询
							.get()
						// 解构出score字段的值,如果没有则默认为undefined
						let {
							score
						} = res.result.data[0] || {}
						if (score > 0 || i > 5) {
							// 清除轮询定时器
							clearInterval(myIntive)
							// 隐藏加载提示
							uni.hideLoading()
							if (score > 0) {
								// 移除最后一条消息
								this.msgList.pop()
								this.$nextTick(() => {
									// 重发消息
									this.retriesSendMsg()
									uni.showToast({
										title: '积分余额:' + score,
										icon: 'none'
									});
								})
							}
						}
					}, 2000);
				}
			},
			async retriesSendMsg() {
				// 检查是否开通uni-push;决定是否启用enableStream
				await this.checkIsOpenPush()
				// 更新最后一条消息的状态为0 表示消息正在发送中
				this.updateLastMsg({
					state: 0
				})
				// 发送消息
				this.send()
			},
			async beforeSendMsg() {
				// 如果开启了广告位需要登录
				if (this.adpid) {
					// 获取本地缓存的token
					let token = uni.getStorageSync('uni_id_token')
					// 如果token不存在
					if (!token) {
						// 弹出提示框
						return uni.showModal({
							// 提示内容
							content: '启用激励视频,客户端需登录并启用安全网络',
							// 不显示取消按钮
							showCancel: false,
							// 确认按钮文本
							confirmText: "查看详情",
							// 弹框关闭后执行的回调函数
							complete() {
								// 文档链接
								let url = "https://uniapp.dcloud.net.cn/uniCloud/uni-ai-chat.html#ad"
								// #ifndef H5
								// 将文档链接复制到剪贴板
								uni.setClipboardData({
									// 复制的内容
									data: url,
									// 不显示提示框
									showToast: false,
									// 复制成功后的回调函数
									success() {
										// 弹出提示框
										uni.showToast({
											// 提示内容
											title: '已复制文档链接,请到浏览器粘贴浏览',
											// 不显示图标
											icon: 'none',
											// 提示框持续时间
											duration: 5000
										});
									}
								})
								// #endif

								// #ifdef H5
								// 在新窗口打开文档链接
								window.open(url)
								// #endif
							}
						});
					}
				}

				// 检查是否开通uni-push;决定是否启用enableStream
				await this.checkIsOpenPush()

				// 如果内容为空
				if (!this.content) {
					// 弹出提示框
					return uni.showToast({
						// 提示内容
						title: '内容不能为空',
						// 不显示图标
						icon: 'none'
					});
				}

				// 将用户输入的消息添加到消息列表中
				this.msgList.push({
					// 标记为非人工智能机器人,即:为用户发送的消息
					isAi: false,
					// 消息内容
					content: this.content,
					// 消息状态为0,表示正在发送中
					state: 0,
					// 消息创建时间
					create_time: Date.now()
DCloud_JSON's avatar
1.0.16  
DCloud_JSON 已提交
405 406
				})

DCloud_JSON's avatar
DCloud_JSON 已提交
407 408
				// 展示最后一条消息
				this.showLastMsg()
DCloud_JSON's avatar
1.0.12  
DCloud_JSON 已提交
409
				// dom加载完成后 清空文本内容
DCloud_JSON's avatar
DCloud_JSON 已提交
410
				this.$nextTick(() => {
DCloud_JSON's avatar
1.0.12  
DCloud_JSON 已提交
411
					this.content = ''
DCloud_JSON's avatar
DCloud_JSON 已提交
412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460
				})
				this.send() // 发送消息
			},
			async send() {
				let messages = []
				// 复制一份,消息列表数据
				let msgs = JSON.parse(JSON.stringify(this.msgList))
				// 带总结的消息 index
				let findIndex = [...msgs].reverse().findIndex(item => item.summarize)
				// console.log('findIndex', findIndex)
				if (findIndex != -1) {
					let aiSummaryIndex = msgs.length - findIndex - 1
					// console.log('aiSummaryIndex', aiSummaryIndex)
					// 将带总结的消息的 内容 更换成 总结
					msgs[aiSummaryIndex].content = msgs[aiSummaryIndex].summarize
					// 拿最后一条带直接的消息作为与ai对话的msg body
					msgs = msgs.splice(aiSummaryIndex, msgs.length - 1)
				} else {
					// 如果未总结过就直接从末尾拿10条
					msgs = msgs.splice(-10)
				}

				// 过滤涉敏问题
				msgs = msgs.filter(msg => !msg.illegal)

				// 根据数据内容设置角色
				messages = msgs.map(item => {
					// 角色默认为用户
					let role = "user"
					// 如果是ai再根据 是否有总结 来设置角色为 system 还是 assistant
					if (item.isAi) {
						role = item.summarize ? 'system' : 'assistant'
					}
					return {
						content: item.content,
						role
					}
				})

				// 在控制台输出 向ai机器人发送的完整消息内容
				console.log('send to ai messages:', messages);

				// 判断是否开启了流式响应模式
				if (this.enableStream) {
					// 创建消息通道
					sseChannel = new uniCloud.SSEChannel()
					// console.log('sseChannel',sseChannel);

					// 监听message事件
DCloud_JSON's avatar
1.0.12  
DCloud_JSON 已提交
461
					sseChannel.on('message', (message) => {
DCloud_JSON's avatar
DCloud_JSON 已提交
462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486
						// console.log('on message', message);
						// 将从云端接收到的消息添加到消息列表中

						// 如果之前未添加过就添加,否则就执行更新最后一条消息
						if (this.sseIndex === 0) {
							this.msgList.push({
								isAi: true,
								content: message,
								create_time: Date.now()
							})
							this.showLastMsg()
						} else {
							this.updateLastMsg(lastMsg => {
								lastMsg.content += message
							})
							this.showLastMsg()
						}
						// 让流式响应计数值递增
						this.sseIndex++
					})

					// 监听end事件,如果云端执行end时传了message,会在客户端end事件内收到传递的消息
					sseChannel.on('end', (e) => {
						// console.log('on end', e);
						// 如果e存在且包含summarize或insufficientScore属性
487
						if (e) {
DCloud_JSON's avatar
DCloud_JSON 已提交
488 489
							// 更新最后一条消息
							this.updateLastMsg(lastMsg => {
DCloud_JSON's avatar
1.0.12  
DCloud_JSON 已提交
490 491 492 493 494 495 496 497
								// 如果e包含illegal属性
								if (e.illegal) {
									// 将最后一条消息的illegal属性更新为e的illegal属性
									lastMsg.illegal = e.illegal
									lastMsg.content = "内容涉及敏感"
									// 倒数第二条(用户发问内容)也需要设置illegal的值
									this.msgList[this.msgList.length - 2].illegal = e.illegal
								}
DCloud_JSON's avatar
DCloud_JSON 已提交
498
								// 如果e包含summarize属性
499
								else if (e.summarize) {
DCloud_JSON's avatar
DCloud_JSON 已提交
500 501
									// 将最后一条消息的summarize属性更新为e的summarize属性
									lastMsg.summarize = e.summarize
DCloud_JSON's avatar
1.0.12  
DCloud_JSON 已提交
502 503
								}
								// 如果e包含insufficientScore属性
504
								else if (e.insufficientScore) {
DCloud_JSON's avatar
DCloud_JSON 已提交
505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532
									// 将最后一条消息的insufficientScore属性更新为e的insufficientScore属性
									lastMsg.insufficientScore
								}
							})
						}

						// 结束流式响应 将流式响应计数值 设置为 0
						this.sseIndex = 0
						// 滚动窗口以显示最新的一条消息
						this.showLastMsg()
					})
					await sseChannel.open() // 等待通道开启
				}

				// 重置skip_callback为false,以便下一次请求可以正常回调
				skip_callback = false
				// 导入uni-ai-chat模块,并设置customUI为true
				const uniAiChat = uniCloud.importObject("uni-ai-chat", {
					customUI: true
				})

				// 发送消息给ai机器人
				uniAiChat.send({
						messages, // 消息列表
						sseChannel // 消息通道
					})
					.then(res => {
						// console.log(111,res);
DCloud_JSON's avatar
1.0.16  
DCloud_JSON 已提交
533 534 535
						if (!sseChannel) {
							if (!res.data) {
								return
DCloud_JSON's avatar
1.0.13  
DCloud_JSON 已提交
536
							}
DCloud_JSON's avatar
1.0.12  
DCloud_JSON 已提交
537 538 539 540
							// 更新最后一条消息的状态为100(发送成功)
							this.updateLastMsg({
								state: 100
							})
DCloud_JSON's avatar
DCloud_JSON 已提交
541 542 543
							// console.log(res, res.reply);
							// 判断是否要跳过本次回调,防止请求未返回时,历史对话已被清空。引起对话顺序错误 导致 对话输入框卡住
							if (!skip_callback) {
DCloud_JSON's avatar
1.0.12  
DCloud_JSON 已提交
544 545 546 547 548 549
								let {
									"reply": content,
									summarize,
									insufficientScore,
									illegal
								} = res.data
DCloud_JSON's avatar
DCloud_JSON 已提交
550 551
								if (illegal) {
									// 如果返回的数据包含illegal属性,就更新最后一条消息的illegal属性为true
DCloud_JSON's avatar
1.0.12  
DCloud_JSON 已提交
552 553 554
									this.updateLastMsg({
										illegal: true
									})
DCloud_JSON's avatar
DCloud_JSON 已提交
555 556 557
								}
								// 将从云端接收到的消息添加到消息列表中
								this.msgList.push({
DCloud_JSON's avatar
1.0.12  
DCloud_JSON 已提交
558 559 560 561 562 563 564 565 566 567 568 569
									// 添加消息创建时间
									create_time: Date.now(),
									// 标记消息为来自AI机器人
									isAi: true,
									// 添加消息内容
									content,
									// 添加消息总结
									summarize,
									// 添加消息分数不足标记
									insufficientScore,
									// 添加消息涉敏标记
									illegal
DCloud_JSON's avatar
DCloud_JSON 已提交
570 571
								})
								// 滚动窗口以显示最新的一条消息
DCloud_JSON's avatar
1.0.12  
DCloud_JSON 已提交
572 573
								this.$nextTick(() => {
									this.showLastMsg()
574
								})
DCloud_JSON's avatar
DCloud_JSON 已提交
575
							} else {
576
								console.log('用户点击了清空按钮,跳过前一次请求的回调。内容:', res.data.reply);
DCloud_JSON's avatar
DCloud_JSON 已提交
577
							}
DCloud_JSON's avatar
1.0.16  
DCloud_JSON 已提交
578 579 580 581
						} else {
							// 处理 sseChannel没结束 云函数提前结束的情况
							sseChannel.close()
							this.sseIndex = 0
DCloud_JSON's avatar
DCloud_JSON 已提交
582 583 584
						}
					})
					.catch(e => {
DCloud_JSON's avatar
1.0.12  
DCloud_JSON 已提交
585
						console.log(e);
DCloud_JSON's avatar
DCloud_JSON 已提交
586
						// 获取消息列表长度
DCloud_JSON's avatar
1.0.12  
DCloud_JSON 已提交
587
						let l = this.msgList.length
DCloud_JSON's avatar
DCloud_JSON 已提交
588 589 590 591
						// console.log(l,this.msgList[l-1]); 

						// 如果最后一条消息的来源是人工智能机器人 就将流式响应计数值设置为0
						if (l && sseChannel && this.msgList[l - 1].isAi) {
DCloud_JSON's avatar
1.0.16  
DCloud_JSON 已提交
592
							sseChannel.close()
DCloud_JSON's avatar
DCloud_JSON 已提交
593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608
							this.sseIndex = 0
						}

						// 更新最后一条消息的状态为-100(发送失败)
						this.updateLastMsg({
							state: -100
						})
						// 弹框提示用户错误原因
						uni.showModal({
							content: JSON.stringify(e.message),
							showCancel: false
						});
					})
			},
			// 滚动窗口以显示最新的一条消息
			showLastMsg() {
DCloud_JSON's avatar
1.0.12  
DCloud_JSON 已提交
609 610 611 612 613 614 615 616
				// 等待DOM更新
				this.$nextTick(() => {
					// 将scrollIntoView属性设置为"last-msg-item",以便滚动窗口到最后一条消息
					this.scrollIntoView = "last-msg-item"
					// 等待DOM更新,即:滚动完成
					this.$nextTick(() => {
						// 将scrollIntoView属性设置为空,以便下次设置滚动条位置可被监听
						this.scrollIntoView = ""
DCloud_JSON's avatar
DCloud_JSON 已提交
617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637
					})
				})
			},
			// 清空消息列表
			clear() {
				// 弹出确认清空聊天记录的提示框
				uni.showModal({
					title: "确认要清空聊天记录?",
					content: '本操作不可撤销',
					complete: (e) => {
						// 如果用户确认清空聊天记录
						if (e.confirm) {
							// 如果存在消息通道,就关闭消息通道
							if (sseChannel) {
								sseChannel.close()
							}
							// 将skip_callback设置为true,以便下一次请求可以正常回调
							skip_callback = true
							// 将流式响应计数值归零
							this.sseIndex = 0
							// 将消息列表清空 
DCloud_JSON's avatar
1.0.16  
DCloud_JSON 已提交
638
							this.msgList.splice(0, this.msgLength);
DCloud_JSON's avatar
DCloud_JSON 已提交
639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659
						}
					}
				});
			}
		}
	}
</script>

<style lang="scss">
	/* #ifdef VUE3 && APP-PLUS */
	@import "@/components/uni-ai-msg/uni-ai-msg.scss";
	/* #endif */

	/* #ifndef APP-NVUE */
	view,
	textarea,
	button,
	.page {
		display: flex;
		box-sizing: border-box;
	}
DCloud_JSON's avatar
1.0.16  
DCloud_JSON 已提交
660

DCloud_JSON's avatar
DCloud_JSON 已提交
661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718
	/* #endif */


	/* #ifndef APP-NVUE */
	page,
	/* #endif */
	.page,
	.container {
		background-color: #efefef;

		/* #ifdef APP-NVUE */
		flex: 1;
		/* #endif */

		/* #ifndef APP-NVUE */
		height: 100vh;
		/* #endif */

		/* #ifdef H5 */
		height: calc(100vh - 44px);
		/* #endif */

		flex-direction: column;
		align-items: center;
		justify-content: center;
	}

	/* #ifndef APP-NVUE */
	.container {
		background-color: #FAFAFA;
	}

	/* #endif */

	.foot-box {
		width: 750rpx;
		display: flex;
		flex-direction: column;
		padding: 10px 0px;
		background-color: #FFF;
	}

	.foot-box-content {
		justify-content: space-around;
	}

	.textarea-box {
		padding: 8px 10px;
		background-color: #f9f9f9;
		border-radius: 5px;
	}

	.textarea-box .textarea {
		max-height: 100px;
		font-size: 14px;
		/* #ifndef APP-NVUE */
		overflow: auto;
		/* #endif */
DCloud_JSON's avatar
1.0.12  
DCloud_JSON 已提交
719
		width: 450rpx;
DCloud_JSON's avatar
1.0.11  
DCloud_JSON 已提交
720
		font-size: 14px;
DCloud_JSON's avatar
DCloud_JSON 已提交
721 722 723 724 725 726 727
	}

	/* #ifdef H5 */
	/*隐藏滚动条*/
	.textarea-box .textarea::-webkit-scrollbar {
		width: 0;
	}
DCloud_JSON's avatar
1.0.12  
DCloud_JSON 已提交
728

DCloud_JSON's avatar
DCloud_JSON 已提交
729 730 731
	/* #endif */

	.input-placeholder {
DCloud_JSON's avatar
1.0.12  
DCloud_JSON 已提交
732
		color: #bbb;
DCloud_JSON's avatar
1.0.11  
DCloud_JSON 已提交
733
		line-height: 18px;
DCloud_JSON's avatar
DCloud_JSON 已提交
734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763
	}

	.trash,
	.send {
		width: 50px;
		height: 30px;
		justify-content: center;
		align-items: center;
		flex-shrink: 0;
	}

	.trash {
		width: 30rpx;
		margin-left: 10rpx;
	}

	.send {
		color: #FFF;
		border-radius: 4px;
		display: flex;
		margin: 0;
		padding: 0;
		font-size: 14px;
		margin-right: 20rpx;
	}

	/* #ifndef APP-NVUE */
	.send::after {
		display: none;
	}
DCloud_JSON's avatar
1.0.16  
DCloud_JSON 已提交
764

DCloud_JSON's avatar
DCloud_JSON 已提交
765 766 767 768 769 770 771 772
	/* #endif */


	.msg-list {
		flex: 1;
		height: 1px;
		width: 750rpx;
	}
DCloud_JSON's avatar
1.0.16  
DCloud_JSON 已提交
773

DCloud_JSON's avatar
DCloud_JSON 已提交
774 775 776 777 778 779 780 781
	.noData {
		margin-top: 15px;
		text-align: center;
		width: 750rpx;
		color: #aaa;
		font-size: 12px;
		justify-content: center;
	}
DCloud_JSON's avatar
1.0.16  
DCloud_JSON 已提交
782

DCloud_JSON's avatar
DCloud_JSON 已提交
783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810
	.tip-ai-ing {
		align-items: center;
		flex-direction: column;
		font-size: 14px;
		color: #919396;
		padding: 15px 0;
	}

	.uni-link {
		margin-left: 5px;
		line-height: 20px;
	}

	/* #ifdef H5 */
	@media screen and (min-width:650px) {
		.foot-box {
			border-top: solid 1px #dde0e2;
		}

		.page {
			width: 100vw;
			flex-direction: row;
		}

		.page * {
			max-width: 950px;
		}

DCloud_JSON's avatar
1.0.16  
DCloud_JSON 已提交
811
		.container
DCloud_JSON's avatar
DCloud_JSON 已提交
812
		{
DCloud_JSON's avatar
1.0.16  
DCloud_JSON 已提交
813 814 815 816 817
			box-shadow: 0 0 5px #e0e1e7;
			margin-top: 44px;
			border-radius: 10px;
			overflow: hidden;
		}
DCloud_JSON's avatar
DCloud_JSON 已提交
818

DCloud_JSON's avatar
1.0.16  
DCloud_JSON 已提交
819 820 821 822 823 824 825 826
		.container .header {
			height: 44px;
			line-height: 44px;
			border-bottom: 1px solid #F0F0F0;
			width: 100vw;
			justify-content: center;
			font-weight: 500;
		}
DCloud_JSON's avatar
DCloud_JSON 已提交
827

DCloud_JSON's avatar
1.0.16  
DCloud_JSON 已提交
828 829 830 831 832
		.content {
			background-color: #f9f9f9;
			position: relative;
			max-width: 90%;
		}
DCloud_JSON's avatar
DCloud_JSON 已提交
833

DCloud_JSON's avatar
1.0.16  
DCloud_JSON 已提交
834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856
		// .copy {
		// 	color: #888888;
		// 	position: absolute;
		// 	right: 8px;
		// 	top: 8px;
		// 	font-size: 12px;
		// 	cursor:pointer;
		// }
		// .copy :hover{
		// 	color: #4b9e5f;
		// }

		.foot-box,
		.foot-box-content,
		.msg-list,
		.msg-item,
		// .create_time,
		.noData,
		.textarea-box,
		.textarea,
		textarea-box {
			width: 100% !important;
		}
DCloud_JSON's avatar
DCloud_JSON 已提交
857

DCloud_JSON's avatar
1.0.16  
DCloud_JSON 已提交
858 859 860 861 862 863
		.textarea-box,
		.textarea,
		textarea,
		textarea-box {
			height: 120px;
		}
DCloud_JSON's avatar
DCloud_JSON 已提交
864

DCloud_JSON's avatar
1.0.16  
DCloud_JSON 已提交
865 866 867 868
		.foot-box,
		.textarea-box {
			background-color: #FFF;
		}
DCloud_JSON's avatar
DCloud_JSON 已提交
869

DCloud_JSON's avatar
1.0.16  
DCloud_JSON 已提交
870 871 872 873 874 875
		.foot-box-content {
			flex-direction: column;
			justify-content: center;
			align-items: flex-end;
			padding-bottom: 0;
		}
DCloud_JSON's avatar
DCloud_JSON 已提交
876

DCloud_JSON's avatar
1.0.16  
DCloud_JSON 已提交
877 878 879
		.menu {
			padding: 0 10px;
		}
DCloud_JSON's avatar
DCloud_JSON 已提交
880

DCloud_JSON's avatar
1.0.16  
DCloud_JSON 已提交
881 882 883 884 885 886 887 888 889
		.menu-item {
			height: 20px;
			justify-content: center;
			align-items: center;
			align-content: center;
			display: flex;
			margin-right: 10px;
			cursor: pointer;
		}
DCloud_JSON's avatar
DCloud_JSON 已提交
890

DCloud_JSON's avatar
1.0.16  
DCloud_JSON 已提交
891 892 893
		.trash {
			opacity: 0.8;
		}
DCloud_JSON's avatar
DCloud_JSON 已提交
894

DCloud_JSON's avatar
1.0.16  
DCloud_JSON 已提交
895 896 897
		.trash image {
			height: 15px;
		}
DCloud_JSON's avatar
DCloud_JSON 已提交
898 899


DCloud_JSON's avatar
1.0.16  
DCloud_JSON 已提交
900 901 902 903
		.textarea-box,
		.textarea-box * {
			// border: 1px solid #000;
		}
DCloud_JSON's avatar
DCloud_JSON 已提交
904

DCloud_JSON's avatar
1.0.16  
DCloud_JSON 已提交
905 906 907 908 909 910
		.send-btn-box .send-btn-tip {
			color: #919396;
			margin-right: 8px;
			font-size: 12px;
			line-height: 28px;
		}
DCloud_JSON's avatar
DCloud_JSON 已提交
911 912 913
	}

	/* #endif */
DCloud_JSON's avatar
DCloud_JSON 已提交
914
</style>