|
@@ -1,6 +1,11 @@
|
|
|
import 'dart:async';
|
|
import 'dart:async';
|
|
|
|
|
|
|
|
|
|
+import 'package:electronic_assistant/base/app_base_request.dart';
|
|
|
import 'package:electronic_assistant/data/api/atmob_api.dart';
|
|
import 'package:electronic_assistant/data/api/atmob_api.dart';
|
|
|
|
|
+import 'package:electronic_assistant/data/bean/member_info.dart';
|
|
|
|
|
+import 'package:electronic_assistant/data/repositories/task_repository.dart';
|
|
|
|
|
+import 'package:electronic_assistant/utils/async_util.dart';
|
|
|
|
|
+import 'package:electronic_assistant/utils/cancel_future.dart';
|
|
|
import 'package:electronic_assistant/utils/event_bus.dart';
|
|
import 'package:electronic_assistant/utils/event_bus.dart';
|
|
|
import 'package:electronic_assistant/utils/mmkv_util.dart';
|
|
import 'package:electronic_assistant/utils/mmkv_util.dart';
|
|
|
import 'package:flutter/cupertino.dart';
|
|
import 'package:flutter/cupertino.dart';
|
|
@@ -11,6 +16,8 @@ import '../../utils/http_handler.dart';
|
|
|
import '../api/request/login_request.dart';
|
|
import '../api/request/login_request.dart';
|
|
|
import '../api/request/verification_code_request.dart';
|
|
import '../api/request/verification_code_request.dart';
|
|
|
import '../api/response/login_response.dart';
|
|
import '../api/response/login_response.dart';
|
|
|
|
|
+import '../api/response/user_info_response.dart';
|
|
|
|
|
+import '../consts/error_code.dart';
|
|
|
|
|
|
|
|
const String EventUserLogin = 'EventUserLogin';
|
|
const String EventUserLogin = 'EventUserLogin';
|
|
|
const String EventUserLogout = 'EventUserLogout';
|
|
const String EventUserLogout = 'EventUserLogout';
|
|
@@ -22,6 +29,9 @@ class AccountRepository {
|
|
|
String? _token;
|
|
String? _token;
|
|
|
String? _phone;
|
|
String? _phone;
|
|
|
final isLogin = false.obs;
|
|
final isLogin = false.obs;
|
|
|
|
|
+ Rxn<MemberInfo> memberInfo = Rxn();
|
|
|
|
|
+
|
|
|
|
|
+ CancelableFuture? _getUserInfoFuture;
|
|
|
|
|
|
|
|
AccountRepository._() {
|
|
AccountRepository._() {
|
|
|
debugPrint('AccountRepository init');
|
|
debugPrint('AccountRepository init');
|
|
@@ -29,6 +39,10 @@ class AccountRepository {
|
|
|
_phone = KVUtil.getString(ACCOUNT_PHONE, null);
|
|
_phone = KVUtil.getString(ACCOUNT_PHONE, null);
|
|
|
if (_token != null && _token!.isNotEmpty) {
|
|
if (_token != null && _token!.isNotEmpty) {
|
|
|
isLogin.value = true;
|
|
isLogin.value = true;
|
|
|
|
|
+ Future.delayed(Duration.zero, () {
|
|
|
|
|
+ refreshUserInfo();
|
|
|
|
|
+ taskRepository.startUnfinishedTask();
|
|
|
|
|
+ });
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -42,23 +56,48 @@ class AccountRepository {
|
|
|
.then(HttpHandler.handle(true));
|
|
.then(HttpHandler.handle(true));
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ Future<void>? refreshUserInfo() {
|
|
|
|
|
+ if (_getUserInfoFuture != null) {
|
|
|
|
|
+ _getUserInfoFuture?.cancel();
|
|
|
|
|
+ }
|
|
|
|
|
+ _getUserInfoFuture = AsyncUtil.retryWithExponentialBackoff(
|
|
|
|
|
+ () => _getUserInfo(), 10, (error) {
|
|
|
|
|
+ if (error is ServerErrorException) {
|
|
|
|
|
+ return error.code != ErrorCode.ERROR_CODE_NO_LOGIN;
|
|
|
|
|
+ }
|
|
|
|
|
+ return true;
|
|
|
|
|
+ });
|
|
|
|
|
+ return _getUserInfoFuture;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ Future<UserInfoResponse> _getUserInfo() {
|
|
|
|
|
+ return atmobApi
|
|
|
|
|
+ .userInfo(AppBaseRequest())
|
|
|
|
|
+ .then(HttpHandler.handle(false))
|
|
|
|
|
+ .then((response) {
|
|
|
|
|
+ memberInfo.value = response.memberInfo;
|
|
|
|
|
+ return response;
|
|
|
|
|
+ });
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
Future<LoginResponse> login(String phone, String code) {
|
|
Future<LoginResponse> login(String phone, String code) {
|
|
|
return atmobApi
|
|
return atmobApi
|
|
|
.login(LoginRequest(phone, code))
|
|
.login(LoginRequest(phone, code))
|
|
|
.then(HttpHandler.handle(false))
|
|
.then(HttpHandler.handle(false))
|
|
|
.then((response) {
|
|
.then((response) {
|
|
|
onLoginSuccess(phone, response.authToken);
|
|
onLoginSuccess(phone, response.authToken);
|
|
|
- refreshUserInfo();
|
|
|
|
|
return response;
|
|
return response;
|
|
|
});
|
|
});
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
void onLoginSuccess(String phone, String? token) {
|
|
void onLoginSuccess(String phone, String? token) {
|
|
|
- this._token = token;
|
|
|
|
|
- this._phone = phone;
|
|
|
|
|
|
|
+ _token = token;
|
|
|
|
|
+ _phone = phone;
|
|
|
KVUtil.putString(ACCOUNT_TOKEN, token);
|
|
KVUtil.putString(ACCOUNT_TOKEN, token);
|
|
|
KVUtil.putString(ACCOUNT_PHONE, phone);
|
|
KVUtil.putString(ACCOUNT_PHONE, phone);
|
|
|
isLogin.value = true;
|
|
isLogin.value = true;
|
|
|
|
|
+ refreshUserInfo();
|
|
|
|
|
+ taskRepository.startUnfinishedTask();
|
|
|
|
|
|
|
|
eventBus.emit(EventUserLogin);
|
|
eventBus.emit(EventUserLogin);
|
|
|
}
|
|
}
|
|
@@ -69,6 +108,8 @@ class AccountRepository {
|
|
|
KVUtil.putString(ACCOUNT_TOKEN, null);
|
|
KVUtil.putString(ACCOUNT_TOKEN, null);
|
|
|
KVUtil.putString(ACCOUNT_PHONE, null);
|
|
KVUtil.putString(ACCOUNT_PHONE, null);
|
|
|
isLogin.value = false;
|
|
isLogin.value = false;
|
|
|
|
|
+ taskRepository.stopTask();
|
|
|
|
|
+ memberInfo.value = null;
|
|
|
|
|
|
|
|
eventBus.emit(EventUserLogout);
|
|
eventBus.emit(EventUserLogout);
|
|
|
}
|
|
}
|
|
@@ -81,8 +122,6 @@ class AccountRepository {
|
|
|
isLogin.value = false;
|
|
isLogin.value = false;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- void refreshUserInfo() {}
|
|
|
|
|
-
|
|
|
|
|
getUserSubName(String? phone) {
|
|
getUserSubName(String? phone) {
|
|
|
String name = StringName.account.tr;
|
|
String name = StringName.account.tr;
|
|
|
if (phone == null) {
|
|
if (phone == null) {
|