feat: 新增银行卡检查插件及相关依赖与配置文件
This commit is contained in:
@@ -0,0 +1,155 @@
|
||||
// 插件备注(账号 × 插件 唯一):本机即时可用 + 回传中心机(跨设备一致)
|
||||
// 存储:%LOCALAPPDATA%\37duAutomation\plugin_notes.json
|
||||
// { "<account>": { "<plugin_id>": { "note": "...", "at": 1730000000 } } }
|
||||
// 回传:POST /api/plugin-notes(账号+插件 upsert,失败只记 WARN 不阻断用户操作)
|
||||
// 拉取:GET /api/plugin-notes?account=(启动/切换插件时合并;本机有、云端无的顺手补报)
|
||||
"use strict";
|
||||
|
||||
const fs = require("fs");
|
||||
const path = require("path");
|
||||
const config = require("./config");
|
||||
const cp = require("./cpclient");
|
||||
const { makeLogger } = require("./logger");
|
||||
|
||||
const logger = makeLogger("notes");
|
||||
|
||||
const NOTES_PATH = path.join(config.APP_DATA, "plugin_notes.json");
|
||||
|
||||
// 设备 ID:与 log-upload / app 同源(%LOCALAPPDATA%\37duAutomation\device_id),本模块自包含避免拉入应用依赖树
|
||||
function deviceId() {
|
||||
const f = path.join(config.APP_DATA, "device_id");
|
||||
try {
|
||||
if (fs.existsSync(f)) {
|
||||
const v = fs.readFileSync(f, "utf-8").trim();
|
||||
if (v) return v;
|
||||
}
|
||||
const id = require("crypto").randomUUID();
|
||||
fs.mkdirSync(config.APP_DATA, { recursive: true });
|
||||
fs.writeFileSync(f, id, "utf-8");
|
||||
return id;
|
||||
} catch (e) {
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
// 限长:备注是"人看的说明",不是数据字段;超长只在本地截断,避免把 AI 表/日志撑爆
|
||||
const MAX_NOTE_LEN = 500;
|
||||
|
||||
function clip(v) {
|
||||
const s = String(v === undefined || v === null ? "" : v).replace(/\r\n/g, "\n").trim();
|
||||
return s.length > MAX_NOTE_LEN ? s.slice(0, MAX_NOTE_LEN) : s;
|
||||
}
|
||||
|
||||
function readAll() {
|
||||
try {
|
||||
const v = JSON.parse(fs.readFileSync(NOTES_PATH, "utf-8"));
|
||||
return v && typeof v === "object" && !Array.isArray(v) ? v : {};
|
||||
} catch (e) {
|
||||
return {}; // 文件不存在/损坏:按空处理(备注是辅助信息,永不阻断主流程)
|
||||
}
|
||||
}
|
||||
|
||||
function writeAll(obj) {
|
||||
try {
|
||||
fs.mkdirSync(config.APP_DATA, { recursive: true });
|
||||
const tmp = NOTES_PATH + ".tmp";
|
||||
fs.writeFileSync(tmp, JSON.stringify(obj), "utf-8");
|
||||
fs.renameSync(tmp, NOTES_PATH);
|
||||
return true;
|
||||
} catch (e) {
|
||||
logger.warning(`插件备注写入失败:${e.message}`);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/** 当前账号某个插件的备注(无则空串) */
|
||||
function get(account, pluginId) {
|
||||
const acct = String(account || "");
|
||||
const pid = String(pluginId || "");
|
||||
if (!acct || !pid) return "";
|
||||
const row = (readAll()[acct] || {})[pid];
|
||||
return row && typeof row === "object" ? String(row.note || "") : "";
|
||||
}
|
||||
|
||||
/** 当前账号全部备注:{ plugin_id: { note, at } } */
|
||||
function all(account) {
|
||||
const acct = String(account || "");
|
||||
if (!acct) return {};
|
||||
const mine = readAll()[acct];
|
||||
return mine && typeof mine === "object" ? mine : {};
|
||||
}
|
||||
|
||||
/** 保存备注(空串 = 清空);仅落本机,不发起网络请求 */
|
||||
function save(account, pluginId, note) {
|
||||
const acct = String(account || "");
|
||||
const pid = String(pluginId || "");
|
||||
if (!acct || !pid) return "";
|
||||
const text = clip(note);
|
||||
const data = readAll();
|
||||
const mine = data[acct] && typeof data[acct] === "object" ? data[acct] : {};
|
||||
if (text) mine[pid] = { note: text, at: Math.floor(Date.now() / 1000) };
|
||||
else delete mine[pid];
|
||||
data[acct] = mine;
|
||||
writeAll(data);
|
||||
return text;
|
||||
}
|
||||
|
||||
/** 上报中心机(异步、失败静默);note 为空表示清空云端记录 */
|
||||
async function push(account, pluginId, note) {
|
||||
if (!config.CONTROLPLANE_URL) return false;
|
||||
const acct = String(account || "").trim();
|
||||
const pid = String(pluginId || "").trim();
|
||||
if (!acct || !pid) return false;
|
||||
try {
|
||||
await cp.request("POST", "/api/plugin-notes", {
|
||||
headers: cp.machineHeaders(),
|
||||
json: { account: acct, plugin_id: pid, note: clip(note), device_id: deviceId() },
|
||||
timeout: 10,
|
||||
});
|
||||
return true;
|
||||
} catch (e) {
|
||||
logger.warning(`插件备注回传失败(本机已保存,下次同步补报):${e.message}`);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/** 从中心机拉取该账号全部备注并合并到本机(云端为准;本机独有的顺手补报) */
|
||||
async function pull(account) {
|
||||
if (!config.CONTROLPLANE_URL) return false;
|
||||
const acct = String(account || "").trim();
|
||||
if (!acct) return false;
|
||||
let cloud = null;
|
||||
try {
|
||||
const r = await cp.request("GET", "/api/plugin-notes", {
|
||||
headers: cp.machineHeaders(), params: { account: acct }, timeout: 10,
|
||||
});
|
||||
cloud = r && r.notes && typeof r.notes === "object" ? r.notes : {};
|
||||
} catch (e) {
|
||||
logger.warning(`插件备注拉取失败(继续用本机缓存):${e.message}`);
|
||||
return false;
|
||||
}
|
||||
const data = readAll();
|
||||
const mine = data[acct] && typeof data[acct] === "object" ? data[acct] : {};
|
||||
const toPush = [];
|
||||
for (const [pid, row] of Object.entries(cloud)) {
|
||||
const note = clip(row && row.note);
|
||||
if (note) mine[pid] = { note, at: Number((row && row.at) || 0) || Math.floor(Date.now() / 1000) };
|
||||
else delete mine[pid];
|
||||
}
|
||||
for (const [pid, row] of Object.entries(mine)) {
|
||||
if (!cloud[pid] && row && row.note) toPush.push([pid, row.note]);
|
||||
}
|
||||
data[acct] = mine;
|
||||
writeAll(data);
|
||||
// 本机独有(例如离线期间改的)补报中心机,逐条失败不影响其余
|
||||
for (const [pid, note] of toPush) {
|
||||
try { await cp.request("POST", "/api/plugin-notes", {
|
||||
headers: cp.machineHeaders(),
|
||||
json: { account: acct, plugin_id: pid, note, device_id: deviceId() },
|
||||
timeout: 10,
|
||||
}); } catch (_) { /* 忽略:下次 pull 再补 */ }
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
module.exports = { NOTES_PATH, MAX_NOTE_LEN, get, all, save, push, pull };
|
||||
@@ -168,6 +168,7 @@ function brief(rec) {
|
||||
finished: rec.finished || 0,
|
||||
duration_sec: rec.duration_sec || 0,
|
||||
summary: rec.summary || "",
|
||||
note: rec.note || "",
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -14,6 +14,7 @@ const scheduler = require("./scheduler");
|
||||
const queue = require("./queue");
|
||||
const licensing = require("./licensing");
|
||||
const runHistory = require("./run-history");
|
||||
const notes = require("./notes");
|
||||
const platformAuth = require("./platform-auth");
|
||||
const platformCreds = require("./platform-creds");
|
||||
const { HUB } = require("./hub");
|
||||
@@ -214,6 +215,7 @@ function finishRun(runId, plugin, status, error, summary) {
|
||||
started: run.started || 0,
|
||||
finished,
|
||||
duration_sec: durationSec,
|
||||
note: run.note || "",
|
||||
logs: Array.isArray(run.logs) ? run.logs : [],
|
||||
});
|
||||
// 日志集中回传:整次运行日志上送中心机(离线自动暂存补报;失败不阻断运行收尾)
|
||||
@@ -233,6 +235,7 @@ function finishRun(runId, plugin, status, error, summary) {
|
||||
started: run.started || 0,
|
||||
finished,
|
||||
duration_sec: durationSec,
|
||||
note: run.note || "",
|
||||
logs: Array.isArray(run.logs) ? run.logs : [],
|
||||
});
|
||||
} catch (e) {
|
||||
@@ -251,6 +254,7 @@ function finishRun(runId, plugin, status, error, summary) {
|
||||
error: ok ? "" : error || "",
|
||||
duration_sec: durationSec,
|
||||
summary: summary || "",
|
||||
note: run.note || "",
|
||||
});
|
||||
// 排队任务的结束提醒(用户手动取消队列里的任务不算:应用内已有明确反馈,不再二次打扰)
|
||||
if (_QUEUED_RUNS.delete(runId)) {
|
||||
@@ -316,6 +320,11 @@ function submitRun(account, plugin, fields, opts) {
|
||||
const enqueuedAt = Date.now() / 1000;
|
||||
const name = plugin.name || plugin.id;
|
||||
const version = plugin.version || "0";
|
||||
// 运行备注:调用方显式传入优先;定时任务/流程编排未传时取该账号对该插件的长期备注
|
||||
let note = String(o.note === undefined || o.note === null ? "" : o.note).trim();
|
||||
if (!note) {
|
||||
try { note = notes.get(account, plugin.id); } catch (_) { note = ""; }
|
||||
}
|
||||
state.registerRun(runId, {
|
||||
status: "queued", // 排队中:尚未占用执行槽,started 为空
|
||||
account,
|
||||
@@ -323,6 +332,7 @@ function submitRun(account, plugin, fields, opts) {
|
||||
name,
|
||||
version,
|
||||
trigger,
|
||||
note,
|
||||
schedule_id: o.schedule_id || "",
|
||||
flow_run_id: o.flow_run_id || "",
|
||||
node_id: o.node_id || "",
|
||||
|
||||
@@ -19,6 +19,7 @@ const runner = require("./runner");
|
||||
const queue = require("./queue");
|
||||
const runHistory = require("./run-history");
|
||||
const lastFields = require("./last-fields");
|
||||
const notes = require("./notes");
|
||||
const pluginConfigs = require("./plugin-configs");
|
||||
const scheduler = require("./scheduler");
|
||||
const tasks = require("./tasks");
|
||||
@@ -605,8 +606,17 @@ async function route(req, res, url) {
|
||||
}
|
||||
// 记住本次参数(不含 password 字段),供执行台「恢复上次参数」
|
||||
lastFields.remember(account, plugin.id, body.fields || {}, plugin.fields);
|
||||
// 运行备注:随执行一起落运行记录并回传中心机(用户可能刚改过备注,这里顺手落本机+上报)
|
||||
const noteText = String(body && body.note !== undefined && body.note !== null ? body.note : "").trim();
|
||||
if (noteText) {
|
||||
notes.save(account, plugin.id, noteText);
|
||||
try { await notes.push(account, plugin.id, noteText); } catch (_) { /* 回传失败不阻断执行 */ }
|
||||
}
|
||||
// 提交即入队:返回排队位置,前端据此提示"已加入队列,第 N 位"
|
||||
const r = runner.submitRun(account, plugin, body.fields || {}, { trigger: "manual" });
|
||||
const r = runner.submitRun(account, plugin, body.fields || {}, {
|
||||
trigger: "manual",
|
||||
note: noteText || notes.get(account, plugin.id),
|
||||
});
|
||||
return sendJson(res, 202, {
|
||||
run_id: r.run_id,
|
||||
queue_position: r.queue_position,
|
||||
@@ -841,6 +851,24 @@ async function route(req, res, url) {
|
||||
return sendJson(res, 200, { fields: rec.fields, at: rec.at });
|
||||
}
|
||||
|
||||
// ---------- 插件备注(账号 × 插件 唯一;本机缓存 + 回传中心机)----------
|
||||
if (p === "/api/plugin-notes" && method === "GET") {
|
||||
const account = session.requireLogin(cookies);
|
||||
// 拉取中心机(跨设备一致):失败静默,直接用本机缓存
|
||||
try { await notes.pull(account); } catch (_) { /* 离线继续用本机 */ }
|
||||
return sendJson(res, 200, { ok: true, notes: notes.all(account) });
|
||||
}
|
||||
if (p === "/api/plugin-notes" && method === "POST") {
|
||||
const account = session.requireLogin(cookies);
|
||||
const body = await readJsonBody(req);
|
||||
const pluginId = String((body && body.plugin_id) || "").trim();
|
||||
if (!pluginId) throw Object.assign(new Error("缺少 plugin_id"), { status: 400 });
|
||||
const note = notes.save(account, pluginId, body && body.note);
|
||||
// 回传失败不阻断用户操作(本机已保存,下次拉取/执行时补报)
|
||||
try { await notes.push(account, pluginId, note); } catch (_) { /* 离线:仅本机 */ }
|
||||
return sendJson(res, 200, { ok: true, plugin_id: pluginId, note });
|
||||
}
|
||||
|
||||
// ---------- 场景配置方案(多配置,SQLite 存储)----------
|
||||
if (p === "/api/plugin-configs" && method === "GET") {
|
||||
const account = session.requireLogin(cookies);
|
||||
|
||||
Reference in New Issue
Block a user