yanjie 4 anni fa
parent
commit
a5c1f64546
1 ha cambiato i file con 96 aggiunte e 51 eliminazioni
  1. 96 51
      pages/conversation/conversation.vue

+ 96 - 51
pages/conversation/conversation.vue

xqd xqd xqd xqd xqd
@@ -8,9 +8,9 @@
 		</view>
 		<!-- 这是聊天列表 当值为true时显示-->
 		<view class="cu-list menu-avatar margin-top-xs" v-if="qiehuan">
-			<view class="cu-item u-border-bottom" v-for="(item,index) in list" :key="index" @click="goliaotian(index)">
+			<view class="cu-item u-border-bottom" v-for="(item,index) in formatData" :key="index" @click="goliaotian(index)">
 				<view class="cu-avatar radius lg" :style="'background-image:url('+item.avatar+');'">
-					<view class="cu-tag badge"></view>
+					<view class="cu-tag badge" v-if="item.unread">{{item.unread}}</view>
 				</view>
 				<view class="content">
 					<view class="text-grey">
@@ -18,11 +18,13 @@
 					</view>
 					<view class="text-gray text-sm flex">
 						<!-- 信息发送者:最近一条消息 -->
-						<text class="text-cut"></text>
+						<text class="text-cut" v-if="item.lastMessage.type=='text'">{{item.lastMessage.payload.text}}</text>
+						<view class="item-info-top_content" v-else-if="item.lastMessage.type == 'audio'">[语音消息]</view>
+						<view class="item-info-top_content" v-else-if="item.lastMessage.type == 'image'">[图片消息]</view>
 					</view>
 				</view>
 				<view class="action">
-					<view class="text-grey text-xs">22:20</view>
+					<view class="text-grey text-xs">{{formatDate(item.lastMessage.timestamp)}}</view>
 				</view>
 			</view>
 			<u-empty text="暂无数据" v-if="list.length<=0" mode="order" margin-top="250"></u-empty>
@@ -42,9 +44,9 @@
 					</view>
 				</view>
 			</view>
-			<u-empty text="暂无数据" v-if="listPhone.length<=0"" mode="order" margin-top="250"></u-empty>
+			<u-empty text="暂无数据" v-if="listPhone.length<=0"" mode=" order" margin-top="250"></u-empty>
 		</view>
-		
+
 	</view>
 </template>
 
@@ -59,75 +61,121 @@
 		components: {
 			GoEasyAudioPlayer
 		},
+		computed: {
+			formatData() {
+				let arr = []
+				this.list.forEach(item => {
+					this.conversations.conversations.forEach(ite => {
+						if (item.flag == ite.userId) {
+							item.unread = ite.unread
+							item.lastMessage = ite.lastMessage
+							arr.push(item)
+							console.log(arr);
+						}
+					})
+				})
+				return arr
+			}
+		},
 		onShow() {
 			var i = 0;
-			while(i<3){
-				console.log('我是i',i);
-				if(typeof getApp().globalData.imService =='undefined'){
-					 let res = uni.getStorageSync('user');
-						getApp().globalData.imService = new IMService();
-						let loginResult = getApp().globalData.imService.login({
-							uuid: res.data.flag,
-							name: res.data.name,
-							avatar: res.data.avatar
-						});
-						if (loginResult) {
-							//连接IM
-							getApp().globalData.imService.connectIM();
-						}
-				}else{
+			while (i < 3) {
+				console.log('我是i', i);
+				if (typeof getApp().globalData.imService == 'undefined') {
+					let res = uni.getStorageSync('user');
+					getApp().globalData.imService = new IMService();
+					let loginResult = getApp().globalData.imService.login({
+						uuid: res.data.flag,
+						name: res.data.name,
+						avatar: res.data.avatar
+					});
+					if (loginResult) {
+						//连接IM
+						getApp().globalData.imService.connectIM();
+					}
+				} else {
 					break;
 				}
-				i++;			
+				i++;
 			}
+
+			this.imService = getApp().globalData.imService;
+			this.imService.onConversationsUpdate = (conversations) => {
+				this.conversations = conversations;
+				this.setUnreadAmount();
+			};
+			var promise = this.imService.latestConversations();
+			promise.then(res => {
+				this.conversations = res.content;
+				console.log("conversations load successfully")
+				this.setUnreadAmount();
+				uni.hideLoading();
+			}).catch(e => {
+				console.log(e)
+			});
 			this.getcnversationList()
-			console.log(this.list)
-			console.log(this.listPhone)
 		},
 		onLoad() {
-		 this.imService = getApp().globalData.imService;
-		 // console.log(this.imService.sendPrivateTextMessage())
-		 // console.log(this.imService.getFrindList())
-			
-		},
-		mounted() {
+
+			// console.log(this.imService.sendPrivateTextMessage())
+			// console.log(this.imService.getFrindList())
+
 		},
-	 
+		mounted() {},
+
 		data() {
 			return {
 				//控制聊天列表和通话记录
 				//false为通话记录,true为聊天列表
 				qiehuan: true,
-				index:0,
+				index: 0,
 				keyword: "",
-				list:[],
-				listPhone:[],
+				list: [],
+				listPhone: [],
+				conversations: {
+					unreadTotal: 0,
+					conversations: []
+				},
+				imService: null
 			}
 		},
 		methods: {
+			setUnreadAmount() {
+				console.log(this.conversations)
+				if (this.conversations.unreadTotal > 0) {
+					uni.setTabBarBadge({
+						index: 1,
+						text: this.conversations.unreadTotal.toString()
+					})
+				} else {
+					uni.hideTabBarRedDot({
+						index: 1
+					})
+				}
+			},
 			//搜索
-			search(e){
-				console.log(e)//输入框的值
+			search(e) {
+				console.log(e) //输入框的值
 			},
-		 
+
 			tonghua() {
 				console.log("点击了通话记录");
-					this.qiehuan = !this.qiehuan
+				this.qiehuan = !this.qiehuan
 			},
 			goliaotian(e) {
 				let data = this.list[e];
-				var index=data.flag.lastIndexOf("\_");
-				var user_id=data.flag.substring(index+1,data.flag.length);
-					 
+				var index = data.flag.lastIndexOf("\_");
+				var user_id = data.flag.substring(index + 1, data.flag.length);
+
 				let obj = {
-					'user_id':user_id,
-					'user_name':data.nickname,
-					'user_avatar':data.avatar,
+					'user_id': user_id,
+					'user_name': data.nickname,
+					'user_avatar': data.avatar,
 				}
 				var urls = "";
-				if(data.order_status==3){
+				if (data.order_status == 3) {
 					urls = "../conversation/conversationWindow?patient=" + JSON.stringify(obj) + "&type=1"
-				}else{
+				} else {
 					urls = "../conversation/conversationWindow?patient=" + JSON.stringify(obj) + "&type=2"
 				}
 				// ?patient=" + JSON.stringify(this.info) + "&type=1
@@ -142,15 +190,12 @@
 			},
 			getcnversationList: async function() {
 				let res = await this.$request.post("doctor/getUserListMsg")
-				if(res.status==0){
-					if(res.data.length!=0){
+				if (res.status == 0) {
+					if (res.data.length != 0) {
 						this.list = res.data;
 					}
 				}
-				
-				 
 			}
-
 		}
 	};
 </script>