Files
zh-chat-flutter/lib/controllers/private_controller.dart
2022-10-26 14:09:51 +08:00

111 lines
3.3 KiB
Dart
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import 'package:chat/models/im/private_conversation_model.dart';
import 'package:chat/services/tim/conversation_service.dart';
import 'package:chat/services/tim/friend_service.dart';
import 'package:chat/services/tim_service.dart';
import 'package:chat/utils/ui_tools.dart';
import 'package:get/get.dart';
import 'package:tencent_im_sdk_plugin/models/v2_tim_friend_info_result.dart';
class PrivateController extends GetxController {
static PrivateController get to => Get.find<PrivateController>();
Rx<PrivateConversationModel> currentFriend =
PrivateConversationModel(userID: '').obs;
@override
void onClose() {
currentFriend.value = PrivateConversationModel(userID: '');
TimService.to.currentConversationId.value = '';
super.onClose();
}
/// 设置当前好友
Future<void> setCurrentFriend(String userID) async {
V2TimFriendInfoResult? info = await TimFriendService.to.friendInfo(userID);
if (info != null) {
TimService.to.currentConversationId.value = 'c2c_' + userID;
currentFriend.value.userID = userID;
currentFriend.value.isFriend =
info.relation == UserRelationEnum.V2TIM_FRIEND_RELATION_TYPE_BOTH_WAY;
currentFriend.value.friendRemark = info.friendInfo!.friendRemark ?? '';
currentFriend.value.userProfile = info.friendInfo!.userProfile;
/// 通过自定义Staffer字段判断是否是客服属于哪个店铺
if (info.friendInfo!.userProfile?.customInfo!['Staffer']?.isNotEmpty ==
true) {
currentFriend.value.shopId =
info.friendInfo!.userProfile?.customInfo!['Staffer'];
}
/// 通过用户自定义字段,判断是否允许陌生人消息
if (info.friendInfo!.userProfile?.customInfo!['Stranger']?.isNotEmpty ==
true) {
currentFriend.value.allowStranger = false;
}
/// 设置会话
currentFriend.value.conversation =
await TimConversationService.to.getById('c2c_' + userID);
currentFriend.refresh();
}
}
Future<void> togglePinned() async {
var res = await TimConversationService.to.setOnTop(
currentFriend.value.conversation!,
);
if (res) {
currentFriend.value.conversation =
await TimConversationService.to.getById(
'c2c_' + currentFriend.value.userID,
);
currentFriend.refresh();
UiTools.toast('修改成功');
}
}
Future<void> changeReceiveOpt() async {
var res = await TimConversationService.to.setReceiveOpt(
currentFriend.value.conversation!,
);
if (res) {
currentFriend.value.conversation =
await TimConversationService.to.getById(
'c2c_' + currentFriend.value.userID,
);
currentFriend.refresh();
UiTools.toast('修改成功');
}
}
Future<void> setRemark(String remark) async {
var result = await TimFriendService.to.setFriendRemark(
currentFriend.value.userID,
remark,
);
if (result) {
currentFriend.value.friendRemark = remark;
currentFriend.value.conversation =
await TimConversationService.to.getById(
'c2c_' + currentFriend.value.userID,
);
currentFriend.refresh();
TimConversationService.to.fetchList();
UiTools.toast('备注修改成功');
Get.back();
}
}
}