cloudroam
10 天以前 04b138d3836e03c9adbcbd367fd71d92905c5206
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package com.example.firstapp.network
 
import android.content.Context
import android.content.Intent
import android.widget.Toast
import com.example.firstapp.activity.LoginActivity
import com.example.firstapp.utils.PreferencesManager
import com.google.gson.JsonParser
import okhttp3.Interceptor
import okhttp3.Response
import okhttp3.ResponseBody
import okio.Buffer
import kotlin.concurrent.thread
 
/**
 * 响应拦截器 - 处理业务错误码
 * 根据后端提供的接口规范,检测token失效的情况
 */
class ResponseInterceptor(private val context: Context) : Interceptor {
    override fun intercept(chain: Interceptor.Chain): Response {
        val request = chain.request()
        val response = chain.proceed(request)
        
        try {
            // 只处理成功的响应
            if (response.isSuccessful) {
                val responseBody = response.body
                if (responseBody != null) {
                    val source = responseBody.source()
                    source.request(Long.MAX_VALUE)
                    val buffer = source.buffer.clone()
                    val responseBodyString = buffer.readUtf8()
                    
                    try {
                        // 解析JSON
                        val jsonObject = JsonParser.parseString(responseBodyString).asJsonObject
                        
                        // 检查业务状态码
                        if (jsonObject.has("code")) {
                            val code = jsonObject.get("code").asString
                            
                            // 如果状态码表示token失效或登录过期
                            if (code == "401" || code == "-1" || code == "1001") {
                                // 检查返回的错误消息
                                val message = if (jsonObject.has("msg")) jsonObject.get("msg").asString else "登录已失效,请重新登录"
                                
                                if (message.contains("token") || 
                                    message.contains("登录") || 
                                    message.contains("认证") || 
                                    message.contains("授权")) {
                                    
                                    // 清除本地token
                                    PreferencesManager.clearUserData()
                                    
                                    // 在主线程中显示提示并跳转到登录页面
                                    thread {
                                        android.os.Handler(context.mainLooper).post {
                                            Toast.makeText(context, message, Toast.LENGTH_LONG).show()
                                            
                                            // 创建跳转到登录页面的Intent,并添加清除任务栈的标志
                                            val intent = Intent(context, LoginActivity::class.java).apply {
                                                flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
                                            }
                                            context.startActivity(intent)
                                        }
                                    }
                                }
                            }
                        }
                    } catch (e: Exception) {
                        e.printStackTrace()
                    }
                    
                    // 因为我们已经读取了响应体,需要重新创建一个新的响应体
                    val newResponseBody = ResponseBody.create(responseBody.contentType(), responseBodyString)
                    return response.newBuilder().body(newResponseBody).build()
                }
            }
        } catch (e: Exception) {
            e.printStackTrace()
        }
        
        return response
    }