关于接口

This commit is contained in:
fengge 2026-03-25 17:49:27 +08:00
parent 2adcdaf91f
commit 4e3bab9474
3 changed files with 83 additions and 5 deletions

View File

@ -11,8 +11,8 @@ android {
applicationId = "com.stand.standapp" applicationId = "com.stand.standapp"
minSdk = 24 minSdk = 24
targetSdk = 34 targetSdk = 34
versionCode = 1 versionCode = 2
versionName = "1.0.0" versionName = "1.0.1"
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner" testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
} }

View File

@ -43,6 +43,19 @@
.log-item { margin-bottom: 4px; border-bottom: 1px solid #334155; padding-bottom: 2px; } .log-item { margin-bottom: 4px; border-bottom: 1px solid #334155; padding-bottom: 2px; }
.log-time { color: #94a3b8; margin-right: 5px; } .log-time { color: #94a3b8; margin-right: 5px; }
.log-msg { word-break: break-all; } .log-msg { word-break: break-all; }
/* Custom Modal Styles */
.modal-overlay {
display: none; position: fixed; top: 0; left: 0; width: 100%; height: 100%;
background: rgba(0,0,0,0.5); z-index: 1000; align-items: center; justify-content: center;
}
.modal-content {
background: white; padding: 20px; border-radius: 12px; width: 85%; max-width: 400px;
box-shadow: 0 10px 25px rgba(0,0,0,0.2);
}
.modal-header { font-weight: bold; font-size: 18px; margin-bottom: 15px; color: #004A8F; border-bottom: 1px solid #eee; padding-bottom: 10px; }
.modal-body { font-size: 14px; line-height: 1.8; color: #444; word-break: break-all; }
.modal-footer { margin-top: 20px; text-align: right; }
</style> </style>
</head> </head>
<body> <body>
@ -66,7 +79,8 @@
<p style="font-size: 12px; color: #666;">(适用于蓝牙/USB 小票机,传输原始 JSON)</p> <p style="font-size: 12px; color: #666;">(适用于蓝牙/USB 小票机,传输原始 JSON)</p>
<button class="btn btn-orange" onclick="callUpdate()">3. 检查系统更新</button> <button class="btn btn-orange" onclick="callUpdate()">3. 检查系统更新</button>
<button class="btn btn-red" onclick="callExit()">4. 退出应用</button> <button class="btn" style="background: #64748b;" onclick="callAbout()">4. 关于应用</button>
<button class="btn btn-red" onclick="callExit()">5. 退出应用</button>
<!-- WebSocket 控制区 --> <!-- WebSocket 控制区 -->
<div class="ws-card"> <div class="ws-card">
@ -177,6 +191,19 @@
Telewave SDK Connectivity v1.0 Telewave SDK Connectivity v1.0
</div> </div>
<!-- 自定义“关于”弹窗 -->
<div id="aboutModal" class="modal-overlay" onclick="closeAbout()">
<div class="modal-content" onclick="event.stopPropagation()">
<div class="modal-header">关于应用信息</div>
<div id="aboutBody" class="modal-body">
正在获取信息...
</div>
<div class="modal-footer">
<button class="btn" style="padding: 10px 20px; background: #64748b; margin: 0;" onclick="closeAbout()">确认</button>
</div>
</div>
</div>
<script> <script>
// 初始化界面 // 初始化界面
document.getElementById('current-time').innerText = new Date().toLocaleString(); document.getElementById('current-time').innerText = new Date().toLocaleString();
@ -239,6 +266,32 @@
} }
} }
function callAbout() {
if (window.android && window.android.getAboutInfo) {
const infoStr = window.android.getAboutInfo();
try {
const info = JSON.parse(infoStr);
const html = `
<strong>应用名称:</strong> ${info.appName}<br>
<strong>版本号:</strong> <span style="color: #3b82f6;">${info.versionName}</span><br>
<strong>设备型号:</strong> ${info.model}<br>
<strong>系统版本:</strong> Android ${info.osVersion}
`;
document.getElementById('aboutBody').innerHTML = html;
document.getElementById('aboutModal').style.display = 'flex';
} catch (e) {
document.getElementById('aboutBody').innerText = "获取信息解析失败: " + infoStr;
document.getElementById('aboutModal').style.display = 'flex';
}
} else {
alert("关于接口未就绪");
}
}
function closeAbout() {
document.getElementById('aboutModal').style.display = 'none';
}
// 安卓回调函数:退出确认时执行 // 安卓回调函数:退出确认时执行
function addExitText() { function addExitText() {
const tag = document.getElementById('inject-status'); const tag = document.getElementById('inject-status');
@ -258,6 +311,7 @@
let clientId = "WEB_" + Math.random().toString(36).substr(2, 9); let clientId = "WEB_" + Math.random().toString(36).substr(2, 9);
let heartbeatTimer = null; let heartbeatTimer = null;
let reconnectTimer = null; let reconnectTimer = null;
let isManualClose = false; // 新增:手动断开标记
const logList = document.getElementById('log-list'); const logList = document.getElementById('log-list');
document.getElementById('ws-client-id').innerText = clientId; document.getElementById('ws-client-id').innerText = clientId;
@ -301,6 +355,7 @@
const finalUrl = rawUrl.replace("{clientId}", clientId); const finalUrl = rawUrl.replace("{clientId}", clientId);
addLog("正在尝试连接: " + finalUrl, false); addLog("正在尝试连接: " + finalUrl, false);
isManualClose = false; // 启动连接时重置标记
try { try {
ws = new WebSocket(finalUrl); ws = new WebSocket(finalUrl);
@ -324,8 +379,12 @@
updateWSStatus(false); updateWSStatus(false);
addLog("连接关闭", false); addLog("连接关闭", false);
stopHeartbeat(); stopHeartbeat();
// 5秒后自动重连
reconnectTimer = setTimeout(connectWS, 5000); // 只有非手动断开(如网络异常)时才自动重连
if (!isManualClose) {
addLog("异常断开5秒后自动重连...", false);
reconnectTimer = setTimeout(connectWS, 5000);
}
}; };
ws.onerror = (err) => { ws.onerror = (err) => {
@ -339,6 +398,7 @@
function toggleWS() { function toggleWS() {
if (ws && ws.readyState === WebSocket.OPEN) { if (ws && ws.readyState === WebSocket.OPEN) {
isManualClose = true; // 标记为手动断开
clearTimeout(reconnectTimer); clearTimeout(reconnectTimer);
reconnectTimer = null; reconnectTimer = null;
ws.close(); ws.close();

View File

@ -103,6 +103,24 @@ class AndroidInterface(private val activity: MainActivity) {
} }
} }
/**
* JS 调用获取关于信息
*/
@JavascriptInterface
fun getAboutInfo(): String {
return try {
val packageInfo = activity.packageManager.getPackageInfo(activity.packageName, 0)
val json = org.json.JSONObject()
json.put("appName", activity.getString(R.string.app_name))
json.put("versionName", packageInfo.versionName)
json.put("model", android.os.Build.MODEL)
json.put("osVersion", android.os.Build.VERSION.RELEASE)
json.toString()
} catch (e: Exception) {
"{ \"appName\": \"StandApp\", \"versionName\": \"1.0.0\" }"
}
}
/** /**
* JS 调用保存消息记录 * JS 调用保存消息记录
*/ */