From 5bd3672c0bf8b99ff4fade378a3dc50d75d4bbef Mon Sep 17 00:00:00 2001
From: 18034727885 <973609879@qq.com>
Date: 星期一, 11 五月 2026 14:51:53 +0800
Subject: [PATCH] 修复问题

---
 common/src/main/java/com/xw/common/net/interceptor/HeaderInterceptor.kt  |   12 ++++--
 app/src/main/java/com/xw/yunwei/net/interceptor/YiHuHeaderInterceptor.kt |   55 +++++++++++----------------
 app/src/main/java/com/xw/yunwei/ui/user/login/LoginVM.kt                 |    9 ----
 app/src/main/java/com/xw/yunwei/net/UserApi.kt                           |    9 ++++
 app/src/main/java/com/xw/yunwei/net/UserRepository.kt                    |    4 ++
 5 files changed, 44 insertions(+), 45 deletions(-)

diff --git a/app/src/main/java/com/xw/yunwei/net/UserApi.kt b/app/src/main/java/com/xw/yunwei/net/UserApi.kt
index d8dc9a6..9c3ce7a 100644
--- a/app/src/main/java/com/xw/yunwei/net/UserApi.kt
+++ b/app/src/main/java/com/xw/yunwei/net/UserApi.kt
@@ -15,6 +15,7 @@
 import retrofit2.Call
 import retrofit2.http.Body
 import retrofit2.http.GET
+import retrofit2.http.Header
 import retrofit2.http.POST
 import retrofit2.http.Path
 import retrofit2.http.Query
@@ -30,6 +31,14 @@
     @POST("account/maintenance-app/base/v1/verificationCodeLogin")
     fun loginByCode(@Body body: RequestBody): Call<HttpResponse<LoginModel>>
 
+    /*通过第三方token登录*/
+    @GET("account/ssoAuthSdk/appLogin")
+    fun loginByThirdToken(
+        @Query("deviceType") deviceType: Int,
+        @Header("customHeader") customHeader: String = "1",
+        @Header("Authorization") Authorization: String
+    ): Call<HttpResponse<LoginModel>>
+
     /*获取手机验证码*/
     @POST("account/maintenance-app/base/v1/getVerificationCode")
     fun getVerificationCode(@Body body: RequestBody): Call<HttpResponse<Any>>
diff --git a/app/src/main/java/com/xw/yunwei/net/UserRepository.kt b/app/src/main/java/com/xw/yunwei/net/UserRepository.kt
index 1c57533..89adf89 100644
--- a/app/src/main/java/com/xw/yunwei/net/UserRepository.kt
+++ b/app/src/main/java/com/xw/yunwei/net/UserRepository.kt
@@ -15,6 +15,10 @@
 
     suspend fun loginByCode(map: Map<String, Any?>) = api.loginByCode(getBody(map)).interceptAwait()
 
+    /*通过第三方token登录*/
+    suspend fun loginByThirdToken(token: String) =
+        api.loginByThirdToken(deviceType = 1, Authorization = token).interceptAwait()
+
     suspend fun modifierPsw(map: Map<String, Any?>) = api.modifierPsw(getBody(map)).interceptAwait()
     suspend fun read(id: Long) = api.read(id).interceptAwait()
     suspend fun getAppPermission() = api.getAppPermission().interceptAwait()
diff --git a/app/src/main/java/com/xw/yunwei/net/interceptor/YiHuHeaderInterceptor.kt b/app/src/main/java/com/xw/yunwei/net/interceptor/YiHuHeaderInterceptor.kt
index 0c8e7fe..f3d0d0d 100644
--- a/app/src/main/java/com/xw/yunwei/net/interceptor/YiHuHeaderInterceptor.kt
+++ b/app/src/main/java/com/xw/yunwei/net/interceptor/YiHuHeaderInterceptor.kt
@@ -9,10 +9,10 @@
 import okhttp3.Response
 import okio.Buffer
 import java.nio.charset.StandardCharsets
-import java.util.Locale
 import javax.crypto.Mac
 import javax.crypto.SecretKey
 import javax.crypto.spec.SecretKeySpec
+
 
 //翼虎网络接口header拦截器
 class YiHuHeaderInterceptor : Interceptor {
@@ -20,7 +20,6 @@
     override fun intercept(chain: Interceptor.Chain): Response {
         val original = chain.request()
         val publicHeader = mutableMapOf(
-            "Content-Type" to "application/json",
             "XT-Client-Type" to "Android",
             "XT-DeviceId" to DeviceId.get(),
             "XT-Nonce" to System.currentTimeMillis().toString(),
@@ -47,11 +46,9 @@
         }.toMutableMap()
         map.putAll(publicHeader)
         val body = original.body
-        when {
-            body is FormBody -> {
-                (0 until body.size).forEach {
-                    map[body.name(it)] = body.value(it)
-                }
+        if (body is FormBody) {
+            (0 until body.size).forEach {
+                map[body.name(it)] = body.value(it)
             }
         }
         //第1步,生成parameter
@@ -76,40 +73,32 @@
         Log.d(TAG, "intercept: strToSign:${strToSign}")
         // 第4步. 最终签名值
         val sign = encryptHmacSHA256(strToSign)
+        Log.d(TAG, "intercept: sign:${sign}")
         publicHeader["XT-Signature"] = sign
+        publicHeader["Content-Type"] = "application/json"
         val request = original.newBuilder()
         publicHeader.forEach { (key, value) -> request.addHeader(key, value) }
         return chain.proceed(request.build())
     }
 
-    // 签名入口, data - 需要签名的字符串; secretKey -
-    private fun encryptHmacSHA256(data: String): String {
-        val encodedData = encryptHmac(data, BuildConfig.YIHU_APP_SECRET, "HmacSHA256") ?: return ""
-        return byte2hexString(encodedData)
-    }
-
-    private fun encryptHmac(data: String, secret: String, algorithm: String?): ByteArray? {
+    fun encryptHmacSHA256(data: String): String {
         try {
-            val secretKey: SecretKey =
-                SecretKeySpec(secret.toByteArray(StandardCharsets.UTF_8), algorithm)
-            val mac = Mac.getInstance(secretKey.algorithm)
-            mac.init(secretKey)
-            return mac.doFinal(data.toByteArray(StandardCharsets.UTF_8))
-        } catch (e: Exception) {
-            e.printStackTrace()
-        }
-        return null
-    }
-
-    private fun byte2hexString(bytes: ByteArray): String {
-        val sign = StringBuilder()
-        for (aByte in bytes) {
-            val hex = Integer.toHexString(aByte.toInt() and 0xFF)
-            if (hex.length == 1) {
-                sign.append('0')
+            val key: SecretKey =
+                SecretKeySpec(
+                    BuildConfig.YIHU_APP_SECRET.toByteArray(StandardCharsets.UTF_8),
+                    "HmacSHA256"
+                )
+            val mac = Mac.getInstance(key.algorithm)
+            mac.init(key)
+            val sig = mac.doFinal(data.toByteArray(StandardCharsets.UTF_8))
+            val hex = java.lang.StringBuilder(sig.size * 2)
+            for (b in sig) {
+                hex.append(Character.forDigit((b.toInt() shr 4) and 0xF, 16))
+                hex.append(Character.forDigit(b.toInt() and 0xF, 16))
             }
-            sign.append(hex.lowercase(Locale.getDefault()))
+            return hex.toString()
+        } catch (e: Exception) {
+            throw RuntimeException(e)
         }
-        return sign.toString()
     }
 }
\ No newline at end of file
diff --git a/app/src/main/java/com/xw/yunwei/ui/user/login/LoginVM.kt b/app/src/main/java/com/xw/yunwei/ui/user/login/LoginVM.kt
index 30deb5c..69c81bc 100644
--- a/app/src/main/java/com/xw/yunwei/ui/user/login/LoginVM.kt
+++ b/app/src/main/java/com/xw/yunwei/ui/user/login/LoginVM.kt
@@ -4,7 +4,6 @@
 import android.os.CountDownTimer
 import android.view.View
 import androidx.lifecycle.MutableLiveData
-import com.amap.location.support.util.TextUtils
 import com.xw.common.Constants
 import com.xw.common.base.BaseViewModel
 import com.xw.common.utils.KeyPreferences
@@ -13,7 +12,6 @@
 import com.xw.yunwei.MyApp
 import com.xw.yunwei.R
 import com.xw.yunwei.model.AgreeInfo
-import com.xw.yunwei.model.YiHuLoginInfo
 import com.xw.yunwei.net.UserRepository
 import com.xw.yunwei.net.YiHuRepository
 import com.xw.yunwei.ui.MainActivity
@@ -80,12 +78,7 @@
             onCollect = { yiHuData ->
                 yiHuData?.let {
                     launchRequest(tips = "登录中...") {
-                        val map = mapOf(
-                            "phoneNumber" to phone.value,
-                            "verificationCode" to code.value,
-                            "deviceType" to 1
-                        )
-                        val data = userRepository.loginByCode(map)
+                        val data = userRepository.loginByThirdToken(yiHuData.access_token!!)
                         data?.let {
                             Constants.token = it.token?.access_token ?: ""
                             MMKVManager.getInstance()
diff --git a/common/src/main/java/com/xw/common/net/interceptor/HeaderInterceptor.kt b/common/src/main/java/com/xw/common/net/interceptor/HeaderInterceptor.kt
index 693b0f2..ecb94b2 100644
--- a/common/src/main/java/com/xw/common/net/interceptor/HeaderInterceptor.kt
+++ b/common/src/main/java/com/xw/common/net/interceptor/HeaderInterceptor.kt
@@ -1,7 +1,5 @@
 package com.xw.common.net.interceptor
 
-import android.os.Build
-import com.blankj.utilcode.util.DeviceUtils
 import com.xw.common.Constants.token
 import okhttp3.Interceptor
 import okhttp3.Response
@@ -10,8 +8,14 @@
 
     override fun intercept(chain: Interceptor.Chain): Response {
         val original = chain.request()
-        val request = original.newBuilder()
-            .header("Authorization", token)
+        val request = if (original.header("customHeader") != "1") {
+            original.newBuilder()
+                .header("Authorization", token)
+                .removeHeader("customHeader")
+        } else {
+            original.newBuilder()
+                .removeHeader("customHeader")
+        }
         return chain.proceed(request.build())
     }
 }

--
Gitblit v1.7.1