app-starlock/star_lock/lib/tools/store_service.dart
2023-07-29 18:33:48 +08:00

60 lines
2.1 KiB
Dart

import 'package:get/get.dart';
import 'package:get_storage/get_storage.dart';
import '../login/login/entity/LoginEntity.dart';
final box = GetStorage();
class StoreService<T> extends GetxService {
static StoreService get to => Get.find<StoreService>();
Future<StoreService<T>> init() async {
await GetStorage.init();
await resetUserInfo();
return this;
}
Future<void> save(String? key, dynamic value) => box.write(key!, value);
T read(String? key) => box.read(key!);
Future remove(String? key) => box.remove(key!);
bool hasData(String? key) => box.hasData(key!);
final String _loginUserInfoKey = 'LOGIN_USER_INFO';
final String _deviceUUID = 'DEVICE_ID';
final String _languageCode = 'LANGUAGE_CODE';
final String _userAccount = 'USER_ACCOUNT';
LoginEntity? _loginEntity;
// LoginEntity get loginEntity => _loginEntity!;
Future resetUserInfo() async{
if(hasData(_loginUserInfoKey)){
_loginEntity = LoginEntity.fromJson(box.read(_loginUserInfoKey));
}
}
Future removeUserInfo() => remove(_loginUserInfoKey);
Future saveLogInInfo(LoginEntity entity) async {
_loginEntity = LoginEntity.fromJson(entity.toJson());
save(_loginUserInfoKey, entity.toJson());
if(_loginEntity != null && _loginEntity!.content != null && _loginEntity!.content!.email != null && _loginEntity!.content!.email!.isNotEmpty) {
save(_userAccount, _loginEntity?.content?.email);
}
}
// String getDeviceId() => hasData(_deviceUUID!) ? read(_deviceUUID!): "";
Future saveDeviceId(String uuid) => save(_deviceUUID, uuid);
// String getLanguageCode() => hasData(_languageCode) ? read(_languageCode): "";
Future saveLanguageCode(String code) => save(_languageCode, code);
bool get hadToken => _loginEntity !=null && _loginEntity!.content!.token!.isNotEmpty;
String? get userToken => hadToken ? _loginEntity!.content!.token : "";
// String getLastUserAccount() => hasData(_userAccount) ? read(_userAccount): "";
void removeLastUserAccount() => remove(_userAccount);
// String get localUserAccount => getLastUserAccount();
}