This commit is contained in:
parent
09f00f8b72
commit
2adcdaf91f
|
|
@ -46,14 +46,18 @@ class AndroidInterface(private val activity: MainActivity) {
|
||||||
*/
|
*/
|
||||||
@JavascriptInterface
|
@JavascriptInterface
|
||||||
fun printRawData(data: String) {
|
fun printRawData(data: String) {
|
||||||
Log.d("Print", "收到 H5 原始打印数据: $data")
|
try {
|
||||||
Handler(Looper.getMainLooper()).post {
|
Log.d("Print", "收到 H5 原始打印数据: $data")
|
||||||
// 这里可以弹出一个选择框让用户选择打印机,或者根据配置自动连接
|
Handler(Looper.getMainLooper()).post {
|
||||||
// 常见的打印流程:1. 解析数据 -> 2. 转换成 ESC/POS 指令 -> 3. 发送到通道
|
try {
|
||||||
Toast.makeText(activity, "H5指令打印: $data", Toast.LENGTH_LONG).show()
|
// 这里可以弹出一个选择框让用户选择打印机,或者根据配置自动连接
|
||||||
|
Toast.makeText(activity, "H5指令打印: $data", Toast.LENGTH_SHORT).show()
|
||||||
// 预留不同通道的入口
|
} catch (e: Exception) {
|
||||||
// connectAndPrint(data)
|
Log.e("Print", "Toast显示失败", e)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (e: Exception) {
|
||||||
|
Log.e("Print", "指令解析失败", e)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -63,7 +67,11 @@ class AndroidInterface(private val activity: MainActivity) {
|
||||||
@JavascriptInterface
|
@JavascriptInterface
|
||||||
fun startUpdate(apkUrl: String) {
|
fun startUpdate(apkUrl: String) {
|
||||||
Handler(Looper.getMainLooper()).post {
|
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
|
@JavascriptInterface
|
||||||
fun exitApp() {
|
fun exitApp() {
|
||||||
Handler(Looper.getMainLooper()).post {
|
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
|
@JavascriptInterface
|
||||||
fun saveMessage(message: String) {
|
fun saveMessage(message: String) {
|
||||||
val prefs = activity.getSharedPreferences("msg_records", Context.MODE_PRIVATE)
|
try {
|
||||||
val currentMsgs = prefs.getString("history", "") ?: ""
|
val prefs = activity.getSharedPreferences("msg_records", Context.MODE_PRIVATE)
|
||||||
val updatedMsgs = if (currentMsgs.isEmpty()) message else "$message|---|$currentMsgs"
|
val currentMsgs = prefs.getString("history", "") ?: ""
|
||||||
// 限制保存最近5000条
|
val updatedMsgs = if (currentMsgs.isEmpty()) message else "$message|---|$currentMsgs"
|
||||||
val list = updatedMsgs.split("|---|").take(5000)
|
// 限制保存最近5000条
|
||||||
prefs.edit().putString("history", list.joinToString("|---|")).apply()
|
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
|
@JavascriptInterface
|
||||||
fun getMessages(): String {
|
fun getMessages(): String {
|
||||||
val prefs = activity.getSharedPreferences("msg_records", Context.MODE_PRIVATE)
|
return try {
|
||||||
return prefs.getString("history", "") ?: ""
|
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
|
@JavascriptInterface
|
||||||
fun clearMessages() {
|
fun clearMessages() {
|
||||||
activity.getSharedPreferences("msg_records", Context.MODE_PRIVATE).edit().clear().apply()
|
try {
|
||||||
Toast.makeText(activity, "消息记录已清空", Toast.LENGTH_SHORT).show()
|
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)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -11,7 +11,7 @@ object AppConfig {
|
||||||
private const val KEY_PASSWORD = "saved_password"
|
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 {
|
private fun getPrefs(context: Context): SharedPreferences {
|
||||||
return context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE)
|
return context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE)
|
||||||
|
|
|
||||||
|
|
@ -25,6 +25,8 @@ class MainActivity : AppCompatActivity() {
|
||||||
|
|
||||||
// 从配置中获取服务器地址
|
// 从配置中获取服务器地址
|
||||||
val serverUrl = AppConfig.getServerUrl(this)
|
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)
|
mAgentWeb = AgentWeb.with(this)
|
||||||
.setAgentWebParent(container, LinearLayout.LayoutParams(-1, -1))
|
.setAgentWebParent(container, LinearLayout.LayoutParams(-1, -1))
|
||||||
|
|
@ -32,7 +34,7 @@ class MainActivity : AppCompatActivity() {
|
||||||
.addJavascriptInterface("android", AndroidInterface(this))
|
.addJavascriptInterface("android", AndroidInterface(this))
|
||||||
.createAgentWeb()
|
.createAgentWeb()
|
||||||
.ready()
|
.ready()
|
||||||
.go("https://template.gyouzhe.com/example/print_demo.html?t=\${System.currentTimeMillis()}")
|
.go(finalUrl)
|
||||||
|
|
||||||
// 配置 WebView 属性以支持 SSE 和跨域
|
// 配置 WebView 属性以支持 SSE 和跨域
|
||||||
mAgentWeb?.let { agent ->
|
mAgentWeb?.let { agent ->
|
||||||
|
|
|
||||||
|
|
@ -56,7 +56,7 @@ class UpdateManager(private val context: Context) {
|
||||||
}
|
}
|
||||||
} catch (e: Exception) { 0 }
|
} 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 client = OkHttpClient()
|
||||||
val request = Request.Builder().url(url).build()
|
val request = Request.Builder().url(url).build()
|
||||||
|
|
@ -67,26 +67,45 @@ class UpdateManager(private val context: Context) {
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onResponse(call: Call, response: Response) {
|
override fun onResponse(call: Call, response: Response) {
|
||||||
val body = response.body?.string() ?: return
|
|
||||||
try {
|
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)
|
val json = JSONObject(body)
|
||||||
if (json.getInt("code") == 0) {
|
if (json.getInt("code") == 0) {
|
||||||
val data = json.optJSONObject("data")
|
val data = json.optJSONObject("data")
|
||||||
if (data != null) {
|
if (data != null) {
|
||||||
val newVersionCode = data.getInt("versionCode")
|
val newVersionCode = data.optInt("versionCode", 0)
|
||||||
if (newVersionCode > currentVersionCode) {
|
if (newVersionCode > currentVersionCode) {
|
||||||
val versionName = data.getString("versionName")
|
val versionName = data.optString("versionName", "New")
|
||||||
val content = data.getString("updateContent")
|
val content = data.optString("updateContent", "优化系统体验")
|
||||||
val id = data.getLong("id")
|
val id = data.optLong("id", -1)
|
||||||
|
|
||||||
(context as? AppCompatActivity)?.runOnUiThread {
|
if (id != -1L) {
|
||||||
showUpdateDialog(versionName, content, id)
|
(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) {
|
} 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) {
|
override fun onFailure(call: Call, e: IOException) {
|
||||||
handler.post {
|
handler.post {
|
||||||
progressDialog?.dismiss()
|
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) {
|
if (!response.isSuccessful) {
|
||||||
handler.post {
|
handler.post {
|
||||||
progressDialog?.dismiss()
|
progressDialog?.dismiss()
|
||||||
Toast.makeText(context, "下载失败: 错误码 ${response.code}", Toast.LENGTH_LONG).show()
|
Toast.makeText(context, "下载失败: ${response.code}", Toast.LENGTH_SHORT).show()
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
val body = response.body
|
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()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue