Browse Source

聊天记录

zs 2 năm trước cách đây
mục cha
commit
603fa8f1e9
2 tập tin đã thay đổi với 84 bổ sung20 xóa
  1. 1 1
      pages/message/index.vue
  2. 83 19
      pagesMessage/message/info.vue

+ 1 - 1
pages/message/index.vue

@@ -134,7 +134,7 @@
 					that.$set(that, `page`, page)
 					let skip = page * limit;
 					that.$set(that, `skip`, skip)
-					that.searchMarket();
+					that.search();
 					uni.hideLoading();
 				}
 			},

+ 83 - 19
pagesMessage/message/info.vue

@@ -4,7 +4,8 @@
 			<view class="one">
 				<!-- 聊天内容 -->
 				<scroll-view class="scroll-view" scroll-y="true" scroll-with-animation="true"
-					:scroll-into-view="scrollToView">
+					:scroll-into-view="scrollToView" refresher-enabled="true" :refresher-triggered="triggered"
+					@refresherrefresh="getFresh">
 					<view class="list-scroll-view">
 						<view class="chat-ls" v-for="(item,index) in msgList" :key="index" :id="'msg'+ index">
 							<view class="chat-time" v-if="item.time != ''">
@@ -65,6 +66,14 @@
 				shop: {},
 				// 历史记录
 				msgList: [],
+				total: 0,
+				skip: 0,
+				limit: 10,
+				page: 0,
+				// 判断是否跳到最后一条
+				is_bottom: true,
+				// 判断是否下拉刷新复位
+				triggered: false,
 				scrollToView: '', //滑动最后一条信息
 				isSocketOpen: false, //socket是否打开
 				pingpangTimes: null, //socket心跳计时器
@@ -76,6 +85,7 @@
 			const that = this;
 			// that.initWebpack(); //初始化
 			that.closeType = 1 //进入改为1,代表如果断开链接自动重连
+			that.clearPage();
 			if (that.id) that.search()
 		},
 		onLoad: async function(e) {
@@ -104,31 +114,46 @@
 			// 查询历史记录
 			async search() {
 				const that = this;
+				let info = {
+					skip: that.skip,
+					limit: that.limit,
+					room: that.id
+				}
 				let res;
 				res = await that.$api(`/chatRecord`, `GET`, {
-					room: that.id
+					...info,
 				}, 'chat');
 				if (res.errcode == '0') {
-					that.$set(that, `msgList`, res.data);
+					let msgList = [...res.data, ...that.msgList];
+					msgList.sort(function(a, b) {
+						return a.time > b.time ? 1 : -1;
+					});
+					that.$set(that, `msgList`, msgList);
+					that.$set(that, `total`, res.total)
+
 				} else {
 					uni.showToast({
 						title: res.errmsg,
 						icon: 'none'
 					})
 				}
-				res = await that.$api(`/room/${that.id}`, `GET`, {}, 'chat')
-				if (res.errcode == '0') {
-					that.$set(that, `shop`, res.data && res.data.shop);
-					if (res.data && res.data.shop && res.data.shop.name) {
-						uni.setNavigationBarTitle({
-							title: res.data.shop.name
-						});
+				// 查询房间信息
+				// 如果是下拉刷新聊天记录不请求房间信息
+				if (that.is_bottom == true) {
+					res = await that.$api(`/room/${that.id}`, `GET`, {}, 'chat')
+					if (res.errcode == '0') {
+						that.$set(that, `shop`, res.data && res.data.shop);
+						if (res.data && res.data.shop && res.data.shop.name) {
+							uni.setNavigationBarTitle({
+								title: res.data.shop.name
+							});
+						}
+					} else {
+						uni.showToast({
+							title: res.errmsg,
+							icon: 'none'
+						})
 					}
-				} else {
-					uni.showToast({
-						title: res.errmsg,
-						icon: 'none'
-					})
 				}
 				let id = that.msgList.filter(i => {
 					return i.speaker != that.user._id;
@@ -136,6 +161,7 @@
 				let ids = id.map(i => {
 					return i._id
 				})
+				// 信息已读
 				res = await that.$api(`/chatRecord/read`, `POST`, {
 					ids
 				}, 'chat')
@@ -148,9 +174,12 @@
 					})
 				}
 				// 跳转到最后一条数据 与前面的:id进行对照
-				that.$nextTick(function() {
-					that.scrollToView = 'msg' + (that.msgList.length - 1)
-				})
+				// 如果是下拉刷新聊天记录不跳到最后一条
+				if (that.is_bottom == true) {
+					that.$nextTick(function() {
+						that.scrollToView = 'msg' + (that.msgList.length - 1)
+					})
+				}
 			},
 			// 初始化websocket链接
 			initWebpack() {
@@ -314,7 +343,42 @@
 				that.$nextTick(function() {
 					that.scrollToView = 'msg' + (that.msgList.length - 1)
 				})
-			}
+			},
+			// 下拉刷新分页
+			getFresh(e) {
+				const that = this;
+				that.triggered = true;
+				let list = that.msgList;
+				let limit = that.limit;
+				setTimeout(() => {
+					if (that.total > list.length) {
+						uni.showLoading({
+							title: '加载中',
+							mask: true
+						})
+						let page = that.page + 1;
+						that.$set(that, `page`, page)
+						let skip = page * limit;
+						that.$set(that, `skip`, skip)
+						that.$set(that, `is_bottom`, false)
+						that.search();
+						uni.hideLoading();
+					} else {
+						uni.showToast({
+							title: `没有更多聊天记录了`,
+							icon: 'none'
+						})
+					}
+					that.triggered = false;
+				}, 1000)
+			},
+			clearPage() {
+				const that = this;
+				that.$set(that, `msgList`, [])
+				that.$set(that, `skip`, 0)
+				that.$set(that, `limit`, 10)
+				that.$set(that, `page`, 0)
+			},
 		}
 	}
 </script>