|
|
@@ -2,19 +2,16 @@ package com.miekir.eden.ui.mine.login;
|
|
|
|
|
|
import android.text.TextUtils;
|
|
|
|
|
|
-import com.miekir.eden.R;
|
|
|
import com.miekir.eden.base.ApiService;
|
|
|
import com.miekir.eden.base.CommonApi;
|
|
|
import com.miekir.eden.bean.BeiUser;
|
|
|
+import com.miekir.eden.bean.IPInfoBean;
|
|
|
import com.miekir.eden.constant.ConstantString;
|
|
|
import com.miekir.eden.constant.EdenError;
|
|
|
-import com.miekir.eden.exception.BeiException;
|
|
|
import com.miekir.eden.net.RetrofitHelper;
|
|
|
-import com.miekir.eden.tool.StringTool;
|
|
|
import com.miekir.mvp.presenter.BasePresenter;
|
|
|
import com.miekir.network.core.base.BaseObserver;
|
|
|
|
|
|
-import io.reactivex.Observable;
|
|
|
import io.reactivex.android.schedulers.AndroidSchedulers;
|
|
|
import io.reactivex.schedulers.Schedulers;
|
|
|
|
|
|
@@ -25,40 +22,67 @@ import io.reactivex.schedulers.Schedulers;
|
|
|
*/
|
|
|
public class LoginPresenter extends BasePresenter<ILoginView> {
|
|
|
|
|
|
- public void submitLogin(String email, String password) {
|
|
|
+ public void submitLoginWithPassword(String email, String password) {
|
|
|
RetrofitHelper.getInstance()
|
|
|
- .getRequestApi(CommonApi.class)
|
|
|
- .getIPInfo()
|
|
|
- .flatMap(ipInfo -> {
|
|
|
- // 暂不支持中国地区
|
|
|
- if (!TextUtils.equals(email, ConstantString.NAME_ADMIN)) {
|
|
|
- if ((ipInfo == null || TextUtils.equals("CN", ipInfo.getCountrycode()))) {
|
|
|
- return Observable.error(new BeiException(EdenError.REGION_INVALID, StringTool.getString(R.string.login_area_not_support)));
|
|
|
+ .getRequestApi(ApiService.class)
|
|
|
+ .loginOrRegister(email, password).subscribeOn(Schedulers.io())
|
|
|
+ .observeOn(AndroidSchedulers.mainThread())
|
|
|
+ .subscribe(new BaseObserver<BeiUser>() {
|
|
|
+ @Override
|
|
|
+ public void onSuccess(int code, BeiUser result) {
|
|
|
+ if (getView() == null) {
|
|
|
+ return;
|
|
|
}
|
|
|
+
|
|
|
+ // 老用户直接登录
|
|
|
+ long existTime = System.currentTimeMillis() - result.registerTimeMillis;
|
|
|
+ boolean isOldUser = Math.abs(existTime) > (24 * 60 * 60 * 1000);
|
|
|
+ if (isOldUser) {
|
|
|
+ getView().onLoginResult(result, EdenError.SUCCESS);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (TextUtils.equals(result.email, ConstantString.NAME_ADMIN)) {
|
|
|
+ getView().onLoginResult(result, EdenError.SUCCESS);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ getRegionInfo(result);
|
|
|
}
|
|
|
|
|
|
- return RetrofitHelper.getInstance()
|
|
|
- .getRequestApi(ApiService.class)
|
|
|
- .loginOrRegister(email, password);
|
|
|
- })
|
|
|
+ @Override
|
|
|
+ public void onFailure(int code, Throwable e, String errMsg) {
|
|
|
+ if (getView() != null) {
|
|
|
+ getView().onLoginResult(null, EdenError.COMMON);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ private void getRegionInfo(BeiUser user) {
|
|
|
+ RetrofitHelper.getInstance()
|
|
|
+ .getRequestApi(CommonApi.class)
|
|
|
+ .getIPInfo()
|
|
|
.subscribeOn(Schedulers.io())
|
|
|
.observeOn(AndroidSchedulers.mainThread())
|
|
|
- .subscribe(new BaseObserver<BeiUser>() {
|
|
|
+ .subscribe(new BaseObserver<IPInfoBean>() {
|
|
|
@Override
|
|
|
- public void onSuccess(int code, BeiUser result) {
|
|
|
+ public void onSuccess(int code, IPInfoBean ipInfo) {
|
|
|
if (getView() != null) {
|
|
|
- getView().onLoginResult(result, EdenError.SUCCESS);
|
|
|
+ // 暂不支持中国地区
|
|
|
+ if ((ipInfo == null || TextUtils.equals("CN", ipInfo.getCountrycode()))) {
|
|
|
+ getView().onLoginResult(null, EdenError.REGION_INVALID);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ getView().onLoginResult(user, EdenError.SUCCESS);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public void onFailure(int code, Throwable e, String errMsg) {
|
|
|
if (getView() != null) {
|
|
|
- if (e instanceof BeiException) {
|
|
|
- getView().onLoginResult(null, ((BeiException) e).getCode());
|
|
|
- } else {
|
|
|
- getView().onLoginResult(null, EdenError.COMMON);
|
|
|
- }
|
|
|
+ // 获取区域信息失败
|
|
|
+ getView().onLoginResult(null, EdenError.REGION_GET_FAILED);
|
|
|
}
|
|
|
}
|
|
|
});
|