Files
crawler-plugin/frontend-vue/src/shared/bridges/pywebview.ts
T
huangzd1997 ee9a32a272 feat(brand): 失败任务「重新提交」入口+排队期显示执行方人话文案(3700 客户端半边)
- brand.ts:resubmitBrandTask 走桥 resubmit_brand_task(重跑必须本机客户端建单执行,
  线上 15126 无从替用户重跑;老客户端 available=false 提示升级);BrandLocalBridge 登记
  桥形状;BrandLineProgressInfo 补 progress_text 与 queued 档注释。
- BrandBrandTab.vue:failed/cancelled 行出「重新提交」按钮(确认框点名沿用策略与通道);
  排队期 extraLines 显示 progress_text(已等待 N 秒;同机 M/K 槽在用),countLabel 不再
  摆恒 0 的「0/265」——用户从「排队中/0 行」无从分辨排队与卡死。
- pywebview.ts:open_jd_login 注释改按自动链路口径(见下一笔,桥本身零改动)。
- 判据:brand-resubmit-entry.test.ts(桥形状+入口 wiring+源码绊线)+
  task-progress-view-wiring.test.ts +排队文案透出绊线+catch-silence 台账 blocks 334→335
  (resubmitTask 的 try/catch 有用户可见出口,非静默,silent 与 perFile 未动)。
2026-11-23 17:30:05 +08:00

354 lines
13 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
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<string, {
proxy_url?: string;
proxy_mode?: number;
proxy_provider?: string;
proxy_account?: string;
proxy_password?: string;
}>;
/** 本机继承快照的作用域标记(CR-12:同一 app.json 才能继承)。 */
api_secret_os_scope?: string;
/** 登出时留给本机下个账号的密钥快照(结构由 shared/utils/api-secret-store 定义)。 */
api_secret_inherit?: Record<string, unknown> | null;
aliprice_usename?: string;
aliprice_pwd?: string;
}
export interface PywebviewApi {
/**
* 把页面登录态令牌交给桌面端 Python 侧(审查 A1/A3):
* Python 调用自家 Java 接口时会带上 Authorization: Bearer <token>,服务端可按人鉴权。
* 网页形态(降级桥)无此方法,页面静默跳过。
*/
set_user_token?: (token: string) => Promise<unknown>;
close?: () => Promise<void>;
minimize?: () => Promise<void>;
maximize?: () => Promise<void>;
toggle_maximize?: () => Promise<void>;
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<string | null>;
select_brand_xlsx_files?: () => Promise<string[]>;
select_brand_folder?: () => Promise<string | null>;
/** 本机展开文件夹下的 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<number>;
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-23「登录京麦」按钮退役,原 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(真失败)。
* 调用方=shared/stores/jd-login-autoflow(同步分类 jd_login、任务致命公告、建单/重试
* 预检三路自动触发)。老客户端没有这个方法 ⇒ 自动链路静默跳过并提示升级。
*/
open_jd_login?: () => Promise<{
success: boolean;
code?: string;
message?: string;
error?: string;
}>;
/**
* 叫停本机正在执行的任务(桌面桥 `main.py::WindowAPI.stop_task`):京东采集走
* `amazon/jd/desktop_worker.stop` → 给独立 worker 的 .job 记录置 stop_requested,
* worker 下一行/下一页就收手,不必等下一批分片回传或 60 秒心跳。
* 删除任务前调用:任务行一删,子进程只能等「任务不存在」才收手,期间仍占着京麦
* 浏览器锁(登录京麦被 jd_busy 挡、下一单排队)。4.0.12 起就在 expose 名单里,
* 老客户端同样可用;纯浏览器降级桥没有该方法,调用方按服务端兜底处理。
*/
stop_task?: (taskId: number) => Promise<{ success: boolean; message?: string; error?: string }>;
// —— ASIN 变体采集(VariantCollectionAPI,vc_* 前缀)——
vc_select_input_files?: () => Promise<string[]>;
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<DesktopConfig>;
save_config?: (data: DesktopConfigUpdate) => Promise<DesktopConfig>;
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);
}