139 lines
3.9 KiB
Dart
139 lines
3.9 KiB
Dart
import 'dart:async';
|
|
|
|
import 'package:talk/network/NetworkConfig.dart';
|
|
import 'package:talk/network/RequestCenter.dart';
|
|
|
|
import '../../beans/character_info_bean.dart';
|
|
import '../../beans/chat_info_bean.dart';
|
|
import '../../beans/send_message_bean.dart';
|
|
import '../../network/BaseEntity.dart';
|
|
|
|
class ChatModel {
|
|
StreamController streamController = StreamController.broadcast();
|
|
|
|
ChatModel() {
|
|
setUp();
|
|
}
|
|
|
|
void setUp() {}
|
|
|
|
///获取人物信息
|
|
Future<void> getCharacterInfo(characterId) async {
|
|
RequestCenter.instance.requestGet(NetworkConfig.getCharacterInfo, {"characterId": characterId}, (BaseEntity dataEntity) {
|
|
|
|
if (dataEntity.code == 0) {
|
|
CharacterInfoBean characterInfoBean = CharacterInfoBean.fromJson(dataEntity.data);
|
|
|
|
streamController.sink.add({
|
|
'code': "getCharacterInfo", //有数据
|
|
'data': characterInfoBean
|
|
});
|
|
} else {
|
|
streamController.sink.add({
|
|
'code': "-1", //有数据
|
|
'data': dataEntity.message
|
|
});
|
|
}
|
|
}, (ErrorEntity errorEntity) {
|
|
print("errorEntity==${errorEntity.message}");
|
|
});
|
|
}
|
|
|
|
///获取聊天内容
|
|
Future<void> getChatInfo(characterId) async {
|
|
RequestCenter.instance.requestGet(NetworkConfig.getChatInfo, {"characterId": characterId}, (BaseEntity dataEntity) {
|
|
print("dataEntity==$dataEntity");
|
|
|
|
if (dataEntity.code == 0) {
|
|
List<ChatInfoBean> data = (dataEntity.data as List<dynamic>).map((e) => ChatInfoBean.fromJson(e as Map<String, dynamic>)).toList();
|
|
|
|
|
|
streamController.sink.add({
|
|
'code': "getChatInfo", //有数据
|
|
'data': data
|
|
});
|
|
} else {
|
|
streamController.sink.add({
|
|
'code': "-1", //有数据
|
|
'data': dataEntity.message
|
|
});
|
|
}
|
|
}, (ErrorEntity errorEntity) {
|
|
print("errorEntity==${errorEntity.message}");
|
|
});
|
|
}
|
|
|
|
///发送消息聊天
|
|
Future<void> sendMessage(characterId, message) async {
|
|
RequestCenter.instance.request(NetworkConfig.sendMessage, {
|
|
"characterId": characterId,
|
|
"message": message,
|
|
}, (BaseEntity dataEntity) {
|
|
print("dataEntity==${dataEntity.data}");
|
|
|
|
if (dataEntity.code == 0) {
|
|
SendMessageBean sendMessageBean = SendMessageBean.fromJson(dataEntity.data);
|
|
|
|
streamController.sink.add({
|
|
'code': "sendMessage", //有数据
|
|
'data': sendMessageBean
|
|
});
|
|
} else {
|
|
streamController.sink.add({
|
|
'code': "-1", //有数据
|
|
'data': dataEntity.message
|
|
});
|
|
}
|
|
}, (ErrorEntity errorEntity) {
|
|
print("errorEntity==${errorEntity.message}");
|
|
});
|
|
}
|
|
|
|
///删除单条或多条信息
|
|
Future<void> delChatByIds(id, characterId) async {
|
|
RequestCenter.instance.request(NetworkConfig.delChatByIds, {
|
|
"id": id,
|
|
"characterId": characterId,
|
|
}, (BaseEntity dataEntity) {
|
|
print("dataEntity==$dataEntity");
|
|
|
|
if (dataEntity.code == 0) {
|
|
streamController.sink.add({
|
|
'code': "delChatByIds", //有数据
|
|
'data': dataEntity.data
|
|
});
|
|
} else {
|
|
streamController.sink.add({
|
|
'code': "-1", //有数据
|
|
'data': dataEntity.message
|
|
});
|
|
}
|
|
}, (ErrorEntity errorEntity) {
|
|
print("errorEntity==${errorEntity.message}");
|
|
});
|
|
}
|
|
|
|
///清空聊天信息(重启聊天)
|
|
Future<void> delChat(characterId) async {
|
|
RequestCenter.instance.request(NetworkConfig.delChat, {
|
|
"characterId": characterId,
|
|
}, (BaseEntity dataEntity) {
|
|
print("dataEntity==$dataEntity");
|
|
|
|
if (dataEntity.code == 0) {
|
|
streamController.sink.add({
|
|
'code': "delChat", //有数据
|
|
'data': dataEntity.data
|
|
});
|
|
} else {
|
|
streamController.sink.add({
|
|
'code': "-1", //有数据
|
|
'data': dataEntity.message
|
|
});
|
|
}
|
|
}, (ErrorEntity errorEntity) {
|
|
print("errorEntity==${errorEntity.message}");
|
|
});
|
|
}
|
|
}
|