This commit is contained in:
parent
4ae7a9d676
commit
e33e64df14
|
|
@ -112,6 +112,22 @@
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
<div style="margin-bottom: 20px;">
|
||||||
|
<div style="font-size: 13px; font-weight: bold; color: #64748b; margin-bottom: 8px;">App 弹窗交互</div>
|
||||||
|
<div style="display: grid; grid-template-columns: 1fr 1fr; gap: 8px;">
|
||||||
|
<button class="btn" style="margin:0; padding:10px; font-size:13px; background: #e11d48;" onclick="triggerRestartDialog()">触发重启提示</button>
|
||||||
|
<button class="btn" style="margin:0; padding:10px; font-size:13px; background: #0d9488;" onclick="triggerJsonDialog()">发送 JSON 弹窗</button>
|
||||||
|
</div>
|
||||||
|
<div style="margin-top: 8px;">
|
||||||
|
<span class="ptt-label">提示标题:</span>
|
||||||
|
<input type="text" id="dialog-title" class="ws-input" value="系统通知" style="margin-bottom: 5px;">
|
||||||
|
<span class="ptt-label">提示内容:</span>
|
||||||
|
<input type="text" id="dialog-msg" class="ws-input" value="检测到配置更新,请重启应用以生效。">
|
||||||
|
<span class="ptt-label">JSON 数据:</span>
|
||||||
|
<textarea id="dialog-json" class="ws-input" rows="3" style="resize:vertical;">{"type":"alert","level":"warning","message":"测试消息","timestamp":1778505194}</textarea>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
<hr style="border: none; border-top: 1px solid #eee; margin: 20px 0;">
|
<hr style="border: none; border-top: 1px solid #eee; margin: 20px 0;">
|
||||||
|
|
||||||
<button class="btn" style="background: #475569;" onclick="callSystemPrint()">1. 调用系统标准打印 (A4)</button>
|
<button class="btn" style="background: #475569;" onclick="callSystemPrint()">1. 调用系统标准打印 (A4)</button>
|
||||||
|
|
@ -399,6 +415,31 @@
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function triggerRestartDialog() {
|
||||||
|
if (window.android && window.android.showRestartDialog) {
|
||||||
|
var msg = document.getElementById('dialog-msg').value;
|
||||||
|
var title = document.getElementById('dialog-title').value;
|
||||||
|
window.android.showRestartDialog(msg, title);
|
||||||
|
} else {
|
||||||
|
alert("showRestartDialog 接口不可用");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function triggerJsonDialog() {
|
||||||
|
if (window.android && window.android.showJsonDialog) {
|
||||||
|
var json = document.getElementById('dialog-json').value;
|
||||||
|
try {
|
||||||
|
JSON.parse(json);
|
||||||
|
} catch(e) {
|
||||||
|
alert("JSON 格式错误: " + e.message);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
window.android.showJsonDialog(json);
|
||||||
|
} else {
|
||||||
|
alert("showJsonDialog 接口不可用");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// 调用系统打印
|
// 调用系统打印
|
||||||
function callSystemPrint() {
|
function callSystemPrint() {
|
||||||
if (window.android && window.android.printPage) {
|
if (window.android && window.android.printPage) {
|
||||||
|
|
|
||||||
|
|
@ -81,6 +81,8 @@ class AndroidInterface(private val activity: MainActivity) {
|
||||||
"getMessages" -> getMessages()
|
"getMessages" -> getMessages()
|
||||||
"clearMessages" -> { clearMessages(); "" }
|
"clearMessages" -> { clearMessages(); "" }
|
||||||
"onPageFinished" -> { onPageFinished(); "" }
|
"onPageFinished" -> { onPageFinished(); "" }
|
||||||
|
"showRestartDialog" -> { showRestartDialog(args.optString(0, ""), args.optString(1, "提示")); "" }
|
||||||
|
"showJsonDialog" -> { showJsonDialog(args.optString(0, "")); "" }
|
||||||
else -> {
|
else -> {
|
||||||
Timber.tag("Bridge").w("Unknown method: $method")
|
Timber.tag("Bridge").w("Unknown method: $method")
|
||||||
""
|
""
|
||||||
|
|
@ -422,17 +424,44 @@ class AndroidInterface(private val activity: MainActivity) {
|
||||||
activity.cancelTimeoutTimer()
|
activity.cancelTimeoutTimer()
|
||||||
activity.findViewById<android.view.View>(R.id.loading_layout)?.visibility = android.view.View.GONE
|
activity.findViewById<android.view.View>(R.id.loading_layout)?.visibility = android.view.View.GONE
|
||||||
|
|
||||||
// 正式界面模式下,页面加载完成后传递 token 给前端
|
// 正式界面模式下,页面加载完成后传递登录数据给前端
|
||||||
val isOfficial = activity.isOfficialMode()
|
val isOfficial = activity.isOfficialMode()
|
||||||
val token = activity.getEssToken()
|
val data = activity.getLoginData()
|
||||||
Timber.tag("AndroidInterface").d("onPageFinished: isOfficial=$isOfficial, token=$token")
|
Timber.tag("AndroidInterface").d("onPageFinished: isOfficial=$isOfficial, data=$data")
|
||||||
if (isOfficial && token.isNotEmpty()) {
|
if (isOfficial && data.isNotEmpty()) {
|
||||||
com.stand.standapp.MainActivity.executeJs("if(window.setAppToken){window.setAppToken('$token');}")
|
val escaped = data.replace("'", "\\'").replace("\n", "\\n")
|
||||||
Timber.tag("AndroidInterface").d("Called setAppToken with token")
|
com.stand.standapp.MainActivity.executeJs("if(window.appLogin){window.appLogin('$escaped');}")
|
||||||
|
Timber.tag("AndroidInterface").d("Called appLogin")
|
||||||
}
|
}
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
Timber.tag("UI").e(e, "隐藏加载布局失败")
|
Timber.tag("UI").e(e, "隐藏加载布局失败")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun showRestartDialog(message: String, title: String) {
|
||||||
|
Handler(Looper.getMainLooper()).post {
|
||||||
|
com.stand.standapp.MainActivity.showPttUpdateDialog(message, title)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun showJsonDialog(jsonStr: String) {
|
||||||
|
Handler(Looper.getMainLooper()).post {
|
||||||
|
try {
|
||||||
|
val formatted = try {
|
||||||
|
val obj = org.json.JSONObject(jsonStr)
|
||||||
|
obj.toString(2)
|
||||||
|
} catch (_: Exception) {
|
||||||
|
jsonStr
|
||||||
|
}
|
||||||
|
androidx.appcompat.app.AlertDialog.Builder(activity)
|
||||||
|
.setTitle("JSON 数据")
|
||||||
|
.setMessage(formatted)
|
||||||
|
.setPositiveButton("确认", null)
|
||||||
|
.show()
|
||||||
|
} catch (e: Exception) {
|
||||||
|
Timber.tag("UI").e(e, "showJsonDialog error")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -151,9 +151,9 @@ class LoginActivity : AppCompatActivity() {
|
||||||
AppConfig.setSavedPassword(this@LoginActivity, "")
|
AppConfig.setSavedPassword(this@LoginActivity, "")
|
||||||
}
|
}
|
||||||
|
|
||||||
// 提取 token
|
// 提取登录数据
|
||||||
val data = json.optJSONObject("data")
|
val data = json.optJSONObject("data")
|
||||||
val essToken = data?.optString("access_token", "") ?: ""
|
val loginData = data?.toString() ?: "{}"
|
||||||
|
|
||||||
Toast.makeText(this@LoginActivity, "登录成功", Toast.LENGTH_SHORT).show()
|
Toast.makeText(this@LoginActivity, "登录成功", Toast.LENGTH_SHORT).show()
|
||||||
|
|
||||||
|
|
@ -187,10 +187,10 @@ class LoginActivity : AppCompatActivity() {
|
||||||
// 3. 初始化打印机 (异步)
|
// 3. 初始化打印机 (异步)
|
||||||
PrinterManager.init(this@LoginActivity)
|
PrinterManager.init(this@LoginActivity)
|
||||||
|
|
||||||
// 4. 启动 MainActivity,传递登录模式和 token
|
// 4. 启动 MainActivity,传递登录模式和数据
|
||||||
val intent = Intent(this@LoginActivity, MainActivity::class.java)
|
val intent = Intent(this@LoginActivity, MainActivity::class.java)
|
||||||
intent.putExtra("official_mode", officialMode)
|
intent.putExtra("official_mode", officialMode)
|
||||||
intent.putExtra("ess_token", essToken)
|
intent.putExtra("login_data", loginData)
|
||||||
startActivity(intent)
|
startActivity(intent)
|
||||||
finish()
|
finish()
|
||||||
} else {
|
} else {
|
||||||
|
|
|
||||||
|
|
@ -125,7 +125,7 @@ class MainActivity : AppCompatActivity() {
|
||||||
private lateinit var geckoView: GeckoView
|
private lateinit var geckoView: GeckoView
|
||||||
|
|
||||||
private var officialMode = false
|
private var officialMode = false
|
||||||
private var essToken = ""
|
private var loginData = ""
|
||||||
|
|
||||||
private val mHandler = android.os.Handler(android.os.Looper.getMainLooper())
|
private val mHandler = android.os.Handler(android.os.Looper.getMainLooper())
|
||||||
private val TIMEOUT_MS = 10000L
|
private val TIMEOUT_MS = 10000L
|
||||||
|
|
@ -139,9 +139,9 @@ class MainActivity : AppCompatActivity() {
|
||||||
instance = this
|
instance = this
|
||||||
showPendingDialogIfNeeded()
|
showPendingDialogIfNeeded()
|
||||||
|
|
||||||
// 读取登录模式和 token
|
// 读取登录模式和数据
|
||||||
officialMode = intent.getBooleanExtra("official_mode", false)
|
officialMode = intent.getBooleanExtra("official_mode", false)
|
||||||
essToken = intent.getStringExtra("ess_token") ?: ""
|
loginData = intent.getStringExtra("login_data") ?: ""
|
||||||
|
|
||||||
try {
|
try {
|
||||||
setContentView(R.layout.activity_main)
|
setContentView(R.layout.activity_main)
|
||||||
|
|
@ -248,12 +248,13 @@ class MainActivity : AppCompatActivity() {
|
||||||
|
|
||||||
override fun onPageStop(session: GeckoSession, success: Boolean) {
|
override fun onPageStop(session: GeckoSession, success: Boolean) {
|
||||||
Timber.tag("MainActivity").d("Page stopped, success: $success")
|
Timber.tag("MainActivity").d("Page stopped, success: $success")
|
||||||
// 正式界面模式下,页面加载完成后传递 token 给前端
|
// 正式界面模式下,页面加载完成后传递登录数据给前端
|
||||||
// if (success && officialMode && essToken.isNotEmpty()) {
|
// if (success && officialMode && loginData.isNotEmpty()) {
|
||||||
// // 延迟 500ms 确保 JS 上下文就绪
|
// // 延迟 500ms 确保 JS 上下文就绪
|
||||||
// mHandler.postDelayed({
|
// mHandler.postDelayed({
|
||||||
// executeJs("if(window.setAppToken){window.setAppToken('$essToken');}")
|
// val escaped = loginData.replace("'", "\\'").replace("\n", "\\n")
|
||||||
// Timber.tag("MainActivity").d("Called setAppToken via onPageStop")
|
// executeJs("if(window.appLogin){window.appLogin('$escaped');}")
|
||||||
|
// Timber.tag("MainActivity").d("Called appLogin with loginData")
|
||||||
// }, 500)
|
// }, 500)
|
||||||
// }
|
// }
|
||||||
}
|
}
|
||||||
|
|
@ -461,7 +462,7 @@ class MainActivity : AppCompatActivity() {
|
||||||
|
|
||||||
fun isOfficialMode(): Boolean = officialMode
|
fun isOfficialMode(): Boolean = officialMode
|
||||||
|
|
||||||
fun getEssToken(): String = essToken
|
fun getLoginData(): String = loginData
|
||||||
|
|
||||||
private fun showErrorPage() {
|
private fun showErrorPage() {
|
||||||
findViewById<android.view.View>(R.id.loading_layout)?.visibility = android.view.View.GONE
|
findViewById<android.view.View>(R.id.loading_layout)?.visibility = android.view.View.GONE
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue