From 7198dd1eb41a3b5dd1003a67375b02f929bc869b Mon Sep 17 00:00:00 2001 From: liufumingren Date: Sat, 17 Oct 2026 10:02:48 +0800 Subject: [PATCH] =?UTF-8?q?=E5=89=8D=E7=AB=AF=EF=BC=9A=E6=96=B0=E5=A2=9E?= =?UTF-8?q?=E4=B8=8D=E9=9C=80=E8=A6=81=E7=99=BB=E5=BD=95=E7=9A=84=E3=80=8C?= =?UTF-8?q?=E6=9C=AC=E6=9C=BA=E8=BD=AF=E4=BB=B6=E3=80=8D=E5=85=AC=E5=85=B1?= =?UTF-8?q?=E9=A1=B5=20/local=3Fcode=3D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 给 Chrome 插件用:点运单号旁的按钮直接打开这一页(插件里的默认目标地址模板已改成 http://localhost:7301/local?code={code},线上换域名即可)。 - 打开即探活 GET http://localhost:17311/api/health: - 连不上/超时/被 CORS 拦 → 「服务暂不可用」+「本地打印服务未启动,请下载安装后再试」 + 居中的「下载 云仓视频溯源软件」按钮 + 「重新检测」(页面上也打出探测地址,方便排查); - 可用 → 用 ?code= 调 GET http://localhost:17311/api/query/{code},把返回按 「项目 / 值」两列展示,另附可展开的原始 JSON(本机软件返回结构未知,做通用渲染)。 - 没带 code 时给一个输入框手动查;提交后把 code 同步进地址栏(命名路由 local-service)。 - 路由 meta.public,并且**已登录时也不被踢回首页**(和扫码确认页一样始终显示, 因为插件/手机是直接打开它的)。 - 本机服务地址可用 VITE_LOCAL_SERVICE 覆盖(换端口或改用 127.0.0.1 时)。 ⚠️ 本机软件需要放行 CORS(Access-Control-Allow-Origin),否则浏览器会拦掉跨域请求, 表现和「软件没启动」一样(页面只能提示服务暂不可用)。 实测:7301 前端 + 一个模拟的 17311 本机服务 —— 无服务时提示文案与居中正确; 起服务后 /local?code=321408064481797 自动查出 6 行信息;不带 code 时手动输入可查。 --- frontend/src/api/local.ts | 79 ++++++++ frontend/src/router/index.ts | 8 +- frontend/src/views/LocalServiceView.vue | 237 ++++++++++++++++++++++++ 3 files changed, 323 insertions(+), 1 deletion(-) create mode 100644 frontend/src/api/local.ts create mode 100644 frontend/src/views/LocalServiceView.vue diff --git a/frontend/src/api/local.ts b/frontend/src/api/local.ts new file mode 100644 index 0000000..0fd0327 --- /dev/null +++ b/frontend/src/api/local.ts @@ -0,0 +1,79 @@ +/** + * 本机软件(打印服务)客户端 —— 由 Chrome 插件打开的公共页用它查运单信息 + * + * 本机服务默认监听 `http://localhost:17311`: + * - `GET /api/health` 探活:能连通就是「软件已启动」 + * - `GET /api/query/{code}` 按运单号查信息 + * + * ⚠️ 这是**跨域**请求(页面同源是我们的站点,目标是 localhost:17311): + * 本机软件必须允许 CORS(`Access-Control-Allow-Origin` 放行站点来源),否则浏览器会拦掉, + * 表现和「软件没启动」一样 —— 页面统一提示「服务暂不可用」。 + * + * 地址可用 `VITE_LOCAL_SERVICE` 覆盖(换端口 / 换 127.0.0.1 时用)。 + */ + +/** 本机服务地址(去掉末尾斜杠) */ +export const LOCAL_BASE = ((import.meta.env.VITE_LOCAL_SERVICE as string | undefined) || 'http://localhost:17311') + .replace(/\/+$/, ''); + +/** 本机服务返回非 2xx / 超时 / 跨域失败时抛它 */ +export class LocalServiceError extends Error { + /** HTTP 状态码(网络层失败时为 0) */ + status: number; + + constructor(message: string, status = 0) { + super(message); + this.name = 'LocalServiceError'; + this.status = status; + } +} + +/** 带超时的 GET;返回解析后的 JSON(不是 JSON 就给纯文本) */ +async function get(path: string, timeoutMs: number): Promise { + const controller = new AbortController(); + const timer = setTimeout(() => controller.abort(), timeoutMs); + try { + const res = await fetch(`${LOCAL_BASE}${path}`, { + method: 'GET', + headers: { Accept: 'application/json' }, + signal: controller.signal, + }); + if (!res.ok) throw new LocalServiceError(`本地服务返回 ${res.status}`, res.status); + + const text = await res.text(); + if (!text) return null as T; + try { + return JSON.parse(text) as T; + } catch { + return text as unknown as T; + } + } finally { + clearTimeout(timer); + } +} + +/** + * 探活:本机软件是否在跑 + * + * 只要 `GET /api/health` 能拿到 2xx 就算可用;返回体里显式写了 `ok:false` 也算不可用。 + * 连不上 / 超时 / 被 CORS 拦掉 → false(页面统一提示「服务暂不可用」)。 + */ +export async function checkLocalHealth(): Promise { + try { + const data = await get('/api/health', 2500); + if (data && typeof data === 'object' && 'ok' in data) { + return Boolean((data as { ok?: unknown }).ok); + } + return true; + } catch { + return false; + } +} + +/** + * 按运单号 / 码查信息(本机软件返回什么就原样给什么,展示层做通用渲染) + * @param code 运单号(来自插件跳转的 `?code=`) + */ +export function queryLocalInfo(code: string): Promise { + return get(`/api/query/${encodeURIComponent(code)}`, 8000); +} diff --git a/frontend/src/router/index.ts b/frontend/src/router/index.ts index 484e06a..0236c45 100644 --- a/frontend/src/router/index.ts +++ b/frontend/src/router/index.ts @@ -12,6 +12,9 @@ const routes: RouteRecordRaw[] = [ { path: '/reset', name: 'reset', component: () => import('@/views/ResetView.vue'), meta: { public: true, bare: true } }, // 手机扫码后打开:确认登录(公开) { path: '/wechat', name: 'wechat-confirm', component: () => import('@/views/WechatConfirmView.vue'), meta: { public: true, bare: true } }, + // 本机软件(打印服务)公共页:**不需要登录**,Chrome 插件打开 `/local?code=<运单号>` + // 页面自己探活 http://localhost:17311/api/health,再查 /api/query/{code} + { path: '/local', name: 'local-service', component: () => import('@/views/LocalServiceView.vue'), meta: { public: true, bare: true } }, { path: '/', name: 'home', component: () => import('@/views/HomeView.vue') }, { path: '/devices', name: 'devices', component: () => import('@/views/DevicesView.vue') }, @@ -32,7 +35,10 @@ router.beforeEach((to) => { if (!to.meta.public && !logged) { return { name: 'login', query: { redirect: to.fullPath } }; } - if (to.meta.public && logged && to.name !== 'wechat-confirm') { + // 已登录时这些公开页也照常显示(扫码确认页 / 本机软件页:由插件或手机直接打开, + // 不该被踢回首页) + const alwaysShow = to.name === 'wechat-confirm' || to.name === 'local-service'; + if (to.meta.public && logged && !alwaysShow) { return { name: 'home' }; } return true; diff --git a/frontend/src/views/LocalServiceView.vue b/frontend/src/views/LocalServiceView.vue new file mode 100644 index 0000000..239c756 --- /dev/null +++ b/frontend/src/views/LocalServiceView.vue @@ -0,0 +1,237 @@ + + + + +