|
@@ -1,22 +1,30 @@
|
|
|
|
|
+import 'dart:async';
|
|
|
|
|
+
|
|
|
import 'package:get/get.dart';
|
|
import 'package:get/get.dart';
|
|
|
import 'package:injectable/injectable.dart';
|
|
import 'package:injectable/injectable.dart';
|
|
|
|
|
+import 'package:location/base/app_base_request.dart';
|
|
|
import 'package:location/data/api/atmob_api.dart';
|
|
import 'package:location/data/api/atmob_api.dart';
|
|
|
import 'package:location/data/api/request/login_request.dart';
|
|
import 'package:location/data/api/request/login_request.dart';
|
|
|
import 'package:location/data/api/request/send_code_request.dart';
|
|
import 'package:location/data/api/request/send_code_request.dart';
|
|
|
import 'package:location/data/bean/member_status_info.dart';
|
|
import 'package:location/data/bean/member_status_info.dart';
|
|
|
|
|
+import 'package:location/data/bean/user_info.dart';
|
|
|
import 'package:location/data/consts/error_code.dart';
|
|
import 'package:location/data/consts/error_code.dart';
|
|
|
|
|
+import 'package:location/data/repositories/friends_repository.dart';
|
|
|
|
|
+import 'package:location/di/get_it.dart';
|
|
|
|
|
+import 'package:location/resource/string.gen.dart';
|
|
|
|
|
+import 'package:location/utils/async_util.dart';
|
|
|
import 'package:location/utils/atmob_log.dart';
|
|
import 'package:location/utils/atmob_log.dart';
|
|
|
import 'package:location/utils/http_handler.dart';
|
|
import 'package:location/utils/http_handler.dart';
|
|
|
import 'package:location/utils/mmkv_util.dart';
|
|
import 'package:location/utils/mmkv_util.dart';
|
|
|
-
|
|
|
|
|
import '../api/response/login_response.dart';
|
|
import '../api/response/login_response.dart';
|
|
|
|
|
|
|
|
@lazySingleton
|
|
@lazySingleton
|
|
|
class AccountRepository {
|
|
class AccountRepository {
|
|
|
final AtmobApi atmobApi;
|
|
final AtmobApi atmobApi;
|
|
|
-
|
|
|
|
|
- final String keyAccountLoginPhoneNum = 'key_account_login_phone_num';
|
|
|
|
|
- final String keyAccountLoginToken = 'key_account_login_token';
|
|
|
|
|
|
|
+ final String tag = "AccountRepository";
|
|
|
|
|
+ static final String keyAccountLoginPhoneNum = 'key_account_login_phone_num';
|
|
|
|
|
+ static final String keyAccountLoginToken = 'key_account_login_token';
|
|
|
|
|
+ static final String keyAccountLoginUserId = 'key_account_login_user_id';
|
|
|
|
|
|
|
|
RxnString loginPhoneNum = RxnString();
|
|
RxnString loginPhoneNum = RxnString();
|
|
|
RxBool isLogin = RxBool(false);
|
|
RxBool isLogin = RxBool(false);
|
|
@@ -25,16 +33,29 @@ class AccountRepository {
|
|
|
int? _lastRequestCodeTime;
|
|
int? _lastRequestCodeTime;
|
|
|
int _errorCodeTimes = 0;
|
|
int _errorCodeTimes = 0;
|
|
|
|
|
|
|
|
- static String? token;
|
|
|
|
|
|
|
+ Timer? refreshMemberHandler;
|
|
|
|
|
+ CancelableFuture? memberStatusFuture;
|
|
|
|
|
+
|
|
|
|
|
+ static String? token = KVUtil.getString(keyAccountLoginToken, null);
|
|
|
|
|
+
|
|
|
|
|
+ late final FriendsRepository friendsRepository;
|
|
|
|
|
+
|
|
|
|
|
+ final Rx<UserInfo> locationUserInfo = Rx<UserInfo>(
|
|
|
|
|
+ UserInfo(id: "-1", phoneNumber: StringName.locationMine, isMine: true));
|
|
|
|
|
|
|
|
AccountRepository(this.atmobApi) {
|
|
AccountRepository(this.atmobApi) {
|
|
|
- token = KVUtil.getString(keyAccountLoginToken, null);
|
|
|
|
|
|
|
+ AtmobLog.d(tag, '$tag....init');
|
|
|
|
|
+
|
|
|
isLogin.bindStream(
|
|
isLogin.bindStream(
|
|
|
loginPhoneNum.map((value) {
|
|
loginPhoneNum.map((value) {
|
|
|
return value?.isNotEmpty == true;
|
|
return value?.isNotEmpty == true;
|
|
|
}),
|
|
}),
|
|
|
);
|
|
);
|
|
|
loginPhoneNum.value = KVUtil.getString(keyAccountLoginPhoneNum, null);
|
|
loginPhoneNum.value = KVUtil.getString(keyAccountLoginPhoneNum, null);
|
|
|
|
|
+
|
|
|
|
|
+ friendsRepository = FriendsRepository.getInstance();
|
|
|
|
|
+
|
|
|
|
|
+ refreshMemberStatus();
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
Future<void> loginSendCode(String phoneNum) {
|
|
Future<void> loginSendCode(String phoneNum) {
|
|
@@ -75,16 +96,79 @@ class AccountRepository {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
void onLoginSuccess(String phoneNum, String authToken) {
|
|
void onLoginSuccess(String phoneNum, String authToken) {
|
|
|
- AccountRepository.token = token;
|
|
|
|
|
|
|
+ AccountRepository.token = authToken;
|
|
|
loginPhoneNum.value = phoneNum;
|
|
loginPhoneNum.value = phoneNum;
|
|
|
|
|
|
|
|
KVUtil.putString(keyAccountLoginPhoneNum, phoneNum);
|
|
KVUtil.putString(keyAccountLoginPhoneNum, phoneNum);
|
|
|
KVUtil.putString(keyAccountLoginToken, authToken);
|
|
KVUtil.putString(keyAccountLoginToken, authToken);
|
|
|
|
|
|
|
|
refreshMemberStatus();
|
|
refreshMemberStatus();
|
|
|
|
|
+
|
|
|
|
|
+ friendsRepository.refreshFriends();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ void logout() {
|
|
|
|
|
+ token = null;
|
|
|
|
|
+
|
|
|
|
|
+ refreshMemberHandler?.cancel();
|
|
|
|
|
+
|
|
|
|
|
+ KVUtil.putString(keyAccountLoginPhoneNum, null);
|
|
|
|
|
+ KVUtil.putString(keyAccountLoginToken, null);
|
|
|
|
|
+ KVUtil.putString(keyAccountLoginUserId, null);
|
|
|
|
|
+ loginPhoneNum.value = null;
|
|
|
|
|
+ memberStatusInfo.value = null;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- void refreshMemberStatus() {}
|
|
|
|
|
|
|
+ void refreshMemberStatus() {
|
|
|
|
|
+ memberStatusFuture?.cancel();
|
|
|
|
|
+ memberStatusFuture = AsyncUtil.retryWithExponentialBackoff(
|
|
|
|
|
+ () => getMemberStatus(), 10, predicate: (error) {
|
|
|
|
|
+ if (error is ServerErrorException) {
|
|
|
|
|
+ return error.code != ErrorCode.noLoginError;
|
|
|
|
|
+ }
|
|
|
|
|
+ return true;
|
|
|
|
|
+ });
|
|
|
|
|
+ memberStatusFuture?.then((data) {
|
|
|
|
|
+ AtmobLog.d(tag, "getMemberStatus success: ${memberStatusInfo.value}");
|
|
|
|
|
+ }).catchError((error) {
|
|
|
|
|
+ AtmobLog.e(tag, "getMemberStatus error: $error");
|
|
|
|
|
+ });
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ Future<MemberStatusInfo?> getMemberStatus() {
|
|
|
|
|
+ return atmobApi
|
|
|
|
|
+ .getMemberStatus(AppBaseRequest())
|
|
|
|
|
+ .then(HttpHandler.handle(true))
|
|
|
|
|
+ .then((response) {
|
|
|
|
|
+ refreshMemberHandler?.cancel();
|
|
|
|
|
+ if (response != null) {
|
|
|
|
|
+ KVUtil.putString(keyAccountLoginUserId, response.userId);
|
|
|
|
|
+ if (!response.permanent && !response.expired) {
|
|
|
|
|
+ refreshMemberHandler = Timer(
|
|
|
|
|
+ Duration(
|
|
|
|
|
+ milliseconds:
|
|
|
|
|
+ response.endTimestamp - response.serverTimestamp),
|
|
|
|
|
+ () => refreshMemberStatus());
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ return response;
|
|
|
|
|
+ }).then((response) {
|
|
|
|
|
+ if (response == null) {
|
|
|
|
|
+ return null;
|
|
|
|
|
+ }
|
|
|
|
|
+ MemberStatusInfo memberStatusInfo = MemberStatusInfo(
|
|
|
|
|
+ level: response.level,
|
|
|
|
|
+ endTimestamp: response.endTimestamp,
|
|
|
|
|
+ expired: response.expired,
|
|
|
|
|
+ permanent: response.permanent);
|
|
|
|
|
+ this.memberStatusInfo.value = memberStatusInfo;
|
|
|
|
|
+ return memberStatusInfo;
|
|
|
|
|
+ });
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ static AccountRepository getInstance() {
|
|
|
|
|
+ return getIt.get<AccountRepository>();
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
class RequestCodeTooOftenException implements Exception {
|
|
class RequestCodeTooOftenException implements Exception {
|