Skip to content

Commit

Permalink
docs: 更新文档
Browse files Browse the repository at this point in the history
  • Loading branch information
liangjingkanji committed Aug 16, 2023
1 parent 8cc3ec2 commit 44dfa20
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 19 deletions.
19 changes: 10 additions & 9 deletions docs/interceptor.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,22 @@ NetConfig.initialize(Api.HOST, this) {
}
```

演示客户端自动刷新token的拦截器
客户端token自动续期示例

```kotlin
/**
* 演示如何自动刷新token令牌
*/
class RefreshTokenInterceptor : Interceptor {
override fun intercept(chain: Interceptor.Chain): Response {
val request = chain.request()
val response = chain.proceed(request) // 如果token失效

val response = chain.proceed(request)
return synchronized(RefreshTokenInterceptor::class.java) {
if (response.code == 401 && UserConfig.isLogin && !request.url.pathSegments.contains("token")) {
val json = Net.get("token").execute<String>() // 同步刷新token
UserConfig.token = JSONObject(json).optString("token")
if (response.code == 401 && UserConfig.isLogin && !request.url.encodedPath.contains(Api.Token)) {
val tokenInfo = Net.get(Api.Token).execute<TokenModel>() // 同步请求token
if (tokenInfo.isExpired) {
// token过期抛出异常, 由全局错误处理器处理, 在其中可以跳转到登陆界面提示用户重新登陆
throw ResponseException(response, "登录状态失效")
} else {
UserConfig.token = tokenInfo.token
}
chain.proceed(request)
} else {
response
Expand Down
1 change: 1 addition & 0 deletions sample/src/main/java/com/drake/net/sample/constants/Api.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@ object Api {
const val CONFIG = "/config"
const val USER_INFO = "/userInfo"
const val TIME = "/time"
const val Token = "/token"
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,28 @@ package com.drake.net.sample.interceptor

import com.drake.net.Net
import com.drake.net.exception.ResponseException
import com.drake.net.sample.constants.Api
import com.drake.net.sample.constants.UserConfig
import com.drake.net.sample.model.TokenModel
import okhttp3.Interceptor
import okhttp3.Response
import org.json.JSONObject


/**
* 演示如何自动刷新token令牌
* 客户端token自动续期示例
*/
class RefreshTokenInterceptor : Interceptor {
override fun intercept(chain: Interceptor.Chain): Response {
val request = chain.request()
val response = chain.proceed(request) // 如果token失效

val response = chain.proceed(request)
return synchronized(RefreshTokenInterceptor::class.java) {
if (response.code == 401 && UserConfig.isLogin && !request.url.pathSegments.contains("token")) {
val json = Net.get("token").execute<String>() // 同步刷新token
val jsonObject = JSONObject(json)
if (jsonObject.getBoolean("isExpired")) {
// token刷新失败跳转到登录界面重新登录, 建议在错误处理器[NetErrorHandler]处理所有错误请求, 此处抛出异常即可
if (response.code == 401 && UserConfig.isLogin && !request.url.encodedPath.contains(Api.Token)) {
val tokenInfo = Net.get(Api.Token).execute<TokenModel>() // 同步请求token
if (tokenInfo.isExpired) {
// token过期抛出异常, 由全局错误处理器处理, 在其中可以跳转到登陆界面提示用户重新登陆
throw ResponseException(response, "登录状态失效")
} else {
UserConfig.token = jsonObject.optString("token")
UserConfig.token = tokenInfo.token
}
chain.proceed(request)
} else {
Expand Down
9 changes: 9 additions & 0 deletions sample/src/main/java/com/drake/net/sample/model/TokenModel.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.drake.net.sample.model

import kotlinx.serialization.Serializable

@Serializable
data class TokenModel(
var token: String = "",
var isExpired: Boolean = false
)

0 comments on commit 44dfa20

Please sign in to comment.