From ea098a2a63eae7fc720a5de7fba19224f48dd9f2 Mon Sep 17 00:00:00 2001 From: level <1364007522@qq.com> Date: Tue, 29 Jul 2025 15:51:09 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=80=E9=94=AE=E8=BF=9E=E6=9B=B4=E6=94=B9?= =?UTF-8?q?=E5=AE=A2=E6=9C=8D=E8=8E=B7=E5=8F=96=E6=96=B9=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/yingmao/clash/base/PublicViewMode.kt | 69 +++++++++++-------- .../com/yingmao/clash/net/http/HttpApi.kt | 7 +- .../com/yingmao/clash/net/http/NetService.kt | 10 +-- .../yingmao/clash/webview/BZYWebViewKeFu.kt | 2 +- .../clash/webview/CustomServiceHelper.kt | 50 ++++++++++++++ .../clash/webview/CustomerServiceWebView.kt | 42 ++++------- 6 files changed, 117 insertions(+), 63 deletions(-) create mode 100644 app/src/main/java/com/yingmao/clash/webview/CustomServiceHelper.kt diff --git a/app/src/main/java/com/yingmao/clash/base/PublicViewMode.kt b/app/src/main/java/com/yingmao/clash/base/PublicViewMode.kt index 52804fd..2d19b6e 100644 --- a/app/src/main/java/com/yingmao/clash/base/PublicViewMode.kt +++ b/app/src/main/java/com/yingmao/clash/base/PublicViewMode.kt @@ -189,34 +189,6 @@ open class PublicViewMode : BaseViewMode() { } } - fun getYmKefuHost(callback: AsyncBodyCallback) { - val baseUrl=BuildConfig.service_url - val rt = - NetService.instances.createFreeBaseUrl(baseUrl) - doAsyncNoNeedReturn(HttpReqType.GET_HOST) { - try { - var host = rt.getKefuHost() - callback.suc(host) - } catch (e: Exception) { - e.printStackTrace() - callback.err() - } - } - } - fun getCustomServiceHost(callback: AsyncBodyCallback) { - val rt = - NetService.instances.createFreeBaseUrl("https://pubtofile.oss-cn-hongkong.aliyuncs.com/") - doAsyncNoNeedReturn(HttpReqType.GET_HOST) { - try { - val host = rt.getYingMaoKefuHost() - callback.suc(host) - } catch (e: Exception) { - e.printStackTrace() - callback.err() - } - } - } - fun getTzKefuHost(callback: AsyncBodyCallback) { val rt = NetService.instances.createFreeBaseUrl("https://yingmao.oss-cn-hongkong.aliyuncs.com/") @@ -320,4 +292,45 @@ open class PublicViewMode : BaseViewMode() { } } } + fun getLocalHost(callback: AsyncBodyCallback) { + val baseUrl=BuildConfig.service_url + val rt = NetService.instances.createFreeBaseUrl(baseUrl) + doAsyncNoNeedReturn(HttpReqType.GET_HOST) { + try { //TODO bug 会引起崩溃 + var host = rt.getLocalKefuHost() + callback.suc(host) + } catch (e: Exception) { + e.printStackTrace() + callback.err() + } + } + } + fun getCustomServiceNet(callback: AsyncBodyCallback) { + val rt = + NetService.instances.createFreeBaseUrl("https://pubtofilegz.oss-rg-china-mainland.aliyuncs.com/") + doAsyncNoNeedReturn(HttpReqType.GET_HOST) { + try { + val host = rt.getKklNetHost() + android.util.Log.i("getCustomServiceHost","Kf host::"+host) + callback.suc(host) + } catch (e: Exception) { + e.printStackTrace() + callback.err() + } + } + } + fun getCustomServiceNetS3(callback: AsyncBodyCallback) { + val rt = + NetService.instances.createFreeBaseUrl("https://wfjsq.s3.ap-east-1.amazonaws.com/") + doAsyncNoNeedReturn(HttpReqType.GET_HOST) { + try { + val host = rt.getKklNetS3() + android.util.Log.i("getCustomServiceHost","Kf host::"+host) + callback.suc(host) + } catch (e: Exception) { + e.printStackTrace() + callback.err() + } + } + } } \ No newline at end of file diff --git a/app/src/main/java/com/yingmao/clash/net/http/HttpApi.kt b/app/src/main/java/com/yingmao/clash/net/http/HttpApi.kt index efd3172..b0230b2 100644 --- a/app/src/main/java/com/yingmao/clash/net/http/HttpApi.kt +++ b/app/src/main/java/com/yingmao/clash/net/http/HttpApi.kt @@ -240,10 +240,13 @@ interface HttpApi { //客服链接 @GET("/oss/kf/yjl_kefu.txt") - suspend fun getKefuHost(): String + suspend fun getLocalKefuHost(): String + @GET("kf/yjl_kefu.txt") + suspend fun getKklNetHost(): String @GET("/yjl_kefu.txt") - suspend fun getYingMaoKefuHost(): String + suspend fun getKklNetS3(): String + @GET("/taizi_kefu.txt") suspend fun getTaiZiKefuHost(): String diff --git a/app/src/main/java/com/yingmao/clash/net/http/NetService.kt b/app/src/main/java/com/yingmao/clash/net/http/NetService.kt index c870f2b..a50155f 100644 --- a/app/src/main/java/com/yingmao/clash/net/http/NetService.kt +++ b/app/src/main/java/com/yingmao/clash/net/http/NetService.kt @@ -127,11 +127,11 @@ class NetService private constructor() : INetService { // .sslSocketFactory(getSSLSocketFactory(), CustomTrustManager())//信任所有证书 .sslSocketFactory(getSSLSocketFactory(), trustAllCerts[0] as X509TrustManager) .hostnameVerifier(getHostnameVerifier()!!) - if (BuildConfig.DEBUG) { - builder.addInterceptor(loggingInterceptor) -// builder.addInterceptor(ExceptionLoggingInterceptor()) - } -// builder.addInterceptor(loggingInterceptor) +// if (BuildConfig.DEBUG) { +// builder.addInterceptor(loggingInterceptor) +//// builder.addInterceptor(ExceptionLoggingInterceptor()) +// } + builder.addInterceptor(loggingInterceptor) return builder.build() } diff --git a/app/src/main/java/com/yingmao/clash/webview/BZYWebViewKeFu.kt b/app/src/main/java/com/yingmao/clash/webview/BZYWebViewKeFu.kt index b01a729..cbad886 100644 --- a/app/src/main/java/com/yingmao/clash/webview/BZYWebViewKeFu.kt +++ b/app/src/main/java/com/yingmao/clash/webview/BZYWebViewKeFu.kt @@ -114,7 +114,7 @@ class BZYWebViewKeFu : BaseActivity() { var mmkvHost = MMKV.defaultMMKV() .decodeString(BZYConstants.MMKV_KEY_KEFU_HOST, "") // com.cncat.vpn.common.log.Log.d("mmkvHost kf ==>${mmkvHost}") - webViewAtyVM.getYmKefuHost(object : AsyncBodyCallback { + webViewAtyVM.getLocalHost(object : AsyncBodyCallback { override suspend fun suc(str: String) { // com.cncat.vpn.common.log.Log.d("str==>${str}") if (str != mmkvHost) { diff --git a/app/src/main/java/com/yingmao/clash/webview/CustomServiceHelper.kt b/app/src/main/java/com/yingmao/clash/webview/CustomServiceHelper.kt new file mode 100644 index 0000000..8b7de64 --- /dev/null +++ b/app/src/main/java/com/yingmao/clash/webview/CustomServiceHelper.kt @@ -0,0 +1,50 @@ +package com.yingmao.clash.webview +import android.util.Log +import com.yingmao.clash.base.PublicViewMode +import com.yingmao.clash.listener.AsyncBodyCallback + +class CustomServiceHelper(private val splashAtyVM: PublicViewMode) { + + fun getHostWithFallback(callback: (String) -> Unit) { + tryGetHost( + { splashAtyVM.getLocalHost(it) }, + { splashAtyVM.getCustomServiceNet(it) }, + { splashAtyVM.getCustomServiceNetS3(it) }, + callback + ) + } + + private fun tryGetHost( + first: (AsyncBodyCallback) -> Unit, + second: (AsyncBodyCallback) -> Unit, + third: (AsyncBodyCallback) -> Unit, + callback: (String) -> Unit + ) { + first(createCallback("getLocalHost method success", "getLocalHost method failed", callback) { + second(createCallback("getCustomServiceNet method success", "getCustomServiceNet method failed", callback) { + third(createCallback("getCustomServiceNetS3 method success", "getCustomServiceNetS3 methods failed", callback) { + callback("") // 所有方法都失败 + }) + }) + }) + } + + private fun createCallback( + successLog: String, + errorLog: String, + callback: (String) -> Unit, + onFailure: () -> Unit + ): AsyncBodyCallback { + return object : AsyncBodyCallback { + override suspend fun suc(str: String) { + Log.e("data", successLog) + callback(str) // 成功时回调 + } + + override fun err() { + Log.e("data", errorLog) + onFailure() // 失败时尝试下一个方法 + } + } + } +} diff --git a/app/src/main/java/com/yingmao/clash/webview/CustomerServiceWebView.kt b/app/src/main/java/com/yingmao/clash/webview/CustomerServiceWebView.kt index 9a44083..231f08d 100644 --- a/app/src/main/java/com/yingmao/clash/webview/CustomerServiceWebView.kt +++ b/app/src/main/java/com/yingmao/clash/webview/CustomerServiceWebView.kt @@ -28,7 +28,10 @@ import com.yingmao.clash.R import com.yingmao.clash.base.BaseActivity import com.yingmao.clash.conf.BZYConstants import com.yingmao.clash.listener.AsyncBodyCallback +import com.yingmao.clash.view.RxToast import com.yingmao.clash.vm.WebViewAtyVM +import kotlinx.coroutines.MainScope +import kotlinx.coroutines.launch import java.net.URLEncoder @@ -89,46 +92,31 @@ class CustomerServiceWebView:BaseActivity() { // } getCustomerServiceUrl() } - private fun getCustomerServiceUrl() { val userName = MMKV.defaultMMKV().decodeString(BZYConstants.MMKV_KEY_REMEMBER_USERNAME, "") val enName = URLEncoder.encode(userName, "UTF-8") Log.d("getCustomerServiceUrl enName==>${enName}") + if (TextUtils.isEmpty(customerServiceHost)) { val mmkvHost = MMKV.defaultMMKV() .decodeString(BZYConstants.MMKV_KEY_KEFU_HOST, "") - webViewVM.getYmKefuHost(object : AsyncBodyCallback { - override suspend fun suc(str: String) { + + CustomServiceHelper(webViewVM).getHostWithFallback { str -> + if(str.isEmpty()){ + RxToast.info("客服地址获取失败!") + return@getHostWithFallback + } + MainScope().launch { if (str != mmkvHost) { MMKV.defaultMMKV() .encode(BZYConstants.MMKV_KEY_KEFU_HOST, str) } customerServiceHost = str csWebView.loadUrl(str+"&app_account=${enName}"+"&app_t=${resources.getString(R.string.yijianlian)}"+"&app_v=${BuildConfig.VERSION_NAME}."+BuildConfig.productId) - } - - override fun err() { - webViewVM.getCustomServiceHost(object : AsyncBodyCallback { - override suspend fun suc(str: String) { - if (str != mmkvHost) { - MMKV.defaultMMKV() - .encode(BZYConstants.MMKV_KEY_KEFU_HOST, str) - } - csWebView.loadUrl(str+"&app_account=${enName}"+"&app_t=${resources.getString(R.string.yijianlian)}"+"&app_v=${BuildConfig.VERSION_NAME}."+BuildConfig.productId) - } - - override fun err() { - if (!TextUtils.isEmpty(mmkvHost)) { - csWebView.loadUrl(mmkvHost+"&app_account=${enName}"+"&app_t=${resources.getString(R.string.yijianlian)}"+"&app_v=${BuildConfig.VERSION_NAME}."+BuildConfig.productId) - - } - } - - }) - } - - }) - + } + } else { + csWebView.loadUrl(customerServiceHost + "&app_account=${enName}"+"&app_t=${resources.getString(R.string.yijianlian)}"+"&app_v=${BuildConfig.VERSION_NAME}."+BuildConfig.productId) + } initKeyboardListener() } private fun initKeyboardListener() {