Files
2026-10-11 12:56:38 +08:00

59 lines
2.2 KiB
JavaScript

// Wails API 适配层
//
// 前端业务代码只用 window.eh.*,这里把它接到 Wails 的 Go bindings 上。
// 这样以后即便换框架,也只要改这一个文件。
//
// 命名从 eh.* 改成 hd4k.* —— 站点换了,标识也跟着换,避免误以为还是 EH。
const go = () => window.go && window.go.main && window.go.main.App;
function requireGo() {
const g = go();
if (!g) throw new Error('Wails 后端未就绪');
return g;
}
window.hd4k = {
// ---- 设置 ----
getSettings: async () => requireGo().GetSettings(),
setSettings: async (patch) => requireGo().SetSettings(patch),
previewDir: async (tpl) => requireGo().PreviewDir(tpl),
placeholders: async () => requireGo().Placeholders(),
sites: async () => requireGo().Sites(),
// ---- 任务 ----
listTasks: async () => requireGo().ListTasks(),
addTasks: async (text) => requireGo().AddTasks(text),
removeTask: async (id) => requireGo().RemoveTask(id),
clearFinished: async () => requireGo().ClearFinished(),
countDone: async () => requireGo().CountDone(),
clearAll: async (onlySelected) => requireGo().ClearAll(!!onlySelected),
requeueFailed: async () => requireGo().RequeueFailed(),
selectAll: async (ids) => requireGo().SelectAll(ids),
// ---- 引擎 ----
status: async () => requireGo().Status(),
engineInfo: async () => requireGo().EngineInfo(),
start: async (onlyIds) => requireGo().Start(onlyIds || []),
stop: async () => requireGo().Stop(),
// ---- 扫描下载目录(用 .hd4k.json 清单补全丢失的任务信息)----
scanDownloadDir: async () => requireGo().ScanDownloadDir(),
// ---- 系统 ----
pickDir: async () => requireGo().PickDir(),
exportFailed: async (id) => {
const path = await requireGo().ExportFailed(id);
return path ? { ok: true, filePath: path } : { ok: false };
},
openPath: async (p) => requireGo().OpenPath(p),
showItem: async (p) => requireGo().ShowItem(p),
// ---- 事件 ----
onTaskUpdate: (cb) => window.runtime.EventsOn('task:update', cb),
onLog: (cb) => window.runtime.EventsOn('log', cb),
onDone: (cb) => window.runtime.EventsOn('engine:done', cb),
};
// 兼容别名:renderer.js 里的业务代码统一用 window.eh.*
window.eh = window.hd4k;