test_jsbridge.html 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  1. <!doctype html>
  2. <html lang="zh-CN">
  3. <head>
  4. <meta charset="UTF-8" />
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  6. <title>FlashLink JS Bridge 测试</title>
  7. <style>
  8. * {
  9. margin: 0;
  10. padding: 0;
  11. box-sizing: border-box;
  12. }
  13. body {
  14. font-family:
  15. -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto,
  16. "Helvetica Neue", Arial, sans-serif;
  17. background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  18. color: #333;
  19. /* 使用原生属性设置安全边距 */
  20. padding-top: calc(var(--status-bar-height, 0px) + 10px);
  21. padding-bottom: calc(var(--bottom-bar-height, 0px) + 10px);
  22. min-height: 100vh;
  23. }
  24. .container {
  25. max-width: 600px;
  26. margin: 0 auto;
  27. padding: 15px;
  28. }
  29. .header {
  30. background: white;
  31. border-radius: 15px;
  32. padding: 20px;
  33. margin-bottom: 15px;
  34. box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
  35. }
  36. .header h1 {
  37. font-size: 24px;
  38. margin-bottom: 15px;
  39. color: #667eea;
  40. }
  41. .info-grid {
  42. display: grid;
  43. grid-template-columns: 1fr 1fr;
  44. gap: 10px;
  45. font-size: 13px;
  46. }
  47. .info-item {
  48. background: #f8f9fa;
  49. padding: 10px;
  50. border-radius: 8px;
  51. }
  52. .info-label {
  53. color: #666;
  54. font-size: 11px;
  55. margin-bottom: 5px;
  56. }
  57. .info-value {
  58. color: #333;
  59. font-weight: 600;
  60. word-break: break-all;
  61. }
  62. .card {
  63. background: white;
  64. border-radius: 15px;
  65. padding: 20px;
  66. margin-bottom: 15px;
  67. box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
  68. }
  69. .card h2 {
  70. font-size: 18px;
  71. margin-bottom: 15px;
  72. color: #667eea;
  73. }
  74. .vpn-status {
  75. display: flex;
  76. justify-content: space-between;
  77. align-items: center;
  78. padding: 15px;
  79. background: #f8f9fa;
  80. border-radius: 10px;
  81. margin-bottom: 15px;
  82. }
  83. .status-indicator {
  84. display: flex;
  85. align-items: center;
  86. gap: 10px;
  87. }
  88. .status-dot {
  89. width: 12px;
  90. height: 12px;
  91. border-radius: 50%;
  92. background-color: #ccc;
  93. animation: pulse 2s infinite;
  94. }
  95. .status-dot.connected {
  96. background-color: #10b981;
  97. }
  98. .status-dot.connecting {
  99. background-color: #f59e0b;
  100. }
  101. .status-dot.disconnected {
  102. background-color: #ef4444;
  103. }
  104. @keyframes pulse {
  105. 0%,
  106. 100% {
  107. opacity: 1;
  108. }
  109. 50% {
  110. opacity: 0.5;
  111. }
  112. }
  113. .btn-grid {
  114. display: grid;
  115. grid-template-columns: 1fr 1fr;
  116. gap: 10px;
  117. }
  118. .btn {
  119. padding: 12px 20px;
  120. border: none;
  121. border-radius: 10px;
  122. background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  123. color: white;
  124. font-size: 14px;
  125. font-weight: 600;
  126. cursor: pointer;
  127. transition:
  128. transform 0.2s,
  129. box-shadow 0.2s;
  130. }
  131. .btn:active {
  132. transform: scale(0.95);
  133. }
  134. .btn-full {
  135. grid-column: 1 / -1;
  136. }
  137. .btn-danger {
  138. background: linear-gradient(135deg, #ef4444 0%, #dc2626 100%);
  139. }
  140. .btn-success {
  141. background: linear-gradient(135deg, #10b981 0%, #059669 100%);
  142. }
  143. .log {
  144. background: #1e293b;
  145. color: #94a3b8;
  146. border-radius: 10px;
  147. padding: 15px;
  148. max-height: 300px;
  149. overflow-y: auto;
  150. font-size: 12px;
  151. font-family: "Courier New", monospace;
  152. }
  153. .log-item {
  154. margin-bottom: 8px;
  155. line-height: 1.5;
  156. }
  157. .log-time {
  158. color: #64748b;
  159. }
  160. .log-success {
  161. color: #10b981;
  162. }
  163. .log-error {
  164. color: #ef4444;
  165. }
  166. .log-info {
  167. color: #3b82f6;
  168. }
  169. </style>
  170. </head>
  171. <body>
  172. <div class="container">
  173. <!-- 设备信息 -->
  174. <div class="header">
  175. <h1>🚀 FlashLink JS Bridge</h1>
  176. <div class="info-grid">
  177. <div class="info-item">
  178. <div class="info-label">状态栏高度</div>
  179. <div class="info-value" id="statusBarHeight">-</div>
  180. </div>
  181. <div class="info-item">
  182. <div class="info-label">底部安全区</div>
  183. <div class="info-value" id="bottomBarHeight">-</div>
  184. </div>
  185. <div class="info-item">
  186. <div class="info-label">平台</div>
  187. <div class="info-value" id="platform">-</div>
  188. </div>
  189. <div class="info-item">
  190. <div class="info-label">版本</div>
  191. <div class="info-value" id="version">-</div>
  192. </div>
  193. </div>
  194. </div>
  195. <!-- VPN 控制 -->
  196. <div class="card">
  197. <h2>🔐 VPN 控制</h2>
  198. <div class="vpn-status">
  199. <div class="status-indicator">
  200. <div class="status-dot" id="statusDot"></div>
  201. <div>
  202. <div style="font-weight: 600" id="vpnStatus">未知</div>
  203. <div style="font-size: 12px; color: #666" id="vpnTimer">
  204. 00:00:00
  205. </div>
  206. </div>
  207. </div>
  208. </div>
  209. <div class="btn-grid">
  210. <button class="btn btn-success" onclick="handleConnectVPN()">
  211. ⚡ 连接
  212. </button>
  213. <button class="btn btn-danger" onclick="handleDisconnectVPN()">
  214. ⏸️ 断开
  215. </button>
  216. <button class="btn btn-full" onclick="handleGetVPNStatus()">
  217. 🔍 查询状态
  218. </button>
  219. </div>
  220. </div>
  221. <!-- 其他功能 -->
  222. <div class="card">
  223. <h2>🛠️ 其他功能</h2>
  224. <div class="btn-grid">
  225. <button class="btn" onclick="handleSetStatusBarColor()">
  226. 🎨 切换状态栏
  227. </button>
  228. <button class="btn" onclick="handleOpenApp()">📱 打开微信</button>
  229. <button class="btn btn-danger btn-full" onclick="handleExit()">
  230. ❌ 退出 WebView
  231. </button>
  232. </div>
  233. </div>
  234. <!-- 日志 -->
  235. <div class="card">
  236. <h2>📋 日志</h2>
  237. <div class="log" id="logContainer"></div>
  238. </div>
  239. </div>
  240. <script>
  241. let logCount = 0;
  242. const maxLogs = 50;
  243. // 日志函数
  244. function addLog(message, type = "info") {
  245. const logContainer = document.getElementById("logContainer");
  246. const time = new Date().toLocaleTimeString("zh-CN", { hour12: false });
  247. const logClass = `log-${type}`;
  248. const logItem = document.createElement("div");
  249. logItem.className = "log-item";
  250. logItem.innerHTML = `<span class="log-time">[${time}]</span> <span class="${logClass}">${message}</span>`;
  251. logContainer.insertBefore(logItem, logContainer.firstChild);
  252. logCount++;
  253. if (logCount > maxLogs) {
  254. logContainer.removeChild(logContainer.lastChild);
  255. logCount--;
  256. }
  257. }
  258. // 更新 VPN 状态显示
  259. function updateVPNStatusUI(status) {
  260. const statusDot = document.getElementById("statusDot");
  261. const statusText = document.getElementById("vpnStatus");
  262. statusDot.className = "status-dot " + status;
  263. const statusMap = {
  264. connected: "已连接",
  265. connecting: "连接中",
  266. disconnected: "未连接",
  267. unknown: "未知",
  268. };
  269. statusText.textContent = statusMap[status] || status;
  270. }
  271. // 页面加载完成后初始化
  272. window.onload = function () {
  273. addLog("页面加载完成,等待原生注入...", "info");
  274. setTimeout(initNativeAttrs, 200);
  275. };
  276. // 初始化原生属性
  277. function initNativeAttrs() {
  278. if (window.nativeAttrs) {
  279. document.getElementById("statusBarHeight").textContent =
  280. window.nativeAttrs.statusBarHeight + "px";
  281. document.getElementById("bottomBarHeight").textContent =
  282. window.nativeAttrs.bottomBarHeight + "px";
  283. document.getElementById("platform").textContent =
  284. window.nativeAttrs.platform;
  285. document.getElementById("version").textContent =
  286. window.nativeAttrs.version;
  287. // 设置 CSS 变量
  288. document.documentElement.style.setProperty(
  289. "--status-bar-height",
  290. window.nativeAttrs.statusBarHeight + "px",
  291. );
  292. document.documentElement.style.setProperty(
  293. "--bottom-bar-height",
  294. window.nativeAttrs.bottomBarHeight + "px",
  295. );
  296. addLog("✅ 原生属性注入成功", "success");
  297. addLog(
  298. `📱 平台: ${window.nativeAttrs.platform}, 版本: ${window.nativeAttrs.version}`,
  299. "info",
  300. );
  301. // 初始化 VPN 状态
  302. handleGetVPNStatus();
  303. } else {
  304. addLog("❌ 原生属性注入失败", "error");
  305. }
  306. }
  307. // 设置 VPN 状态回调
  308. window.nativeCallbacks = {
  309. onVPNStatusChanged: function (status) {
  310. addLog(`📡 VPN 状态变化: ${status}`, "info");
  311. updateVPNStatusUI(status);
  312. },
  313. onVPNConnected: function () {
  314. addLog("✅ VPN 连接成功!", "success");
  315. updateVPNStatusUI("connected");
  316. },
  317. onVPNConnecting: function () {
  318. addLog("⏳ VPN 连接中...", "info");
  319. updateVPNStatusUI("connecting");
  320. },
  321. onVPNDisconnected: function () {
  322. addLog("⏸️ VPN 已断开", "info");
  323. updateVPNStatusUI("disconnected");
  324. },
  325. };
  326. // 功能函数
  327. async function handleConnectVPN() {
  328. try {
  329. addLog("🚀 正在连接 VPN...", "info");
  330. const result = await window.native.connectVPN(26, "ca");
  331. if (result.success) {
  332. addLog("✅ VPN 连接请求已发送", "success");
  333. } else {
  334. addLog(`❌ 连接失败: ${result.error}`, "error");
  335. }
  336. } catch (error) {
  337. addLog(`❌ 发生错误: ${error}`, "error");
  338. }
  339. }
  340. async function handleDisconnectVPN() {
  341. try {
  342. addLog("⏸️ 正在断开 VPN...", "info");
  343. const result = await window.native.disconnectVPN();
  344. if (result.success) {
  345. addLog("✅ VPN 断开请求已发送", "success");
  346. } else {
  347. addLog(`❌ 断开失败: ${result.error}`, "error");
  348. }
  349. } catch (error) {
  350. addLog(`❌ 发生错误: ${error}`, "error");
  351. }
  352. }
  353. async function handleGetVPNStatus() {
  354. try {
  355. const result = await window.native.getVPNStatus();
  356. if (result.success) {
  357. const status = result.data.status;
  358. const timer = result.data.timer;
  359. document.getElementById("vpnTimer").textContent = timer;
  360. updateVPNStatusUI(status);
  361. addLog(`📊 VPN 状态: ${status}, 时长: ${timer}`, "info");
  362. } else {
  363. addLog(`❌ 查询失败: ${result.error}`, "error");
  364. }
  365. } catch (error) {
  366. addLog(`❌ 发生错误: ${error}`, "error");
  367. }
  368. }
  369. async function handleSetStatusBarColor() {
  370. // 只支持 dark 和 light 两种模式
  371. const currentMode = document.body.dataset.statusBarMode || "light";
  372. const newMode = currentMode === "dark" ? "light" : "dark";
  373. try {
  374. addLog(`🎨 切换状态栏模式: ${currentMode} -> ${newMode}`, "info");
  375. const result = await window.native.setStatusBarColor(newMode);
  376. if (result.success) {
  377. document.body.dataset.statusBarMode = newMode;
  378. addLog(
  379. `✅ 状态栏模式切换成功: ${newMode} (${newMode === "dark" ? "白色图标" : "黑色图标"})`,
  380. "success",
  381. );
  382. } else {
  383. addLog(`❌ 设置失败: ${result.error}`, "error");
  384. }
  385. } catch (error) {
  386. addLog(`❌ 发生错误: ${error}`, "error");
  387. }
  388. }
  389. async function handleOpenApp() {
  390. try {
  391. addLog("📱 正在打开微信...", "info");
  392. const result = await window.native.openApp(
  393. "com.tencent.mm",
  394. "weixin://",
  395. );
  396. if (result.success) {
  397. addLog("✅ 应用已打开", "success");
  398. } else {
  399. addLog(`❌ 打开失败: ${result.error}`, "error");
  400. }
  401. } catch (error) {
  402. addLog(`❌ 发生错误: ${error}`, "error");
  403. }
  404. }
  405. async function handleExit() {
  406. try {
  407. addLog("👋 正在退出 WebView...", "info");
  408. await window.native.exit();
  409. } catch (error) {
  410. addLog(`❌ 退出失败: ${error}`, "error");
  411. }
  412. }
  413. // 监听 VPN 状态变化事件
  414. window.addEventListener("vpnStatusChanged", function (event) {
  415. console.log("收到 VPN 状态变化事件:", event.detail.status);
  416. });
  417. // 初始日志
  418. addLog("🎉 欢迎使用 FlashLink JS Bridge 测试页面", "success");
  419. </script>
  420. </body>
  421. </html>