From 803fcb38d250e5a10d4fc83a3d06a3d0ffb72a5c Mon Sep 17 00:00:00 2001
From: "844143714@qq,com" <844143714@qq,com>
Date: Sun, 26 Apr 2026 20:38:42 +0800
Subject: [PATCH] ptt
---
app/src/main/assets/print_demo.html | 216 ++++++++----------
.../kingway/ptt/CallBackResolution.java | 5 +
.../com/stand/standapp/AndroidInterface.kt | 69 ++++++
.../java/com/stand/standapp/LoginActivity.kt | 12 +-
.../java/com/stand/standapp/MainActivity.kt | 23 +-
.../stand/standapp/printer/PrinterManager.kt | 17 +-
6 files changed, 212 insertions(+), 130 deletions(-)
diff --git a/app/src/main/assets/print_demo.html b/app/src/main/assets/print_demo.html
index ffbb7ba..572cf1f 100644
--- a/app/src/main/assets/print_demo.html
+++ b/app/src/main/assets/print_demo.html
@@ -157,49 +157,35 @@
- PTT 业务接口测试
+ PTT 业务功能测试
+ PTT 未登录
+
+
+
+
+
-
-
-
-
-
-
-
-
-
-
-
-
+
+
+
+
-
PTT 接口响应将在此展示...
+
等待 PTT 登录成功后激活功能...
@@ -637,121 +623,109 @@
setTimeout(loadHistory, 500);
- // === PTT 接口测试逻辑 ===
- var PTT_BASE = "http://127.0.0.1:8080/api/external/ptt";
+ // === PTT 业务测试逻辑 ===
+ var pttBtn = document.getElementById('ptt-speak-btn');
+ var pttStatusTag = document.getElementById('ptt-status-tag');
+ var pttResult = document.getElementById('ptt-result');
function showPttResult(msg) {
- var container = document.getElementById('ptt-result');
var time = new Date().toLocaleTimeString();
var item = document.createElement('div');
item.className = 'log-item';
- var displayMsg = typeof msg === 'string' ? msg : JSON.stringify(msg, null, 2);
- item.innerHTML = '[' + time + ']' + displayMsg + '';
- container.insertBefore(item, container.firstChild);
+ item.innerHTML = '[' + time + ']' + msg + '';
+ pttResult.insertBefore(item, pttResult.firstChild);
}
- // 移除 async/await,使用 Promise
- function pttRequest(path, method, query, body) {
- if (method === undefined) method = 'GET';
- if (query === undefined) query = {};
- if (body === undefined) body = null;
-
- var url = PTT_BASE + path;
- var qParams = [];
- for (var key in query) {
- qParams.push(encodeURIComponent(key) + "=" + encodeURIComponent(query[key]));
+ // PTT 登录成功回调 (由 Android 调用)
+ function onPttLoginSuccess() {
+ pttStatusTag.innerText = "PTT 已登录";
+ pttStatusTag.className = "ws-status ws-connected";
+
+ // 激活按钮
+ pttBtn.disabled = false;
+ pttBtn.style.background = "#ef4444";
+ pttBtn.style.cursor = "pointer";
+
+ var actionBtns = document.querySelectorAll('.ptt-action-btn');
+ for (var i = 0; i < actionBtns.length; i++) {
+ actionBtns[i].disabled = false;
}
- if (qParams.length > 0) url += "?" + qParams.join("&");
+
+ showPttResult("PTT 业务初始化成功,对讲功能已就绪");
+ }
- var options = {
- method: method,
- headers: { 'Content-Type': 'application/json' }
- };
- if (body) options.body = JSON.stringify(body);
+ // 修复 PTT 按键逻辑:支持 TouchEvents 并处理异常中断
+ if (pttBtn) {
+ var isDown = false;
- fetch(url, options)
- .then(function(response) {
- if (path === '/record/play') {
- showPttResult("流式播放接口已调用,请查看网络请求或直接点击预览链接");
- return;
+ var startSpeak = function(e) {
+ if (pttBtn.disabled) return;
+ e.preventDefault();
+ if (!isDown) {
+ isDown = true;
+ pttBtn.style.background = "#b91c1c";
+ pttBtn.innerText = "正在通话...";
+ if (window.android && window.android.pttSpeakPlay) {
+ window.android.pttSpeakPlay();
}
- return response.json();
- })
- .then(function(data) {
- if (data) showPttResult(data);
- })
- .catch(function(e) {
- showPttResult("请求失败: " + e.message);
- });
- }
+ showPttResult("申请话权...");
+ }
+ };
- function pttCreateGroup() {
- var name = document.getElementById('ptt-name').value;
- var account = document.getElementById('ptt-account').value;
- pttRequest('/group', 'POST', { gname: name }, [account]);
- }
+ var stopSpeak = function(e) {
+ if (isDown) {
+ isDown = false;
+ pttBtn.style.background = "#ef4444";
+ pttBtn.innerText = "长按对讲 (PTT)";
+ if (window.android && window.android.pttSpeakRelease) {
+ window.android.pttSpeakRelease();
+ }
+ showPttResult("释放话权");
+ }
+ };
- function pttDeleteGroup() {
- var gid = document.getElementById('ptt-gid').value;
- pttRequest('/group/' + gid, 'DELETE');
- }
-
- function pttAddMembers() {
- var gid = document.getElementById('ptt-gid').value;
- var account = document.getElementById('ptt-account').value;
- pttRequest('/group/' + gid + '/members', 'POST', {}, [account]);
- }
-
- function pttKickMembers() {
- var gid = document.getElementById('ptt-gid').value;
- var account = document.getElementById('ptt-account').value;
- pttRequest('/group/' + gid + '/members', 'DELETE', {}, [account]);
+ // 绑定事件
+ pttBtn.addEventListener('touchstart', startSpeak);
+ pttBtn.addEventListener('mousedown', startSpeak);
+
+ // 关键:除了 end/up,还要处理 cancel 和 leave
+ pttBtn.addEventListener('touchend', stopSpeak);
+ pttBtn.addEventListener('touchcancel', stopSpeak);
+ pttBtn.addEventListener('mouseup', stopSpeak);
+ pttBtn.addEventListener('mouseleave', stopSpeak);
}
function pttGetGroups() {
- pttRequest('/groups', 'GET', { page: 1, limit: 10 });
+ if (window.android && window.android.pttGetGroupInfo) {
+ window.android.pttGetGroupInfo();
+ showPttResult("已请求群组列表");
+ }
}
- function pttGetTempGroups() {
- pttRequest('/temp-groups', 'GET', { page: 1, limit: 10 });
- }
-
- function pttGetRecords() {
+ function pttGetMembers() {
var gid = document.getElementById('ptt-gid').value;
- var now = new Date();
- var end = now.getFullYear() + '-' + (now.getMonth() + 1) + '-' + now.getDate() + ' ' + now.getHours() + ':' + now.getMinutes() + ':' + now.getSeconds();
- var start = "2024-01-01 00:00:00";
- pttRequest('/group/' + gid + '/records', 'POST', { start: start, end: end, page: 1, limit: 5 }, []);
+ if (window.android && window.android.pttGetGroupMemberInfo) {
+ window.android.pttGetGroupMemberInfo(gid);
+ showPttResult("已请求群组 [" + gid + "] 成员");
+ }
}
- function pttGetRecordText() {
- var path = document.getElementById('ptt-record-path').value;
- if (!path) return alert("请输入录音路径");
- pttRequest('/record/text', 'GET', { path: path });
- }
-
- function pttPlayRecord() {
- var path = document.getElementById('ptt-record-path').value;
- if (!path) return alert("请输入录音路径");
- var url = PTT_BASE + '/record/play?path=' + encodeURIComponent(path);
- window.open(url, '_blank');
- showPttResult("已尝试打开播放链接: " + url);
- }
-
- function pttCreateTempGroup() {
- var name = document.getElementById('ptt-name').value;
- var account = document.getElementById('ptt-account').value;
- pttRequest('/temp-group', 'POST', { gname: name, creatorid: 1001 }, [account]);
- }
-
- function pttDeleteTempGroup() {
+ function pttJumpGroup() {
var gid = document.getElementById('ptt-gid').value;
- pttRequest('/temp-group/' + gid, 'DELETE');
+ if (window.android && window.android.pttJumpGroup) {
+ window.android.pttJumpGroup(gid);
+ showPttResult("尝试切换至群组: " + gid);
+ }
}
- function pttGetTempMembers() {
- var gid = document.getElementById('ptt-gid').value;
- pttRequest('/temp-group/' + gid + '/members', 'GET');
+ function pttForceRelease() {
+ if (window.android && window.android.pttForceRelease) {
+ window.android.pttForceRelease();
+ isDown = false;
+ pttBtn.style.background = "#ef4444";
+ pttBtn.innerText = "长按对讲 (PTT)";
+ showPttResult("执行强制释放");
+ }
}