diff --git a/frontend/src/api/local.ts b/frontend/src/api/local.ts index 0fd0327..01a3a14 100644 --- a/frontend/src/api/local.ts +++ b/frontend/src/api/local.ts @@ -77,3 +77,45 @@ export async function checkLocalHealth(): Promise { export function queryLocalInfo(code: string): Promise { return get(`/api/query/${encodeURIComponent(code)}`, 8000); } + +/** + * 叫醒已安装的本机软件:**自定义 URL Scheme**(安装包写进注册表的那种,像 `thunder://`、 + * `baiduyunguanjia://` 一样) + * + * 网页**无法直接执行 exe**:只能让浏览器去打开 `xxx://…`,由系统按注册表找到 exe 并拉起( + * Chrome/Edge 会先弹「要打开 xxx 吗?」)。所以:先试这个,再轮询 health 确认真的起来了。 + * + * 协议名用 `VITE_APP_PROTOCOL` 配(默认 `yuncang`),必须与安装包里注册的一致。 + */ +export const APP_PROTOCOL = (import.meta.env.VITE_APP_PROTOCOL as string | undefined) || 'yuncang'; + +/** 拼出唤醒地址(把 code 一并带过去,软件起来后可以直接定位到该运单) */ +export function appLaunchUrl(code?: string): string { + const query = code ? `?code=${encodeURIComponent(code)}` : ''; + return `${APP_PROTOCOL}://open${query}`; +} + +/** + * 触发唤醒。用隐藏 iframe 而不是 `location.href`: + * 协议没注册时 Chrome 会直接在当前标签报错页,用 iframe 就只是静默失败。 + */ +export function launchApp(code?: string): void { + const iframe = document.createElement('iframe'); + iframe.style.display = 'none'; + iframe.src = appLaunchUrl(code); + document.body.appendChild(iframe); + setTimeout(() => iframe.remove(), 1500); +} + +/** + * 轮询探活:本机软件从被叫醒到 HTTP 服务可用需要几秒 + * @return 超时前探到返回 true,否则 false + */ +export async function waitForHealth(timeoutMs = 12000, intervalMs = 1000): Promise { + const deadline = Date.now() + timeoutMs; + while (Date.now() < deadline) { + if (await checkLocalHealth()) return true; + await new Promise(resolve => setTimeout(resolve, intervalMs)); + } + return false; +} diff --git a/frontend/src/views/LocalServiceView.vue b/frontend/src/views/LocalServiceView.vue index 239c756..aa27a1f 100644 --- a/frontend/src/views/LocalServiceView.vue +++ b/frontend/src/views/LocalServiceView.vue @@ -12,7 +12,15 @@ */ import { computed, onMounted, ref } from 'vue'; import { useRoute, useRouter } from 'vue-router'; -import { checkLocalHealth, queryLocalInfo, LOCAL_BASE, LocalServiceError } from '@/api/local'; +import { + APP_PROTOCOL, + checkLocalHealth, + launchApp, + queryLocalInfo, + waitForHealth, + LOCAL_BASE, + LocalServiceError, +} from '@/api/local'; /** * 安装包下载地址:换成真实地址,或用环境变量 `VITE_SOFTWARE_DOWNLOAD_URL` 覆盖 @@ -23,23 +31,45 @@ const DOWNLOAD_URL = (import.meta.env.VITE_SOFTWARE_DOWNLOAD_URL as string | und const route = useRoute(); const router = useRouter(); -type Status = 'checking' | 'offline' | 'ready'; +type Status = 'checking' | 'offline' | 'launching' | 'ready'; const status = ref('checking'); const code = ref(String(route.query.code || '').trim()); const info = ref(null); const querying = ref(false); const error = ref(''); +const launchFailed = ref(false); /** 探活:连不上就是「软件没启动」 */ async function detect() { status.value = 'checking'; error.value = ''; + launchFailed.value = false; const alive = await checkLocalHealth(); status.value = alive ? 'ready' : 'offline'; if (alive) await runQuery(); } +/** + * 「启动本机软件」:用自定义协议叫醒(浏览器会问一次),再轮询探活 + * + * 网页不能直接跑 exe —— 只能让系统按注册的协议去拉起(百度云盘/迅雷就是这个路子)。 + */ +async function launch() { + launchFailed.value = false; + status.value = 'launching'; + launchApp(code.value.trim() || undefined); + + const alive = await waitForHealth(); + if (alive) { + status.value = 'ready'; + await runQuery(); + return; + } + launchFailed.value = true; + status.value = 'offline'; +} + /** 按码查信息(没填码就只停在输入框) */ async function runQuery() { const target = code.value.trim(); @@ -91,10 +121,14 @@ onMounted(detect);
云仓视频溯源系统
- + +