From 2adcdaf91ff5ab642aff1caff7edc31296968f91 Mon Sep 17 00:00:00 2001 From: fengge <844143714@qq.com> Date: Sun, 22 Mar 2026 15:08:24 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/stand/standapp/AndroidInterface.kt | 67 +++++++++++++------ .../main/java/com/stand/standapp/AppConfig.kt | 2 +- .../java/com/stand/standapp/MainActivity.kt | 6 +- .../java/com/stand/standapp/UpdateManager.kt | 54 +++++++++++---- 4 files changed, 93 insertions(+), 36 deletions(-) diff --git a/StandAPP/app/src/main/java/com/stand/standapp/AndroidInterface.kt b/StandAPP/app/src/main/java/com/stand/standapp/AndroidInterface.kt index c0d9128..f79bf91 100644 --- a/StandAPP/app/src/main/java/com/stand/standapp/AndroidInterface.kt +++ b/StandAPP/app/src/main/java/com/stand/standapp/AndroidInterface.kt @@ -46,14 +46,18 @@ class AndroidInterface(private val activity: MainActivity) { */ @JavascriptInterface fun printRawData(data: String) { - Log.d("Print", "收到 H5 原始打印数据: $data") - Handler(Looper.getMainLooper()).post { - // 这里可以弹出一个选择框让用户选择打印机,或者根据配置自动连接 - // 常见的打印流程:1. 解析数据 -> 2. 转换成 ESC/POS 指令 -> 3. 发送到通道 - Toast.makeText(activity, "H5指令打印: $data", Toast.LENGTH_LONG).show() - - // 预留不同通道的入口 - // connectAndPrint(data) + try { + Log.d("Print", "收到 H5 原始打印数据: $data") + Handler(Looper.getMainLooper()).post { + try { + // 这里可以弹出一个选择框让用户选择打印机,或者根据配置自动连接 + Toast.makeText(activity, "H5指令打印: $data", Toast.LENGTH_SHORT).show() + } catch (e: Exception) { + Log.e("Print", "Toast显示失败", e) + } + } + } catch (e: Exception) { + Log.e("Print", "指令解析失败", e) } } @@ -63,7 +67,11 @@ class AndroidInterface(private val activity: MainActivity) { @JavascriptInterface fun startUpdate(apkUrl: String) { Handler(Looper.getMainLooper()).post { - UpdateManager(activity).checkUpdate() + try { + UpdateManager(activity).checkUpdate() + } catch (e: Exception) { + Log.e("Update", "启动更新检查失败", e) + } } } @@ -73,7 +81,12 @@ class AndroidInterface(private val activity: MainActivity) { @JavascriptInterface fun exitApp() { Handler(Looper.getMainLooper()).post { - activity.showExitDialog() + try { + activity.showExitDialog() + } catch (e: Exception) { + Log.e("App", "显示退出弹窗失败", e) + activity.finish() + } } } @@ -95,12 +108,16 @@ class AndroidInterface(private val activity: MainActivity) { */ @JavascriptInterface fun saveMessage(message: String) { - val prefs = activity.getSharedPreferences("msg_records", Context.MODE_PRIVATE) - val currentMsgs = prefs.getString("history", "") ?: "" - val updatedMsgs = if (currentMsgs.isEmpty()) message else "$message|---|$currentMsgs" - // 限制保存最近5000条 - val list = updatedMsgs.split("|---|").take(5000) - prefs.edit().putString("history", list.joinToString("|---|")).apply() + try { + val prefs = activity.getSharedPreferences("msg_records", Context.MODE_PRIVATE) + val currentMsgs = prefs.getString("history", "") ?: "" + val updatedMsgs = if (currentMsgs.isEmpty()) message else "$message|---|$currentMsgs" + // 限制保存最近5000条 + val list = updatedMsgs.split("|---|").take(5000) + prefs.edit().putString("history", list.joinToString("|---|")).apply() + } catch (e: Exception) { + Log.e("Storage", "消息保存异常", e) + } } /** @@ -108,8 +125,12 @@ class AndroidInterface(private val activity: MainActivity) { */ @JavascriptInterface fun getMessages(): String { - val prefs = activity.getSharedPreferences("msg_records", Context.MODE_PRIVATE) - return prefs.getString("history", "") ?: "" + return try { + val prefs = activity.getSharedPreferences("msg_records", Context.MODE_PRIVATE) + prefs.getString("history", "") ?: "" + } catch (e: Exception) { + "" + } } /** @@ -117,7 +138,13 @@ class AndroidInterface(private val activity: MainActivity) { */ @JavascriptInterface fun clearMessages() { - activity.getSharedPreferences("msg_records", Context.MODE_PRIVATE).edit().clear().apply() - Toast.makeText(activity, "消息记录已清空", Toast.LENGTH_SHORT).show() + try { + activity.getSharedPreferences("msg_records", Context.MODE_PRIVATE).edit().clear().apply() + Handler(Looper.getMainLooper()).post { + Toast.makeText(activity, "记录已清空", Toast.LENGTH_SHORT).show() + } + } catch (e: Exception) { + Log.e("Storage", "清空记录异常", e) + } } } diff --git a/StandAPP/app/src/main/java/com/stand/standapp/AppConfig.kt b/StandAPP/app/src/main/java/com/stand/standapp/AppConfig.kt index de1777d..62d2893 100644 --- a/StandAPP/app/src/main/java/com/stand/standapp/AppConfig.kt +++ b/StandAPP/app/src/main/java/com/stand/standapp/AppConfig.kt @@ -11,7 +11,7 @@ object AppConfig { private const val KEY_PASSWORD = "saved_password" // 默认服务器地址 - const val DEFAULT_SERVER_URL = "http://152.32.186.199:38080" + const val DEFAULT_SERVER_URL = "https://template.gyouzhe.com" private fun getPrefs(context: Context): SharedPreferences { return context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE) diff --git a/StandAPP/app/src/main/java/com/stand/standapp/MainActivity.kt b/StandAPP/app/src/main/java/com/stand/standapp/MainActivity.kt index 2bdb293..ab87b68 100644 --- a/StandAPP/app/src/main/java/com/stand/standapp/MainActivity.kt +++ b/StandAPP/app/src/main/java/com/stand/standapp/MainActivity.kt @@ -16,7 +16,7 @@ class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) - + try { setContentView(R.layout.activity_main) @@ -25,6 +25,8 @@ class MainActivity : AppCompatActivity() { // 从配置中获取服务器地址 val serverUrl = AppConfig.getServerUrl(this) + val finalUrl = "$serverUrl/example/print_demo.html?t=\${System.currentTimeMillis()}" + Log.d("MainActivity", "Loading URL: $finalUrl") mAgentWeb = AgentWeb.with(this) .setAgentWebParent(container, LinearLayout.LayoutParams(-1, -1)) @@ -32,7 +34,7 @@ class MainActivity : AppCompatActivity() { .addJavascriptInterface("android", AndroidInterface(this)) .createAgentWeb() .ready() - .go("https://template.gyouzhe.com/example/print_demo.html?t=\${System.currentTimeMillis()}") + .go(finalUrl) // 配置 WebView 属性以支持 SSE 和跨域 mAgentWeb?.let { agent -> diff --git a/StandAPP/app/src/main/java/com/stand/standapp/UpdateManager.kt b/StandAPP/app/src/main/java/com/stand/standapp/UpdateManager.kt index 19997ba..a9c8352 100644 --- a/StandAPP/app/src/main/java/com/stand/standapp/UpdateManager.kt +++ b/StandAPP/app/src/main/java/com/stand/standapp/UpdateManager.kt @@ -56,8 +56,8 @@ class UpdateManager(private val context: Context) { } } catch (e: Exception) { 0 } - val url = "$serverUrl/app-version/check-update?versionCode=$currentVersionCode" - + val url = "$serverUrl/api/app-version/check-update?versionCode=$currentVersionCode" + val client = OkHttpClient() val request = Request.Builder().url(url).build() @@ -67,26 +67,45 @@ class UpdateManager(private val context: Context) { } override fun onResponse(call: Call, response: Response) { - val body = response.body?.string() ?: return try { + val body = response.body?.string() ?: throw IOException("返回内容为空") + if (!response.isSuccessful) { + handler.post { Toast.makeText(context, "更新检查失败: ${response.code}", Toast.LENGTH_SHORT).show() } + return + } + val json = JSONObject(body) if (json.getInt("code") == 0) { val data = json.optJSONObject("data") if (data != null) { - val newVersionCode = data.getInt("versionCode") + val newVersionCode = data.optInt("versionCode", 0) if (newVersionCode > currentVersionCode) { - val versionName = data.getString("versionName") - val content = data.getString("updateContent") - val id = data.getLong("id") + val versionName = data.optString("versionName", "New") + val content = data.optString("updateContent", "优化系统体验") + val id = data.optLong("id", -1) - (context as? AppCompatActivity)?.runOnUiThread { - showUpdateDialog(versionName, content, id) + if (id != -1L) { + (context as? AppCompatActivity)?.runOnUiThread { + try { + showUpdateDialog(versionName, content, id) + } catch (e: Exception) { + Toast.makeText(context, "显示更新弹窗失败", Toast.LENGTH_SHORT).show() + } + } } + } else { + handler.post { Toast.makeText(context, "当前已是最新版本", Toast.LENGTH_SHORT).show() } } + } else { + handler.post { Toast.makeText(context, "当前已是最新版本", Toast.LENGTH_SHORT).show() } } + } else { + val msg = json.optString("msg", "未知错误") + handler.post { Toast.makeText(context, "更新服务提示: $msg", Toast.LENGTH_SHORT).show() } } } catch (e: Exception) { - Log.e("UpdateManager", "解析结果失败", e) + Log.e("UpdateManager", "解析或处理更新结果失败", e) + handler.post { Toast.makeText(context, "更新信息解析失败", Toast.LENGTH_SHORT).show() } } } }) @@ -131,7 +150,7 @@ class UpdateManager(private val context: Context) { override fun onFailure(call: Call, e: IOException) { handler.post { progressDialog?.dismiss() - Toast.makeText(context, "连接服务器失败: ${e.message}", Toast.LENGTH_LONG).show() + Toast.makeText(context, "连接服务器失败", Toast.LENGTH_SHORT).show() } } @@ -139,12 +158,21 @@ class UpdateManager(private val context: Context) { if (!response.isSuccessful) { handler.post { progressDialog?.dismiss() - Toast.makeText(context, "下载失败: 错误码 ${response.code}", Toast.LENGTH_LONG).show() + Toast.makeText(context, "下载失败: ${response.code}", Toast.LENGTH_SHORT).show() } return } val body = response.body - if (body != null) saveFileToPublic(body) + if (body != null) { + try { + saveFileToPublic(body) + } catch (e: Exception) { + handler.post { + progressDialog?.dismiss() + Toast.makeText(context, "保存失败", Toast.LENGTH_SHORT).show() + } + } + } } }) }