191 lines
5.5 KiB
Dart
191 lines
5.5 KiB
Dart
import 'dart:convert';
|
|
|
|
import 'package:flutter/cupertino.dart';
|
|
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
|
|
import 'package:aslan/chatvibe_core/constants/at_global_config.dart';
|
|
import 'package:aslan/chatvibe_core/utilities/at_message_utils.dart';
|
|
import 'package:aslan/chatvibe_core/utilities/at_room_utils.dart';
|
|
import 'package:aslan/chatvibe_data/sources/local/data_persistence.dart';
|
|
import 'package:aslan/main.dart';
|
|
import 'package:provider/provider.dart';
|
|
|
|
import 'package:aslan/chatvibe_core/routes/at_fluro_navigator.dart';
|
|
import 'package:aslan/chatvibe_core/utilities/at_heartbeat_utils.dart';
|
|
import 'package:aslan/chatvibe_domain/models/res/login_res.dart';
|
|
import 'package:aslan/chatvibe_features/auth/login_route.dart';
|
|
import 'package:aslan/chatvibe_managers/rtc_manager.dart';
|
|
import 'package:aslan/chatvibe_managers/rtm_manager.dart';
|
|
import 'package:aslan/chatvibe_data/sources/local/floating_screen_manager.dart';
|
|
|
|
import '../../models/enum/at_props_type.dart';
|
|
|
|
typedef UserManager = AccountStorage;
|
|
|
|
class AccountStorage {
|
|
static AccountStorage? _instance;
|
|
|
|
AccountStorage._internal();
|
|
|
|
factory AccountStorage() {
|
|
return _instance ??= AccountStorage._internal();
|
|
}
|
|
|
|
String token = "";
|
|
ChatVibeLoginRes? _currentUser;
|
|
|
|
///获取当前用户
|
|
ChatVibeLoginRes? getCurrentUser() {
|
|
if (_currentUser != null) {
|
|
return _currentUser;
|
|
}
|
|
var userJson = DataPersistence.getCurrentUser();
|
|
if (userJson.isNotEmpty) {
|
|
_currentUser = ChatVibeLoginRes.fromJson(jsonDecode(userJson));
|
|
}
|
|
return _currentUser;
|
|
}
|
|
|
|
///同步优化数据
|
|
void setCurrentUser(ChatVibeLoginRes user) {
|
|
_currentUser = user;
|
|
setToken(_currentUser!.token ?? "");
|
|
String userJson = jsonEncode(_currentUser?.toJson());
|
|
if (userJson.isNotEmpty) {
|
|
DataPersistence.setCurrentUser(userJson);
|
|
}
|
|
}
|
|
|
|
///获取自己佩戴的头饰信息
|
|
PropsResources? getHeaddress() {
|
|
PropsResources? pr;
|
|
_currentUser?.userProfile?.useProps?.forEach((value) {
|
|
if (value.propsResources?.type == ATPropsType.AVATAR_FRAME.name) {
|
|
///判断有没有过期
|
|
if (int.parse(value.expireTime ?? "0") >
|
|
DateTime.now().millisecondsSinceEpoch) {
|
|
pr = value.propsResources;
|
|
}
|
|
}
|
|
});
|
|
return pr;
|
|
}
|
|
|
|
///获取自己佩戴的气泡框
|
|
PropsResources? getChatbox() {
|
|
PropsResources? pr;
|
|
_currentUser?.userProfile?.useProps?.forEach((value) {
|
|
if (value.propsResources?.type == ATPropsType.CHAT_BUBBLE.name) {
|
|
///判断有没有过期
|
|
if (int.parse(value.expireTime ?? "0") >
|
|
DateTime.now().millisecondsSinceEpoch) {
|
|
pr = value.propsResources;
|
|
}
|
|
}
|
|
});
|
|
return pr;
|
|
}
|
|
|
|
///获取自己佩戴的坐骑信息
|
|
PropsResources? getMountains() {
|
|
PropsResources? pr;
|
|
_currentUser?.userProfile?.useProps?.forEach((value) {
|
|
if (value.propsResources?.type == ATPropsType.RIDE.name) {
|
|
///判断有没有过期
|
|
if (int.parse(value.expireTime ?? "0") >
|
|
DateTime.now().millisecondsSinceEpoch) {
|
|
pr = value.propsResources;
|
|
}
|
|
}
|
|
});
|
|
return pr;
|
|
}
|
|
|
|
PropsResources? cpr;
|
|
|
|
///获取自己VIP信息
|
|
PropsResources? getVIP() {
|
|
PropsResources? pr;
|
|
_currentUser?.userProfile?.useProps?.forEach((value) {
|
|
if (value.propsResources?.type == ATPropsType.NOBLE_VIP.name) {
|
|
///判断有没有过期
|
|
if (int.parse(value.expireTime ?? "0") >
|
|
DateTime.now().millisecondsSinceEpoch) {
|
|
pr = value.propsResources;
|
|
}
|
|
}
|
|
});
|
|
//vip变化了
|
|
if (cpr?.name != pr?.name) {
|
|
Provider.of<RtcProvider>(
|
|
navigatorKey.currentState!.context,
|
|
listen: false,
|
|
).needUpDataUserInfo =
|
|
true;
|
|
}
|
|
cpr = pr;
|
|
return pr;
|
|
}
|
|
|
|
///获取VIp过期时间
|
|
String getVIPExpireTime() {
|
|
String expireTime = "0";
|
|
_currentUser?.userProfile?.useProps?.forEach((value) {
|
|
if (value.propsResources?.type == ATPropsType.NOBLE_VIP.name) {
|
|
expireTime = value.expireTime ?? "0";
|
|
}
|
|
});
|
|
return expireTime;
|
|
}
|
|
|
|
///获取自己资料卡信息
|
|
PropsResources? getDataCard() {
|
|
PropsResources? pr;
|
|
_currentUser?.userProfile?.useProps?.forEach((value) {
|
|
if (value.propsResources?.type == ATPropsType.DATA_CARD.name) {
|
|
///判断有没有过期
|
|
if (int.parse(value.expireTime ?? "0") >
|
|
DateTime.now().millisecondsSinceEpoch) {
|
|
pr = value.propsResources;
|
|
}
|
|
}
|
|
});
|
|
return pr;
|
|
}
|
|
|
|
String getToken() {
|
|
if (token.isNotEmpty) {
|
|
return token;
|
|
}
|
|
token = DataPersistence.getToken();
|
|
return token;
|
|
}
|
|
|
|
void setToken(String tk) {
|
|
token = tk;
|
|
DataPersistence.setToken(token);
|
|
}
|
|
|
|
void _cleanUser() {
|
|
token = "";
|
|
_currentUser = null;
|
|
DataPersistence.setToken("");
|
|
DataPersistence.setCurrentUser("");
|
|
}
|
|
|
|
///退出登录
|
|
void logout(BuildContext context) {
|
|
_cleanUser();
|
|
ATHeartbeatUtils.cancelTimer();
|
|
Provider.of<RtcProvider>(context, listen: false).extRoom(true);
|
|
Provider.of<RtmProvider>(context, listen: false).logout();
|
|
ATNavigatorUtils.push(context, LoginRouter.login, clearStack: true);
|
|
ATRoomUtils.closeAllDialogs();
|
|
ATMessageUtils.redPacketFutureCache.clear();
|
|
ATGlobalConfig.isEntryVehicleAnimation = true;
|
|
ATGlobalConfig.isGiftSpecialEffects = true;
|
|
ATGlobalConfig.isFloatingAnimationInGlobal = true;
|
|
ATGlobalConfig.isLuckGiftSpecialEffects = true;
|
|
OverlayManager().dispose();
|
|
}
|
|
}
|