Files
sweet_chat_app/lib/models/im_message.dart
2026-06-19 21:10:03 +08:00

62 lines
1.2 KiB
Dart

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: '',
);
}
}