65 lines
1.8 KiB
Dart
65 lines
1.8 KiB
Dart
import 'package:chat/routes/auth_routes.dart';
|
||
import 'package:get/get.dart';
|
||
import 'package:get_storage/get_storage.dart';
|
||
import 'package:tencent_im_sdk_plugin/tencent_im_sdk_plugin.dart';
|
||
|
||
class AuthService extends GetxService {
|
||
static AuthService get to => Get.find<AuthService>();
|
||
|
||
final _box = GetStorage();
|
||
|
||
/// 供外部使用的,判断是否登录的状态
|
||
get isUserLogin => isLogin.value;
|
||
|
||
/// 登录状态记录,可监听的,这样ever才能监听到
|
||
final RxBool isLogin = false.obs;
|
||
|
||
/// 供请求时调用,载入内存,是为了每次使用的时候,不需要从磁盘获取
|
||
late String userId = '';
|
||
late String userSig = '';
|
||
|
||
/// 获取存储的token,这个可以做到持久化存储
|
||
String get _userSig =>
|
||
_box.read('userSig') ??
|
||
'eJwtzEELgjAYxvHvsnPIu7VNJ3ToIIKsIAp2ljbrRYylJs3ou2fq8fk98P*Qiz5Hg2tJSlgEZDNvtO7RY4UzixU7W5feoyUp5QAxVVzR5XFvj62bXAjBAGDRHpu-SSnZlseKrxW8TU1p9sV4v3YJoyFrDrV*QYu5yrQ*2cqUzyHkPpixOMbJjnx-EqUv9A__';
|
||
String get _userId => _box.read('userId') ?? '5';
|
||
|
||
@override
|
||
void onInit() {
|
||
super.onInit();
|
||
|
||
if (_userSig.isNotEmpty) {
|
||
isLogin.value = true;
|
||
userSig = _userSig;
|
||
userId = _userId;
|
||
}
|
||
|
||
// ever(_isLogin, (_) {
|
||
// if (_ == true) {
|
||
// Get.offAllNamed(AppRoutes.app);
|
||
// } else {
|
||
// Get.offAllNamed(AuthRoutes.index);
|
||
// }
|
||
// });
|
||
}
|
||
|
||
Future<bool> login(String address) async {
|
||
_box.write('userId', '5');
|
||
userId = '5';
|
||
isLogin.value = true;
|
||
|
||
return true;
|
||
}
|
||
|
||
/// 退出登录
|
||
void logout() async {
|
||
await TencentImSDKPlugin.v2TIMManager.logout();
|
||
_box.remove('userSig');
|
||
_box.remove('userId');
|
||
userSig = '';
|
||
userId = '';
|
||
isLogin.value = false;
|
||
Get.offAllNamed(AuthRoutes.index);
|
||
}
|
||
}
|