前端:/local 页支持「叫醒」已安装的本机软件(自定义协议 + 轮询探活)
网页没法直接执行 exe,只能走「自定义 URL Scheme」(百度云盘 baiduyunguanjia://、迅雷 thunder:// 都是这个套路):浏览器让系统按注册表里的协议去拉起应用。 - api/local.ts 新增 APP_PROTOCOL(VITE_APP_PROTOCOL,默认 yuncang)、appLaunchUrl()、 launchApp()(隐藏 iframe 触发,避免协议没注册时整页报错)、waitForHealth()(1s 一次, 默认最多等 12s)。 - 离线态多一个「已安装,启动本机软件」按钮:点完进 launching 态,轮询 health; 起来了就自动查询原来的 code;12s 还没起来则提示「可能没安装 / 没注册启动协议」, 并保留下载按钮。 - 下载按钮、探活提示与居中布局保持不变。 实测(7301 + 模拟的 17311):先打开显示「服务暂不可用」;点启动后进入「正在启动本机软件…」, 轮询期间把模拟服务起起来,页面自动切到查询结果(4 行)。
This commit is contained in:
parent
7198dd1eb4
commit
9be56db385
@ -77,3 +77,45 @@ export async function checkLocalHealth(): Promise<boolean> {
|
|||||||
export function queryLocalInfo(code: string): Promise<unknown> {
|
export function queryLocalInfo(code: string): Promise<unknown> {
|
||||||
return get<unknown>(`/api/query/${encodeURIComponent(code)}`, 8000);
|
return get<unknown>(`/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<boolean> {
|
||||||
|
const deadline = Date.now() + timeoutMs;
|
||||||
|
while (Date.now() < deadline) {
|
||||||
|
if (await checkLocalHealth()) return true;
|
||||||
|
await new Promise(resolve => setTimeout(resolve, intervalMs));
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|||||||
@ -12,7 +12,15 @@
|
|||||||
*/
|
*/
|
||||||
import { computed, onMounted, ref } from 'vue';
|
import { computed, onMounted, ref } from 'vue';
|
||||||
import { useRoute, useRouter } from 'vue-router';
|
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` 覆盖
|
* 安装包下载地址:换成真实地址,或用环境变量 `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 route = useRoute();
|
||||||
const router = useRouter();
|
const router = useRouter();
|
||||||
|
|
||||||
type Status = 'checking' | 'offline' | 'ready';
|
type Status = 'checking' | 'offline' | 'launching' | 'ready';
|
||||||
|
|
||||||
const status = ref<Status>('checking');
|
const status = ref<Status>('checking');
|
||||||
const code = ref(String(route.query.code || '').trim());
|
const code = ref(String(route.query.code || '').trim());
|
||||||
const info = ref<unknown>(null);
|
const info = ref<unknown>(null);
|
||||||
const querying = ref(false);
|
const querying = ref(false);
|
||||||
const error = ref('');
|
const error = ref('');
|
||||||
|
const launchFailed = ref(false);
|
||||||
|
|
||||||
/** 探活:连不上就是「软件没启动」 */
|
/** 探活:连不上就是「软件没启动」 */
|
||||||
async function detect() {
|
async function detect() {
|
||||||
status.value = 'checking';
|
status.value = 'checking';
|
||||||
error.value = '';
|
error.value = '';
|
||||||
|
launchFailed.value = false;
|
||||||
const alive = await checkLocalHealth();
|
const alive = await checkLocalHealth();
|
||||||
status.value = alive ? 'ready' : 'offline';
|
status.value = alive ? 'ready' : 'offline';
|
||||||
if (alive) await runQuery();
|
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() {
|
async function runQuery() {
|
||||||
const target = code.value.trim();
|
const target = code.value.trim();
|
||||||
@ -91,10 +121,14 @@ onMounted(detect);
|
|||||||
<div class="card bare-card local-card">
|
<div class="card bare-card local-card">
|
||||||
<div class="brand-line">云仓视频溯源系统</div>
|
<div class="brand-line">云仓视频溯源系统</div>
|
||||||
|
|
||||||
<!-- ① 正在检测 -->
|
<!-- ① 正在检测 / 正在启动 -->
|
||||||
<template v-if="status === 'checking'">
|
<template v-if="status === 'checking'">
|
||||||
<p class="center hint">正在检测本机打印服务…</p>
|
<p class="center hint">正在检测本机打印服务…</p>
|
||||||
</template>
|
</template>
|
||||||
|
<template v-else-if="status === 'launching'">
|
||||||
|
<p class="center hint">正在启动本机软件…(浏览器问“要打开吗”请点允许)</p>
|
||||||
|
<p class="center hint tiny">等它启动完成后本页会自动继续</p>
|
||||||
|
</template>
|
||||||
|
|
||||||
<!-- ② 本机软件没启动 / 连不上 -->
|
<!-- ② 本机软件没启动 / 连不上 -->
|
||||||
<template v-else-if="status === 'offline'">
|
<template v-else-if="status === 'offline'">
|
||||||
@ -105,9 +139,14 @@ onMounted(detect);
|
|||||||
<p class="center">
|
<p class="center">
|
||||||
<a class="btn download" :href="DOWNLOAD_URL" target="_blank" rel="noopener">下载 云仓视频溯源软件</a>
|
<a class="btn download" :href="DOWNLOAD_URL" target="_blank" rel="noopener">下载 云仓视频溯源软件</a>
|
||||||
</p>
|
</p>
|
||||||
|
<p class="center">
|
||||||
|
<button class="btn btn-ghost" type="button" @click="launch">已安装,启动本机软件</button>
|
||||||
|
</p>
|
||||||
|
<p v-if="launchFailed" class="center hint tiny">
|
||||||
|
没有反应?可能是没安装、或安装时没注册启动协议({{ APP_PROTOCOL }}://),先下载安装吧。
|
||||||
|
</p>
|
||||||
<p class="center hint tiny">
|
<p class="center hint tiny">
|
||||||
已安装?请先启动软件,再点
|
再点 <a href="#" @click.prevent="detect">重新检测</a>
|
||||||
<a href="#" @click.prevent="detect">重新检测</a>
|
|
||||||
<br>
|
<br>
|
||||||
(当前探测地址:{{ LOCAL_BASE }})
|
(当前探测地址:{{ LOCAL_BASE }})
|
||||||
</p>
|
</p>
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user