uni-app 73聊天类封装(八)-添加聊天记录

chat.js

import $U from "./util.js";
import $H from './request.js';
class chat { 
	constructor(arg) { 
		this.url = arg.url
		this.isOnline = false 
		this.socket = null
		// 获取当前用户相关信息
		let user = $U.getStorage('user');
		this.user = user ? JSON.parse(user) : { },
			// 初始化聊天对象
			this.TO = false;
		// 连接和监听
		if (this.user.token) { 
			this.connectSocket()
		}
	}
	// 连接socket
	connectSocket() { 
		console.log(this.user);
		this.socket = uni.connectSocket({ 
			url: this.url + '?token=' + this.user.token,
			complete: () => { }
		})
		// 监听连接成功
		this.socket.onOpen(() => this.onOpen())
		// 监听接收信息
		this.socket.onMessage((res) => this.onMessage(res))
		// 监听断开
		this.socket.onClose(() => this.onClose())
		// 监听错误
		this.socket.onError(() => this.onError())
	}
	// 监听打开
	onOpen() { 
		// 用户状态上线
		this.isOnline = true;
		console.log('socket连接成功');

		// 获取用户离线消息
	}
	// 监听关闭
	onClose() { 
		// 用户下线
		this.isOnline = false;
		this.socket = null;
		console.log('socket连接关闭');
	}
	// 监听消息
	onMessage(data) { 
		console.log('监听消息', data);
	}
	// 监听连接错误
	onError() { 
		// 用户下线
		this.isOnline = false;
		this.socket = null;
		console.log('socket连接错误');
	}
	// 关闭连接
	close() { 
		this.socket.close()
	}
	// 创建聊天对象
	createChatObject(detail) { 
		this.TO = detail;
		console.log('创建聊天对象', this.TO)
	}
	// 销毁聊天对象
	destoryChatObject() { 
		this.TO = false
	}
	// 组织发送信息格式
	formatSendData(params) { 
		return { 
			id: 0, // 唯一id,后端生成,用于撤回指定消息
			from_avatar: this.user.avatar, // 发送者头像
			from_name: this.user.nickname || this.user.username, // 发送者昵称
			from_id: this.user.id, // 发送者id
			to_id: params.to_id || this.TO.id, // 接收人/群 id
			to_name: params.to_name || this.TO.name, // 接收人/群 名称
			to_avatar: params.to_avatar || this.TO.avatar, // 接收人/群 头像
			chat_type: params.chat_type || this.TO.chat_type, // 接收类型
			type: params.type, // 消息类型
			data: params.data, // 消息内容
			options: params.options ? params.options : { }, // 其他参数
			create_time: (new Date()).getTime(), // 创建时间
			isremove: 0, // 是否撤回
			sendStatus: params.sendStatus ? params.sendStatus : "pending" // 发送状态,success发送成功,fail发送失败,pending发送中
		}
	}
	// 发送信息
	send(message) { 
		return new Promise((result, reject) => { 
			// 添加消息历史记录
			this.addChatDetail();
			// let { 
			// data,
			// k
			// } = this.addChatDetail(params, true);
			// 置顶消息记录

			// 验证是否上线
			$H.post('/chat/send', { 
				to_id: this.TO.id,
				type: message.type,
				chat_type: this.TO.chat_type,
				data: message.data,
			}).then(res => { 
				// 发送成功
				message.id = res.id 
				message.sendStatus = 'success'
				result(res);
			}).catch(err => { 
				// 发送失败
				message.sendStatus = 'fail'
				// 断线重连提示
				result(err);
			});
		})

	}
    // 添加聊天记录
	addChatDetail(message,isSend=true){ 
		console.log('添加到聊天记录');
		if(!id){ 
			return { 
				data:{ },
				k:0
			}
		}
		// 获取对方id
		let id = isSend ? message.to_id : message.from_id;
		// key值:chetDetail_当前用户id_会话类型_接收人/群id
		let key = `chetDetail_${ this.user.id}_${ message.chat_type}_${ id}`;
		// 获取原来的聊天记录
		let list = this.getChatdetail(key)
		console.log('获取原来的聊天记录',list);
		// 标识
		message.key = 'k'+list.length;
		list.push(message);
		// 加入存储
		console.log('加入存储',k);
		this.setStorage(key,list);
		// 返回
		return { 
			data:message,
			k:message.k
		}
	}
	// 获取聊天记录
	getChatdetail(key=false){ 
		key = key ? key : `chatDetail_${ this.user.id}_${ this.TO.chat_type}_${ this.TO.id}`;
		return this.getStorage(key);
	}
	// 获取存储
	getStorage(key){ 
		let list = $U.getStorage(key);
		return list ? JSON.parse(list) : [];
	}
	// 设置存储
	setStorage(key,value){ 
		return $U.setStorage(key,JSON.stringify(value));
	}
}
export default chat

感谢大家观看,我们下期见

    原文作者:2019ab
    原文地址: https://blog.csdn.net/ab15176142633/article/details/120471951
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞