增加了Token刷新机制
This commit is contained in:
1
.idea/gradle.xml
generated
1
.idea/gradle.xml
generated
@@ -23,7 +23,6 @@
|
||||
</set>
|
||||
</option>
|
||||
<option name="resolveModulePerSourceSet" value="false" />
|
||||
<option name="useQualifiedModuleNames" value="true" />
|
||||
</GradleProjectSettings>
|
||||
</option>
|
||||
</component>
|
||||
|
||||
@@ -1,6 +1,9 @@
|
||||
package com.mogo.cloud.network.interceptor
|
||||
|
||||
import com.mogo.cloud.network.BaseData
|
||||
import com.mogo.cloud.passport.MoGoAiCloudClient
|
||||
import com.mogo.utils.logger.Logger
|
||||
import com.mogo.utils.network.utils.GsonUtil
|
||||
import okhttp3.Interceptor
|
||||
import okhttp3.MediaType
|
||||
import okhttp3.Response
|
||||
@@ -26,7 +29,16 @@ class HttpPassportInterceptor : Interceptor {
|
||||
bodySize = if (contentLength != -1L) "$contentLength-byte" else "unknown-length"
|
||||
contentType = responseBody.contentType()
|
||||
responseContent = responseBody.string()
|
||||
Logger.d("响应结果", "responseContent:$responseContent")
|
||||
val response: BaseData<*>? = GsonUtil.objectFromJson(responseContent, BaseData::class.java)
|
||||
// 处理Token异常
|
||||
if (response?.code == 100046 ||
|
||||
response?.code == 100045 ||
|
||||
response?.code == 100005 ||
|
||||
response?.code == 100006 ||
|
||||
response?.code == 520003) {
|
||||
Logger.d("HttpPassportInterceptor", "Token 异常:$response")
|
||||
MoGoAiCloudClient.getInstance().refreshToken()
|
||||
}
|
||||
consumedResponse = true
|
||||
}
|
||||
return if (consumedResponse) response.newBuilder().body(ResponseBody.create(contentType, responseContent)).build() else response
|
||||
|
||||
@@ -72,6 +72,8 @@ public class MoGoAiCloudClient {
|
||||
}
|
||||
|
||||
public void refreshToken() {
|
||||
Log.i(TAG, "═══════════════刷新Token═════════════");
|
||||
|
||||
if (mAiCloudClientConfig != null) {
|
||||
ThirdLoginParam thirdLoginParam = ThirdLoginParam.of(
|
||||
mAiCloudClientConfig.getThirdPartyDeviceId(),
|
||||
@@ -90,13 +92,10 @@ public class MoGoAiCloudClient {
|
||||
Log.i(TAG, "║ Token:" + result.token);
|
||||
Log.i(TAG, "═════════════════════════════════════");
|
||||
|
||||
// 本地存储
|
||||
SpStorage.setSn(result.sn);
|
||||
SpStorage.setToken(result.token);
|
||||
// 变量赋值
|
||||
if (mAiCloudClientConfig != null) {
|
||||
mAiCloudClientConfig.sn = result.sn;
|
||||
mAiCloudClientConfig.token = result.token;
|
||||
mAiCloudClientConfig.setSn(result.sn);
|
||||
mAiCloudClientConfig.setToken(result.token);
|
||||
}
|
||||
|
||||
// 循环调用将数据传出去
|
||||
@@ -126,8 +125,8 @@ public class MoGoAiCloudClient {
|
||||
Log.e(TAG, "═════════════════════════════════════");
|
||||
// 变量赋值
|
||||
if (mAiCloudClientConfig != null) {
|
||||
mAiCloudClientConfig.sn = "";
|
||||
mAiCloudClientConfig.token = "";
|
||||
mAiCloudClientConfig.setSn("");
|
||||
mAiCloudClientConfig.setToken("");
|
||||
}
|
||||
for (IMoGoTokenCallback tokenCallback : mTokenCallbacks) {
|
||||
tokenCallback.onError(code, msg);
|
||||
|
||||
@@ -44,12 +44,12 @@ public class MoGoAiCloudClientConfig {
|
||||
/**
|
||||
* 服务器分配给应用的令牌,用于网络请求校验
|
||||
*/
|
||||
String token = "";
|
||||
private String token = "";
|
||||
|
||||
/**
|
||||
* 服务器根据设备ID 分配的SN,用于网络请求校验
|
||||
*/
|
||||
String sn = "";
|
||||
private String sn = "";
|
||||
|
||||
/**
|
||||
* 循环检测延时时间
|
||||
@@ -170,11 +170,10 @@ public class MoGoAiCloudClientConfig {
|
||||
* @return Token信息
|
||||
*/
|
||||
public String getToken() {
|
||||
|
||||
if (TextUtils.isEmpty(token)) {
|
||||
Logger.e(TAG, "Token 获取失败……");
|
||||
if (TextUtils.isEmpty(SpStorage.getToken())) {
|
||||
Logger.e(TAG, "本地 Token 获取失败……");
|
||||
}
|
||||
return token;
|
||||
return SpStorage.getToken();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -183,10 +182,32 @@ public class MoGoAiCloudClientConfig {
|
||||
* @return SN信息
|
||||
*/
|
||||
public String getSn() {
|
||||
if (TextUtils.isEmpty(sn)) {
|
||||
Logger.e(TAG, "SN 获取失败……");
|
||||
if (TextUtils.isEmpty(SpStorage.getSn())) {
|
||||
Logger.e(TAG, "本地 SN 获取失败……");
|
||||
}
|
||||
return sn;
|
||||
return SpStorage.getSn();
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置Token
|
||||
*
|
||||
* @param token 平台分配的token信息
|
||||
*/
|
||||
public void setToken(String token) {
|
||||
this.token = token;
|
||||
// 存储本地
|
||||
SpStorage.setToken(token);
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置SN
|
||||
*
|
||||
* @param sn 平台分配的sn信息
|
||||
*/
|
||||
public void setSn(String sn) {
|
||||
this.sn = sn;
|
||||
// 存储本地
|
||||
SpStorage.setSn(sn);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -274,6 +295,7 @@ public class MoGoAiCloudClientConfig {
|
||||
", token='" + token + '\'' +
|
||||
", sn='" + sn + '\'' +
|
||||
", mLoopCheckDelay=" + mLoopCheckDelay +
|
||||
", mIsUseExternalLocation=" + mIsUseExternalLocation +
|
||||
", mIHttpDnsCurrentLocation=" + mIHttpDnsCurrentLocation +
|
||||
'}';
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user