- sdk-log: AGP 9.x mappingFileProvider 适配、LogQueue optNullableString - sdk-core: 移除重复 getUserInfo() 函数 - sdk-webview: XuqmSDK.getUserInfo() → .userInfo 属性访问 Co-Authored-By: Claude <noreply@anthropic.com>
83 行
2.9 KiB
Kotlin
83 行
2.9 KiB
Kotlin
package com.xuqm.sdk.webview
|
|
|
|
import android.os.Build
|
|
import android.widget.Toast
|
|
import com.xuqm.sdk.XuqmSDK
|
|
import org.json.JSONObject
|
|
|
|
/**
|
|
* Standard JSBridge handlers for xuqm.* messages.
|
|
* These provide default implementations for hosts that don't use a custom protocol.
|
|
*
|
|
* Usage: register in XWebViewView's xwvMessageHandler or onMessage callback.
|
|
*/
|
|
object XWebViewStandardHandlers {
|
|
|
|
fun handle(message: String, webView: XWebViewController?): String {
|
|
val json = runCatching { JSONObject(message) }.getOrNull() ?: return errorResponse("invalid JSON")
|
|
val name = json.optString("name", "")
|
|
return when (name) {
|
|
"xuqm.getDeviceInfo" -> handleGetDeviceInfo()
|
|
"xuqm.getToken" -> handleGetToken()
|
|
"xuqm.getUserInfo" -> handleGetUserInfo()
|
|
"xuqm.closeWebView" -> handleCloseWebView(webView)
|
|
"xuqm.showToast" -> handleShowToast(json)
|
|
else -> errorResponse("unknown handler: $name")
|
|
}
|
|
}
|
|
|
|
private fun handleGetDeviceInfo(): String {
|
|
val data = JSONObject().apply {
|
|
put("platform", "android")
|
|
put("osVersion", Build.VERSION.RELEASE)
|
|
put("sdkVersion", Build.VERSION.SDK_INT)
|
|
put("manufacturer", Build.MANUFACTURER)
|
|
put("model", Build.MODEL)
|
|
put("brand", Build.BRAND)
|
|
}
|
|
return successResponse(data)
|
|
}
|
|
|
|
private fun handleGetToken(): String {
|
|
val token = XuqmSDK.currentLoginSession?.userSig ?: ""
|
|
val data = JSONObject().apply { put("token", token) }
|
|
return successResponse(data)
|
|
}
|
|
|
|
private fun handleGetUserInfo(): String {
|
|
val userInfo = XuqmSDK.userInfo
|
|
if (userInfo == null) return errorResponse("not logged in")
|
|
val data = JSONObject().apply {
|
|
put("userId", userInfo.userId ?: "")
|
|
put("nickname", userInfo.name ?: "")
|
|
put("avatar", userInfo.avatar ?: "")
|
|
put("phone", userInfo.phone ?: "")
|
|
}
|
|
return successResponse(data)
|
|
}
|
|
|
|
private fun handleCloseWebView(webView: XWebViewController?): String {
|
|
webView?.let { wv ->
|
|
if (wv.canGoBack()) wv.goBack() else wv.loadUrl("about:blank")
|
|
}
|
|
return successResponse(JSONObject())
|
|
}
|
|
|
|
private fun handleShowToast(json: JSONObject): String {
|
|
val message = json.optString("message", "")
|
|
if (message.isNotBlank()) {
|
|
val ctx = XuqmSDK.appContext
|
|
android.os.Handler(android.os.Looper.getMainLooper()).post {
|
|
Toast.makeText(ctx, message, Toast.LENGTH_SHORT).show()
|
|
}
|
|
}
|
|
return successResponse(JSONObject())
|
|
}
|
|
|
|
private fun successResponse(data: JSONObject): String =
|
|
JSONObject().apply { put("code", 0); put("data", data) }.toString()
|
|
|
|
private fun errorResponse(message: String): String =
|
|
JSONObject().apply { put("code", -1); put("message", message) }.toString()
|
|
}
|