消息界面

目录src/views/home/home.vue

介绍

用户及群组房间状态更新展示

涉及接口

// 获取房间信息接口
export function roomGet(){
    return request({
        url: '/api/v2/room/get',
        method: 'get'
    })
}

// 获取通讯录列表(群聊)
export function userRoomRelationGet () {
    return request({
        url: '/api/v2/userRoomRelation/get',
        method: 'get'
    })
}

/* 注册socketio */
export function setup() {
	// 创建添加新好友套接字连接
	if (window.roomSocket == undefined) {
		// 创建聊天室套接字监听
		window.roomSocket = io.connect(process.env.VUE_APP_CLIENT_SOCKET + '/room');
		window.roomSocket.on('join', (data) => {
			//逻辑处理
		});
		//监听回复的消息
		window.roomSocket.on('leave', (data) => {
			//逻辑处理
		});
		///监听回复的消息
		window.roomSocket.on('chat', (data) => {
			// 回复根据标志分类todo
			response(data).then(res=>{
				let data = res.data
				//逻辑处理,存放indexdDB,存放一份实时的在vuex
				let msgList = JSON.parse(JSON.stringify(store.getters.msgList))
				let index = utils.arr.getIndexByTime(data['created_at'], msgList)
				//console.log(index);//未找到索引说明是他人发送的消息
				if(typeof index !== 'undefined'){
					msgList[index]['send_status'] = 1
				}else{
					msgList = msgList.concat(data)
				}
				//console.log(data)
				store.dispatch('updateMsgList', msgList)
				let reqData = {
					room_uuid :data['room_uuid'],
					created_at :data['created_at'],
					send_status: 1
				}
				if(store.getters.currentRoomSaveAction == 0){
					updateRoomMsg(reqData)
				}else if(store.getters.currentRoomSaveAction == 1){
					roomMsgUpdate(reqData)
				}
	
			})
		});

		send('join', {}, 'broadcast')
		//如果当前存在房间则进入
		if(store.getters.currentRoomUuid !== ''&& store.getters.currentRoomName !== ''){
		  send('join', {
			name: store.getters.currentRoomName,
			room_uuid: store.getters.currentRoomUuid,
			type: store.getters.currentRoomType,
			save_action: store.getters.currentRoomSaveAction
		  })
		}
		//监听好友请求
		window.roomSocket.on('beg', (data) => {
			response(data).then(res=>{
				let data = res.data
				if (data['action'] == 'beg_add') {
					// 复制原来的值
					data['data']['user_id'] = data['data']['id'];
					// 删除原来的键
					delete data['data']['id'];
					// 增加状态,0申请,1通过,2拒绝
					data['data']['status'] = 0
					Toast({ mes: `${data.data.nick_name}申请加你好友` });
					addAddressBookBeg(data['data'])
				}
				if (data['action'] == 'beg_success') {
					Toast({ mes: '发送成功,对方已收到申请' });
				}
				if(data['action'] == 'beg_add_success' ){
					Toast({ mes: '对方已同意添加好友' });
				}
			})
		});

		//监听单聊房间动态消息
		window.roomSocket.on('room', (data) => {
			response(data).then(res=>{
				let data = res.data
				//console.log(data)
				store.dispatch('updateRoomList', data)
			})
		});
		//监听群聊房间动态消息
		window.roomSocket.on('groupRoom', (data) => {
			response(data).then(res=>{
				let data = res.data
				//console.log(data)
				store.dispatch('updateGroupRoomList', data)
			})
		});
	}
}

/**
 * 加入聊天室
 * @param {*} data 
 */
export function joinChatSend(data){
    return send('join', data)
}