完善token重试逻辑和多设备登录
This commit is contained in:
@ -12,6 +12,7 @@ import com.gh.common.util.DialogUtils;
|
||||
import com.gh.common.util.PackageUtils;
|
||||
import com.gh.common.util.RunningUtils;
|
||||
import com.gh.common.util.ShareUtils;
|
||||
import com.gh.common.util.StringUtils;
|
||||
import com.gh.download.DownloadManager;
|
||||
import com.gh.gamecenter.R;
|
||||
import com.gh.gamecenter.eventbus.EBShowDialog;
|
||||
@ -22,6 +23,8 @@ import com.readystatesoftware.systembartint.SystemBarTintManager.SystemBarConfig
|
||||
import org.greenrobot.eventbus.EventBus;
|
||||
import org.greenrobot.eventbus.Subscribe;
|
||||
import org.greenrobot.eventbus.ThreadMode;
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
@ -124,6 +127,20 @@ public abstract class BaseActivity extends BaseAppCompatToolBarActivity {
|
||||
}
|
||||
}
|
||||
});
|
||||
} else if ("loginException".equals(showDialog.getType())) {
|
||||
try {
|
||||
JSONObject object = new JSONObject(showDialog.getPath());
|
||||
JSONObject device = object.getJSONObject("device");
|
||||
String manufacturer = device.getString("manufacturer");
|
||||
String model = device.getString("model");
|
||||
DialogUtils.showAlertDialog(this, "你的账号已在另外一台设备登录"
|
||||
, StringUtils.buildString("(", manufacturer, "-", model, ")")
|
||||
, "知道了", null, null, null);
|
||||
} catch (JSONException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -45,11 +45,15 @@ object CollectionUtils {
|
||||
override fun onFailure(e: HttpException?) {
|
||||
super.onFailure(e)
|
||||
if (e != null) {
|
||||
var string = e.response()?.errorBody()?.string()
|
||||
var errorBody = JSONObject(string)
|
||||
if (errorBody.getInt("status") == 40031) {
|
||||
listener.onSuccess()
|
||||
return
|
||||
try {
|
||||
val string = e.response()?.errorBody()?.string()
|
||||
val errorBody = JSONObject(string)
|
||||
if (errorBody.getInt("status") == 40031) {
|
||||
listener.onSuccess()
|
||||
return
|
||||
}
|
||||
} catch (e : Exception) {
|
||||
e.printStackTrace()
|
||||
}
|
||||
}
|
||||
listener.onError()
|
||||
|
||||
@ -109,13 +109,11 @@ public class PersonalFragment extends BaseFragment implements GetLoginDataUtils.
|
||||
View mLoginMessgeHint;
|
||||
|
||||
public final static int LOGIN_CODE = 0x300; // 登录相关的requestCode && resultCode
|
||||
public final static int CHANGE_UDERINFO = 0x310; // 登出标识
|
||||
|
||||
public final static String LOGIN_TAG = "login_tag"; //登入标识
|
||||
public final static String LOGOUT_TAG = "logout_tag"; // 登出标识
|
||||
|
||||
public final static int CHANGE_UDERINFO = 0x310; // 登出标识
|
||||
|
||||
|
||||
private UserInfoEntity mUserInfoEntity;
|
||||
|
||||
@Override
|
||||
|
||||
@ -2,20 +2,24 @@ package com.gh.gamecenter.retrofit
|
||||
|
||||
import android.content.Context
|
||||
import com.gh.common.util.DeviceUtils
|
||||
import com.gh.common.util.DialogUtils
|
||||
import com.gh.common.util.LoginUtils
|
||||
import com.gh.common.util.StringUtils
|
||||
import com.gh.gamecenter.entity.LoginResponseEntity
|
||||
import com.gh.gamecenter.entity.UserInfoEntity
|
||||
import com.gh.gamecenter.eventbus.EBReuse
|
||||
import com.gh.gamecenter.eventbus.EBShowDialog
|
||||
import com.gh.gamecenter.personal.PersonalFragment
|
||||
import com.lightgame.config.CommonDebug
|
||||
import com.lightgame.utils.Utils
|
||||
import okhttp3.*
|
||||
import okhttp3.Response
|
||||
import org.greenrobot.eventbus.EventBus
|
||||
import org.json.JSONException
|
||||
import org.json.JSONObject
|
||||
import retrofit2.HttpException
|
||||
import java.io.IOException
|
||||
import java.util.*
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
* 重试connection,需要add as interceptor而不是network interceptor,可以多次执行proceed
|
||||
@ -66,18 +70,29 @@ class OkHttpRetryInterceptor internal constructor(context: Context) : Intercepto
|
||||
}
|
||||
|
||||
val body = RequestBody.create(MediaType.parse("application/json"), jsonObject.toString())
|
||||
|
||||
// 同步请求
|
||||
// 同步请求 TODO 测试未通过
|
||||
RetrofitManager
|
||||
.getUsersea()
|
||||
.refreshToken(body)
|
||||
.subscribe(object : com.gh.gamecenter.retrofit.Response<LoginResponseEntity>() {
|
||||
override fun onResponse(loginResponseEntity: LoginResponseEntity?) {
|
||||
LoginUtils.saveLoginToken(mContext, loginResponseEntity)
|
||||
val newBuilder = request.newBuilder()
|
||||
newBuilder.addHeader("TOKEN", loginResponseEntity?.accessToken?.value)
|
||||
request = newBuilder.build()
|
||||
response = doRequest(chain, request)
|
||||
val accessToken = loginResponseEntity?.accessToken?.value
|
||||
// Utils.log("=======accessToken::" + accessToken)
|
||||
|
||||
RetrofitManager
|
||||
.getApi()
|
||||
.getUserInfo(accessToken)
|
||||
.subscribe(object : com.gh.gamecenter.retrofit.Response<UserInfoEntity>(){
|
||||
override fun onResponse(userInfo: UserInfoEntity?) {
|
||||
super.onResponse(userInfo)
|
||||
LoginUtils.saveUserInfo(mContext, userInfo)
|
||||
val newBuilder = request.newBuilder()
|
||||
newBuilder.header("TOKEN", loginResponseEntity?.accessToken?.value)
|
||||
request = newBuilder.build()
|
||||
response = doRequest(chain, request)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
override fun onFailure(e: HttpException?) {
|
||||
@ -85,22 +100,15 @@ class OkHttpRetryInterceptor internal constructor(context: Context) : Intercepto
|
||||
try {
|
||||
val responseBody = e?.response()?.errorBody()
|
||||
val string = responseBody?.string()
|
||||
// Utils.log("=======string::" + string)
|
||||
val content = JSONObject(string)
|
||||
val code = content.getInt("code")
|
||||
Utils.log("=======CODE::" + code + "==" + string)
|
||||
if (code == 40802) { // 其他设备登录了该账号
|
||||
Utils.log("=======40802::" + string)
|
||||
val device = content.getJSONObject("device")
|
||||
|
||||
val manufacturer = device.getString("manufacturer")
|
||||
val model = device.getString("model")
|
||||
|
||||
DialogUtils.showAlertDialog(mContext, "你的账号已在另外一台设备登录"
|
||||
, StringUtils.buildString("(", manufacturer, "-", model, ")")
|
||||
, "知道了", null, null, null)
|
||||
|
||||
tryCount = 4
|
||||
LoginUtils.cleanUserData(mContext)
|
||||
// TODO 要不要调用退出登录接口
|
||||
EventBus.getDefault().post(EBShowDialog("loginException", string)) // 打开提示框
|
||||
EventBus.getDefault().post(EBReuse(PersonalFragment.LOGOUT_TAG))
|
||||
}
|
||||
} catch (e1: Exception) {
|
||||
e1.printStackTrace()
|
||||
|
||||
Reference in New Issue
Block a user