237 lines
6.6 KiB
Dart
237 lines
6.6 KiB
Dart
import 'dart:async';
|
|
import 'dart:convert';
|
|
|
|
import 'package:flutter/cupertino.dart';
|
|
import 'package:yumi/app/constants/sc_global_config.dart';
|
|
import 'package:yumi/shared/tools/sc_message_utils.dart';
|
|
import 'package:yumi/shared/tools/sc_room_utils.dart';
|
|
import 'package:yumi/shared/data_sources/sources/local/data_persistence.dart';
|
|
import 'package:yumi/shared/data_sources/sources/local/durable_auth_storage.dart';
|
|
import 'package:provider/provider.dart';
|
|
import 'package:yumi/app/routes/sc_fluro_navigator.dart';
|
|
import 'package:yumi/shared/business_logic/models/res/login_res.dart';
|
|
import 'package:yumi/services/audio/rtc_manager.dart';
|
|
import 'package:yumi/services/audio/rtm_manager.dart';
|
|
import 'package:yumi/shared/data_sources/sources/local/floating_screen_manager.dart';
|
|
|
|
import '../../../tools/sc_heartbeat_utils.dart';
|
|
import '../../models/enum/sc_props_type.dart';
|
|
|
|
typedef UserManager = AccountStorage;
|
|
|
|
class AccountStorage {
|
|
static AccountStorage? _instance;
|
|
|
|
AccountStorage._internal();
|
|
|
|
factory AccountStorage() {
|
|
return _instance ??= AccountStorage._internal();
|
|
}
|
|
|
|
String token = "";
|
|
SocialChatLoginRes? _currentUser;
|
|
|
|
///获取当前用户
|
|
SocialChatLoginRes? getCurrentUser() {
|
|
if (_currentUser != null) {
|
|
return _currentUser;
|
|
}
|
|
var userJson = DataPersistence.getCurrentUser();
|
|
if (userJson.isNotEmpty) {
|
|
_currentUser = _decodeCurrentUser(userJson);
|
|
if (_currentUser == null) {
|
|
unawaited(_clearPersistedSession(clearDurable: false));
|
|
}
|
|
}
|
|
return _currentUser;
|
|
}
|
|
|
|
///同步优化数据
|
|
void setCurrentUser(SocialChatLoginRes user) {
|
|
_currentUser = user;
|
|
token = _currentUser!.token ?? "";
|
|
String userJson = jsonEncode(_currentUser?.toJson());
|
|
if (userJson.isNotEmpty) {
|
|
unawaited(_persistSession(token: token, currentUserJson: userJson));
|
|
}
|
|
}
|
|
|
|
///获取自己佩戴的头饰信息
|
|
PropsResources? getHeaddress() {
|
|
PropsResources? pr;
|
|
_currentUser?.userProfile?.useProps?.forEach((value) {
|
|
if (socialChatIsAvatarFrameResource(value.propsResources)) {
|
|
///判断有没有过期
|
|
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 == SCPropsType.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 == SCPropsType.RIDE.name) {
|
|
///判断有没有过期
|
|
if (int.parse(value.expireTime ?? "0") >
|
|
DateTime.now().millisecondsSinceEpoch) {
|
|
pr = value.propsResources;
|
|
}
|
|
}
|
|
});
|
|
return pr;
|
|
}
|
|
|
|
///获取自己资料卡信息
|
|
PropsResources? getDataCard() {
|
|
PropsResources? pr;
|
|
_currentUser?.userProfile?.useProps?.forEach((value) {
|
|
if (value.propsResources?.type == SCPropsType.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;
|
|
}
|
|
|
|
Future<void> restoreDurableSessionIfNeeded() async {
|
|
final localToken = DataPersistence.getToken().trim();
|
|
final localUserJson = DataPersistence.getCurrentUser().trim();
|
|
final localUser = _decodeCurrentUser(localUserJson);
|
|
if (localToken.isNotEmpty && localUser != null) {
|
|
token = localToken;
|
|
_currentUser = localUser;
|
|
return;
|
|
}
|
|
|
|
final session = await DurableAuthStorage.readSession();
|
|
if (session == null) {
|
|
if (localToken.isNotEmpty || localUserJson.isNotEmpty) {
|
|
unawaited(_clearPersistedSession(clearDurable: false));
|
|
}
|
|
return;
|
|
}
|
|
|
|
token = session.token;
|
|
_currentUser = _decodeCurrentUser(session.currentUserJson);
|
|
if (_currentUser == null) {
|
|
token = "";
|
|
await _clearPersistedSession();
|
|
return;
|
|
}
|
|
await _persistSession(
|
|
token: session.token,
|
|
currentUserJson: session.currentUserJson,
|
|
);
|
|
}
|
|
|
|
void setToken(String tk) {
|
|
token = tk;
|
|
unawaited(_persistToken(token));
|
|
}
|
|
|
|
void _cleanUser() {
|
|
token = "";
|
|
_currentUser = null;
|
|
unawaited(_clearPersistedSession());
|
|
unawaited(DataPersistence.clearPendingChannelAuth());
|
|
}
|
|
|
|
///退出登录
|
|
void logout(BuildContext context) {
|
|
_cleanUser();
|
|
SCHeartbeatUtils.cancelTimer();
|
|
try {
|
|
Provider.of<RtcProvider>(
|
|
context,
|
|
listen: false,
|
|
).exitCurrentVoiceRoomSession(true);
|
|
} catch (_) {}
|
|
try {
|
|
Provider.of<RtmProvider>(context, listen: false).logout();
|
|
} catch (_) {}
|
|
SCNavigatorUtils.pushLoginIfNeeded(context, clearStack: true);
|
|
try {
|
|
SCRoomUtils.closeAllDialogs();
|
|
} catch (_) {}
|
|
SCMessageUtils.redPacketFutureCache.clear();
|
|
SCGlobalConfig.resetVisualEffectSwitchesToRecommendedDefaults();
|
|
try {
|
|
OverlayManager().dispose();
|
|
} catch (_) {}
|
|
}
|
|
|
|
SocialChatLoginRes? _decodeCurrentUser(String userJson) {
|
|
if (userJson.trim().isEmpty) {
|
|
return null;
|
|
}
|
|
try {
|
|
final decoded = jsonDecode(userJson);
|
|
if (decoded is Map) {
|
|
return SocialChatLoginRes.fromJson(Map<String, dynamic>.from(decoded));
|
|
}
|
|
} catch (_) {}
|
|
return null;
|
|
}
|
|
|
|
Future<void> _persistToken(String value) async {
|
|
try {
|
|
await DataPersistence.setToken(value);
|
|
} catch (_) {}
|
|
}
|
|
|
|
Future<void> _persistSession({
|
|
required String token,
|
|
required String currentUserJson,
|
|
}) async {
|
|
try {
|
|
await DataPersistence.setToken(token);
|
|
await DataPersistence.setCurrentUser(currentUserJson);
|
|
} catch (_) {}
|
|
await DurableAuthStorage.saveSession(
|
|
token: token,
|
|
currentUserJson: currentUserJson,
|
|
);
|
|
}
|
|
|
|
Future<void> _clearPersistedSession({bool clearDurable = true}) async {
|
|
try {
|
|
await DataPersistence.setToken("");
|
|
await DataPersistence.setCurrentUser("");
|
|
} catch (_) {}
|
|
if (clearDurable) {
|
|
await DurableAuthStorage.clearSession();
|
|
}
|
|
}
|
|
}
|