|
|
@@ -6,6 +6,7 @@ import androidx.lifecycle.LiveData;
|
|
|
import androidx.lifecycle.MutableLiveData;
|
|
|
import androidx.lifecycle.Transformations;
|
|
|
|
|
|
+import com.atmob.auth.AtmobAuth;
|
|
|
import com.atmob.common.data.KVUtils;
|
|
|
import com.atmob.common.logging.AtmobLog;
|
|
|
import com.datarecovery.master.R;
|
|
|
@@ -16,7 +17,6 @@ import com.datarecovery.master.data.api.request.QuickLoginRequest;
|
|
|
import com.datarecovery.master.data.api.request.SendCodeRequest;
|
|
|
import com.datarecovery.master.data.api.response.LoginResponse;
|
|
|
import com.datarecovery.master.data.api.response.UserInfoResponse;
|
|
|
-import com.datarecovery.master.data.consts.ErrorCode;
|
|
|
import com.datarecovery.master.sdk.qiyu.QiYuHelper;
|
|
|
import com.datarecovery.master.utils.BoxingUtil;
|
|
|
import com.datarecovery.master.utils.RxHttpHandler;
|
|
|
@@ -36,7 +36,6 @@ import atmob.rxjava.utils.RxJavaUtil;
|
|
|
@Singleton
|
|
|
public class AccountRepository {
|
|
|
|
|
|
-
|
|
|
private static final String TAG = "AccountRepository";
|
|
|
private static final String KEY_LOGIN_PHONE_NUM = "key_account_repo_login_phone_num";
|
|
|
private static final String KEY_LOGIN_USER_ID = "key_account_repo_login_user_id";
|
|
|
@@ -44,19 +43,10 @@ public class AccountRepository {
|
|
|
private final MutableLiveData<String> loginPhoneNum = new MutableLiveData<>();
|
|
|
private final LiveData<Boolean> isLogin = Transformations.map(loginPhoneNum, phoneNum -> !TextUtils.isEmpty(phoneNum));
|
|
|
private final MutableLiveData<?> memberStatusInfo = new MutableLiveData<>();
|
|
|
- public static String token;
|
|
|
private final AtmobApi atmobApi;
|
|
|
private final DeviceFuncRepository deviceFuncRepository;
|
|
|
private final PayRepository payRepository;
|
|
|
|
|
|
- private int errorCodeTimes;
|
|
|
-
|
|
|
- private long lastRequestCodeTime;
|
|
|
-
|
|
|
- static {
|
|
|
- token = KVUtils.getDefault().getString(KEY_LOGIN_TOKEN, null);
|
|
|
- }
|
|
|
-
|
|
|
private boolean requestUserInfoDisabled = false;
|
|
|
|
|
|
@Inject
|
|
|
@@ -69,22 +59,16 @@ public class AccountRepository {
|
|
|
deviceFuncRepository.refreshFuncAuths();
|
|
|
}
|
|
|
|
|
|
- public LiveData<String> getLoginPhoneNum() {
|
|
|
- return loginPhoneNum;
|
|
|
+ public static String getUserId() {
|
|
|
+ return KVUtils.getDefault().getString(KEY_LOGIN_USER_ID, "");
|
|
|
}
|
|
|
|
|
|
- public LiveData<Boolean> getIsLogin() {
|
|
|
- return isLogin;
|
|
|
+ public static String getKeyLoginPhoneNum() {
|
|
|
+ return KVUtils.getDefault().getString(KEY_LOGIN_PHONE_NUM, "");
|
|
|
}
|
|
|
|
|
|
- private void onLoginSuccess(String phoneNum, String token) {
|
|
|
- AccountRepository.token = token;
|
|
|
- setLoginPhoneNum(phoneNum);
|
|
|
- deviceFuncRepository.refreshFuncAuths();
|
|
|
- payRepository.refreshOrderPageList();
|
|
|
- KVUtils.getDefault().putString(KEY_LOGIN_TOKEN, token);
|
|
|
- //查找并存储userId
|
|
|
- refreshUserInfo();
|
|
|
+ public LiveData<String> getLoginPhoneNum() {
|
|
|
+ return loginPhoneNum;
|
|
|
}
|
|
|
|
|
|
private void setLoginPhoneNum(String phoneNum) {
|
|
|
@@ -96,22 +80,25 @@ public class AccountRepository {
|
|
|
|
|
|
}
|
|
|
|
|
|
+ public LiveData<Boolean> getIsLogin() {
|
|
|
+ return isLogin;
|
|
|
+ }
|
|
|
+
|
|
|
+ private void onLoginSuccess(String phoneNum, String token) {
|
|
|
+ AtmobAuth.resetToken(token);
|
|
|
+ setLoginPhoneNum(phoneNum);
|
|
|
+ deviceFuncRepository.refreshFuncAuths();
|
|
|
+ payRepository.refreshOrderPageList();
|
|
|
+ //查找并存储userId
|
|
|
+ refreshUserInfo();
|
|
|
+ }
|
|
|
|
|
|
public Single<?> requestUserCode(String phoneNum) {
|
|
|
- long currentTime = System.currentTimeMillis();
|
|
|
- if (currentTime - lastRequestCodeTime < 60 * 1000) {
|
|
|
- return Single.error(new RequestCodeTooOftenException());
|
|
|
- }
|
|
|
return atmobApi.loginSendCode(new SendCodeRequest(phoneNum))
|
|
|
.compose(RxHttpHandler.handle(true))
|
|
|
- .compose(RxJavaUtil.SingleSchedule.io2Main())
|
|
|
- .doOnSuccess(o -> {
|
|
|
- lastRequestCodeTime = currentTime;
|
|
|
- errorCodeTimes = 0;
|
|
|
- });
|
|
|
+ .compose(RxJavaUtil.SingleSchedule.io2Main());
|
|
|
}
|
|
|
|
|
|
-
|
|
|
private void refreshUserInfo() {
|
|
|
if (requestUserInfoDisabled) {
|
|
|
return;
|
|
|
@@ -126,7 +113,7 @@ public class AccountRepository {
|
|
|
public void onSuccess(@NonNull UserInfoResponse userInfoResponse) {
|
|
|
requestUserInfoDisabled = false;
|
|
|
setLoginPhoneNum(userInfoResponse.getPhone());
|
|
|
- QiYuHelper.setUserInfo(getKeyLoginPhoneNum(), userInfoResponse.getUserId(), token);
|
|
|
+ QiYuHelper.setUserInfo(getKeyLoginPhoneNum(), userInfoResponse.getUserId(), AtmobAuth.getToken());
|
|
|
KVUtils.getDefault().putString(KEY_LOGIN_USER_ID, userInfoResponse.getUserId());
|
|
|
}
|
|
|
|
|
|
@@ -144,26 +131,11 @@ public class AccountRepository {
|
|
|
}
|
|
|
|
|
|
public Single<LoginResponse> login(String phoneNum, String verificationCode) {
|
|
|
- if (errorCodeTimes >= 5) {
|
|
|
- return Single.error(new LoginTooOftenException());
|
|
|
- }
|
|
|
LoginRequest loginRequest = new LoginRequest(phoneNum, verificationCode);
|
|
|
return atmobApi.loginUserLogin(loginRequest)
|
|
|
.compose(RxHttpHandler.handle(true))
|
|
|
.compose(RxJavaUtil.SingleSchedule.io2Main())
|
|
|
- .doOnSuccess(response -> {
|
|
|
- errorCodeTimes = 0;
|
|
|
- onLoginSuccess(phoneNum, response.getToken());
|
|
|
- })
|
|
|
- .doOnError(throwable -> {
|
|
|
- RxHttpHandler.ServerErrorException serverErrorException
|
|
|
- = throwable instanceof RxHttpHandler.ServerErrorException ? ((RxHttpHandler.ServerErrorException) throwable) : null;
|
|
|
- if (serverErrorException != null) {
|
|
|
- if (serverErrorException.getCode() == ErrorCode.ERROR_CODE_VERIFICATION_CODE_ERROR) {
|
|
|
- errorCodeTimes++;
|
|
|
- }
|
|
|
- }
|
|
|
- });
|
|
|
+ .doOnSuccess(response -> onLoginSuccess(phoneNum, response.getToken()));
|
|
|
}
|
|
|
|
|
|
public void requestUserLogout() {
|
|
|
@@ -189,7 +161,7 @@ public class AccountRepository {
|
|
|
if (!BoxingUtil.boxing(isLogin.getValue())) {
|
|
|
return;
|
|
|
}
|
|
|
- token = null;
|
|
|
+ AtmobAuth.resetToken("");
|
|
|
deviceFuncRepository.refreshFuncAuths();
|
|
|
payRepository.refreshOrderPageList();
|
|
|
KVUtils.getDefault().putString(KEY_LOGIN_PHONE_NUM, "");
|
|
|
@@ -220,19 +192,4 @@ public class AccountRepository {
|
|
|
.compose(RxJavaUtil.SingleSchedule.io2Main())
|
|
|
.doOnSuccess((response) -> onLoginSuccess(null, response.getToken()));
|
|
|
}
|
|
|
-
|
|
|
- public static String getUserId() {
|
|
|
- return KVUtils.getDefault().getString(KEY_LOGIN_USER_ID, "");
|
|
|
- }
|
|
|
-
|
|
|
- public static String getKeyLoginPhoneNum() {
|
|
|
- return KVUtils.getDefault().getString(KEY_LOGIN_PHONE_NUM, "");
|
|
|
- }
|
|
|
-
|
|
|
- public static class RequestCodeTooOftenException extends Exception {
|
|
|
- }
|
|
|
-
|
|
|
- public static class LoginTooOftenException extends Exception {
|
|
|
- }
|
|
|
-
|
|
|
}
|