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 @@ + + + + +