소스 검색

[aio] 脉诊添加获取报告地址功能

cc12458 9 달 전
부모
커밋
53d5d3fb55

+ 1 - 1
app/build.gradle.kts

@@ -54,7 +54,7 @@ android {
     minSdk = 26
     targetSdk = 35
     versionCode = getVersionCode()
-    versionName = "2.2.1"
+    versionName = "2.3.0"
 
     testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
   }

+ 1 - 8
app/src/aio-prod/res/values/browser_proxy.xml

@@ -1,12 +1,5 @@
 <?xml version="1.0" encoding="utf-8"?>
 <resources>
   <bool name="browser_http_proxy_enabled">true</bool>
-  <string-array name="browser_proxy_pool">
-    <item>https://hybrid.reborn-tech.com -> https://wx.hzliuzhi.com/mz/hybrid</item>
-    <item>https://api.reborn-tech.com -> https://wx.hzliuzhi.com/mz/api</item>
-    <item>https://taiyi.oss-accelerate.aliyuncs.com -> https://wx.hzliuzhi.com/mz/oss</item>
-    <item>https://taiyi.oss-cn-beijing.aliyuncs.com -> https://wx.hzliuzhi.com/mz/oss</item>
-    <item>https://oss-accelerate.aliyuncs.com/taiyi -> https://wx.hzliuzhi.com/mz/oss</item>
-    <item>https://oss-cn-beijing.aliyuncs.com/taiyi -> https://wx.hzliuzhi.com/mz/oss</item>
-  </string-array>
+  <string-array name="browser_proxy_pool" />
 </resources>

+ 0 - 64
app/src/aio/assets/browser/bridge.js

@@ -1,64 +0,0 @@
-class Bridge extends EventTarget {
-  #pool = new Map();
-
-  static getInstance() {
-    return Bridge._instance ?? (Bridge._instance = new Bridge());
-  }
-
-  static get Platform() {
-    return window['AndroidBridge'];
-  }
-
-  static get UUID() {
-    return crypto.randomUUID();
-  }
-
-  static pulse(userId) {
-    const { promise, ...resolvers } = Promise.withResolvers();
-    this.getInstance().#postMessage('pulse', { userId }, resolvers);
-    return promise;
-  }
-
-  static print(payload) {
-    const { promise, ...resolvers } = Promise.withResolvers();
-    this.getInstance().#postMessage('print', payload, resolvers);
-    return promise;
-  }
-
-  dispatch(message) {
-    console.log('log:bridge:js', `[dispatch] 接收消息: ${message}`);
-    try {
-      const { type, callbackId, payload }  = JSON.parse(message);
-      if (callbackId) {
-        const { resolve, reject } = this.#pool.get(callbackId) ?? {};
-        this.#pool.delete(callbackId);
-        if (payload.code === 0) resolve(payload.data)
-        else reject(payload.message)
-      } else {
-        event = new CustomEvent(type, { detail: payload });
-        super.dispatchEvent(event);
-      }
-    } catch (e) {
-      console.log('log:bridge:js', `[analysis] 解析消息错误: ${e.message}`);
-      throw e;
-    }
-  }
-
-  addEventListener(type, callback, options) {
-    super.addEventListener(type, callback, options);
-    return () => super.removeEventListener(type, callback);
-  }
-
-  #postMessage(type, payload, resolvers) {
-    const callbackId = `${type}:${Bridge.UUID}`;
-    this.#pool.set(callbackId, resolvers);
-    const message = JSON.stringify({ type, payload, callbackId })
-    Bridge.Platform.postMessage(message);
-    console.log('log:bridge:js', `[dispatch] 发送消息: ${message}`);
-  }
-}
-
-window['Bridge'] = Bridge;
-window['bridge'] = Bridge.getInstance();
-
-window.print = Bridge.print.bind(Bridge);

+ 50 - 3
core/src/main/java/com/hzliuzhi/applet/core/shared/SharedFlowHub.kt

@@ -1,5 +1,6 @@
 package com.hzliuzhi.applet.core.shared
 
+import com.google.gson.JsonElement
 import kotlinx.coroutines.flow.MutableSharedFlow
 import kotlinx.coroutines.flow.asSharedFlow
 
@@ -16,7 +17,7 @@ object SharedFlowHub {
     return try {
       Event(
         type = this.type,
-        payload = this.payload as? P,
+        payload = this.payload.convertTo<P>(),
         callback = this.callback as? ((C) -> Unit)
       )
     } catch (e: Exception) {
@@ -29,7 +30,53 @@ object SharedFlowHub {
     return callback as? ((C) -> Unit)
   }
 
-  inline fun <reified P> Event<*, *>.payloadAs(): P? {
-    return payload as? P
+  inline fun <reified P> Event<*, *>.payloadAs() = payload.convertTo<P>()
+
+  inline fun <reified P> Any?.convertTo(): P? {
+    return when (P::class) {
+      String::class -> when (this) {
+        is String -> this as P
+        is JsonElement -> this.asString as P
+        else -> this?.toString() as? P
+      }
+
+      Int::class -> when (this) {
+        is Int -> this as P
+        is Number -> this.toInt() as P
+        is JsonElement -> this.asInt as P
+        is String -> this.toIntOrNull() as? P
+        else -> null
+      }
+
+      Long::class -> when (this) {
+        is Long -> this as P
+        is Number -> this.toLong() as P
+        is JsonElement -> this.asLong as P
+        is String -> this.toLongOrNull() as? P
+        else -> null
+      }
+
+      Boolean::class -> when (this) {
+        is Boolean -> this as P
+        is JsonElement -> this.asBoolean as P
+        is String -> this.toBoolean() as P
+        else -> null
+      }
+
+      Double::class -> when (this) {
+        is Double -> this as P
+        is Number -> this.toDouble() as P
+        is JsonElement -> this.asDouble as P
+        is String -> this.toDoubleOrNull() as? P
+        else -> null
+      }
+
+      JsonElement::class -> when (this) {
+        is JsonElement -> this as P
+        else -> null
+      }
+
+      else -> this as? P
+    }
   }
 }

+ 1 - 0
gradle.properties

@@ -25,6 +25,7 @@ android.nonTransitiveRClass=true
 
 project.aio=debug
 project.aio-test=aio
+project.aio-prod=aio-test
 project.aio-CQ=aio-test
 
 project.pda=debug

+ 2 - 2
library/browser/src/main/assets/browser/bridge.js

@@ -13,9 +13,9 @@ class Bridge extends EventTarget {
     return crypto.randomUUID();
   }
 
-  static pulse(userId) {
+  static pulse(userId, cfg = {}) {
     const { promise, ...resolvers } = Promise.withResolvers();
-    this.getInstance().#postMessage('pulse', { userId }, resolvers);
+    this.getInstance().#postMessage('pulse', { ...cfg, userId }, resolvers);
     return promise;
   }
 

+ 14 - 0
library/device/pulse/src/main/java/com/hzliuzhi/applet/device/pulse/PulseEventHandler.kt

@@ -43,6 +43,12 @@ class PulseEventHandle(private val activity: Activity, scope: CoroutineScope) {
             }
           }
 
+          "${SharedFlowHub.WEBVIEW_BRIDGE_EVENT}:pulse:url:get" -> {
+            _event.cast<String, JsonElement>()?.also { event ->
+              handlePulseUrlById(event.payload, event.callback ?: {})
+            }
+          }
+
           else -> _event.callbackAs<Payload<Unit>>()?.invoke(Payload.error(message = "[pulse] 未实现 ${_event.type}"))
         }
       }
@@ -53,5 +59,13 @@ class PulseEventHandle(private val activity: Activity, scope: CoroutineScope) {
     if (pulse == null || pulse.userId.isNullOrEmpty()) Payload.error<PulseResult?>(message = "[pulse] 参数解析错误").also { callback(it) }
     else TaiYiUtil.start(activity, "six:${pulse.userId}", callback)
   }
+
+  private suspend fun handlePulseUrlById(id: String?, callback: (JsonElement) -> Unit) {
+    if (id.isNullOrBlank()) Payload.error<Unit>(message = "[pulse] 参数解析错误(measureId 为空)").toJson()?.also(callback)
+    else runCatching { TaiYiUtil.getReportUrl(id, activity) }
+      .map { Payload.data(it).toJson() }
+      .getOrElse { e -> Payload.error<Unit>(message = "[pulse] 获取报告URL失败: ${e.message}").toJson() }
+      ?.also(callback)
+  }
 }
 

+ 11 - 6
library/device/pulse/src/main/java/com/hzliuzhi/applet/device/pulse/util/TaiYiUtil.kt

@@ -2,14 +2,17 @@ package com.hzliuzhi.applet.device.pulse.util
 
 import android.app.Activity
 import android.app.Application
+import android.content.Context
 import android.content.res.Resources
 import com.hzliuzhi.applet.core.shared.Payload
 import com.hzliuzhi.applet.core.util.proxy
 import com.hzliuzhi.applet.device.pulse.PulseResult
 import com.hzliuzhi.applet.device.pulse.R
+import com.taiyi.tyusbsdk.pulse.ProxyManager
 import com.taiyi.tyusbsdk.pulse.TaiyiConfig
 import com.taiyi.tyusbsdk.pulse.TaiyiManager
 import com.taiyi.tyusbsdk.pulse.net.HttpImpl
+import com.taiyi.tyusbsdk.pulse.net.HttpUtil
 import com.taiyi.zhimai.ui.activity.MeasureMainActivity
 import kotlinx.coroutines.suspendCancellableCoroutine
 import kotlin.coroutines.resume
@@ -40,20 +43,22 @@ object TaiYiUtil {
     )
   }
 
-  fun getReportUrlSync(measureId: String) = TaiyiManager.getInstance().getUrl(measureId)
-  suspend fun getReportUrlUrl(measureId: String) {
-    suspendCancellableCoroutine { cont ->
+  private fun getReportUrlSync(measureId: String): String = TaiyiManager.getInstance().getUrl(measureId)
+  suspend fun getReportUrl(measureId: String, context: Context) = suspendCancellableCoroutine { continuation ->
+    if (HttpUtil.AuthorizationKey != "Authorization") continuation.resume(getReportUrlSync(measureId))
+    else run {
+      HttpUtil.getInstance().getRequestQueue(context.applicationContext)
       TaiyiManager.getInstance().getAsyncUrl(measureId, object : HttpImpl<String> {
         override fun showError(message: String?) {
-          cont.resumeWithException(Exception(message))
+          continuation.takeIf { it.isActive }?.resumeWithException(Exception(message))
         }
 
         override fun showResponse(url: String) {
-          cont.resume(url)
+          continuation.takeIf { it.isActive }?.resume(url)
         }
       })
     }
-  }
+  }.let { ProxyManager.getInstance().pool.entries.firstOrNull { (key, _) -> it.startsWith(key) }?.let { (key, value) -> "$value${it.removePrefix(key)}" } ?: it }
 }
 
 private fun Resources.OSSAgency(): TaiyiConfig.OSSAgency {