import { webFallbackApi } from './web-fallback'; import { isDesktopRuntime } from '@/shared/runtime-env'; export interface UploadedJavaFile { fileKey: string; originalFilename: string; localPath: string; size: number; relativePath?: string; /** OSS 上传模式下返回的跨节点访问地址(uploadOss=true 时桌面桥可能附带) */ url?: string; } export type ProxyMode = 1 | 2; /** ASIN 变体采集任务项(/batch/list 返回元素,后端数值状态 0排队/1执行/2成功/3失败) */ export interface VariantTaskListItem { task_id: string; status?: number | string; task_type?: string; /** 最近更新时间(后台有则返回) */ update_time?: string; /** 任务创建时间(后台有则返回) */ create_time?: string; /** 任务完成时间(后台有则返回) */ finish_time?: string; res_type?: number | string; res_mes?: string; file_url?: string[] | string; task_data?: { file_url?: string; execute_country?: string[]; proxy_url?: string; proxy_mode?: number | string; /** * 排队位次:仅排队中(status=0)的任务由 /batch/list 附带, * 供页面显示「前面还有 N 个任务」。 * * 之所以嵌在 task_data 里而不是顶层:桌面客户端对桥返回的字段有白名单, * 顶层新字段到不了页面,而 task_data 是客户端唯一原样透传的开放对象。 */ queue?: { /** 位次(1-based);不在待处理队列里为 null */ position?: number | null; /** 前面还有几个任务(= position - 1) */ ahead?: number | null; /** 待处理队列总长 */ len?: number | null; }; [key: string]: unknown; }; } /** ASIN 变体采集单任务查询 data 段 */ export interface VariantTaskQueryData { message?: string; msg?: string; res_mes?: string; res_type?: number | string; file_url?: string[] | string; task_data?: VariantTaskListItem['task_data']; [key: string]: unknown; } export interface DesktopConfig { proxy_url?: string; proxy_mode?: number | string; /** 代理供应商:qingguo(青果,默认)/ jikip(极客);老配置没有该键时按链接域名识别。 */ proxy_provider?: string; /** 青果隧道端点的代理账号(Authkey);极客不需要。 */ proxy_account?: string; /** 青果隧道端点的代理密码(Authpwd);极客不需要。 */ proxy_password?: string; aliprice_usename?: string; aliprice_pwd?: string; [key: string]: unknown; } export interface DesktopConfigUpdate { proxy_url?: string; proxy_mode?: ProxyMode; /** 代理供应商:qingguo(青果,默认)/ jikip(极客)。 */ proxy_provider?: string; /** 青果隧道代理账号/密码(Authkey/Authpwd);空串表示清除。 */ proxy_account?: string; proxy_password?: string; current_uid?: string; /** 全局 proxy_url 的归属 uid(CR-04:只有 owner 匹配才允许回退)。 */ proxy_uid?: string; /** 显式共享白名单:整机共享或指定 uid 列表(CR-04)。 */ proxy_shared?: boolean; proxy_shared_uids?: string[]; proxy_users?: Record; /** 本机继承快照的作用域标记(CR-12:同一 app.json 才能继承)。 */ api_secret_os_scope?: string; /** 登出时留给本机下个账号的密钥快照(结构由 shared/utils/api-secret-store 定义)。 */ api_secret_inherit?: Record | null; aliprice_usename?: string; aliprice_pwd?: string; } export interface PywebviewApi { /** * 把页面登录态令牌交给桌面端 Python 侧(审查 A1/A3): * Python 调用自家 Java 接口时会带上 Authorization: Bearer ,服务端可按人鉴权。 * 网页形态(降级桥)无此方法,页面静默跳过。 */ set_user_token?: (token: string) => Promise; close?: () => Promise; minimize?: () => Promise; maximize?: () => Promise; toggle_maximize?: () => Promise; select_files?: (options?: { filters?: Array<{ name?: string; extensions?: string; }>; multiple?: boolean; }) => Promise<{ paths?: string[]; } | null>; save_image?: ( urlOrData: string, filename?: string, ) => Promise<{ success: boolean; path?: string; error?: string }>; save_image_to_folder?: ( urlOrData: string, dirPath: string, filename?: string, ) => Promise<{ success: boolean; path?: string; error?: string }>; select_folder?: () => Promise; select_brand_xlsx_files?: () => Promise; select_brand_folder?: () => Promise; /** 本机展开文件夹下的 xlsx 文件(供品牌检测"选择文件夹");新版桌面客户端提供 */ expand_brand_folder?: ( folder: string, ) => Promise<{ success: boolean; items?: Array<{ absolutePath: string; relativePath: string }>; error?: string }>; /** * 品牌检测本机采集(V212):把这一单交给本机 Flask 执行并返回 task_id。 * 老客户端没有这个方法 ⇒ 页面据此把「本机采集」置灰并要求升级。 */ run_brand_task?: ( payload: { files: unknown[]; strategy: string; uid: number }, ) => Promise<{ success: boolean; task_id?: number; collect_channel?: string; error?: string }>; upload_file_to_java?: ( filePath: string, relativePath?: string, uploadOss?: boolean, ) => Promise<{ success: boolean; message?: string; data?: UploadedJavaFile; error?: string; }>; save_file_from_url?: ( url: string, filename: string, ) => Promise<{ success: boolean; path?: string; error?: string }>; save_file_from_url_new?: ( url: string, filename: string, ) => Promise<{ success: boolean; path?: string; error?: string }>; save_file_from_url_with_progress?: ( url: string, filename: string, downloadId: string, ) => Promise<{ success: boolean; path?: string; error?: string }>; save_template_xlsx?: () => Promise<{ success: boolean; path?: string; error?: string; }>; save_template_zip?: () => Promise<{ success: boolean; path?: string; error?: string; }>; /** 工具台首页「立即下载」:本地查找教程压缩包并弹窗复制(对齐主程序 _on_dl_click) */ save_workbench_zip?: () => Promise<{ success: boolean; path?: string; size_mb?: number; not_found?: boolean; error?: string; }>; /** 用系统默认浏览器打开链接(桌面端下载教程包等走外部浏览器,可看到下载进度) */ open_external_url?: (url: string) => Promise<{ success: boolean; error?: string }>; /** 版本更新(桌面专属):下载更新包并启动 update.exe 后退出主程序(原 Flask /api/update/do 桥化) */ do_update_app?: (fileUrl: string) => Promise<{ success: boolean; error?: string }>; /** 桌面客户端本地版本号(与 /api/version/latest 比较检测更新;新版客户端提供) */ get_app_version?: () => Promise<{ success: boolean; version?: string; error?: string }>; launch_yaoayanui?: () => Promise<{ success: boolean; path?: string; error?: string; alreadyRunning?: boolean; }>; /** 京东发布独立协议:品牌单仅允许明确支持该协议的客户端创建或修改品牌重试。 */ jd_publish_protocol?: () => Promise; enqueue_json?: ( data: unknown, ) => Promise<{ success: boolean; queue_size?: number; error?: string; code?: string }>; /** * 京东建单页「同步分类」:现拉本机已登录京麦店铺的「店内分类」(2026-11-19 起 * 选项集正本,替代冻结快照——每家店的店内分类名字与 id 都不同)。执行体在客户端 * amazon/jd/shop_categories.py,全程返回 dict;code 分档 jd_busy(京东任务在跑, * amber notice)/ jd_login(京麦未登录)/ jd_page(页面结构变更,红色 error)。 * 老客户端没有这个方法 ⇒ 页面把「同步分类」按钮置灰并提示升级。 */ get_jd_shop_categories?: () => Promise<{ success: boolean; shopName?: string; shopId?: string; categories?: Array<{ label: string; value: string }>; code?: string; error?: string; }>; /** * 京东采集建单页「登录京麦」(2026-11-20):起本机京东采集专用浏览器、停在京麦 * 登录页等人工登录(执行体在客户端 amazon/jd/login_window.py,登录态常驻采集专用 * profile chrome_data_jd,登一次后续任务复用)。code 分档:成功 logged_in / * already_logged_in(带 message);失败 jd_busy(京东任务在跑)/ jd_login_running * (登录窗口已在等待,可恢复)/ window_closed(用户关了窗口,再点一次)/ * timeout(等待超时,窗口保持打开)/ jd_page(真失败)。 * 老客户端没有这个方法 ⇒ 页面把「登录京麦」按钮置灰并提示升级。 */ open_jd_login?: () => Promise<{ success: boolean; code?: string; message?: string; error?: string; }>; // —— ASIN 变体采集(VariantCollectionAPI,vc_* 前缀)—— vc_select_input_files?: () => Promise; vc_start_collect?: (params: { mode: 'asin' | 'file'; files?: string[]; asins?: string[]; countries: string[]; uid: string; }) => Promise<{ success: boolean; tasks?: Array<{ task_id: string; source?: string; execute_country?: string[] }>; error?: string; }>; vc_query_task?: ( taskId: string, ) => Promise<{ success: boolean; status?: number | string; data?: VariantTaskQueryData; error?: string; }>; vc_list_tasks?: (query: { uid: string; page: number; page_size: number; }) => Promise<{ success: boolean; page?: number; total_page?: number; total?: number; tasks?: VariantTaskListItem[]; error?: string; }>; vc_export_task?: ( taskId: string, ) => Promise<{ success: boolean; file_id?: string; error?: string }>; vc_query_export?: ( fileId: string, ) => Promise<{ success: boolean; status?: number | string; file_url?: string; file_name?: string; error?: string; }>; vc_download_files_as_zip?: (params: { urls: string[]; zip_name: string; download_id: string; }) => Promise<{ success: boolean; path?: string; error?: string }>; read_config?: () => Promise; save_config?: (data: DesktopConfigUpdate) => Promise; get_device_id?: () => Promise<{ success: boolean; device_id?: string; error?: string }>; } declare global { interface Window { pywebview?: { api?: PywebviewApi; }; } } let cachedPywebviewApi: PywebviewApi | undefined; let pywebviewReadyBound = false; /** 当前是否运行在桌面客户端(pywebview)运行时;Web 独立部署下恒为 false(见 runtime-env.ts 注释) */ export { isDesktopRuntime }; function syncPywebviewApi() { cachedPywebviewApi = window.pywebview?.api; if (cachedPywebviewApi) { isDesktopRuntime.value = true; } return cachedPywebviewApi; } function bindPywebviewReady() { if ( pywebviewReadyBound || typeof window === "undefined" || !window.addEventListener ) return; pywebviewReadyBound = true; window.addEventListener("pywebviewready", () => { syncPywebviewApi(); }); } bindPywebviewReady(); export function getPywebviewApi() { const real = syncPywebviewApi() || cachedPywebviewApi; if (real) return real; // 纯浏览器环境(Web 独立部署 / dev 预览):返回浏览器降级桥, // 未在降级桥中实现的方法为 undefined,页面代码走既有的"请在本地客户端中打开"分支 if (typeof window !== 'undefined') return webFallbackApi; return undefined; } /** 是否运行在真实 pywebview 桌面桥环境(Web 降级桥不算桌面运行时) */ export function hasPywebview() { return Boolean(syncPywebviewApi() || cachedPywebviewApi); }