chat.vue 11.2 KB
Newer Older
DCloud_JSON's avatar
DCloud_JSON 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 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 405 406 407 408 409 410 411 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 461 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 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514
<template>
	<view class="page">
		<view class="container">
			<text class="noData" v-if="msgList.length === 0">没有对话记录</text>
			<scroll-view :scroll-into-view="scrollIntoView" scroll-y="true" class="msg-list" :enable-flex="true">
				<view v-for="(msg,index) in msgList" :class="{reverse:!msg.isAi}" class="msg-item" :key="index">
					<view class="userInfo">
						<image class="avatar" src="@/static/avatar.png" mode="widthFix"></image>
					</view>
					<view style="flex-direction: column;">
						<text class="create_time">{{toFriendlyTime(msg.create_time)}}</text>
						<view class="content">
							<!-- <uni-ai-msg :content="msg.content"></uni-ai-msg> -->
							<text :selectable="true">{{msg.content}}</text>
							<text v-if="index == msgList.length-1 && msg.isAi && sseIndex" class="blinking-cursor">|</text>
						</view>
					</view>
					<uni-icons v-if="index == msgList.length-1 && !msg.isAi && msg.state != 100 && msgStateIcon(msg)"
						@click="msg.state == -100 ? retriesSendMsg() : ''" :color="msg.state===0?'#999':'#d22'"
						:type="msgStateIcon(msg)" class="msgStateIcon">
					</uni-icons>
				</view>
				<view id="last-msg-item"></view>
			</scroll-view>

			<view class="foot-box">
				<view class="trash">
					<uni-icons @click="clear" type="trash" size="24" color="#888"></uni-icons>
				</view>
				<view class="textarea-box">
					<textarea v-model="content" class="textarea" :auto-height="true" :disabled="inputBoxDisabled"
						:placeholder="placeholderText" :maxlength="-1" placeholder-class="input-placeholder"></textarea>
				</view>
				<button @click="beforeSendMsg" :disabled="inputBoxDisabled || !content" class="send">发送</button>
			</view>
		</view>
		<label class="set-stream">
			<text style="font-size: 12px;">stream:</text>
			<switch style="transform: scale(0.7);" checked="true" :checked="stream" @change="changeStream" />
		</label>
	</view>
</template>

<script>
	import toFriendlyTime from './toFriendlyTime.js';
	export default {
		data() {
			return {
				scrollIntoView: "",
				msgList: [],
				content: "",
				sseIndex: 0,
				stream:false
			}
		},
		computed: {
			inputBoxDisabled() {
				if (this.sseIndex !== 0) {
					return true
				}

				let length = this.msgList.length
				if (length) {
					return !this.msgList[length - 1].isAi
				} else {
					return false
				}
			},
			placeholderText() {
				if (this.inputBoxDisabled) {
					return 'uni-ai正在回复中'
				} else {
					// #ifdef H5
					return window.innerWidth > 960 ? '请输入内容,ctrl + enter 发送' : '请输入要发给uni-ai的内容'
					// #endif
					return '请输入要发给uni-ai的内容'
				}
			}
		},
		watch: {
			msgList(msgList) {
				// uni.setStorageSync('uni-ai-msg', msgList)
			}
		},
		async mounted() {
			// for (let i = 0; i < 15; i++) {
			// 	this.msgList.push({
			// 		isAi: i % 2 == true,
			// 		content: "1-" + i
			// 	})
			// }

			this.msgList = uni.getStorageSync('uni-ai-msg') || []

			// this.msgList.pop()

			console.log('this.msgList', this.msgList);
			this.showLastMsg()

			//获得消息输入框对象
			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) {
						this.beforeSendMsg();
					}
				};
				textareaDom.onkeyup = e => {
					//松开adjunct键
					if ([16, 17, 18, 93].includes(e.keyCode)) {
						adjunctKeydown = false;
					}
				};
			}
		},
		methods: {
			changeStream(e){
				console.log('e',e.detail.value);
				this.stream = e.detail.value
			},
			toFriendlyTime(t) {
				return toFriendlyTime(t)
			},
			retriesSendMsg() {
				this.send()
			},
			beforeSendMsg() {
				if(!this.content){
					return uni.showToast({
						title: '内容不能为空',
						icon: 'none'
					});
				}
				
				this.msgList.push({
					isAi: false,
					content: this.content,
					state: 0,
					create_time: Date.now()
				})
				this.showLastMsg()
				// 清空文本内容
				this.$nextTick(() => {
					this.content = ''
				})
				this.send()
			},
			async send() {
				let messages = []
				// 复制一份,消息列表数据
				let msgs = [...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)
				}

				messages = msgs.map(item => {
					let role = "user"
					if (item.isAi) {
						role = item.summarize ? 'system' : 'assistant'
					}
					return {
						content: item.content,
						role
					}
				})

				console.log('send to ai messages:', messages);
				
				
				let SSEChannel = false;
				if(this.stream){
					SSEChannel = new uniCloud.SSEChannel() // 创建消息通道
					SSEChannel.on('message', (message) => { // 监听message事件
						console.log('on message', message);
						if (this.sseIndex === 0) {
							this.msgList.push({
								isAi: true,
								content: message,
								create_time: Date.now()
							})
							this.showLastMsg()
						} else {
							let length = this.msgList.length,
								lastMsg = this.msgList[length - 1]
							lastMsg.content +=  "\n" + message
							this.msgList.splice(length - 1, 1, lastMsg)
							this.showLastMsg()
						}
						this.sseIndex++
					})
					SSEChannel.on('end', (e) => { // 监听end事件,如果云端执行end时传了message,会在客户端end事件内收到传递的消息
						console.log('on end', e);
						if(e.summarize){
							let length = this.msgList.length,
								lastMsg = this.msgList[length - 1]
							lastMsg.summarize = e.summarize
							this.msgList.splice(length - 1, 1, lastMsg)
						}
						this.sseIndex = 0
						this.showLastMsg()
					})
					await SSEChannel.open() // 等待通道开启
				}
				

				uniCloud.callFunction({
						name: "uni-ai-chat",
						data: {
							messages,
							SSEChannel
						}
					})
					.then(res => {
						let index = this.msgList.length - 1
						let lastItem = this.msgList[index]
						lastItem.state = 100
						this.msgList.splice(index, 1, lastItem)

						if (!SSEChannel) {
							console.log(res, res.result.reply);
							this.msgList.push({
								isAi: true,
								content: res.result.reply,
								summarize: res.result.summarize,
								create_time: Date.now()
							})
							this.showLastMsg()
						}
					})
					.catch(e => {
						console.log(e);
						let index = this.msgList.length - 1
						let lastItem = this.msgList[index]
						lastItem.state = -100
						this.msgList.splice(index, 1, lastItem)

						uni.showModal({
							content: JSON.stringify(e.message),
							showCancel: false
						});
					})
			},
			showLastMsg() {
				this.$nextTick(() => {
					this.scrollIntoView = "last-msg-item"
					this.$nextTick(() => {
						this.scrollIntoView = ""
					})
				})
			},
			msgStateIcon(msg) {
				switch (msg.state) {
					case 0:
						//	发送中
						return 'spinner-cycle'
						break;
					case -100:
						//	发送失败
						return 'refresh-filled'
						break;
					case -200:
						//	禁止发送(内容不合法)
						return 'info-filled'
						break;
					default:
						return false
						break;
				}
			},
			clear() {
				uni.showModal({
					title: "确认要清空聊天记录?",
					content: '本操作不可撤销',
					complete: (e) => {
						if (e.confirm) {
							this.msgList = []
						}
					}
				});

			}
		}
	}
</script>

<style lang="scss">
	/* #ifndef APP-NVUE */
	view,
	textarea,
	button,
	* {
		display: flex;
		box-sizing: border-box;
	}
	/* #endif */

	/* #ifndef APP-NVUE */
	page,
	/* #endif */
	.page,
	.container {
		background-color: #efefef;
		width: 750rpx;
		flex: 1;
		height: calc(100vh - 44px);
		flex-direction: column;
		align-items: center;
	}

	/* #ifndef APP-NVUE */
	.container {
		background-color: #FAFAFA;
	}
	.container,
	.container * {
		max-width: 550px;
	}
	/* #endif */

	.foot-box {
		width: 750rpx;
		display: flex;
		justify-content: space-around;
		align-items: self-start;
		padding: 10px 0px;
		background-color: #FFF;
	}

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

	.textarea-box .textarea {
		max-height: 100px;
		width: 460rpx;
		font-size: 14px;
		/* #ifndef APP-NVUE */
		overflow: auto;
		/* #endif */
	}

	/* #ifndef APP-NVUE */
	/*隐藏滚动条*/
	.textarea-box .textarea::-webkit-scrollbar {
		width: 0;
	}
	/* #endif */

	.input-placeholder {
		color: #bbb;
	}

	.trash,
	.send {
		/* border: 1px solid #000; */
		width: 50px;
		height: 30px;
		justify-content: center;
		align-items: center;
	}

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

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

	/* #ifndef APP-NVUE */
	.send::after {
		display: none;
	}

	/* #endif */


	.msg-list {
		flex: 1;
		height: 1px;
		width: 750rpx;
	}

	.userInfo {
		flex-direction: column;
	}

	.msg-item {
		position: relative;
		width: 750rpx;
		flex-direction: row;
		padding: 0 15px;
		padding-bottom: 15px;
	}

	.msgStateIcon {
		position: relative;
		top: 5px;
		right: 5px;
		align-self: center;
	}

	.avatar {
		width: 40px;
		border-radius: 2px;
	}

	.create_time {
		font-size: 12px;
		padding: 5px;
		padding-top: 0;
		color: #aaa;
	}

	.content {
		max-width: 500rpx;
		background-color: #FFF;
		border-radius: 5px;
		/* border-top-left-radius: 0; */
		padding: 12px 10px;
		margin-left: 10px;
		/* #ifndef APP-NVUE */
		word-break: break-all;
		user-select: text;
		cursor: text;
		/* #endif */
	}

	/* #ifndef APP-NVUE */
	.content {
		display: inline;
	}
	.content * {
		display: inline;
	}
	/* #endif */


	.reverse {
		flex-direction: row-reverse;
	}

	.reverse .content {
		margin-left: 0;
		margin-right: 10px;
		border-top-left-radius: 15px;
		// border-top-right-radius: 0;
	}

	.reverse-align {
		align-items: flex-end;
	}

	.noData {
		margin-top: 15px;
		text-align: center;
		width: 750rpx;
		color: #aaa;
		font-size: 12px;
		justify-content: center;
	}

	.blinking-cursor {
		color: blue;
		font-weight: bold;
		animation: blinking 1s infinite;
	}
	
	@keyframes blinking {
		from {
			opacity: 1.0;
		}
		to {
			opacity: 0.0;
		}
	}
	
	.set-stream{
		position: fixed;
		right: 0px;
		top: 0;
		z-index: 999;
		padding: 5px;
		justify-content: center;
		align-items: center;
	}
</style>