修改老用户同步逻辑
This commit is contained in:
@ -184,15 +184,14 @@ public class LoginUtils {
|
||||
}
|
||||
saveLoginToken(context, response);
|
||||
|
||||
if (loginTag == LoginTag.oldUserPhone) {
|
||||
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context);
|
||||
String syncDeviceID = sp.getString("syncDeviceID", null);
|
||||
if (!TextUtils.isEmpty(syncDeviceID)) {
|
||||
syncUserData(context, syncDeviceID, listener, loginTag);
|
||||
return;
|
||||
}
|
||||
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context);
|
||||
String syncDeviceID = sp.getString("syncDeviceID", null);
|
||||
if (!loginTag.equals(LoginTag.refresh) && !TextUtils.isEmpty(syncDeviceID)) {
|
||||
syncUserData(context, syncDeviceID, listener, loginTag);
|
||||
sp.edit().putString("syncDeviceID", null).apply(); // 清空
|
||||
} else {
|
||||
getUserData(context, listener, loginTag);
|
||||
}
|
||||
getUserData(context, listener, loginTag);
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -216,6 +215,7 @@ public class LoginUtils {
|
||||
Utils.toast(context, context.getString(R.string.login_failure_hint));
|
||||
}
|
||||
} catch (Exception e1) {
|
||||
Utils.toast(context, context.getString(R.string.login_failure_hint));
|
||||
e1.printStackTrace();
|
||||
}
|
||||
cleanUserData(context);
|
||||
@ -223,41 +223,25 @@ public class LoginUtils {
|
||||
});
|
||||
}
|
||||
|
||||
private static void syncUserData(final Context context, String syncDeviceID
|
||||
, final onLoginCallBackListener listener, final LoginTag loginTag) {
|
||||
private static void syncUserData(final Context context, String syncDeviceID,
|
||||
final onLoginCallBackListener listener,
|
||||
final LoginTag loginTag) {
|
||||
|
||||
String loginType;
|
||||
if (LoginTag.phone.equals(loginTag)) {
|
||||
loginType = "mobile";
|
||||
} else {
|
||||
loginType = loginTag.toString();
|
||||
}
|
||||
|
||||
HashMap<String, String> map = new HashMap<>();
|
||||
map.put("device_id", syncDeviceID);
|
||||
map.put("login_type", loginType);
|
||||
RequestBody body = RequestBody.create(MediaType.parse("application/json"), new JSONObject(map).toString());
|
||||
RetrofitManager.getInstance(context).getApi().syncUserData(body)
|
||||
.subscribeOn(Schedulers.io())
|
||||
.observeOn(AndroidSchedulers.mainThread())
|
||||
.subscribe(new Response<ResponseBody>() {
|
||||
@Override
|
||||
public void onResponse(ResponseBody response) {
|
||||
super.onResponse(response);
|
||||
getUserData(context, listener, loginTag);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onFailure(HttpException e) {
|
||||
super.onFailure(e);
|
||||
// if (e == null) {
|
||||
// Utils.toast(context, "请检查网络是否可用");
|
||||
// return;
|
||||
// }
|
||||
// try {
|
||||
// ResponseBody responseBody = e.response().errorBody();
|
||||
// String string = responseBody.string();
|
||||
// JSONObject content = new JSONObject(string);
|
||||
// int code = content.getInt("code");
|
||||
// outputErrorHint(context, code);
|
||||
// } catch (Exception e1) {
|
||||
// e1.printStackTrace();
|
||||
// }
|
||||
getUserData(context, listener, loginTag);
|
||||
}
|
||||
});
|
||||
.subscribe(userInfoResponse(context, loginTag, listener));
|
||||
}
|
||||
|
||||
// 注销登录
|
||||
@ -282,19 +266,6 @@ public class LoginUtils {
|
||||
super.onFailure(e);
|
||||
cleanUserData(context);
|
||||
listener.onCompleted();
|
||||
// if (e == null) {
|
||||
// Utils.toast(context, "请检查网络是否可用");
|
||||
// return;
|
||||
// }
|
||||
// try {
|
||||
// ResponseBody responseBody = e.response().errorBody();
|
||||
// String string = responseBody.string();
|
||||
// JSONObject content = new JSONObject(string);
|
||||
// int code = content.getInt("code");
|
||||
// outputErrorHint(context, code);
|
||||
// } catch (Exception e1) {
|
||||
// e1.printStackTrace();
|
||||
// }
|
||||
}
|
||||
});
|
||||
}
|
||||
@ -334,43 +305,50 @@ public class LoginUtils {
|
||||
}
|
||||
}
|
||||
|
||||
private static Response<UserInfoEntity> userInfoResponse(final Context context,
|
||||
final LoginTag loginTag,
|
||||
final onLoginCallBackListener listener) {
|
||||
return new Response<UserInfoEntity>() {
|
||||
@Override
|
||||
public void onResponse(UserInfoEntity response) {
|
||||
super.onResponse(response);
|
||||
|
||||
saveUserInfo(context, response);
|
||||
if (listener != null) {
|
||||
listener.onLogin(response, loginTag);
|
||||
}
|
||||
|
||||
if (loginTag != null && !LoginTag.refresh.equals(loginTag)) {
|
||||
Utils.toast(context, "登录成功");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onFailure(HttpException e) {
|
||||
super.onFailure(e);
|
||||
if (listener != null) {
|
||||
listener.onLoginFailure();
|
||||
}
|
||||
if (loginTag != null) {
|
||||
Utils.toast(context, context.getString(R.string.login_failure_hint));
|
||||
}
|
||||
if (loginTag != null && loginTag.equals(LoginTag.qq)) {
|
||||
GetLoginDataUtils.getInstance(context).QQLogout();
|
||||
}
|
||||
cleanUserData(context);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
// 获取用户信息
|
||||
public static void getUserData(final Context context, final onLoginCallBackListener listener, final LoginTag loginTag) {
|
||||
|
||||
RetrofitManager.getInstance(context)
|
||||
.getApi()
|
||||
.getUserInfo()
|
||||
.subscribeOn(Schedulers.io())
|
||||
.observeOn(AndroidSchedulers.mainThread())
|
||||
.subscribe(new Response<UserInfoEntity>() {
|
||||
@Override
|
||||
public void onResponse(UserInfoEntity response) {
|
||||
super.onResponse(response);
|
||||
|
||||
saveUserInfo(context, response);
|
||||
if (listener != null) {
|
||||
listener.onLogin(response, loginTag);
|
||||
}
|
||||
|
||||
if (loginTag != null && !LoginUtils.LoginTag.refresh.equals(loginTag)) {
|
||||
Utils.toast(context, "登录成功");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onFailure(HttpException e) {
|
||||
super.onFailure(e);
|
||||
if (listener != null) {
|
||||
listener.onLoginFailure();
|
||||
}
|
||||
if (loginTag != null) {
|
||||
Utils.toast(context, context.getString(R.string.login_failure_hint));
|
||||
}
|
||||
if (loginTag != null && loginTag.equals(LoginTag.qq)) {
|
||||
GetLoginDataUtils.getInstance(context).QQLogout();
|
||||
}
|
||||
cleanUserData(context);
|
||||
}
|
||||
});
|
||||
.subscribe(userInfoResponse(context, loginTag, listener));
|
||||
}
|
||||
|
||||
// 获取本地缓存用户信息
|
||||
@ -677,6 +655,7 @@ public class LoginUtils {
|
||||
// 登录回调
|
||||
public interface onLoginCallBackListener {
|
||||
void onLogin(UserInfoEntity entity, LoginTag loginTag);
|
||||
|
||||
void onLoginFailure();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user