index_1.vue 18.3 KB
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 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628
<template>
	<view class="im-v">
		<view @click="hideDrawer">
			<mescroll-body ref="mescrollRef" bottom="50%" @init="mescrollInit" :down="downOption" @down="downCallback"
				:up="upOption">
				<!-- 无更多消息 -->
				<view v-if="isEnd" class="msg-end">没有更多消息了</view>

				<view class="msg-list">
					<!-- 消息列表 (必须配置id,以便定位) -->
					<view class="msg-list-item" v-for="(msg,index) in msgList" :key="index" :id="'msg'+msg.id">
						<view class="my" v-if="userInfo.userId === msg.sendUserId">
							<!-- 左-消息 -->
							<view class="content">
								<!-- 文字消息 -->
								<view v-if="msg.contentType==='text'" class="msg-text">
									<block v-for="(item,i) in msg.msgContent" :key="i">
										<text class="msg-text-txt" v-if="item.type=='text'">{{item.content}}</text>
										<image class="msg-text-emoji" :src="item.content" v-if="item.type=='emjio'" />
									</block>
								</view>
								<!-- 图片消息 -->
								<view v-if="msg.contentType=='image'" class="msg-img"
									@click="showPic(msg.msgContent.path)">
									<image lazy-load="true" :src="msg.msgContent.path"
										:style="{'width': msg.msgContent.width+'px','height': msg.msgContent.height+'px'}">
									</image>
								</view>
								<!-- 语言消息 -->
								<view v-if="msg.contentType==='voice'" class="msg-text msg-voice"
									@click="playVoice(msg)" :class="playMsgid == msg.id?'play':''">
									<view class="length">{{msg.msgContent.length}}</view>
									<view class="icon my-voice"></view>
								</view>
							</view>
							<!-- 右-头像 -->
							<view class="avatar">
								<u-avatar :src="baseURL+userInfo.headIcon" size="80"></u-avatar>
							</view>
						</view>
						<!-- 收到的消息 -->
						<view class="other" v-else>
							<!-- 左-头像 -->
							<view class="avatar">
								<u-avatar :src="baseURL+headIcon" size="80"></u-avatar>
							</view>
							<!-- 右-消息 -->
							<view class="content">
								<!-- 文字消息 -->
								<view v-if="msg.contentType ==='text'" class="msg-text">
									<block v-for="(item,i) in msg.msgContent" :key="i">
										<text class="msg-text-txt" v-if="item.type=='text'">{{item.content}}</text>
										<image class="msg-text-emoji" :src="item.content" v-if="item.type=='emjio'" />
									</block>
								</view>
								<!-- 图片消息 -->
								<view v-if="msg.contentType=='image'" class="msg-img"
									@click="showPic(msg.msgContent.path)">
									<image lazy-load="true" :src="msg.msgContent.path"
										:style="{'width': msg.msgContent.width+'px','height': msg.msgContent.height+'px'}">
									</image>
								</view>
								<!-- 语音消息 -->
								<view v-if="msg.contentType==='voice'" class="msg-text msg-voice"
									@click="playVoice(msg)" :class="playMsgid == msg.id?'play':''">
									<view class="icon other-voice"></view>
									<view class="length">{{msg.msgContent.length}}</view>
								</view>
							</view>
						</view>
					</view>
				</view>
			</mescroll-body>
		</view>
		<!-- 抽屉栏 -->
		<view class="popup-layer u-border-top" :class="popupLayerClass" @touchmove.stop.prevent="discard">
			<swiper class="emoji-swiper" indicator-dots="true" duration="150" v-show="showEmoji">
				<swiper-item v-for="(page,pid) in emojiTree" :key="pid">
					<view v-for="(em,eid) in page" :key="eid" @click="addEmoji(em)" class="emoji-item">
						<image mode="widthFix" :src="getEmojiUrl(em.url)" class="emoji-item-img"></image>
					</view>
				</swiper-item>
			</swiper>
			<view class="more-layer" v-show="showMore">
				<view class="list">
					<view class="box" @click="chooseImage('album')">
						<text class="icon tupian2"></text>
					</view>
					<!-- #ifndef H5 -->
					<view class="box" @click="chooseImage('camera')">
						<text class="icon paizhao"></text>
					</view>
					<!-- #endif -->
				</view>
			</view>
		</view>
		<view class="input-box" :class="popupLayerClass" @touchmove.stop.prevent="discard">
			<!-- #ifdef H5 -->
			<view class="input-box-icon icon biaoqing" @click="chooseEmoji"></view>
			<!-- #endif -->
			<!-- #ifndef H5 -->
			<view class="input-box-icon icon" :class="isVoice?'jianpan':'yuyin'" @click="switchVoice"></view>
			<!-- #endif -->
			<view class="voice-mode" :class="[isVoice?'':'hidden',recording?'recording':'']" @touchstart="voiceBegin"
				@touchmove.stop.prevent="voiceIng" @touchend="voiceEnd" @touchcancel="voiceCancel">{{voiceTis}}</view>
			<view class="text-mode" v-if="!isVoice">
				<view class="input-area">
					<textarea auto-height :cursor-spacing="8" maxlength="500" v-model="textMsg" @focus="textareaFocus"
						:focus="textFocus" />
				</view>
			</view>
			<!-- #ifndef H5 -->
			<view class="input-box-icon icon biaoqing" @click="chooseEmoji"></view>
			<!-- #endif -->
			<view class="input-box-icon icon add" @click="openMore"></view>
			<view class="send-btn" @click="sendText" v-if="!isVoice">发送</view>
		</view>
		<!-- 录音UI效果 -->
		<view class="record" :class="recording?'':'hidden'">
			<view class="ing" :class="willStop?'hidden':''">
				<view class="icon luyin2"></view>
			</view>
			<view class="cancel" :class="willStop?'':'hidden'">
				<view class="icon chehui"></view>
			</view>
			<view class="tis" :class="willStop?'change':''">{{recordTis}}</view>
		</view>
	</view>
</template>

<script>
	import chat from '@/libs/chat.js'
	import {
		emojiList,
		emojiTree
	} from '@/pages/message/emoji/emoji.js'
	import MescrollMixin from "@/uni_modules/mescroll-uni/components/mescroll-uni/mescroll-mixins.js";
	import {
		mapGetters
	} from 'vuex'

	export default {
		mixins: [MescrollMixin],
		name: 'im',
		data() {
			return {
				formUserId: '',
				headIcon: '',
				name: '',
				downOption: {
					auto: true,
				},
				upOption: {
					use: false,
					toTop: {
						src: ''
					}
				},
				currentPage: 1,
				pageSize: 30,
				//录音相关参数
				// #ifndef H5
				//H5不能录音
				RECORDER: uni.getRecorderManager(),
				// #endif
				//播放语音相关参数
				// #ifndef MP-ALIPAY
				AUDIO: uni.createInnerAudioContext(),
				// #endif
				playMsgid: null,
				popupLayerClass: '',
				textFocus: false,
				showMore: false,
				showEmoji: false,
				emojiList,
				emojiTree,
				msgList: [],
				isEnd: false,
				isVoice: false,
				voiceTis: '按住 说话',
				recordTis: "手指上滑 取消发送",
				recording: false,
				willStop: false,
				initPoint: {
					identifier: 0,
					Y: 0
				},
				recordTimer: null,
				recordLength: 0,
				textMsg: '',
				msgImageList: []
			}
		},
		computed: {
			...mapGetters(['userInfo']),
			baseURL() {
				return this.define.baseURL
			}
		},
		watch: {},
		onLoad(option) {
			this.formUserId = option.formUserId;
			this.headIcon = option.headIcon;
			this.name = option.name;
			uni.$on('getMessageList', data => {
				this.getMessageList(data)
			})
			uni.$on('addMsg', data => {
				this.addMsg(data)
			})
			this.$store.commit('chat/SET_FORMUSERID', this.formUserId)
			const updateReadMessage = {
				method: "UpdateReadMessage",
				formUserId: this.formUserId,
				token: uni.getStorageSync('token')
			}
			chat.sendMsg(JSON.stringify(updateReadMessage))
			uni.setNavigationBarTitle({
				title: option.name
			});
			//语音自然播放结束
			// #ifndef MP-ALIPAY
			this.AUDIO.onEnded((res) => {
				this.playMsgid = null;
			});
			// #endif
			// #ifndef H5 || MP-ALIPAY
			//录音开始事件
			this.RECORDER.onStart((e) => {
				this.recordBegin(e);
			});
			//录音结束事件
			this.RECORDER.onStop((e) => {
				this.recordEnd(e);
			});
			// #endif
		},
		onUnload() {
			uni.$off('getMessageList')
			uni.$off('addMsg')
			this.$store.commit('chat/SET_FORMUSERID', '')
			// #ifndef MP-ALIPAY
			this.AUDIO.stop();
			// #endif
		},
		methods: {
			getMessageList(data) {
				let msgImageList = []
				const list = data.list.map(o => {
					if (o.contentType === 'image') {
						if (o.content) {
							let content = {}
							if (typeof(o.content) === 'string') {
								content = JSON.parse(o.content)
							} else {
								content = o.content
							}
							msgImageList.push(this.baseURL + content.path)
						}
					}
					return this.dealMsg(o)
				})
				this.msgImageList = [...msgImageList, ...this.msgImageList]
				let topMsg = this.msgList[0]
				this.msgList = [...list, ...this.msgList]
				if (data.list.length < data.pagination.pageSize) {
					this.mescroll.lockDownScroll(true)
					this.isEnd = true
				}
				this.$nextTick(() => {
					if (this.currentPage <= 2) {
						this.mescroll.scrollTo(99999, 0)
					} else if (topMsg) {
						let view = uni.createSelectorQuery().select('#msg' + topMsg.id);
						view.boundingClientRect(v => {
							this.mescroll.scrollTo(v.top - 100, 0)
						}).exec();
					}
				})
			},
			downCallback() {
				const messageList = {
					method: "MessageList",
					toUserId: this.formUserId,
					formUserId: this.userInfo.userId,
					token: uni.getStorageSync('token'),
					currentPage: this.currentPage,
					pageSize: this.pageSize,
					sord: "desc"
				}
				chat.sendMsg(JSON.stringify(messageList))
				this.currentPage++;
				this.mescroll.endSuccess();
			},
			discard() {
				return;
			},
			switchVoice() {
				this.hideDrawer();
				this.isVoice = !this.isVoice;
			},
			openMore() {
				if (this.showMore) return this.hideDrawer()
				this.showMore = true;
				this.showEmoji = false;
				this.openDrawer();
			},
			openDrawer() {
				this.isVoice = false;
				this.popupLayerClass = 'showLayer';
			},
			hideDrawer() {
				this.popupLayerClass = '';
				setTimeout(() => {
					this.showMore = false;
					this.showEmoji = false;
				}, 150);
			},
			textareaFocus() {
				this.hideDrawer();
			},
			chooseEmoji() {
				if (this.showEmoji) return this.hideDrawer()
				this.showMore = false;
				this.showEmoji = true;
				this.openDrawer();
			},
			addEmoji(em) {
				this.textMsg += em.alt;
			},
			getEmojiUrl(url) {
				return require('@/pages/message/emoji/' + url)
			},
			chooseImage(type) {
				uni.chooseImage({
					// #ifdef H5
					count: 1,
					// #endif
					sourceType: [type],
					sizeType: ['original', 'compressed'],
					success: res => {
						this.hideDrawer();
						for (let i = 0; i < res.tempFilePaths.length; i++) {
							uni.uploadFile({
								url: this.define.comUploadUrl + 'IM',
								filePath: res.tempFilePaths[i],
								name: 'file',
								header: {
									Authorization: uni.getStorageSync('token') || ''
								},
								success: (uploadFileRes) => {
									const response = uploadFileRes.data ? JSON.parse(uploadFileRes
										.data) : {}
									if (uploadFileRes.statusCode !== 200) return this.$u.toast(
										response.msg)
									if (!response.data || !response.data.name) return
									const name = response.data.name
									uni.getImageInfo({
										src: res.tempFilePaths[i],
										success: (image) => {
											// #ifdef MP-WEIXIN
											wx.getFileSystemManager().readFile({
												filePath: image.path,
												encoding: "base64",
												success: (data) => {
													let msg = {
														name,
														width: image
															.width,
														height: image
															.height,
													};
													this.sendMessage(msg,
														'image');
												},
											});
											// #endif
											// #ifdef APP-PLUS
											let path = plus.io
												.convertLocalFileSystemURL(image.path);
											let fileReader = new plus.io.FileReader();
											fileReader.readAsDataURL(path);
											fileReader.onloadend = (evt) => {
												let msg = {
													name,
													width: image.width,
													height: image.height,
												};
												this.sendMessage(msg, 'image');
											}
											// #endif
											// #ifdef H5
											let file = document.getElementsByTagName(
												"input")[0].files[0];
											let reader = new FileReader();
											reader.readAsDataURL(file);
											reader.onload = (e) => {
												let msg = {
													name,
													width: image.width,
													height: image.height,
												};
												this.sendMessage(msg, 'image');
											}
											// #endif
										}
									});
								}
							});
						}
					}
				});
			},
			addMsg(data) {
				if (data.method === 'receiveMessage') {
					const updateReadMessage = {
						method: "UpdateReadMessage",
						formUserId: this.formUserId,
						token: uni.getStorageSync('token')
					}
					chat.sendMsg(JSON.stringify(updateReadMessage))
				}
				data.id = this.$u.guid()
				if (data.contentType === "text") {
					data.msgContent = this.replaceEmoji(data.content)
				}
				if (data.contentType === "image") {
					this.msgImageList.push(this.baseURL + data.content.path)
					data.msgContent = this.setPicSize(data.content)
					data.msgContent.path = this.baseURL + data.content.path
				}
				if (data.contentType === "voice") {
					data.msgContent = data.content
				}
				this.msgList.push(data)
				this.$nextTick(() => {
					this.mescroll.scrollTo(99999, 0)
				})
			},
			dealMsg(item) {
				if (item.contentType === "text") {
					item.msgContent = this.replaceEmoji(item.content)
				}
				if (item.contentType === "image") {
					item.msgContent = this.setPicSize(JSON.parse(item.content))
					item.msgContent.path = this.baseURL + item.msgContent.path
				}
				if (item.contentType === "voice") {
					item.msgContent = JSON.parse(item.content)
				}
				return item
			},
			sendText() {
				if (!this.textMsg) return
				this.hideDrawer()
				this.sendMessage(this.textMsg, 'text')
				this.textMsg = ''
				// this.textFocus = true
			},
			sendMessage(content, type) {
				const messageObj = {
					method: "SendMessage",
					token: uni.getStorageSync('token'),
					toUserId: this.formUserId,
					messageType: type,
					messageContent: content
				}
				chat.sendMsg(JSON.stringify(messageObj))
			},
			voiceBegin(e) { // 录音开始
				this.RECORDER.stop();
				if (e.touches.length > 1) {
					return;
				}
				this.initPoint.Y = e.touches[0].clientY;
				this.initPoint.identifier = e.touches[0].identifier;
				this.RECORDER.start({
					format: "mp3"
				}); //录音开始,
			},
			recordBegin(e) { //录音开始UI效果
				this.recording = true;
				this.voiceTis = '松开 结束';
				this.recordLength = 0;
				this.recordTimer = setInterval(() => {
					this.recordLength++;
				}, 1000)
			},
			voiceCancel() { // 录音被打断
				this.recording = false;
				this.voiceTis = '按住 说话';
				this.recordTis = '手指上滑 取消发送'
				this.willStop = true; //不发送录音
				this.RECORDER.stop(); //录音结束
			},
			voiceIng(e) { // 录音中(判断是否触发上滑取消发送)
				if (!this.recording) return
				let touche = e.touches[0];
				//上滑一个导航栏的高度触发上滑取消发送
				if (this.initPoint.Y - touche.clientY >= uni.upx2px(100)) {
					this.willStop = true;
					this.recordTis = '松开手指 取消发送'
				} else {
					this.willStop = false;
					this.recordTis = '手指上滑 取消发送'
				}
			},
			voiceEnd(e) { // 结束录音
				if (!this.recording) return
				this.recording = false;
				this.voiceTis = '按住 说话';
				this.recordTis = '手指上滑 取消发送'
				this.RECORDER.stop(); //录音结束
			},
			recordEnd(e) { //录音结束(回调文件)
				if (!this.willStop) {
					let min = parseInt(this.recordLength / 60);
					let sec = this.recordLength % 60;
					min = min < 10 ? '0' + min : min;
					sec = sec < 10 ? '0' + sec : sec;
					if (sec < '01') {
						this.willStop = true;
						this.$u.toast('说话时间太短');
						return
					}
					// #ifdef MP-WEIXIN
					wx.getFileSystemManager().readFile({
						filePath: e.tempFilePath,
						encoding: "base64",
						success: (data) => {
							let msg = {
								base64: data.data,
								length: min + ':' + sec
							};
							this.sendMessage(msg, 'voice')
						},
					});
					// #endif
					// #ifdef APP-PLUS
					let path = plus.io.convertLocalFileSystemURL(e.tempFilePath);
					let fileReader = new plus.io.FileReader();
					fileReader.readAsDataURL(path);
					fileReader.onloadend = (evt) => {
						let msg = {
							base64: evt.target.result.replace(/data:audio\/.*;base64,/, ''),
							length: min + ':' + sec
						};
						this.sendMessage(msg, 'voice');
					}
					// #endif
				} else {
					// console.log('取消发送录音');
				}
				this.willStop = false;
			},
			setPicSize(content) { //处理图片尺寸,如果不处理宽高,新进入页面加载图片时候会闪
				// 让图片最长边等于设置的最大长度,短边等比例缩小,图片控件真实改变,区别于aspectFit方式。
				let maxW = uni.upx2px(350); //350是定义消息图片最大宽度
				let maxH = uni.upx2px(350); //350是定义消息图片最大高度
				if (content.width > maxW || content.height > maxH) {
					let scale = content.width / content.height;
					content.width = scale > 1 ? maxW : maxH * scale;
					content.height = scale > 1 ? maxW / scale : maxH;
				}
				return content;
			},
			replaceEmoji(str) { //替换表情符号为图片
				let replacedStr = str.replace(/\[([^(\]|\[)]*)\]/g, item => 'nccncc' + item + 'nccncc');
				let strArr = replacedStr.split(/nccnccnccncc|nccncc/g)
				strArr = strArr.filter(o => o)
				let contentList = []
				for (let i = 0; i < strArr.length; i++) {
					let item = {
						content: strArr[i],
						type: 'emjio'
					}
					if (/\[([^(\]|\[)]*)\]/.test(strArr[i])) {
						let content = ''
						for (let j = 0; j < this.emojiList.length; j++) {
							let row = this.emojiList[j];
							if (row.alt == strArr[i]) {
								content = this.getEmojiUrl(row.url)
								break
							}
						}
						item = {
							content: content,
							type: 'emjio'
						}
					} else {
						item = {
							content: strArr[i],
							type: 'text'
						}
					}
					contentList.push(item)
				}
				return contentList
			},
			showPic(path) { // 预览图片
				uni.previewImage({
					indicator: "none",
					current: path,
					urls: this.msgImageList
				});
			},
			playVoice(msg) { // 播放语音
				this.AUDIO.stop();
				this.AUDIO.src = this.baseURL + msg.msgContent.path;
				if (this.playMsgid != null && this.playMsgid == msg.id) {
					this.$nextTick(() => {
						this.AUDIO.stop();
					});
					this.playMsgid = null;
				} else {
					this.$nextTick(() => {
						this.AUDIO.play();
					});
					this.playMsgid = msg.id;
				}
			},
		}
	}
</script>

<style lang="scss">
	@import "./index.scss";
</style>