638 lines
20 KiB
Dart
638 lines
20 KiB
Dart
import 'dart:async';
|
||
import 'dart:convert';
|
||
import 'dart:io';
|
||
|
||
import 'package:circle_app/circle_app/chat/widget/svg_dialog.dart';
|
||
import 'package:circle_app/circle_app/circle/logic.dart';
|
||
import 'package:circle_app/circle_app/minefragment/logic.dart';
|
||
import 'package:circle_app/circle_app/msg/logic.dart';
|
||
import 'package:circle_app/circle_app/world_call_out/logic.dart';
|
||
import 'package:circle_app/circle_app/world_call_out/view.dart';
|
||
import 'package:circle_app/main.dart';
|
||
import 'package:circle_app/net/api.dart';
|
||
import 'package:circle_app/net/dio_manager.dart';
|
||
import 'package:circle_app/router/routers.dart';
|
||
import 'package:circle_app/utils/util.dart';
|
||
import 'package:connectivity/connectivity.dart';
|
||
import 'package:event_bus/event_bus.dart';
|
||
import 'package:flutter/material.dart';
|
||
import 'package:flutter/services.dart';
|
||
import 'package:flutter_native_splash/flutter_native_splash.dart';
|
||
import 'package:get/get.dart';
|
||
import 'package:shared_preferences/shared_preferences.dart';
|
||
import 'package:tencent_cloud_chat_uikit/data_services/core/core_services.dart';
|
||
import 'package:tencent_cloud_chat_uikit/data_services/message/message_services.dart';
|
||
import 'package:tencent_cloud_chat_uikit/data_services/services_locatar.dart';
|
||
import 'package:tencent_cloud_chat_uikit/tencent_cloud_chat_uikit.dart';
|
||
|
||
import '../../commons/Widgets/today_people_dialog.dart';
|
||
import '../../utils/PaymentUtils.dart';
|
||
import '../../utils/SharedPreferencesHelper.dart';
|
||
import '../../utils/eventBus.dart';
|
||
import '../../view/notice.dart';
|
||
import '../circle/view.dart';
|
||
import '../circle_list/logic.dart';
|
||
import '../circle_list/view.dart';
|
||
import '../dialog/UpdateDialog.dart';
|
||
import '../invite/logic.dart';
|
||
import '../minefragment/view.dart';
|
||
import '../msg/view.dart';
|
||
import 'like_peole_sended_call_out_dialog.dart';
|
||
import 'state.dart';
|
||
import 'package:intl/intl.dart';
|
||
|
||
class HomeLogic extends GetxController with WidgetsBindingObserver {
|
||
late TabController tabController;
|
||
var connectivitySub;
|
||
ConnectivityResult? currentResult;
|
||
User? model;
|
||
int currentIndex = 0;
|
||
int showMsgTipCount = 0;
|
||
String inventStr = '邀请好友';
|
||
String inventTipStr = '邀请更多圈友一起玩';
|
||
Widget currentPage = Container();
|
||
|
||
final HomeState state = HomeState();
|
||
|
||
List<Widget> tabs = [];
|
||
static final MessageService _messageService =
|
||
serviceLocator<MessageService>();
|
||
final RxString unreadSIze = "0".obs;
|
||
|
||
final RxBool showcirlceUnred = false.obs;
|
||
late V2TimSimpleMsgListener msgListener;
|
||
InterestsBean mycircle = InterestsBean(lists: []);
|
||
|
||
StreamSubscription? sub;
|
||
|
||
bool isUpdateVersion = false;
|
||
|
||
bool isProd = true;
|
||
|
||
bool isFirst = true;
|
||
|
||
String accountId = '';
|
||
|
||
String inviteCode = '';
|
||
|
||
var link = '';
|
||
@override
|
||
void onClose() {
|
||
// TODO: implement onClose
|
||
super.onClose();
|
||
connectivitySub.cancel();
|
||
TencentImSDKPlugin.v2TIMManager.v2TIMMessageManager
|
||
.removeAdvancedMsgListener();
|
||
WidgetsBinding.instance.removeObserver(this);
|
||
sub!.cancel();
|
||
}
|
||
|
||
@override
|
||
void onInit() async {
|
||
super.onInit();
|
||
unreadSIze.value = '0';
|
||
|
||
loadEnvTypeData();
|
||
await checkInfo();
|
||
// 设置网络变化监听
|
||
connectListener();
|
||
getIMData();
|
||
|
||
setFirstData();
|
||
|
||
loadMyInfoData();
|
||
|
||
sub = EventBusManager.on<RefreshUnread>().listen((event) {
|
||
getUnreadSize();
|
||
});
|
||
|
||
WidgetsBinding.instance.addObserver(this);
|
||
|
||
addMsgListener();
|
||
|
||
checkVersion();
|
||
loadMyCircleData();
|
||
|
||
initWxApi();
|
||
|
||
awaitShowTodayDialog();
|
||
}
|
||
|
||
void addMsgListener() {
|
||
msgListener = V2TimSimpleMsgListener(onRecvC2CTextMessage: (
|
||
msgID,
|
||
userInfo,
|
||
text,
|
||
) async {
|
||
print("收到新消息" + text);
|
||
getUnreadSize();
|
||
}, onRecvC2CCustomMessage: (
|
||
String msgID,
|
||
V2TimUserInfo sender,
|
||
String customData,
|
||
) {
|
||
if (sender.userID == "qpqz_dev_10_10000000000" ||
|
||
sender.userID == "qpqz_prod_10_10000000000") {
|
||
if (customData.isEmpty) {
|
||
return;
|
||
}
|
||
|
||
print("123123123123" + customData);
|
||
Map<String, dynamic> jsonData = json.decode(customData);
|
||
int event = jsonData['event'];
|
||
Map<String, dynamic> content = jsonData['content'];
|
||
switch (event) {
|
||
case 1: //关注
|
||
String ageMsg =
|
||
"${content['genderName']}·${content['age']}·${content['roleName']}·${content['orientationName']}";
|
||
if (content['isFollow'] == 1) {
|
||
//互关
|
||
showFloatingButtonOverlay(Get.context!, content['nickname'],
|
||
ageMsg, content['avatarThumb'], 3, () {
|
||
pushChatPage(content['id'].toString(), content['imId'],
|
||
content['nickname']);
|
||
});
|
||
} else {
|
||
showFloatingButtonOverlay(Get.context!, content['nickname'],
|
||
ageMsg, content['avatarThumb'], 1, () {
|
||
setLike(content['id'].toString());
|
||
});
|
||
}
|
||
|
||
break;
|
||
case 2: //上线
|
||
|
||
String ageMsg =
|
||
"${content['genderName']}·${content['age']}·${content['roleName']}·${content['orientationName']}";
|
||
|
||
showFloatingButtonOverlay(Get.context!, content['nickname'], ageMsg,
|
||
content['avatarThumb'], 2, () {
|
||
pushChatPage(content['id'].toString(), content['imId'],
|
||
content['nickname']);
|
||
});
|
||
break;
|
||
case 9: //有新人加入
|
||
{
|
||
CircleLogic logic = Get.find<CircleLogic>();
|
||
logic.isNewPeopleRed = true;
|
||
logic.update();
|
||
}
|
||
break;
|
||
case 8: //有新的喊话
|
||
{
|
||
World_call_outLogic logic = Get.find<World_call_outLogic>();
|
||
logic.isNewMsg = true;
|
||
logic.update();
|
||
}
|
||
break;
|
||
case 10: //有新访客
|
||
{
|
||
MsgLogic logic = Get.find<MsgLogic>();
|
||
logic.getMode();
|
||
}
|
||
break;
|
||
}
|
||
}
|
||
});
|
||
|
||
TencentImSDKPlugin.v2TIMManager.v2TIMMessageManager.addAdvancedMsgListener(
|
||
listener: V2TimAdvancedMsgListener(onRecvNewMessage: (message) async {
|
||
getUnreadSize();
|
||
print(message);
|
||
if (message.elemType == MessageElemType.V2TIM_ELEM_TYPE_CUSTOM) {
|
||
if (message.customElem?.data != null) {
|
||
print(message.customElem!.data!);
|
||
Map jsonData = jsonDecode(message.customElem!.data!);
|
||
if (jsonData.containsKey('event')) {
|
||
String event = jsonData['event'].toString();
|
||
|
||
if (int.parse(event) == 1) {
|
||
Map content = jsonData['content'];
|
||
String ageMsg =
|
||
"${content['genderName']}·${content['age']}·${content['roleName']}·${content['orientationName']}";
|
||
if (content['isFollow'] == 1) {
|
||
//互关
|
||
showFloatingButtonOverlay(Get.context!, content['nickname'],
|
||
ageMsg, content['avatarThumb'], 3, () {
|
||
pushChatPage(content['id'].toString(), content['imId'],
|
||
content['nickname']);
|
||
});
|
||
} else {
|
||
showFloatingButtonOverlay(Get.context!, content['nickname'],
|
||
ageMsg, content['avatarThumb'], 1, () {
|
||
setLike(content['id'].toString());
|
||
});
|
||
}
|
||
} else if (int.parse(event) == 2) {
|
||
Map content = jsonData['content'];
|
||
String ageMsg =
|
||
"${content['genderName']}·${content['age']}·${content['roleName']}·${content['orientationName']}";
|
||
|
||
showFloatingButtonOverlay(Get.context!, content['nickname'],
|
||
ageMsg, content['avatarThumb'], 2, () {
|
||
pushChatPage(content['id'].toString(), content['imId'],
|
||
content['nickname']);
|
||
});
|
||
} else if (int.parse(event) == 3) {
|
||
//喜欢列表刷新
|
||
SharedPreferencesHelper sp = await SharedPreferencesHelper.getInstance();
|
||
if (await sp.isFirstPeopleSendCallOut()) {
|
||
Future.delayed(Duration(seconds: 20),() {
|
||
EventBusManager.fire(ShowpeopleCallOutNotifi(message.customElem!.desc!.toString()));
|
||
});
|
||
}
|
||
} else if (int.parse(event) == 5) {
|
||
//有人送礼会话置顶
|
||
// print(jsonData.toString());
|
||
var info = jsonDecode(jsonData['content']);
|
||
TencentImSDKPlugin.v2TIMManager.v2ConversationManager
|
||
.pinConversation(
|
||
conversationID: 'c2c_' + info['fromImId'],
|
||
isPinned: info['top'] == 1);
|
||
} else if (int.parse(event) == 9) {
|
||
//有新人加入
|
||
CircleLogic logic = Get.find<CircleLogic>();
|
||
logic.isNewPeopleRed = true;
|
||
logic.update();
|
||
} else if (int.parse(event) == 8) {
|
||
//有新的喊话
|
||
if (Get.isRegistered<World_call_outLogic>()) {
|
||
World_call_outLogic logic = Get.find<World_call_outLogic>();
|
||
logic.isNewMsg = true;
|
||
logic.update();
|
||
}
|
||
} else if (int.parse(event) == 10) {
|
||
//有新访客
|
||
MsgLogic logic = Get.find<MsgLogic>();
|
||
logic.getMode();
|
||
} else if (int.parse(event) == 11) {
|
||
var info = jsonDecode(jsonData['content']);
|
||
EventBusManager.fire(ShowGiftNotifi(info));
|
||
|
||
}
|
||
} else if (jsonData.containsKey('type')) {
|
||
int type = jsonData['type'];
|
||
if (type == 1) {
|
||
String giftUrl = jsonData['gifUrl'];
|
||
if (giftUrl.isNotEmpty) {
|
||
Get.bottomSheet(SvgDialog(url: giftUrl),
|
||
isScrollControlled: true,
|
||
enableDrag: false,
|
||
isDismissible: false);
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}));
|
||
|
||
_messageService.addSimpleMsgListener(listener: msgListener);
|
||
}
|
||
|
||
@override
|
||
void didChangeAppLifecycleState(AppLifecycleState state) {
|
||
switch (state) {
|
||
case AppLifecycleState.inactive:
|
||
break;
|
||
|
||
case AppLifecycleState.resumed: //从后台切换前台,界面可见
|
||
break;
|
||
|
||
case AppLifecycleState.paused: // 界面不可见,后台
|
||
addBadge();
|
||
break;
|
||
|
||
case AppLifecycleState.detached: // APP结束时调用
|
||
break;
|
||
}
|
||
}
|
||
|
||
showSendedCallOutDialog(String desc) {
|
||
|
||
Get.bottomSheet(
|
||
LikePeoleSendedCallOutDialog(
|
||
desc
|
||
),
|
||
isScrollControlled: true,
|
||
enableDrag: false,
|
||
);
|
||
}
|
||
|
||
Future<void> checkVersion() async {
|
||
var data = await DioManager.instance.get(url: Api.APP_VERSION);
|
||
var bean = BaseResponse<UpdateInfo>.fromJson(
|
||
data, (data) => UpdateInfo.fromJson(data));
|
||
if (bean.isSuccess() && bean.data.update == 1) {
|
||
showReportDialog(Get.context!, bean.data.constraint == 0, bean.data);
|
||
}
|
||
}
|
||
|
||
setLike(String userId) async {
|
||
var data = await DioManager.instance
|
||
.post(url: "${Api.setLike + userId}/follow", params: {'status': "1"});
|
||
var bean = BaseResponse<dynamic>.fromJson(
|
||
data,
|
||
(jsonData) => jsonData,
|
||
);
|
||
if (bean.isSuccess()) {
|
||
update();
|
||
}
|
||
showOKToast(bean.msg);
|
||
}
|
||
|
||
getUnreadSize() async {
|
||
if (coreInstance.isLoginSuccess) {
|
||
try {
|
||
V2TimValueCallback<int> getTotalUnreadMessageCountRes =
|
||
await TencentImSDKPlugin.v2TIMManager
|
||
.getConversationManager()
|
||
.getTotalUnreadMessageCount();
|
||
print("未读数量desc=" +
|
||
getTotalUnreadMessageCountRes.desc.toString() +
|
||
"code=" +
|
||
getTotalUnreadMessageCountRes.code.toString() +
|
||
"data=" +
|
||
getTotalUnreadMessageCountRes.data.toString());
|
||
|
||
if (getTotalUnreadMessageCountRes.code == 0) {
|
||
if (getTotalUnreadMessageCountRes.data.toString() !=
|
||
unreadSIze.value) {
|
||
unreadSIze.value = getTotalUnreadMessageCountRes.data.toString();
|
||
}
|
||
}
|
||
} catch (e) {}
|
||
}
|
||
}
|
||
|
||
@override
|
||
Future<V2TimCallback> setOfflinePushStatus(
|
||
{required AppStatus status, int? totalCount}) {
|
||
if (status == AppStatus.foreground) {
|
||
// 当应用status为前台时,上报doForeground()
|
||
return TencentImSDKPlugin.v2TIMManager
|
||
.getOfflinePushManager()
|
||
.doForeground();
|
||
} else {
|
||
// 当应用status为后台时,上报doBackground(),并带上未读数
|
||
return TencentImSDKPlugin.v2TIMManager
|
||
.getOfflinePushManager()
|
||
.doBackground(unreadCount: totalCount ?? 0);
|
||
}
|
||
}
|
||
|
||
@override
|
||
void onReady() async {
|
||
// TODO: implement onReady
|
||
super.onReady();
|
||
|
||
FlutterNativeSplash.remove();
|
||
final logic = Get.find<CircleLogic>();
|
||
logic.loadCirclePeopleData();
|
||
|
||
}
|
||
|
||
addBadge() {
|
||
if (Platform.isAndroid) {
|
||
setOfflinePushStatus(status: AppStatus.foreground);
|
||
}
|
||
}
|
||
|
||
void showReportDialog(
|
||
BuildContext context, bool isDismiss, UpdateInfo updateInfo) {
|
||
showDialog(
|
||
context: context,
|
||
barrierDismissible: false,
|
||
builder: (BuildContext context) {
|
||
return UpdateDialog(
|
||
isDismiss: isDismiss,
|
||
updateInfo: updateInfo,
|
||
);
|
||
});
|
||
}
|
||
|
||
void getIMData() async {
|
||
// _coreInstance.;
|
||
if (!coreInstance.isLoginSuccess) {
|
||
var data = await DioManager.instance.get(url: Api.getIMInfo);
|
||
if (data['code'] == 200) {
|
||
loginIM(
|
||
data['data']['account_id'].toString(), data['data']['user_sig']);
|
||
accountId = data['data']['account_id'].toString();
|
||
} else {
|
||
if (Get.currentRoute != Routes.Login) {
|
||
getIMData();
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
String getInfo() {
|
||
return getBaseImage('image');
|
||
}
|
||
|
||
updateIndex(int index) {
|
||
tabController.animateTo(index,
|
||
duration: Duration(milliseconds: 0), curve: Curves.ease);
|
||
currentIndex = index;
|
||
getUnreadSize();
|
||
if (index == 0) {
|
||
final logic = Get.put(CircleLogic());
|
||
logic.loadCirclePeopleData();
|
||
} else if (index == 4) {
|
||
final logic = Get.put(MinefragmentLogic());
|
||
logic.getMode();
|
||
} else if (index == 3) {
|
||
final logic = Get.find<MsgLogic>();
|
||
EventBusManager.fire(MsgRecommendCardRefresh());
|
||
logic.loadChatData();
|
||
logic.getVisitorMsgData();
|
||
}
|
||
update();
|
||
}
|
||
|
||
void setFirstData() async {
|
||
SharedPreferences sharedPreferences = await SharedPreferences.getInstance();
|
||
sharedPreferences.setString(SharedPreferencesHelper.firstLogin, '1');
|
||
}
|
||
|
||
loadMyCircleData() async {
|
||
var data = await DioManager.instance.get(url: Api.getMyCircleAll);
|
||
|
||
// var bean = BaseResponse<InterestsBean>.fromJson(
|
||
// data, (data) => InterestsBean.fromJson(data));
|
||
|
||
if (data['code'] == 200) {
|
||
List circleList = data['data'];
|
||
if (circleList.isNotEmpty) {
|
||
List<Circle> infoList = [];
|
||
circleList.forEach((element) {
|
||
var info = Circle.fromJson(element);
|
||
infoList.add(info);
|
||
});
|
||
mycircle.lists = infoList;
|
||
if (currentIndex >= mycircle.lists.length - 1) {
|
||
currentIndex = mycircle.lists.length - 1;
|
||
}
|
||
|
||
loadCalloutsNew();
|
||
}
|
||
}
|
||
}
|
||
|
||
loadCalloutsNew() async {
|
||
List<String> idList = [];
|
||
mycircle.lists.forEach((element) {
|
||
idList.add(element.id.toString());
|
||
});
|
||
var data = await DioManager.instance
|
||
.post(url: Api.calloutsNew, params: {"interestIds": idList});
|
||
|
||
if (data['code'] == 200) {
|
||
List infoList = data['data'];
|
||
bool isHasNew = false;
|
||
if (infoList.isNotEmpty) {
|
||
infoList.forEach((element) {
|
||
if (element['hasNew']) {
|
||
isHasNew = true;
|
||
}
|
||
});
|
||
}
|
||
showcirlceUnred.value = isHasNew;
|
||
}
|
||
}
|
||
|
||
/// 设置网络切换监听
|
||
connectListener() {
|
||
connectivitySub = Connectivity()
|
||
.onConnectivityChanged
|
||
.listen((ConnectivityResult result) {
|
||
if (result == ConnectivityResult.none) {
|
||
if (currentIndex == 0) {
|
||
var logic = Get.put(CircleLogic());
|
||
logic.loadData();
|
||
} else if (currentIndex == 1) {
|
||
var logic = Get.put(LikeLogic());
|
||
logic.loadData();
|
||
} else if (currentIndex == 3) {
|
||
final logic = Get.find<MsgLogic>();
|
||
logic.loadData();
|
||
} else if (currentIndex == 4) {
|
||
var logic = Get.put(MinefragmentLogic());
|
||
logic.loadData();
|
||
}
|
||
}
|
||
});
|
||
}
|
||
|
||
void loadEnvTypeData() async {
|
||
var data = await DioManager.instance.get(url: Api.getAppEnv);
|
||
if (data['code'] == 200) {
|
||
isProd = data['data'] == 0;
|
||
update();
|
||
}
|
||
}
|
||
|
||
loadMyInfoData() async {
|
||
var data = await DioManager.instance.get(url: Api.getUserMine);
|
||
var bean = BaseResponse<MineResponseBean>.fromJson(
|
||
data, (data) => MineResponseBean.fromJson(data));
|
||
if (bean.isSuccess()) {
|
||
model = bean.data.user;
|
||
var result = await DioManager.instance.get(url: Api.inviteMainPage);
|
||
if (result['code'] == 200) {
|
||
Autogenerated info = Autogenerated.fromJson(result);
|
||
inviteCode = info.data!.inviteCode!;
|
||
link = info.data!.link!;
|
||
}
|
||
if (isFirst) {
|
||
isFirst = false;
|
||
bool isGreaterThan30Days = isDifferenceGreaterThan30Days(
|
||
(model!.createTime?.isNotEmpty ?? false)
|
||
? model!.createTime!
|
||
: DateTime.now().toString());
|
||
|
||
if (isGreaterThan30Days) {
|
||
currentIndex = 0;
|
||
} else {
|
||
currentIndex = 1;
|
||
}
|
||
updateIndex(currentIndex);
|
||
update();
|
||
}
|
||
}
|
||
}
|
||
|
||
bool isDifferenceGreaterThan30Days(String dateString) {
|
||
// 获取当前时间
|
||
DateTime now = DateTime.now();
|
||
|
||
// 使用日期格式化对象来解析给定的时间字符串
|
||
DateFormat format = DateFormat("yyyy-MM-dd");
|
||
DateTime date = format.parse(dateString);
|
||
|
||
// 计算日期差异
|
||
Duration difference = now.difference(date);
|
||
|
||
// 检查差异是否超过30天
|
||
if (difference.inDays.abs() > 30) {
|
||
return true;
|
||
} else {
|
||
return false;
|
||
}
|
||
}
|
||
|
||
void awaitShowTodayDialog() {
|
||
Future.delayed(Duration(seconds: 60), () async {
|
||
SharedPreferencesHelper sp = await SharedPreferencesHelper.getInstance();
|
||
if (await sp.isFirstAutoMatch()) {
|
||
var data = await DioManager.instance.get(
|
||
url: Api.dayOnline,
|
||
);
|
||
|
||
if (data['code'] == 200) {
|
||
SharedPreferencesHelper sp =
|
||
await SharedPreferencesHelper.getInstance();
|
||
List dataList = data['data'];
|
||
if (dataList.isNotEmpty) {
|
||
var recommendCircleFriendData = data['data'][0];
|
||
Get.bottomSheet(
|
||
TodayPeopleDialog(
|
||
isShow: false,
|
||
url: Api.dayOnline,
|
||
recommendCircleFriendData: recommendCircleFriendData),
|
||
isScrollControlled: true,
|
||
enableDrag: false,
|
||
);
|
||
}
|
||
}
|
||
}
|
||
});
|
||
}
|
||
|
||
checkInfo() async {
|
||
SharedPreferencesHelper sp = await SharedPreferencesHelper.getInstance();
|
||
String phone = sp.getString(SharedPreferencesHelper.LOGINPHONE);
|
||
if (phone != '18800000100') {
|
||
inventStr = '邀请赚钱';
|
||
inventTipStr = '免费领取会员';
|
||
}
|
||
}
|
||
}
|
||
|
||
class conTop {
|
||
bool isTop;
|
||
String conId;
|
||
conTop(this.isTop, this.conId);
|
||
}
|
||
|
||
class MsgRecommendCardRefresh {}
|
||
|
||
class ShowGiftNotifi {
|
||
Map info;
|
||
ShowGiftNotifi(this.info);
|
||
}
|
||
|
||
class ShowpeopleCallOutNotifi {
|
||
String content;
|
||
ShowpeopleCallOutNotifi(this.content);
|
||
} |