feat:增加消息记录功能
This commit is contained in:
61
lib/models/im_message.dart
Normal file
61
lib/models/im_message.dart
Normal file
@@ -0,0 +1,61 @@
|
||||
import 'dart:convert';
|
||||
|
||||
enum ImMessageType {
|
||||
chatPrivate('CHAT_PRIVATE'),
|
||||
chatGroup('CHAT_GROUP'),
|
||||
online('ONLINE'),
|
||||
heartbeat('HEARTBEAT'),
|
||||
system('SYSTEM'),
|
||||
notice('NOTICE');
|
||||
|
||||
final String value;
|
||||
|
||||
const ImMessageType(this.value);
|
||||
}
|
||||
|
||||
class ImMessage {
|
||||
final ImMessageType type;
|
||||
final String from;
|
||||
final String to;
|
||||
final String content;
|
||||
|
||||
ImMessage({
|
||||
required this.type,
|
||||
required this.from,
|
||||
required this.to,
|
||||
required this.content,
|
||||
});
|
||||
|
||||
factory ImMessage.fromJsonString(String json) {
|
||||
return ImMessage.fromJson(jsonDecode(json));
|
||||
}
|
||||
|
||||
factory ImMessage.fromJson(Map<String, dynamic> json) {
|
||||
return ImMessage(
|
||||
type: ImMessageType.values.firstWhere((e) => e.value == json['type']),
|
||||
from: json['from'],
|
||||
to: json['to'],
|
||||
content: json['content'],
|
||||
);
|
||||
}
|
||||
|
||||
Map<String, dynamic> toJson() => {
|
||||
'type': type.value,
|
||||
'from': from,
|
||||
'to': to,
|
||||
'content': content,
|
||||
};
|
||||
|
||||
String toJsonString() {
|
||||
return jsonEncode(toJson());
|
||||
}
|
||||
|
||||
static ImMessage online(int userId) {
|
||||
return ImMessage(
|
||||
type: ImMessageType.online,
|
||||
from: userId.toString(),
|
||||
to: 'SYSTEM',
|
||||
content: '',
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user