dm-125
This commit is contained in:
parent
0dff849752
commit
398b7c6e50
1 changed files with 25 additions and 25 deletions
|
|
@ -10,6 +10,7 @@ import dagger.Provides
|
||||||
import dagger.hilt.InstallIn
|
import dagger.hilt.InstallIn
|
||||||
import dagger.hilt.android.lifecycle.HiltViewModel
|
import dagger.hilt.android.lifecycle.HiltViewModel
|
||||||
import dagger.hilt.components.SingletonComponent
|
import dagger.hilt.components.SingletonComponent
|
||||||
|
import kotlinx.coroutines.Dispatchers
|
||||||
import kotlinx.coroutines.flow.first
|
import kotlinx.coroutines.flow.first
|
||||||
import kotlinx.coroutines.runBlocking
|
import kotlinx.coroutines.runBlocking
|
||||||
import okhttp3.OkHttpClient
|
import okhttp3.OkHttpClient
|
||||||
|
|
@ -31,30 +32,21 @@ object NetworkModule {
|
||||||
.build()
|
.build()
|
||||||
}
|
}
|
||||||
|
|
||||||
// 修正:在這裡注入 okHttpClient
|
|
||||||
@Provides
|
|
||||||
@Singleton
|
|
||||||
fun provideRetrofit(moshi: Moshi, okHttpClient: OkHttpClient): Retrofit {
|
|
||||||
return Retrofit.Builder()
|
|
||||||
.baseUrl("http://192.168.0.189/")
|
|
||||||
.client(okHttpClient) // 關鍵:將攔截器綁定到 Retrofit
|
|
||||||
.addConverterFactory(MoshiConverterFactory.create(moshi))
|
|
||||||
.build()
|
|
||||||
}
|
|
||||||
|
|
||||||
@Provides
|
@Provides
|
||||||
@Singleton
|
@Singleton
|
||||||
fun provideOkHttpClient(tokenManager: TokenManager): OkHttpClient {
|
fun provideOkHttpClient(tokenManager: TokenManager): OkHttpClient {
|
||||||
return OkHttpClient.Builder()
|
return OkHttpClient.Builder()
|
||||||
.connectTimeout(15, TimeUnit.SECONDS)
|
.connectTimeout(15, TimeUnit.SECONDS)
|
||||||
.readTimeout(15, TimeUnit.SECONDS)
|
.readTimeout(15, TimeUnit.SECONDS)
|
||||||
// 🟢 修正 2:如果使用 IP 且噴出 Hostname 錯誤才需要加這段。
|
// 🟢 如果未來改用 HTTPS 且 IP 是 192.168.0.189 遇到憑證問題再開啟
|
||||||
// 如果後端憑證的 SAN/CN 已經有包含 192.168.0.217,這行可以刪除。
|
// .hostnameVerifier { hostname, _ -> hostname == "192.168.0.189" }
|
||||||
.hostnameVerifier { hostname, _ -> hostname == "192.168.0.217" }
|
|
||||||
.addInterceptor { chain ->
|
.addInterceptor { chain ->
|
||||||
// 1. 動態獲取「目前選中帳號」的 Token
|
// 在 OkHttp 的背景執行緒中安全地進行同步等待
|
||||||
// 使用 first() 確保只取目前最新的那一個值
|
val (token, userId) = runBlocking(Dispatchers.IO) {
|
||||||
val token = runBlocking { tokenManager.currentAccessToken.first() }
|
val t = tokenManager.currentAccessToken.first()
|
||||||
|
val id = tokenManager.currentUserId.first()
|
||||||
|
t to id
|
||||||
|
}
|
||||||
|
|
||||||
val requestBuilder = chain.request().newBuilder()
|
val requestBuilder = chain.request().newBuilder()
|
||||||
token?.let {
|
token?.let {
|
||||||
|
|
@ -63,14 +55,12 @@ object NetworkModule {
|
||||||
|
|
||||||
val response = chain.proceed(requestBuilder.build())
|
val response = chain.proceed(requestBuilder.build())
|
||||||
|
|
||||||
// 2. 處理 Token 失效 (401)
|
// 處理 Token 失效 (401)
|
||||||
if (response.code == 401) {
|
if (response.code == 401) {
|
||||||
runBlocking {
|
userId?.let { id ->
|
||||||
// 獲取目前是哪個 ID 失效了
|
// 使用 Dispatchers.IO 確保不會卡死主執行緒
|
||||||
val currentId = tokenManager.currentUserId.first()
|
runBlocking(Dispatchers.IO) {
|
||||||
currentId?.let {
|
tokenManager.logout(id)
|
||||||
// 只登出當前失效的帳號,不影響其他帳號
|
|
||||||
tokenManager.logout(it)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -80,9 +70,19 @@ object NetworkModule {
|
||||||
.build()
|
.build()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Provides
|
||||||
|
@Singleton
|
||||||
|
fun provideRetrofit(moshi: Moshi, okHttpClient: OkHttpClient): Retrofit {
|
||||||
|
return Retrofit.Builder()
|
||||||
|
.baseUrl("http://192.168.0.189/") // 確保與實際伺服器 IP 一致
|
||||||
|
.client(okHttpClient)
|
||||||
|
.addConverterFactory(MoshiConverterFactory.create(moshi))
|
||||||
|
.build()
|
||||||
|
}
|
||||||
|
|
||||||
@Provides
|
@Provides
|
||||||
@Singleton
|
@Singleton
|
||||||
fun provideTecnologiaApi(retrofit: Retrofit): TecnologiaApi {
|
fun provideTecnologiaApi(retrofit: Retrofit): TecnologiaApi {
|
||||||
return retrofit.create(TecnologiaApi::class.java)
|
return retrofit.create(TecnologiaApi::class.java)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Loading…
Reference in a new issue