|
|
@@ -6,8 +6,28 @@ import android.webkit.JavascriptInterface
|
|
|
import androidx.annotation.UiThread
|
|
|
import com.adealink.frame.data.json.froJsonErrorNull
|
|
|
import com.adealink.frame.data.json.toJsonErrorNull
|
|
|
+import com.adealink.frame.locale.language.languageManager
|
|
|
import com.adealink.frame.log.Log
|
|
|
+import com.adealink.frame.network.http.interceptor.ACCEPT_LANGUAGE
|
|
|
+import com.adealink.frame.network.http.interceptor.KEY_API
|
|
|
+import com.adealink.frame.network.http.interceptor.KEY_CHANNEL
|
|
|
+import com.adealink.frame.network.http.interceptor.KEY_CONTENT_TYPE
|
|
|
+import com.adealink.frame.network.http.interceptor.KEY_DEVICE
|
|
|
+import com.adealink.frame.network.http.interceptor.KEY_NETWORK
|
|
|
+import com.adealink.frame.network.http.interceptor.KEY_PACKAGE_NAME
|
|
|
+import com.adealink.frame.network.http.interceptor.KEY_PLATFORM
|
|
|
+import com.adealink.frame.network.http.interceptor.KEY_REQ_ID
|
|
|
+import com.adealink.frame.network.http.interceptor.KEY_TIME
|
|
|
+import com.adealink.frame.network.http.interceptor.KEY_TOKEN
|
|
|
+import com.adealink.frame.network.http.interceptor.KEY_UDID
|
|
|
+import com.adealink.frame.network.http.interceptor.KEY_VERSION_CODE
|
|
|
+import com.adealink.frame.util.PackageUtil
|
|
|
+import com.adealink.frame.util.getDeviceName
|
|
|
+import com.adealink.frame.util.md5
|
|
|
import com.adealink.frame.util.runOnUiThread
|
|
|
+import com.adealink.weparty.App
|
|
|
+import com.adealink.weparty.BuildConfig
|
|
|
+import com.adealink.weparty.storage.AppPref
|
|
|
import com.adealink.weparty.webview.IWebView
|
|
|
import com.adealink.weparty.webview.constant.TAG_WEB_VIEW_JS_BRIDGE
|
|
|
import com.adealink.weparty.webview.jsbridge.callback.JSBridgeCallback
|
|
|
@@ -16,6 +36,7 @@ import com.adealink.weparty.webview.jsbridge.data.JSResponse
|
|
|
import com.adealink.weparty.webview.jsbridge.data.NativeMessage
|
|
|
import com.adealink.weparty.webview.jsbridge.manager.JSBridgeManager
|
|
|
import com.adealink.weparty.webview.jsbridge.method.JSNativeMethod
|
|
|
+import kotlin.random.Random
|
|
|
|
|
|
|
|
|
class JSBridgeImpl(val webView: IWebView, private val interfaceName: String) : JSBridge {
|
|
|
@@ -41,11 +62,13 @@ class JSBridgeImpl(val webView: IWebView, private val interfaceName: String) : J
|
|
|
methodMap.remove(methodName)
|
|
|
}
|
|
|
|
|
|
- override fun <T>notifyNativeMessage(message: NativeMessage<T>) {
|
|
|
+ override fun <T> notifyNativeMessage(message: NativeMessage<T>) {
|
|
|
val messageStr = toJsonErrorNull(message)
|
|
|
if (messageStr.isNullOrEmpty()) {
|
|
|
- Log.e(TAG_WEB_VIEW_JS_BRIDGE,
|
|
|
- "notifyNativeMessage, message is empty")
|
|
|
+ Log.e(
|
|
|
+ TAG_WEB_VIEW_JS_BRIDGE,
|
|
|
+ "notifyNativeMessage, message is empty"
|
|
|
+ )
|
|
|
return
|
|
|
}
|
|
|
val script = "javascript:window.notifyMessage('$messageStr')"
|
|
|
@@ -63,6 +86,29 @@ class JSBridgeImpl(val webView: IWebView, private val interfaceName: String) : J
|
|
|
runOnUiThread { handleJSMessage(json) }
|
|
|
}
|
|
|
|
|
|
+ @JavascriptInterface
|
|
|
+ fun requestHeader(): String {
|
|
|
+ val currentTs = System.currentTimeMillis()
|
|
|
+ val deviceName = getDeviceName()
|
|
|
+ val headers = mutableMapOf<String, String>()
|
|
|
+ headers[KEY_TOKEN] = AppPref.token
|
|
|
+ headers[KEY_REQ_ID] = "$currentTs/${Random(100).nextInt()}/${deviceName}".md5()
|
|
|
+ headers[KEY_UDID] = App.instance.deviceIdService.getMemOldDeviceId()
|
|
|
+ headers[KEY_PACKAGE_NAME] = PackageUtil.getPackageName()
|
|
|
+ headers[KEY_DEVICE] = deviceName
|
|
|
+ headers[KEY_PLATFORM] = "1"
|
|
|
+ headers[KEY_CHANNEL] = "official"
|
|
|
+ headers[KEY_API] = "1"
|
|
|
+ headers[KEY_VERSION_CODE] = BuildConfig.VERSION_CODE.toString()
|
|
|
+ headers[KEY_NETWORK] = "5g"
|
|
|
+ headers[KEY_TIME] = currentTs.toString()
|
|
|
+ headers[KEY_CONTENT_TYPE] = "application/json"
|
|
|
+ headers[ACCEPT_LANGUAGE] = languageManager?.getLanguageCode() ?: ""
|
|
|
+ return (toJsonErrorNull(headers) ?: "").also {
|
|
|
+ Log.d("zhangfei", "requestHeader, header:$it")
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
@UiThread
|
|
|
private fun handleJSMessage(json: String?) {
|
|
|
Log.d(TAG_WEB_VIEW_JS_BRIDGE, "handleJSMessage, json:${json}")
|
|
|
@@ -139,15 +185,19 @@ class JSBridgeImpl(val webView: IWebView, private val interfaceName: String) : J
|
|
|
|
|
|
private fun sendResponseToJS(response: JSResponse<Any>) {
|
|
|
if (TextUtils.isEmpty(callbackId)) {
|
|
|
- Log.e(TAG_WEB_VIEW_JS_BRIDGE,
|
|
|
- "sendResponseToJS, methodName:${methodName}, response:${response}, callbackId is empty")
|
|
|
+ Log.e(
|
|
|
+ TAG_WEB_VIEW_JS_BRIDGE,
|
|
|
+ "sendResponseToJS, methodName:${methodName}, response:${response}, callbackId is empty"
|
|
|
+ )
|
|
|
return
|
|
|
}
|
|
|
|
|
|
val responseStr = toJsonErrorNull(response)
|
|
|
if (responseStr.isNullOrEmpty()) {
|
|
|
- Log.e(TAG_WEB_VIEW_JS_BRIDGE,
|
|
|
- "sendResponseToJS, methodName:${methodName}, response:${response}, to json error")
|
|
|
+ Log.e(
|
|
|
+ TAG_WEB_VIEW_JS_BRIDGE,
|
|
|
+ "sendResponseToJS, methodName:${methodName}, response:${response}, to json error"
|
|
|
+ )
|
|
|
return
|
|
|
}
|
|
|
|
|
|
@@ -163,14 +213,18 @@ class JSBridgeImpl(val webView: IWebView, private val interfaceName: String) : J
|
|
|
}
|
|
|
|
|
|
override fun resolve(data: Any) {
|
|
|
- Log.d(TAG_WEB_VIEW_JS_BRIDGE,
|
|
|
- "resolve, methodName:${methodName}, callbackId:${callbackId}, data:${data}")
|
|
|
+ Log.d(
|
|
|
+ TAG_WEB_VIEW_JS_BRIDGE,
|
|
|
+ "resolve, methodName:${methodName}, callbackId:${callbackId}, data:${data}"
|
|
|
+ )
|
|
|
sendResponseToJS(JSResponse(callbackId = this@CallbackBridge.callbackId, data = data))
|
|
|
}
|
|
|
|
|
|
override fun reject(error: JSResponse.JSError) {
|
|
|
- Log.e(TAG_WEB_VIEW_JS_BRIDGE,
|
|
|
- "reject, methodName:${methodName}, callbackId:${callbackId}, error:${error.message}")
|
|
|
+ Log.e(
|
|
|
+ TAG_WEB_VIEW_JS_BRIDGE,
|
|
|
+ "reject, methodName:${methodName}, callbackId:${callbackId}, error:${error.message}"
|
|
|
+ )
|
|
|
sendResponseToJS(JSResponse(error.code, error.message, this@CallbackBridge.callbackId))
|
|
|
}
|
|
|
|
|
|
@@ -184,17 +238,21 @@ class JSBridgeImpl(val webView: IWebView, private val interfaceName: String) : J
|
|
|
|
|
|
}
|
|
|
|
|
|
- inner class BridgeSupportJSNativeMethod: JSNativeMethod<Map<String,List<String>>, Map<String,Boolean>> {
|
|
|
+ inner class BridgeSupportJSNativeMethod :
|
|
|
+ JSNativeMethod<Map<String, List<String>>, Map<String, Boolean>> {
|
|
|
override val methodName: String = "isBridgeSupport"
|
|
|
|
|
|
- override fun handleMethodCall(data: Map<String,List<String>>, callback: JSBridgeCallback<Map<String,Boolean>>?) {
|
|
|
+ override fun handleMethodCall(
|
|
|
+ data: Map<String, List<String>>,
|
|
|
+ callback: JSBridgeCallback<Map<String, Boolean>>?
|
|
|
+ ) {
|
|
|
val jsNativeMethods = data["data"]
|
|
|
|
|
|
- if(jsNativeMethods == null) {
|
|
|
+ if (jsNativeMethods == null) {
|
|
|
callback?.reject(JSResponse.JSError.CLIENT_HANDLE_REQUEST_DATA_TYPE_ERROR)
|
|
|
return
|
|
|
}
|
|
|
- val result = jsNativeMethods.associateWith { key -> methodMap.containsKey(key) }
|
|
|
+ val result = jsNativeMethods.associateWith { key -> methodMap.containsKey(key) }
|
|
|
callback?.resolve(result)
|
|
|
}
|
|
|
}
|