47 lines
1.1 KiB
Dart
47 lines
1.1 KiB
Dart
import 'package:get/get.dart';
|
||
import 'package:get_storage/get_storage.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;
|
||
|
||
/// 登录的token,供请求时调用,载入内存,是为了每次使用的时候,不需要从磁盘获取
|
||
late String userToken = '';
|
||
|
||
/// 获取存储的token,这个可以做到持久化存储
|
||
String get _userToken => _box.read('userToken') ?? '';
|
||
|
||
@override
|
||
void onInit() {
|
||
super.onInit();
|
||
|
||
if (_userToken.isNotEmpty) {
|
||
isLogin.value = true;
|
||
userToken = _userToken;
|
||
}
|
||
|
||
// ever(_isLogin, (_) {
|
||
// if (_ == true) {
|
||
// Get.offAllNamed(AppRoutes.app);
|
||
// } else {
|
||
// Get.offAllNamed(AuthRoutes.index);
|
||
// }
|
||
// });
|
||
}
|
||
|
||
Future<bool> login(String address) async {
|
||
_box.write('userToken', address);
|
||
userToken = address;
|
||
isLogin.value = true;
|
||
|
||
return true;
|
||
}
|
||
}
|