Compare commits

..

3 Commits

15 changed files with 1594 additions and 269 deletions
+2 -2
View File
@@ -1,12 +1,12 @@
{
"name": "wechat-data-analysis-desktop",
"version": "1.3.0",
"version": "1.7.10",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "wechat-data-analysis-desktop",
"version": "1.3.0",
"version": "1.7.10",
"dependencies": {
"electron-updater": "^6.7.3"
},
+1 -1
View File
@@ -1,7 +1,7 @@
{
"name": "wechat-data-analysis-desktop",
"private": true,
"version": "1.3.0",
"version": "1.7.10",
"main": "src/main.cjs",
"scripts": {
"dev": "node scripts/dev.cjs",
+201 -19
View File
@@ -22,12 +22,12 @@ const fs = require("fs");
const http = require("http");
const net = require("net");
const path = require("path");
const { Worker } = require("worker_threads");
const {
cleanupOutputDirectoryBackup,
getDefaultOutputDirPath,
getEffectiveOutputDirPath,
migrateOutputDirectory,
normalizeDirectoryPath,
rollbackOutputDirectoryChange,
} = require("./output-dir.cjs");
const DEFAULT_BACKEND_HOST = String(process.env.WECHAT_TOOL_HOST || "127.0.0.1").trim() || "127.0.0.1";
@@ -45,6 +45,7 @@ let isQuitting = false;
let desktopSettings = null;
let backendPortChangeInProgress = false;
let outputDirChangeInProgress = false;
let outputDirChangeProgressState = null;
const gotSingleInstanceLock = app.requestSingleInstanceLock();
if (!gotSingleInstanceLock) {
@@ -279,7 +280,9 @@ function resolveOutputDir() {
if (!dataDir) return null;
const envOutputDir = safeNormalizeDirectory(process.env.WECHAT_TOOL_OUTPUT_DIR || "");
const settingsOutputDir = app.isPackaged ? safeNormalizeDirectory(loadDesktopSettings()?.outputDir || "") : "";
// Allow dev-mode desktop runs to persist the chosen output directory too.
// An explicit environment variable still wins so local launch overrides keep working.
const settingsOutputDir = safeNormalizeDirectory(loadDesktopSettings()?.outputDir || "");
let chosen = null;
try {
@@ -705,6 +708,7 @@ function getOutputDirInfo() {
const defaultPath = getDefaultOutputDir() || "";
const currentPath = resolveOutputDir() || defaultPath;
const hasPending = desktopSettings.pendingOutputDir !== null;
const canChange = !!defaultPath && !!currentPath;
const pendingPath =
desktopSettings.pendingOutputDir === null
? ""
@@ -718,8 +722,8 @@ function getOutputDirInfo() {
pendingPath,
hasPending,
lastError: String(desktopSettings.lastOutputDirError || "").trim(),
canChange: !!app.isPackaged,
changeUnavailableReason: app.isPackaged ? "" : "开发模式不支持界面修改 output 目录",
canChange,
changeUnavailableReason: canChange ? "" : "无法定位 output 目录",
};
}
@@ -749,10 +753,6 @@ function setIgnoredUpdateVersion(version) {
}
async function applyOutputDirChange(nextValue) {
if (!app.isPackaged) {
throw new Error("开发模式不支持界面修改 output 目录");
}
const defaultPath = getDefaultOutputDir();
const currentPath = resolveOutputDir();
if (!defaultPath || !currentPath) {
@@ -785,15 +785,41 @@ async function applyOutputDirChange(nextValue) {
const wasBackendRunning = !!backendProc;
let migration = null;
let settingsSwitched = false;
let retainedBackupPath = "";
let backupCleanupWarning = "";
try {
setOutputDirChangeProgressState({
active: true,
stage: "preparing",
message: wasBackendRunning ? "正在暂停后端并准备迁移 output 目录" : "正在准备迁移 output 目录",
percent: 1,
});
if (wasBackendRunning) {
await stopBackendAndWait({ timeoutMs: 10_000 });
}
migration = migrateOutputDirectory({
currentDir: currentPath,
nextDir: nextPath,
migration = await runOutputDirWorker(
"migrate",
{
currentDir: currentPath,
nextDir: nextPath,
},
(progress) => {
setOutputDirChangeProgressState({
active: true,
...progress,
});
}
);
setOutputDirChangeProgressState({
active: true,
stage: "switching",
message: "正在应用新的 output 目录设置",
percent: 99,
currentFile: "",
});
setOutputDirSetting(nextPath);
@@ -803,11 +829,38 @@ async function applyOutputDirChange(nextValue) {
ensureOutputLink();
if (wasBackendRunning) {
setOutputDirChangeProgressState({
active: true,
stage: "restarting",
message: "正在重启后端并应用新的 output 目录",
percent: 99,
});
startBackend();
await waitForBackend({ timeoutMs: 30_000 });
}
retainedBackupPath = migration?.backupDir || "";
if (retainedBackupPath) {
try {
cleanupOutputDirectoryBackup(retainedBackupPath);
retainedBackupPath = "";
} catch (cleanupErr) {
backupCleanupWarning = `;旧 output 目录未能自动删除:${cleanupErr?.message || cleanupErr}`;
logMain(
`[main] failed to clean output dir backup ${retainedBackupPath}: ${cleanupErr?.message || cleanupErr}`
);
}
}
setOutputDirChangeProgressState({
active: true,
stage: "complete",
message: migration?.sourceWasEmpty ? "output 目录已切换" : "output 目录已迁移并切换",
percent: 100,
});
const info = getOutputDirInfo();
const successMessage =
(migration?.sourceWasEmpty ? "output 目录已切换" : "output 目录已迁移并切换") + backupCleanupWarning;
return {
success: true,
changed: true,
@@ -815,16 +868,22 @@ async function applyOutputDirChange(nextValue) {
defaultPath: info.defaultPath,
isDefault: info.isDefault,
pendingPath: info.pendingPath,
backupPath: migration?.backupDir || "",
backupPath: retainedBackupPath,
sourceWasEmpty: !!migration?.sourceWasEmpty,
message: migration?.sourceWasEmpty ? "output 目录已切换" : "output 目录已迁移并切换",
message: successMessage,
};
} catch (err) {
const message = err?.message || String(err);
let rollbackMessage = "";
if (migration?.changed) {
try {
rollbackOutputDirectoryChange({
setOutputDirChangeProgressState({
active: true,
stage: "rolling-back",
message: "迁移失败,正在回滚 output 目录",
percent: 99,
});
await runOutputDirWorker("rollback", {
previousDir: currentPath,
currentDir: nextPath,
backupDir: migration.backupDir,
@@ -969,6 +1028,119 @@ function setWindowProgressBar(value) {
} catch {}
}
function makeIdleOutputDirChangeProgressState() {
return {
active: false,
stage: "idle",
message: "",
percent: 0,
bytesTransferred: 0,
bytesTotal: 0,
itemsTransferred: 0,
itemsTotal: 0,
currentFile: "",
error: "",
};
}
function clampOutputDirProgressNumber(value) {
const n = Number(value);
if (!Number.isFinite(n) || n < 0) return 0;
return n;
}
function normalizeOutputDirChangeProgressState(next = {}) {
const active = next?.active !== false;
const percent = Math.max(0, Math.min(100, Math.round(Number(next?.percent || 0))));
return {
active,
stage: String(next?.stage || (active ? "running" : "idle")),
message: String(next?.message || ""),
percent,
bytesTransferred: clampOutputDirProgressNumber(next?.bytesTransferred),
bytesTotal: clampOutputDirProgressNumber(next?.bytesTotal),
itemsTransferred: clampOutputDirProgressNumber(next?.itemsTransferred),
itemsTotal: clampOutputDirProgressNumber(next?.itemsTotal),
currentFile: String(next?.currentFile || ""),
error: String(next?.error || ""),
};
}
function getOutputDirChangeProgressState() {
if (!outputDirChangeProgressState) {
outputDirChangeProgressState = makeIdleOutputDirChangeProgressState();
}
return outputDirChangeProgressState;
}
function setOutputDirChangeProgressState(next = {}) {
outputDirChangeProgressState = normalizeOutputDirChangeProgressState(next);
sendToRenderer("app:outputDirChangeProgress", outputDirChangeProgressState);
if (!outputDirChangeProgressState.active) {
setWindowProgressBar(-1);
return outputDirChangeProgressState;
}
const ratio =
outputDirChangeProgressState.percent > 0
? Math.max(0.02, Math.min(1, outputDirChangeProgressState.percent / 100))
: 2;
setWindowProgressBar(ratio);
return outputDirChangeProgressState;
}
function clearOutputDirChangeProgressState() {
return setOutputDirChangeProgressState({ active: false });
}
function getOutputDirWorkerScriptPath() {
return path.join(__dirname, "output-dir-worker.cjs");
}
function runOutputDirWorker(action, payload, onProgress) {
return new Promise((resolve, reject) => {
const worker = new Worker(getOutputDirWorkerScriptPath(), {
workerData: {
action: String(action || "migrate"),
payload,
},
});
let settled = false;
const finish = (err, result) => {
if (settled) return;
settled = true;
if (err) reject(err);
else resolve(result);
};
worker.on("message", (message) => {
if (!message || typeof message !== "object") return;
if (message.type === "progress") {
if (typeof onProgress === "function") onProgress(message.progress || {});
return;
}
if (message.type === "result") {
finish(null, message.result);
return;
}
if (message.type === "error") {
finish(new Error(message.error?.message || "output 目录迁移失败"));
}
});
worker.once("error", (err) => {
finish(err);
});
worker.once("exit", (code) => {
if (settled || code === 0) return;
finish(new Error(`output 目录任务异常退出(code=${code}`));
});
});
}
function looksLikeHtml(input) {
if (!input) return false;
const s = String(input);
@@ -1611,14 +1783,21 @@ function startBackend() {
if (backendProc) return backendProc;
startWcdbSidecar();
const resolvedDataPath = resolveDataDir() || getUserDataDir() || repoRoot();
const resolvedOutputPath = resolveOutputDir() || getDefaultOutputDir() || path.join(resolvedDataPath, "output");
const env = {
...process.env,
WECHAT_TOOL_HOST: getBackendBindHost(),
WECHAT_TOOL_PORT: String(getBackendPort()),
WECHAT_TOOL_DATA_DIR: resolvedDataPath,
WECHAT_TOOL_OUTPUT_DIR: resolvedOutputPath,
// Make sure Python prints UTF-8 to stdout/stderr.
PYTHONIOENCODING: process.env.PYTHONIOENCODING || "utf-8",
};
ensureWcdbSidecarEnv(env);
logMain(
`[main] startBackend packaged=${app.isPackaged} port=${env.WECHAT_TOOL_PORT} dataDir=${env.WECHAT_TOOL_DATA_DIR} outputDir=${env.WECHAT_TOOL_OUTPUT_DIR}`
);
// In packaged mode we expect to provide the generated Nuxt output dir via env.
if (app.isPackaged && !env.WECHAT_TOOL_UI_DIR) {
@@ -1626,8 +1805,6 @@ function startBackend() {
}
if (app.isPackaged) {
env.WECHAT_TOOL_DATA_DIR = resolveDataDir() || app.getPath("userData");
env.WECHAT_TOOL_OUTPUT_DIR = resolveOutputDir() || getDefaultOutputDir() || path.join(env.WECHAT_TOOL_DATA_DIR, "output");
try {
fs.mkdirSync(env.WECHAT_TOOL_DATA_DIR, { recursive: true });
fs.mkdirSync(env.WECHAT_TOOL_OUTPUT_DIR, { recursive: true });
@@ -2156,8 +2333,8 @@ function registerWindowIpc() {
pendingPath: "",
hasPending: false,
lastError: err?.message || String(err),
canChange: !!app.isPackaged,
changeUnavailableReason: app.isPackaged ? "" : "开发模式不支持界面修改 output 目录",
canChange: false,
changeUnavailableReason: "无法读取 output 目录信息",
};
}
});
@@ -2166,6 +2343,10 @@ function registerWindowIpc() {
return resolveOutputDir() || "";
});
ipcMain.handle("app:getOutputDirChangeProgress", () => {
return getOutputDirChangeProgressState();
});
ipcMain.handle("app:openOutputDir", async () => {
const outDir = resolveOutputDir();
if (!outDir) throw new Error("无法定位 output 目录");
@@ -2202,6 +2383,7 @@ function registerWindowIpc() {
};
} finally {
outputDirChangeInProgress = false;
clearOutputDirChangeProgressState();
}
});
+74
View File
@@ -0,0 +1,74 @@
const { parentPort, workerData } = require("worker_threads");
const { migrateOutputDirectory, rollbackOutputDirectoryChange } = require("./output-dir.cjs");
function serializeError(err) {
return {
message: err?.message || String(err),
stack: err?.stack ? String(err.stack) : "",
};
}
function createProgressPoster() {
let lastStage = "";
let lastPercent = -1;
let lastSentAt = 0;
return (progress) => {
const stage = String(progress?.stage || "");
const percent = Number(progress?.percent || 0);
const now = Date.now();
const shouldSend =
stage !== lastStage ||
percent >= 100 ||
percent <= 0 ||
percent >= lastPercent + 1 ||
now - lastSentAt >= 120;
if (!shouldSend) return;
lastStage = stage;
lastPercent = percent;
lastSentAt = now;
parentPort?.postMessage({ type: "progress", progress });
};
}
async function main() {
const action = String(workerData?.action || "migrate");
const payload = workerData?.payload && typeof workerData.payload === "object" ? workerData.payload : {};
if (action === "migrate") {
const result = await migrateOutputDirectory({
...payload,
onProgress: createProgressPoster(),
});
parentPort?.postMessage({ type: "result", result });
return;
}
if (action === "rollback") {
parentPort?.postMessage({
type: "progress",
progress: {
stage: "rolling-back",
message: "迁移失败,正在回滚 output 目录",
percent: 99,
bytesTransferred: 0,
bytesTotal: 0,
itemsTransferred: 0,
itemsTotal: 0,
currentFile: "",
},
});
rollbackOutputDirectoryChange(payload);
parentPort?.postMessage({ type: "result", result: { success: true } });
return;
}
throw new Error(`不支持的 output 目录 worker 动作:${action}`);
}
main().catch((err) => {
parentPort?.postMessage({ type: "error", error: serializeError(err) });
process.exitCode = 1;
});
+281 -8
View File
@@ -1,5 +1,6 @@
const fs = require("fs");
const path = require("path");
const { pipeline } = require("stream/promises");
const SENTINEL_NAMES = [
"account_keys.json",
@@ -10,6 +11,17 @@ const SENTINEL_NAMES = [
"logs",
];
const PROGRESS_STAGE_MESSAGES = {
preparing: "正在准备迁移 output 目录",
scanning: "正在扫描 output 目录",
copying: "正在复制 output 数据",
verifying: "正在校验已复制的数据",
switching: "正在切换 output 目录",
"rolling-back": "迁移失败,正在回滚 output 目录",
restarting: "正在重启后端并应用新的 output 目录",
complete: "output 目录迁移完成",
};
function normalizeDirectoryPath(value) {
const text = String(value || "").trim();
if (!text) return "";
@@ -136,7 +148,233 @@ function ensureTargetIsUsable(targetDir) {
}
}
function migrateOutputDirectory({ currentDir, nextDir, now = new Date() }) {
function clampNonNegativeNumber(value) {
const n = Number(value);
if (!Number.isFinite(n) || n < 0) return 0;
return n;
}
function computeProgressPercent(stage, bytesTransferred, bytesTotal, itemsTransferred, itemsTotal) {
if (stage === "preparing") return 1;
if (stage === "scanning") return 2;
if (stage === "verifying") return 96;
if (stage === "switching") return 99;
if (stage === "complete") return 100;
if (stage === "copying") {
const ratio =
bytesTotal > 0
? Math.min(1, bytesTransferred / bytesTotal)
: itemsTotal > 0
? Math.min(1, itemsTransferred / itemsTotal)
: 1;
return Math.max(5, Math.min(94, Math.round(5 + ratio * 89)));
}
return 0;
}
function buildProgressSnapshot({
stage = "preparing",
bytesTransferred = 0,
bytesTotal = 0,
itemsTransferred = 0,
itemsTotal = 0,
currentFile = "",
}) {
const normalizedStage = String(stage || "preparing");
const safeBytesTransferred = clampNonNegativeNumber(bytesTransferred);
const safeBytesTotal = clampNonNegativeNumber(bytesTotal);
const safeItemsTransferred = clampNonNegativeNumber(itemsTransferred);
const safeItemsTotal = clampNonNegativeNumber(itemsTotal);
return {
stage: normalizedStage,
message: PROGRESS_STAGE_MESSAGES[normalizedStage] || "正在迁移 output 目录",
percent: computeProgressPercent(
normalizedStage,
safeBytesTransferred,
safeBytesTotal,
safeItemsTransferred,
safeItemsTotal
),
bytesTransferred: safeBytesTransferred,
bytesTotal: safeBytesTotal,
itemsTransferred: safeItemsTransferred,
itemsTotal: safeItemsTotal,
currentFile: String(currentFile || ""),
};
}
function emitProgress(onProgress, payload) {
if (typeof onProgress !== "function") return;
onProgress(buildProgressSnapshot(payload));
}
function sortDirectoryEntries(entries) {
return entries.sort((a, b) => String(a.name || "").localeCompare(String(b.name || "")));
}
function depthOfRelativePath(relativePath) {
const text = String(relativePath || "").trim();
if (!text) return 0;
return text.split(path.sep).length;
}
function collectCopyManifest(sourceDir) {
const directories = [];
const files = [];
let totalBytes = 0;
const stack = [""];
while (stack.length > 0) {
const relativeDir = stack.pop();
const absoluteDir = relativeDir ? path.join(sourceDir, relativeDir) : sourceDir;
const dirEntries = sortDirectoryEntries(fs.readdirSync(absoluteDir, { withFileTypes: true }));
for (const dirent of dirEntries) {
const relativePath = relativeDir ? path.join(relativeDir, dirent.name) : dirent.name;
const absolutePath = path.join(sourceDir, relativePath);
const stat = fs.lstatSync(absolutePath);
if (dirent.isDirectory()) {
directories.push({
relativePath,
mode: stat.mode,
atime: stat.atime,
mtime: stat.mtime,
});
stack.push(relativePath);
continue;
}
if (dirent.isFile()) {
files.push({
relativePath,
size: stat.size,
mode: stat.mode,
atime: stat.atime,
mtime: stat.mtime,
});
totalBytes += stat.size;
continue;
}
if (dirent.isSymbolicLink()) {
throw new Error(`output 目录包含不支持的符号链接:${relativePath}`);
}
throw new Error(`output 目录包含不支持的文件类型:${relativePath}`);
}
}
directories.sort((a, b) => depthOfRelativePath(a.relativePath) - depthOfRelativePath(b.relativePath));
return {
directories,
files,
totalBytes,
totalItems: directories.length + files.length,
};
}
function applyStatMetadata(targetPath, statLike) {
try {
if (Number.isInteger(statLike?.mode)) {
fs.chmodSync(targetPath, statLike.mode);
}
} catch {}
try {
if (statLike?.atime && statLike?.mtime) {
fs.utimesSync(targetPath, statLike.atime, statLike.mtime);
}
} catch {}
}
async function copyFileWithProgress({ sourcePath, targetPath, mode, onChunk }) {
await fs.promises.mkdir(path.dirname(targetPath), { recursive: true });
const readStream = fs.createReadStream(sourcePath);
readStream.on("data", (chunk) => {
if (typeof onChunk === "function") onChunk(chunk.length);
});
const writeStream = fs.createWriteStream(targetPath, {
flags: "w",
mode: Number.isInteger(mode) ? mode : undefined,
});
await pipeline(readStream, writeStream);
}
async function copyOutputTree({ sourceDir, targetDir, manifest, onProgress }) {
fs.mkdirSync(targetDir, { recursive: true });
let bytesTransferred = 0;
let itemsTransferred = 0;
emitProgress(onProgress, {
stage: "copying",
bytesTransferred,
bytesTotal: manifest.totalBytes,
itemsTransferred,
itemsTotal: manifest.totalItems,
});
for (const dirEntry of manifest.directories) {
const targetPath = path.join(targetDir, dirEntry.relativePath);
fs.mkdirSync(targetPath, { recursive: true });
itemsTransferred += 1;
emitProgress(onProgress, {
stage: "copying",
bytesTransferred,
bytesTotal: manifest.totalBytes,
itemsTransferred,
itemsTotal: manifest.totalItems,
currentFile: dirEntry.relativePath,
});
}
for (const fileEntry of manifest.files) {
const sourcePath = path.join(sourceDir, fileEntry.relativePath);
const targetPath = path.join(targetDir, fileEntry.relativePath);
await copyFileWithProgress({
sourcePath,
targetPath,
mode: fileEntry.mode,
onChunk: (delta) => {
bytesTransferred += clampNonNegativeNumber(delta);
emitProgress(onProgress, {
stage: "copying",
bytesTransferred,
bytesTotal: manifest.totalBytes,
itemsTransferred,
itemsTotal: manifest.totalItems,
currentFile: fileEntry.relativePath,
});
},
});
applyStatMetadata(targetPath, fileEntry);
itemsTransferred += 1;
emitProgress(onProgress, {
stage: "copying",
bytesTransferred,
bytesTotal: manifest.totalBytes,
itemsTransferred,
itemsTotal: manifest.totalItems,
currentFile: fileEntry.relativePath,
});
}
for (let i = manifest.directories.length - 1; i >= 0; i -= 1) {
const dirEntry = manifest.directories[i];
applyStatMetadata(path.join(targetDir, dirEntry.relativePath), dirEntry);
}
}
async function migrateOutputDirectory({ currentDir, nextDir, now = new Date(), onProgress } = {}) {
const currentPath = normalizeDirectoryPath(currentDir);
const targetPath = normalizeDirectoryPath(nextDir);
if (!currentPath || !targetPath) {
@@ -155,15 +393,19 @@ function migrateOutputDirectory({ currentDir, nextDir, now = new Date() }) {
throw new Error("新旧 output 路径不能互相包含");
}
emitProgress(onProgress, { stage: "scanning" });
ensureTargetIsUsable(targetPath);
const sourceExists = pathExists(currentPath);
if (sourceExists && !isDirectory(currentPath)) {
throw new Error("当前 output 路径不是目录");
}
const sourceWasEmpty = !sourceExists || !hasDirectoryContents(currentPath);
if (sourceWasEmpty) {
emitProgress(onProgress, { stage: "switching" });
fs.mkdirSync(targetPath, { recursive: true });
emitProgress(onProgress, { stage: "complete", itemsTransferred: 1, itemsTotal: 1 });
return {
changed: true,
currentDir: currentPath,
@@ -173,18 +415,34 @@ function migrateOutputDirectory({ currentDir, nextDir, now = new Date() }) {
};
}
const manifest = collectCopyManifest(currentPath);
const tempTarget = makeUniqueSiblingPath(targetPath, "migrating", now);
const backupDir = makeUniqueSiblingPath(currentPath, "backup", now);
fs.cpSync(currentPath, tempTarget, {
recursive: true,
force: false,
errorOnExist: true,
preserveTimestamps: true,
});
try {
await copyOutputTree({
sourceDir: currentPath,
targetDir: tempTarget,
manifest,
onProgress,
});
emitProgress(onProgress, {
stage: "verifying",
bytesTransferred: manifest.totalBytes,
bytesTotal: manifest.totalBytes,
itemsTransferred: manifest.totalItems,
itemsTotal: manifest.totalItems,
});
verifyCopiedOutputTree(currentPath, tempTarget);
emitProgress(onProgress, {
stage: "switching",
bytesTransferred: manifest.totalBytes,
bytesTotal: manifest.totalBytes,
itemsTransferred: manifest.totalItems,
itemsTotal: manifest.totalItems,
});
if (pathExists(targetPath)) {
fs.rmSync(targetPath, { recursive: true, force: true });
}
@@ -209,6 +467,13 @@ function migrateOutputDirectory({ currentDir, nextDir, now = new Date() }) {
throw err;
}
emitProgress(onProgress, {
stage: "complete",
bytesTransferred: manifest.totalBytes,
bytesTotal: manifest.totalBytes,
itemsTransferred: manifest.totalItems,
itemsTotal: manifest.totalItems,
});
return {
changed: true,
currentDir: currentPath,
@@ -242,7 +507,15 @@ function rollbackOutputDirectoryChange({ previousDir, currentDir, backupDir, sou
} catch {}
}
function cleanupOutputDirectoryBackup(backupDir) {
const backupPath = normalizeDirectoryPath(backupDir);
if (!backupPath || !pathExists(backupPath)) return false;
fs.rmSync(backupPath, { recursive: true, force: true });
return !pathExists(backupPath);
}
module.exports = {
cleanupOutputDirectoryBackup,
getDefaultOutputDirPath,
getEffectiveOutputDirPath,
hasDirectoryContents,
+6
View File
@@ -84,10 +84,16 @@ contextBridge.exposeInMainWorld("wechatDesktop", {
// Data/output folder helpers
getOutputDirInfo: () => ipcRenderer.invoke("app:getOutputDirInfo"),
getOutputDir: () => ipcRenderer.invoke("app:getOutputDir"),
getOutputDirChangeProgress: () => ipcRenderer.invoke("app:getOutputDirChangeProgress"),
setOutputDir: (dir) => ipcRenderer.invoke("app:setOutputDir", String(dir ?? "")),
openOutputDir: () => ipcRenderer.invoke("app:openOutputDir"),
getAccountInfo: (account) => ipcRenderer.invoke("app:getAccountInfo", String(account || "")),
deleteAccountData: (account) => ipcRenderer.invoke("app:deleteAccountData", String(account || "")),
onOutputDirChangeProgress: (callback) => {
const handler = (_event, progress) => callback(progress);
ipcRenderer.on("app:outputDirChangeProgress", handler);
return () => ipcRenderer.removeListener("app:outputDirChangeProgress", handler);
},
// Auto update
getVersion: () => ipcRenderer.invoke("app:getVersion"),
+35 -10
View File
@@ -5,6 +5,7 @@ const os = require("os");
const path = require("path");
const {
cleanupOutputDirectoryBackup,
getDefaultOutputDirPath,
getEffectiveOutputDirPath,
migrateOutputDirectory,
@@ -55,14 +56,14 @@ test("getEffectiveOutputDirPath prefers env, then settings, then default", () =>
}
});
test("migrateOutputDirectory switches empty source to a new directory", () => {
test("migrateOutputDirectory switches empty source to a new directory", async () => {
const root = makeTempDir();
const currentDir = path.join(root, "current-output");
const nextDir = path.join(root, "custom-output");
try {
fs.mkdirSync(currentDir, { recursive: true });
const result = migrateOutputDirectory({ currentDir, nextDir });
const result = await migrateOutputDirectory({ currentDir, nextDir });
assert.equal(result.changed, true);
assert.equal(result.sourceWasEmpty, true);
assert.equal(result.backupDir, "");
@@ -73,7 +74,7 @@ test("migrateOutputDirectory switches empty source to a new directory", () => {
}
});
test("migrateOutputDirectory blocks non-empty targets", () => {
test("migrateOutputDirectory blocks non-empty targets", async () => {
const root = makeTempDir();
const currentDir = path.join(root, "current-output");
const nextDir = path.join(root, "custom-output");
@@ -84,8 +85,8 @@ test("migrateOutputDirectory blocks non-empty targets", () => {
fs.mkdirSync(nextDir, { recursive: true });
fs.writeFileSync(path.join(nextDir, "existing.txt"), "occupied");
assert.throws(
() => migrateOutputDirectory({ currentDir, nextDir }),
await assert.rejects(
migrateOutputDirectory({ currentDir, nextDir }),
/已有内容/
);
} finally {
@@ -93,15 +94,15 @@ test("migrateOutputDirectory blocks non-empty targets", () => {
}
});
test("migrateOutputDirectory blocks invalid current paths", () => {
test("migrateOutputDirectory blocks invalid current paths", async () => {
const root = makeTempDir();
const currentDir = path.join(root, "current-output");
const nextDir = path.join(root, "custom-output");
try {
fs.writeFileSync(currentDir, "not-a-directory");
assert.throws(
() => migrateOutputDirectory({ currentDir, nextDir }),
await assert.rejects(
migrateOutputDirectory({ currentDir, nextDir }),
/不是目录/
);
} finally {
@@ -109,7 +110,7 @@ test("migrateOutputDirectory blocks invalid current paths", () => {
}
});
test("migrateOutputDirectory copies data and leaves the old directory as a backup", () => {
test("migrateOutputDirectory copies data and leaves the old directory as a backup", async () => {
const root = makeTempDir();
const currentDir = path.join(root, "current-output");
const nextDir = path.join(root, "custom-output");
@@ -120,7 +121,13 @@ test("migrateOutputDirectory copies data and leaves the old directory as a backu
fs.writeFileSync(path.join(currentDir, "databases", "wxid_test", "session.db"), "session");
fs.writeFileSync(path.join(currentDir, "databases", "wxid_test", "contact.db"), "contact");
const result = migrateOutputDirectory({ currentDir, nextDir, now: new Date("2026-03-30T08:00:00Z") });
const progressEvents = [];
const result = await migrateOutputDirectory({
currentDir,
nextDir,
now: new Date("2026-03-30T08:00:00Z"),
onProgress: (progress) => progressEvents.push(progress),
});
assert.equal(result.changed, true);
assert.equal(result.sourceWasEmpty, false);
assert.match(path.basename(result.backupDir), /^current-output\.backup-\d{14}$/);
@@ -129,6 +136,9 @@ test("migrateOutputDirectory copies data and leaves the old directory as a backu
assert.ok(fs.existsSync(path.join(nextDir, "databases", "wxid_test", "session.db")));
assert.ok(fs.existsSync(result.backupDir));
assert.equal(fs.existsSync(currentDir), false);
assert.ok(progressEvents.some((event) => event.stage === "scanning"));
assert.ok(progressEvents.some((event) => event.stage === "copying" && event.percent > 0));
assert.ok(progressEvents.some((event) => event.stage === "complete" && event.percent === 100));
} finally {
cleanupDir(root);
}
@@ -160,3 +170,18 @@ test("rollbackOutputDirectoryChange restores the previous directory", () => {
cleanupDir(root);
}
});
test("cleanupOutputDirectoryBackup removes a completed migration backup directory", () => {
const root = makeTempDir();
const backupDir = path.join(root, "current-output.backup-20260330080100");
try {
fs.mkdirSync(path.join(backupDir, "databases"), { recursive: true });
fs.writeFileSync(path.join(backupDir, "databases", "session.db"), "restored");
assert.equal(cleanupOutputDirectoryBackup(backupDir), true);
assert.equal(fs.existsSync(backupDir), false);
} finally {
cleanupDir(root);
}
});
+104 -1
View File
@@ -214,6 +214,22 @@
<div v-if="desktopOutputDirCanChange" class="text-[11px] text-[#909090]">
修改后会迁移整个 output 目录如果目标目录已有内容会先阻止并提示
</div>
<div v-if="desktopOutputDirProgress" class="rounded-[6px] border border-[#d8efe2] bg-[#f4fbf7] px-2.5 py-2">
<div class="flex items-center justify-between gap-3 text-[11px] text-[#1b6b43]">
<div class="min-w-0 truncate">{{ desktopOutputDirProgressText }}</div>
<div class="shrink-0 tabular-nums">{{ desktopOutputDirProgressPercentText }}</div>
</div>
<div class="mt-1.5 h-2 overflow-hidden rounded-full bg-[#dceee3]">
<div
class="h-full rounded-full bg-[#07b75b] transition-[width] duration-200 ease-out"
:class="desktopOutputDirProgressIndeterminate ? 'animate-pulse' : ''"
:style="{ width: desktopOutputDirProgressBarWidth }"
/>
</div>
<div v-if="desktopOutputDirProgressDetail" class="mt-1 text-[10px] text-[#5d7a68] break-all">
{{ desktopOutputDirProgressDetail }}
</div>
</div>
<div v-if="desktopOutputDirMessage" class="rounded-[6px] border border-[#d8efe2] bg-[#f4fbf7] px-2.5 py-1.5 text-[11px] text-[#1b6b43] whitespace-pre-wrap">
{{ desktopOutputDirMessage }}
</div>
@@ -410,6 +426,8 @@ const desktopOutputDirMessage = ref('')
const desktopOutputDirIsDefault = ref(true)
const desktopOutputDirCanChange = ref(true)
const desktopOutputDirUnavailableReason = ref('')
const desktopOutputDirProgress = ref(null)
let removeDesktopOutputDirProgressListener = null
const desktopOutputDirText = computed(() => {
if (!isDesktopEnv.value) return '仅桌面端可用'
const v = String(desktopOutputDir.value || '').trim()
@@ -424,6 +442,48 @@ const desktopOutputDirPendingText = computed(() => {
const v = String(desktopOutputDirPending.value || '').trim()
return v || ''
})
const desktopOutputDirProgressPercent = computed(() => {
const n = Number(desktopOutputDirProgress.value?.percent || 0)
if (!Number.isFinite(n) || n < 0) return 0
return Math.max(0, Math.min(100, Math.round(n)))
})
const desktopOutputDirProgressPercentText = computed(() => `${desktopOutputDirProgressPercent.value}%`)
const desktopOutputDirProgressText = computed(() => {
const text = String(desktopOutputDirProgress.value?.message || '').trim()
return text || '正在迁移 output 目录'
})
const desktopOutputDirProgressIndeterminate = computed(() => {
const stage = String(desktopOutputDirProgress.value?.stage || '').trim()
return stage === 'preparing' || stage === 'scanning' || stage === 'rolling-back' || stage === 'restarting'
})
const desktopOutputDirProgressBarWidth = computed(() => {
if (!desktopOutputDirProgress.value) return '0%'
if (desktopOutputDirProgressIndeterminate.value) return '28%'
return `${Math.max(6, desktopOutputDirProgressPercent.value)}%`
})
const desktopOutputDirProgressDetail = computed(() => {
const progress = desktopOutputDirProgress.value
if (!progress) return ''
const parts = []
const bytesTotal = Number(progress.bytesTotal || 0)
const bytesTransferred = Number(progress.bytesTransferred || 0)
const itemsTotal = Number(progress.itemsTotal || 0)
const itemsTransferred = Number(progress.itemsTransferred || 0)
if (bytesTotal > 0) {
parts.push(`${formatBytes(bytesTransferred)} / ${formatBytes(bytesTotal)}`)
} else if (itemsTotal > 0) {
parts.push(`${Math.min(itemsTransferred, itemsTotal)} / ${itemsTotal}`)
}
const currentFile = String(progress.currentFile || '').trim()
if (currentFile) {
parts.push(currentFile)
}
return parts.join(' · ')
})
const desktopOutputDirControlsDisabled = computed(() => (
!isDesktopEnv.value || !desktopOutputDirCanChange.value || desktopOutputDirLoading.value || desktopOutputDirApplying.value
))
@@ -442,6 +502,37 @@ const switchTrackClass = (enabled, disabled = false) => {
return enabled ? 'bg-[#07b75b] hover:brightness-95' : 'bg-[#d0d0d0] hover:brightness-95'
}
const formatBytes = (value) => {
const n = Number(value || 0)
if (!Number.isFinite(n) || n <= 0) return '0 B'
const units = ['B', 'KB', 'MB', 'GB', 'TB']
let next = n
let unitIndex = 0
while (next >= 1024 && unitIndex < units.length - 1) {
next /= 1024
unitIndex += 1
}
const digits = next >= 100 || unitIndex === 0 ? 0 : next >= 10 ? 1 : 2
return `${next.toFixed(digits)} ${units[unitIndex]}`
}
const applyDesktopOutputDirProgress = (progress) => {
if (!progress || progress.active === false) {
desktopOutputDirProgress.value = null
return
}
desktopOutputDirProgress.value = { ...progress }
}
const refreshDesktopOutputDirProgress = async () => {
if (!process.client || typeof window === 'undefined') return
if (!window.wechatDesktop?.getOutputDirChangeProgress) return
try {
const progress = await window.wechatDesktop.getOutputDirChangeProgress()
applyDesktopOutputDirProgress(progress)
} catch {}
}
const sectionElements = computed(() => [
{ key: 'desktop', el: desktopSectionRef.value },
{ key: 'startup', el: startupSectionRef.value },
@@ -676,12 +767,13 @@ const applyDesktopOutputDir = async (nextDir) => {
return
}
if (!desktopOutputDirCanChange.value) {
desktopOutputDirError.value = desktopOutputDirUnavailableReason.value || '开发模式不支持界面修改 output 目录'
desktopOutputDirError.value = desktopOutputDirUnavailableReason.value || '当前环境不支持修改 output 目录'
return
}
desktopOutputDirApplying.value = true
desktopOutputDirError.value = ''
desktopOutputDirMessage.value = ''
desktopOutputDirProgress.value = null
try {
const res = await window.wechatDesktop.setOutputDir(String(nextDir ?? '').trim())
if (res?.success === false) {
@@ -856,6 +948,7 @@ watch(() => props.open, async (isOpen) => {
await refreshBackendLogFileInfo()
if (isDesktopEnv.value) {
await refreshDesktopOutputDir()
await refreshDesktopOutputDirProgress()
}
}, { immediate: true })
@@ -864,6 +957,11 @@ onMounted(async () => {
const isElectron = /electron/i.test(String(navigator.userAgent || ''))
isDesktopEnv.value = isElectron && !!window.wechatDesktop
window.addEventListener('keydown', onEscKeydown)
if (window.wechatDesktop?.onOutputDirChangeProgress) {
removeDesktopOutputDirProgressListener = window.wechatDesktop.onOutputDirChangeProgress((progress) => {
applyDesktopOutputDirProgress(progress)
})
}
}
desktopAutoRealtime.value = readLocalBoolSetting(DESKTOP_SETTING_AUTO_REALTIME_KEY, false)
@@ -876,6 +974,7 @@ onMounted(async () => {
await refreshDesktopAutoLaunch()
await refreshDesktopCloseBehavior()
await refreshDesktopOutputDir()
await refreshDesktopOutputDirProgress()
}
await nextTick()
@@ -885,6 +984,10 @@ onMounted(async () => {
onBeforeUnmount(() => {
if (!process.client || typeof window === 'undefined') return
window.removeEventListener('keydown', onEscKeydown)
if (typeof removeDesktopOutputDirProgressListener === 'function') {
removeDesktopOutputDirProgressListener()
removeDesktopOutputDirProgressListener = null
}
})
</script>
+2 -1
View File
@@ -453,7 +453,7 @@ export const useApi = () => {
return await request(`/chat/exports/${encodeURIComponent(String(exportId))}`, { method: 'DELETE' })
}
// 朋友圈导出(离线 HTML zip
// 朋友圈导出(离线 ZIP,支持 HTML / JSON / TXT
const createSnsExport = async (data = {}) => {
return await request('/sns/exports', {
method: 'POST',
@@ -461,6 +461,7 @@ export const useApi = () => {
account: data.account || null,
scope: data.scope || 'selected',
usernames: Array.isArray(data.usernames) ? data.usernames : [],
format: data.format || 'html',
use_cache: data.use_cache == null ? true : !!data.use_cache,
output_dir: data.output_dir == null ? null : String(data.output_dir || '').trim(),
file_name: data.file_name || null
+442 -41
View File
@@ -14,13 +14,61 @@
class="mt-2 w-full px-3 py-2 rounded-md border border-gray-200 bg-white text-sm outline-none focus:ring-2 focus:ring-[#576b95]/30 focus:border-[#576b95]"
/>
<div class="mt-3">
<div class="text-xs font-medium text-gray-700 mb-2">导出格式</div>
<div class="flex flex-wrap gap-2">
<label
v-for="item in exportFormatOptions"
:key="item.value"
class="px-2.5 py-1 text-xs rounded-md border cursor-pointer transition-colors"
:class="exportFormat === item.value ? 'bg-[#03C160] text-white border-[#03C160]' : 'bg-white border-gray-200 text-gray-700 hover:bg-gray-50'"
>
<input v-model="exportFormat" type="radio" :value="item.value" class="hidden" />
<span>{{ item.label }}</span>
</label>
</div>
</div>
<div class="mt-3 space-y-2">
<div class="flex items-center justify-between gap-2">
<div class="text-xs font-medium text-gray-700">&#23548;&#20986;&#30446;&#24405;</div>
<div class="text-[11px] text-gray-400">{{ exportFolderModeText }}</div>
</div>
<div class="px-2.5 py-2 rounded-md border border-gray-200 bg-gray-50 text-xs text-gray-600 break-all min-h-[40px] flex items-center">
{{ exportFolder || '&#26410;&#36873;&#25321;' }}
</div>
<div class="flex gap-2">
<button
type="button"
class="flex-1 px-3 py-2 rounded-md text-sm border border-gray-200 bg-white hover:bg-gray-50 transition-colors disabled:opacity-50 disabled:cursor-not-allowed"
:disabled="exportSaveBusy"
@click="chooseExportFolder"
>
&#36873;&#25321;&#25991;&#20214;&#22841;
</button>
<button
v-if="hasSelectedExportFolder"
type="button"
class="px-3 py-2 rounded-md text-sm border border-gray-200 bg-white hover:bg-gray-50 transition-colors disabled:opacity-50 disabled:cursor-not-allowed"
:disabled="exportSaveBusy"
@click="clearExportFolderSelection"
>
&#28165;&#38500;
</button>
</div>
<div v-if="exportFolderHint" class="text-[11px] text-gray-500 whitespace-pre-wrap">{{ exportFolderHint }}</div>
<div v-if="exportSaveProgressText" class="text-[11px] text-gray-500 whitespace-pre-wrap">{{ exportSaveProgressText }}</div>
<div v-else-if="exportSaveMsg" class="text-[11px] text-green-600 whitespace-pre-wrap">{{ exportSaveMsg }}</div>
<div v-else-if="exportSaveError" class="text-[11px] text-red-600 whitespace-pre-wrap">{{ exportSaveError }}</div>
</div>
<div class="mt-2 flex gap-2">
<button
type="button"
class="flex-1 px-3 py-2 rounded-md text-sm border border-gray-200 bg-white hover:bg-gray-50 transition-colors disabled:opacity-50 disabled:cursor-not-allowed"
@click="onExportAllClick"
:disabled="!selectedAccount || exportJob?.status === 'running'"
title="导出全部朋友圈(HTML 离线 ZIP"
:disabled="!selectedAccount || exportJob?.status === 'running' || exportJob?.status === 'queued'"
:title="`导出全部朋友圈(${exportFormatLabel} ZIP`"
>
导出全部
</button>
@@ -28,23 +76,71 @@
type="button"
class="flex-1 px-3 py-2 rounded-md text-sm border border-gray-200 bg-white hover:bg-gray-50 transition-colors disabled:opacity-50 disabled:cursor-not-allowed"
@click="onExportCurrentClick"
:disabled="!selectedAccount || !selectedSnsUser || exportJob?.status === 'running'"
title="导出当前选中联系人(HTML 离线 ZIP"
:disabled="!selectedAccount || !selectedSnsUser || exportJob?.status === 'running' || exportJob?.status === 'queued'"
:title="`导出当前选中联系人(${exportFormatLabel} ZIP`"
>
导出此人
</button>
</div>
<div v-if="exportError" class="mt-2 text-xs text-red-600 whitespace-pre-wrap">{{ exportError }}</div>
<div v-else-if="exportJob" class="mt-2 text-xs text-gray-500">
<span>导出状态{{ exportJob.status }}</span>
<button
v-if="exportJob.status === 'done' && exportJob.exportId"
type="button"
class="ml-2 text-xs text-[#576b95] hover:underline bg-transparent border-0 p-0"
@click="downloadSnsExport(exportJob.exportId)"
>
下载 ZIP
</button>
<div v-else-if="exportJob" class="mt-3 border border-gray-200 rounded-md bg-gray-50 p-3 text-xs text-gray-700 space-y-2">
<div class="flex items-center justify-between gap-2">
<div class="font-medium text-gray-900 truncate">任务{{ exportJob.exportId }}</div>
<div class="text-gray-500">状态{{ exportStatusText }}</div>
</div>
<div class="flex items-center justify-between">
<div>动态{{ exportJob.progress?.postsExported || 0 }}/{{ exportJob.progress?.postsTotal || 0 }}</div>
<div class="text-gray-500">{{ exportOverallPercent }}%</div>
</div>
<div class="h-2 rounded-full bg-white border border-gray-200 overflow-hidden">
<div class="h-full bg-[#03C160] transition-all duration-300" :style="{ width: exportOverallPercent + '%' }"></div>
</div>
<div class="flex items-center justify-between text-gray-600">
<div>联系人{{ exportJob.progress?.usersDone || 0 }}/{{ exportJob.progress?.usersTotal || 0 }}</div>
<div>格式{{ exportActiveFormatLabel }}</div>
</div>
<div v-if="exportCurrentTargetLabel" class="space-y-1">
<div class="flex items-center justify-between gap-2">
<div class="truncate">
当前{{ exportCurrentTargetLabel }}{{ exportJob.progress?.currentUserPostsDone || 0 }}/{{ exportJob.progress?.currentUserPostsTotal || 0 }}
</div>
<div class="text-gray-500">
<span v-if="exportCurrentPercent != null">{{ exportCurrentPercent }}%</span>
<span v-else></span>
</div>
</div>
<div class="h-2 rounded-full bg-white border border-gray-200 overflow-hidden">
<div
v-if="exportCurrentPercent != null"
class="h-full bg-sky-500 transition-all duration-300"
:style="{ width: exportCurrentPercent + '%' }"
></div>
<div v-else class="h-full bg-sky-500/60 animate-pulse" style="width: 30%"></div>
</div>
</div>
<div class="text-gray-500">
媒体{{ exportJob.progress?.mediaCopied || 0 }}缺失{{ exportJob.progress?.mediaMissing || 0 }}
</div>
<div v-if="exportOutputPathText" class="text-green-600 break-all">
&#24050;&#23548;&#20986;&#21040;&#65306;{{ exportOutputPathText }}
</div>
<div v-if="exportJob.status === 'done'" class="flex flex-wrap gap-3">
<button
v-if="exportJob.exportId && hasWebExportFolder"
type="button"
class="text-xs text-[#576b95] hover:underline bg-transparent border-0 p-0 disabled:text-gray-400 disabled:no-underline disabled:cursor-not-allowed"
:disabled="exportSaveBusy"
@click="saveSnsExportToSelectedFolder()"
>
{{ exportSaveBusy ? '\u4fdd\u5b58\u4e2d\u2026' : exportSaveState === 'success' ? '\u91cd\u65b0\u4fdd\u5b58\u5230\u6587\u4ef6\u5939' : '\u4fdd\u5b58\u5230\u5df2\u9009\u6587\u4ef6\u5939' }}
</button>
</div>
</div>
</div>
@@ -664,7 +760,7 @@ import { useChatAccountsStore } from '~/stores/chatAccounts'
import { usePrivacyStore } from '~/stores/privacy'
import { parseTextWithEmoji } from '~/lib/wechat-emojis'
import { SNS_SETTING_USE_CACHE_KEY, readLocalBoolSetting } from '~/lib/desktop-settings'
import { reportServerErrorFromError } from '~/lib/server-error-logging'
import { reportServerErrorFromError, reportServerErrorFromResponse } from '~/lib/server-error-logging'
useHead({ title: '朋友圈 - 微信数据分析助手' })
@@ -762,12 +858,305 @@ const pageSize = 20
const apiBase = useApiBase()
// 朋友圈导出(HTML 离线 ZIP
// 朋友圈导出(离线 ZIP
const exportFormat = ref('html')
const exportFormatOptions = [
{ value: 'html', label: 'HTML' },
{ value: 'json', label: 'JSON' },
{ value: 'txt', label: 'TXT' }
]
const exportFolder = ref('')
const exportFolderHandle = ref(null)
const exportSaveBusy = ref(false)
const exportSaveMsg = ref('')
const exportSaveError = ref('')
const exportSaveState = ref('idle')
const exportSaveBytesWritten = ref(0)
const exportSaveBytesTotal = ref(0)
const exportAutoSavedFor = ref('')
const exportJob = ref(null)
const exportError = ref('')
let exportEventSource = null
let exportPollTimer = null
const asNumber = (v) => {
const n = Number(v)
return Number.isFinite(n) ? n : 0
}
const clamp01 = (v) => Math.max(0, Math.min(1, Number(v) || 0))
const formatBytes = (value) => {
const bytes = Number(value)
if (!Number.isFinite(bytes) || bytes <= 0) return '0 B'
const units = ['B', 'KB', 'MB', 'GB', 'TB']
let size = bytes
let index = 0
while (size >= 1024 && index < units.length - 1) {
size /= 1024
index += 1
}
const digits = size >= 100 || index === 0 ? 0 : size >= 10 ? 1 : 2
return `${size.toFixed(digits)} ${units[index]}`
}
const resetExportSaveFeedback = ({ resetAutoSavedFor = false } = {}) => {
exportSaveMsg.value = ''
exportSaveError.value = ''
exportSaveState.value = 'idle'
exportSaveBytesWritten.value = 0
exportSaveBytesTotal.value = 0
if (resetAutoSavedFor) exportAutoSavedFor.value = ''
}
const isDesktopExportRuntime = () => {
return !!(process.client && window?.wechatDesktop?.chooseDirectory)
}
const isWebDirectoryPickerSupported = () => {
return !!(process.client && typeof window.showDirectoryPicker === 'function')
}
const hasDesktopExportFolder = computed(() => {
return !!(isDesktopExportRuntime() && String(exportFolder.value || '').trim())
})
const hasWebExportFolder = computed(() => {
return !!(!isDesktopExportRuntime() && isWebDirectoryPickerSupported() && exportFolderHandle.value)
})
const hasSelectedExportFolder = computed(() => {
return !!(hasDesktopExportFolder.value || hasWebExportFolder.value)
})
const exportFormatLabel = computed(() => {
return exportFormatOptions.find((item) => item.value === exportFormat.value)?.label || 'HTML'
})
const exportActiveFormat = computed(() => {
const raw = String(exportJob.value?.options?.format || exportFormat.value || 'html').trim().toLowerCase()
return exportFormatOptions.some((item) => item.value === raw) ? raw : 'html'
})
const exportActiveFormatLabel = computed(() => {
return exportFormatOptions.find((item) => item.value === exportActiveFormat.value)?.label || 'HTML'
})
const exportStatusText = computed(() => {
const status = String(exportJob.value?.status || '').trim()
return {
queued: '排队中',
running: '导出中',
done: '已完成',
error: '失败',
cancelled: '已取消'
}[status] || status || '-'
})
const exportOverallPercent = computed(() => {
const status = String(exportJob.value?.status || '').trim()
if (status === 'done') return 100
const progress = exportJob.value?.progress || {}
const postsTotal = asNumber(progress.postsTotal)
const postsDone = asNumber(progress.postsExported)
if (postsTotal > 0) return Math.round(clamp01(postsDone / postsTotal) * 100)
const usersTotal = asNumber(progress.usersTotal)
const usersDone = asNumber(progress.usersDone)
if (usersTotal > 0) return Math.round(clamp01(usersDone / usersTotal) * 100)
return 0
})
const exportCurrentPercent = computed(() => {
const progress = exportJob.value?.progress || {}
const total = asNumber(progress.currentUserPostsTotal)
const done = asNumber(progress.currentUserPostsDone)
if (total <= 0) return null
return Math.round(clamp01(done / total) * 100)
})
const exportCurrentTargetLabel = computed(() => {
const progress = exportJob.value?.progress || {}
return String(progress.currentDisplayName || progress.currentUsername || '').trim()
})
const exportBackendZipPath = computed(() => {
return String(exportJob.value?.zipPath || '').trim()
})
const exportFolderModeText = computed(() => {
if (isDesktopExportRuntime()) return '\u684c\u9762\u7aef\u76ee\u5f55'
if (isWebDirectoryPickerSupported()) return '\u6d4f\u89c8\u5668\u76ee\u5f55'
return '\u9700\u9009\u62e9\u6587\u4ef6\u5939'
})
const exportFolderHint = computed(() => {
if (isDesktopExportRuntime()) {
return hasDesktopExportFolder.value
? '\u4f1a\u50cf\u666e\u901a\u804a\u5929\u8bb0\u5f55\u5bfc\u51fa\u4e00\u6837\uff0c\u5b8c\u6210\u540e\u76f4\u63a5\u5199\u5165\u4e0a\u9762\u7684\u6587\u4ef6\u5939\u3002'
: '\u8bf7\u5148\u9009\u62e9\u6587\u4ef6\u5939\uff0c\u5bfc\u51fa\u5b8c\u6210\u540e\u4f1a\u76f4\u63a5\u5199\u5165\u8be5\u76ee\u5f55\u3002'
}
if (isWebDirectoryPickerSupported()) {
return hasWebExportFolder.value
? '\u5bfc\u51fa\u5b8c\u6210\u540e\u4f1a\u81ea\u52a8\u4fdd\u5b58\u5230\u6240\u9009\u6d4f\u89c8\u5668\u76ee\u5f55\u3002'
: '\u8bf7\u5148\u9009\u62e9\u6d4f\u89c8\u5668\u76ee\u5f55\uff0c\u5bfc\u51fa\u5b8c\u6210\u540e\u4f1a\u81ea\u52a8\u4fdd\u5b58\u3002'
}
return '\u5f53\u524d\u73af\u5883\u4e0d\u652f\u6301\u76ee\u5f55\u9009\u62e9\uff0c\u8bf7\u4f7f\u7528\u684c\u9762\u7aef\u6216 Chromium \u65b0\u7248\u6d4f\u89c8\u5668\u3002'
})
const guessSnsExportZipName = (job) => {
const raw = String(job?.zipPath || '').trim()
if (raw) {
const name = raw.replace(/\\/g, '/').split('/').pop()
if (name && name.toLowerCase().endsWith('.zip')) return name
}
const format = String(job?.options?.format || exportFormat.value || 'html').trim().toLowerCase() || 'html'
const exportId = String(job?.exportId || '').trim() || 'export'
return `wechat_sns_export_${format}_${exportId}.zip`
}
const exportSaveProgressText = computed(() => {
if (exportSaveState.value !== 'saving') return ''
const fileName = guessSnsExportZipName(exportJob.value)
if (exportSaveBytesTotal.value > 0) {
return `\u6b63\u5728\u4fdd\u5b58\u5230\u6d4f\u89c8\u5668\u76ee\u5f55\uff1a${fileName}\uff08${formatBytes(exportSaveBytesWritten.value)} / ${formatBytes(exportSaveBytesTotal.value)}\uff09`
}
return `\u6b63\u5728\u4fdd\u5b58\u5230\u6d4f\u89c8\u5668\u76ee\u5f55\uff1a${fileName}\uff08${formatBytes(exportSaveBytesWritten.value)}\uff09`
})
const exportOutputPathText = computed(() => {
if (String(exportJob.value?.status || '') !== 'done') return ''
if (hasWebExportFolder.value) return ''
const raw = exportBackendZipPath.value
if (!raw) return ''
if (isDesktopExportRuntime()) return raw
const requestedOutputDir = String(exportJob.value?.options?.outputDir || '').trim()
return requestedOutputDir ? raw : ''
})
const chooseExportFolder = async () => {
exportError.value = ''
resetExportSaveFeedback()
try {
if (!process.client) {
exportError.value = '\u5f53\u524d\u73af\u5883\u4e0d\u652f\u6301\u9009\u62e9\u5bfc\u51fa\u76ee\u5f55'
return
}
if (isDesktopExportRuntime()) {
const result = await window.wechatDesktop.chooseDirectory({ title: '\u9009\u62e9\u5bfc\u51fa\u76ee\u5f55' })
if (result && !result.canceled && Array.isArray(result.filePaths) && result.filePaths.length > 0) {
exportFolder.value = String(result.filePaths[0] || '').trim()
exportFolderHandle.value = null
}
return
}
if (isWebDirectoryPickerSupported()) {
const handle = await window.showDirectoryPicker()
if (handle) {
exportFolderHandle.value = handle
exportFolder.value = `\u6d4f\u89c8\u5668\u76ee\u5f55\uff1a${String(handle.name || '\u5df2\u9009\u62e9')}`
}
return
}
exportError.value = '\u5f53\u524d\u6d4f\u89c8\u5668\u4e0d\u652f\u6301\u76ee\u5f55\u9009\u62e9\uff0c\u8bf7\u4f7f\u7528\u684c\u9762\u7aef\u6216 Chromium \u65b0\u7248\u6d4f\u89c8\u5668'
} catch (error) {
const message = String(error?.message || '').trim()
if (error?.name === 'AbortError' || message.includes('The user aborted a request')) {
return
}
exportError.value = error?.message || '\u9009\u62e9\u5bfc\u51fa\u76ee\u5f55\u5931\u8d25'
}
}
const clearExportFolderSelection = () => {
exportFolder.value = ''
exportFolderHandle.value = null
resetExportSaveFeedback({ resetAutoSavedFor: true })
}
const getSnsExportDownloadUrl = (exportId) => {
return `${apiBase}/sns/exports/${encodeURIComponent(String(exportId || ''))}/download`
}
const saveSnsExportToSelectedFolder = async (options = {}) => {
const autoSave = !!options?.auto
exportError.value = ''
resetExportSaveFeedback()
if (!process.client || !isWebDirectoryPickerSupported()) {
exportError.value = '\u5f53\u524d\u73af\u5883\u4e0d\u652f\u6301\u4fdd\u5b58\u5230\u6d4f\u89c8\u5668\u76ee\u5f55'
return
}
const handle = exportFolderHandle.value
if (!handle || typeof handle.getFileHandle !== 'function') {
exportError.value = '\u8bf7\u5148\u9009\u62e9\u6d4f\u89c8\u5668\u5bfc\u51fa\u76ee\u5f55'
return
}
const exportId = exportJob.value?.exportId
if (!exportId || String(exportJob.value?.status || '') !== 'done') {
exportError.value = '\u5bfc\u51fa\u4efb\u52a1\u5c1a\u672a\u5b8c\u6210'
return
}
exportSaveBusy.value = true
exportSaveState.value = 'saving'
try {
const response = await fetch(getSnsExportDownloadUrl(exportId))
if (!response.ok) {
await reportServerErrorFromResponse(response, {
method: 'GET',
requestUrl: getSnsExportDownloadUrl(exportId),
message: `\u4e0b\u8f7d\u5bfc\u51fa\u6587\u4ef6\u5931\u8d25\uff08${response.status}\uff09`,
source: 'sns.exportDownload'
})
throw new Error(`\u4e0b\u8f7d\u5bfc\u51fa\u6587\u4ef6\u5931\u8d25\uff08${response.status}\uff09`)
}
exportSaveBytesTotal.value = asNumber(response.headers.get('Content-Length'))
const fileName = guessSnsExportZipName(exportJob.value)
const fileHandle = await handle.getFileHandle(fileName, { create: true })
const writable = await fileHandle.createWritable()
if (response.body && typeof response.body.getReader === 'function') {
const reader = response.body.getReader()
try {
while (true) {
const { done, value } = await reader.read()
if (done) break
if (!value || !value.byteLength) continue
await writable.write(value)
exportSaveBytesWritten.value += value.byteLength
}
await writable.close()
} catch (error) {
try {
await reader.cancel()
} catch {}
try {
await writable.abort()
} catch {}
throw error
}
} else {
const blob = await response.blob()
exportSaveBytesWritten.value = asNumber(blob.size)
if (exportSaveBytesTotal.value <= 0) exportSaveBytesTotal.value = exportSaveBytesWritten.value
await writable.write(blob)
await writable.close()
}
exportAutoSavedFor.value = String(exportId)
exportSaveState.value = 'success'
const folderLabel = String(exportFolder.value || '').trim() || '\u5df2\u9009\u76ee\u5f55'
exportSaveMsg.value = autoSave
? `\u6d4f\u89c8\u5668\u76ee\u5f55\u81ea\u52a8\u4fdd\u5b58\u6210\u529f\uff1a${fileName}\n\u4f4d\u7f6e\uff1a${folderLabel}`
: `\u6d4f\u89c8\u5668\u76ee\u5f55\u4fdd\u5b58\u6210\u529f\uff1a${fileName}\n\u4f4d\u7f6e\uff1a${folderLabel}`
} catch (error) {
exportSaveState.value = 'error'
exportSaveError.value = `\u6d4f\u89c8\u5668\u76ee\u5f55\u4fdd\u5b58\u5931\u8d25\uff1a${error?.message || '\u672a\u77e5\u9519\u8bef'}`
} finally {
exportSaveBusy.value = false
}
}
const stopSnsExportPolling = () => {
if (exportEventSource) {
try {
@@ -828,52 +1217,48 @@ const startSnsExportPolling = (exportId) => {
startSnsExportHttpPolling(exportId)
}
const downloadSnsExport = (exportId) => {
if (!process.client) return
const id = String(exportId || '').trim()
if (!id) return
const url = `${apiBase}/sns/exports/${encodeURIComponent(id)}/download`
window.open(url, '_blank', 'noopener,noreferrer')
const ensureSnsExportFolderReady = () => {
if (hasSelectedExportFolder.value) return true
exportError.value = isDesktopExportRuntime() || isWebDirectoryPickerSupported()
? '\u8bf7\u5148\u9009\u62e9\u5bfc\u51fa\u76ee\u5f55'
: '\u5f53\u524d\u73af\u5883\u4e0d\u652f\u6301\u76ee\u5f55\u9009\u62e9\uff0c\u8bf7\u4f7f\u7528\u684c\u9762\u7aef\u6216 Chromium \u65b0\u7248\u6d4f\u89c8\u5668'
return false
}
const onExportAllClick = async () => {
const startSnsExport = async ({ scope, usernames }) => {
if (!selectedAccount.value) return
exportError.value = ''
resetExportSaveFeedback({ resetAutoSavedFor: true })
if (!ensureSnsExportFolderReady()) return
try {
const resp = await api.createSnsExport({
account: selectedAccount.value,
scope: 'all',
usernames: [],
use_cache: snsUseCache.value ? 1 : 0
scope,
usernames: Array.isArray(usernames) ? usernames : [],
format: exportFormat.value,
use_cache: snsUseCache.value ? 1 : 0,
output_dir: hasDesktopExportFolder.value ? String(exportFolder.value || '').trim() : null
})
exportJob.value = resp?.job || null
const exportId = exportJob.value?.exportId
if (exportId) startSnsExportPolling(exportId)
} catch (e) {
exportError.value = e?.message || '创建导出任务失败'
exportError.value = e?.message || '\u521b\u5efa\u5bfc\u51fa\u4efb\u52a1\u5931\u8d25'
}
}
const onExportAllClick = async () => {
await startSnsExport({ scope: 'all', usernames: [] })
}
const onExportCurrentClick = async () => {
if (!selectedAccount.value) return
const uname = String(selectedSnsUser.value || '').trim()
if (!uname) return
exportError.value = ''
try {
const resp = await api.createSnsExport({
account: selectedAccount.value,
scope: 'selected',
usernames: [uname],
use_cache: snsUseCache.value ? 1 : 0
})
exportJob.value = resp?.job || null
const exportId = exportJob.value?.exportId
if (exportId) startSnsExportPolling(exportId)
} catch (e) {
exportError.value = e?.message || '创建导出任务失败'
}
await startSnsExport({ scope: 'selected', usernames: [uname] })
}
// Track failed images per-post, per-index to render placeholders instead of broken <img>.
const mediaErrors = ref({})
@@ -1809,6 +2194,7 @@ watch(
stopSnsExportPolling()
exportJob.value = null
exportError.value = ''
resetExportSaveFeedback({ resetAutoSavedFor: true })
snsUserQuery.value = ''
selectedSnsUser.value = ''
snsUsers.value = []
@@ -1823,6 +2209,21 @@ watch(
{ immediate: true }
)
watch(
() => ({
exportId: String(exportJob.value?.exportId || ''),
status: String(exportJob.value?.status || '')
}),
async ({ exportId, status }) => {
if (!process.client || status !== 'done' || !exportId) return
if (!hasWebExportFolder.value) return
if (exportAutoSavedFor.value === exportId) return
if (exportSaveBusy.value) return
await saveSnsExportToSelectedFolder({ auto: true })
}
)
onMounted(async () => {
privacyStore.init()
+1 -1
View File
@@ -1,6 +1,6 @@
[project]
name = "wechat-decrypt-tool"
version = "1.3.0"
version = "1.7.10"
description = "Modern WeChat database decryption tool with React frontend"
readme = "README.md"
requires-python = ">=3.11"
+1 -1
View File
@@ -1,5 +1,5 @@
"""微信数据库解密工具
"""
__version__ = "1.3.0"
__version__ = "1.7.10"
__author__ = "WeChat Decrypt Tool"
@@ -13,23 +13,26 @@ from ..sns_export_service import SNS_EXPORT_MANAGER
router = APIRouter(route_class=PathFixRoute)
ExportScope = Literal["selected", "all"]
ExportFormat = Literal["html", "json", "txt"]
class SnsExportCreateRequest(BaseModel):
account: Optional[str] = Field(None, description="账号目录名(可选,默认使用第一个)")
scope: ExportScope = Field("selected", description="导出范围:selected=指定联系人;all=全部联系人")
usernames: list[str] = Field(default_factory=list, description="朋友圈 username 列表(scope=selected 时使用)")
format: ExportFormat = Field("html", description="导出格式:html/json/txt")
use_cache: bool = Field(True, description="是否复用导出过程中的本地缓存(默认开启)")
output_dir: Optional[str] = Field(None, description="导出目录绝对路径(可选;不填时使用默认目录)")
file_name: Optional[str] = Field(None, description="导出 zip 文件名(可选,不含/含 .zip 都可)")
@router.post("/api/sns/exports", summary="创建朋友圈导出任务(离线 HTML zip")
@router.post("/api/sns/exports", summary="创建朋友圈导出任务(离线 ZIP,支持 HTML/JSON/TXT")
async def create_sns_export(req: SnsExportCreateRequest):
job = SNS_EXPORT_MANAGER.create_job(
account=req.account,
scope=req.scope,
usernames=req.usernames,
export_format=req.format,
use_cache=bool(req.use_cache),
output_dir=req.output_dir,
file_name=req.file_name,
@@ -111,4 +114,3 @@ async def cancel_sns_export(export_id: str):
if not ok:
raise HTTPException(status_code=404, detail="Export not found.")
return {"status": "success"}
+439 -181
View File
@@ -1,6 +1,6 @@
from __future__ import annotations
"""SNS (Moments) HTML export service (offline ZIP)."""
"""SNS (Moments) export service (offline ZIP)."""
import asyncio
from bisect import bisect_left, bisect_right
@@ -50,6 +50,7 @@ logger = get_logger(__name__)
ExportStatus = Literal["queued", "running", "done", "error", "cancelled"]
ExportScope = Literal["selected", "all"]
ExportFormat = Literal["html", "json", "txt"]
_INVALID_PATH_CHARS = re.compile(r'[<>:"/\\|?*\x00-\x1f]')
_HEX_ONLY_RE = re.compile(r"[^0-9a-fA-F]+")
@@ -408,9 +409,71 @@ def _esc_attr(v: Any) -> str:
return html.escape(str(v or ""), quote=True)
def _json_safe(value: Any) -> Any:
if value is None or isinstance(value, (str, int, float, bool)):
return value
if isinstance(value, dict):
return {str(k): _json_safe(v) for k, v in value.items()}
if isinstance(value, (list, tuple)):
return [_json_safe(v) for v in value]
return str(value)
def _guess_official_name_from_title(title: str) -> str:
t0 = str(title or "").strip()
if not t0:
return ""
m = re.search(r"[《「【](.+?)[》」】]", t0)
return str(m.group(1) or "").strip() if m and m.group(1) else ""
def _format_moment_type_label(post: dict[str, Any]) -> str:
try:
t = int(post.get("type") or 0)
except Exception:
t = 0
if t == 3:
off = post.get("official") if isinstance(post.get("official"), dict) else {}
st0 = off.get("serviceType") if isinstance(off, dict) else None
try:
st = int(st0) if st0 not in (None, "") else None
except Exception:
st = None
prefix = "服务号" if st == 1 else "公众号"
name = str(off.get("displayName") or "").strip() if isinstance(off, dict) else ""
if not name:
name = _guess_official_name_from_title(str(post.get("title") or ""))
return f"{prefix}·{name}" if name else prefix
if t == 28:
ff = post.get("finderFeed") if isinstance(post.get("finderFeed"), dict) else {}
name = str(ff.get("nickname") or "").strip() if isinstance(ff, dict) else ""
return f"视频号·{name}" if name else "视频号"
if t in (5, 42):
name0 = str(post.get("sourceName") or "").strip()
if name0:
return name0
url0 = str(post.get("contentUrl") or "").strip()
if not url0:
ml0 = post.get("media") if isinstance(post.get("media"), list) else []
m0 = ml0[0] if (ml0 and isinstance(ml0[0], dict)) else {}
url0 = str(m0.get("url") or "").strip()
if url0:
s = re.sub(r"^https?://", "", url0.strip(), flags=re.I)
s = s.split("#", 1)[0].split("?", 1)[0].rstrip("/")
return s or ("音乐" if t == 42 else "外部分享")
return "音乐" if t == 42 else "外部分享"
return ""
_SNS_EXPORT_CSS_PATCH = """
/* Moments export tweaks (keep consistent with frontend `sns.vue`). */
body { background-color: #EDEDED; }
.wse-sns-post-list > .wse-sns-post:first-child {
padding-top: 0;
}
.wse-sns-post-list > .wse-sns-post:first-child > .wse-sns-post-row {
padding-top: 12px;
}
.wse-live-photo video { display: none; }
.wse-live-photo:hover video { display: block; }
.wse-live-photo:hover img { display: none; }
@@ -477,7 +540,10 @@ class ExportProgress:
users_done: int = 0
current_username: str = ""
current_display_name: str = ""
posts_total: int = 0
posts_exported: int = 0
current_user_posts_total: int = 0
current_user_posts_done: int = 0
media_copied: int = 0
media_missing: int = 0
@@ -513,7 +579,10 @@ class ExportJob:
"usersDone": self.progress.users_done,
"currentUsername": self.progress.current_username,
"currentDisplayName": self.progress.current_display_name,
"postsTotal": self.progress.posts_total,
"postsExported": self.progress.posts_exported,
"currentUserPostsTotal": self.progress.current_user_posts_total,
"currentUserPostsDone": self.progress.current_user_posts_done,
"mediaCopied": self.progress.media_copied,
"mediaMissing": self.progress.media_missing,
},
@@ -554,6 +623,7 @@ class SnsExportManager:
account: Optional[str],
scope: ExportScope,
usernames: list[str],
export_format: ExportFormat,
use_cache: bool,
output_dir: Optional[str],
file_name: Optional[str],
@@ -568,6 +638,7 @@ class SnsExportManager:
options={
"scope": str(scope or "selected"),
"usernames": [str(u or "").strip() for u in (usernames or []) if str(u or "").strip()],
"format": str(export_format or "html"),
"useCache": bool(use_cache),
"outputDir": str(output_dir or "").strip(),
"fileName": str(file_name or "").strip(),
@@ -627,6 +698,10 @@ class SnsExportManager:
opts = dict(job.options or {})
scope_raw = str(opts.get("scope") or "selected").strip() or "selected"
scope: ExportScope = "all" if scope_raw == "all" else "selected" # type: ignore[assignment]
export_format_raw = str(opts.get("format") or "html").strip().lower() or "html"
if export_format_raw not in {"html", "json", "txt"}:
raise ValueError(f"Unsupported export format: {export_format_raw}")
export_format: ExportFormat = export_format_raw # type: ignore[assignment]
target_usernames = [str(u or "").strip() for u in (opts.get("usernames") or []) if str(u or "").strip()]
if scope == "selected" and not target_usernames:
raise ValueError("No target usernames to export.")
@@ -638,13 +713,13 @@ class SnsExportManager:
base_name = str(opts.get("fileName") or "").strip()
if not base_name:
if scope == "all":
base_name = f"wechat_sns_export_{account_dir.name}_{ts}_{job.export_id}.zip"
base_name = f"wechat_sns_export_{account_dir.name}_{export_format}_{ts}_{job.export_id}.zip"
else:
hint = _safe_name(target_usernames[0], max_len=40) or "selected"
base_name = f"wechat_sns_export_{account_dir.name}_{hint}_{ts}_{job.export_id}.zip"
base_name = f"wechat_sns_export_{account_dir.name}_{hint}_{export_format}_{ts}_{job.export_id}.zip"
if not base_name.lower().endswith(".zip"):
base_name += ".zip"
base_name = _safe_name(base_name, max_len=120) or f"wechat_sns_export_{account_dir.name}_{ts}_{job.export_id}.zip"
base_name = _safe_name(base_name, max_len=120) or f"wechat_sns_export_{account_dir.name}_{export_format}_{ts}_{job.export_id}.zip"
final_zip = (exports_root / base_name).resolve()
tmp_zip = (exports_root / f".{base_name}.{job.export_id}.part").resolve()
@@ -1120,6 +1195,92 @@ class SnsExportManager:
media_written[cache_key] = arc
return arc
def _build_post_json_record(post: dict[str, Any]) -> dict[str, Any]:
item = _json_safe(post)
if isinstance(item, dict):
item["momentTypeLabel"] = _format_moment_type_label(post)
item["createTimeText"] = _format_dt(post.get("createTime"))
return item if isinstance(item, dict) else {"value": item}
def _render_post_text(post: dict[str, Any], index: int) -> str:
ts = _format_dt(post.get("createTime")) or "未知时间"
post_id = str(post.get("id") or post.get("tid") or "").strip()
content_desc = str(post.get("contentDesc") or "").strip()
location = str(post.get("location") or "").strip()
title0 = str(post.get("title") or "").strip()
content_url = str(post.get("contentUrl") or "").strip()
moment_label = _format_moment_type_label(post)
media_list = post.get("media") if isinstance(post.get("media"), list) else []
likes = post.get("likes") if isinstance(post.get("likes"), list) else []
comments = post.get("comments") if isinstance(post.get("comments"), list) else []
lines = [f"#{index}", f"时间: {ts}"]
if post_id:
lines.append(f"ID: {post_id}")
if moment_label:
lines.append(f"类型: {moment_label}")
if content_desc:
lines.append("内容:")
lines.append(content_desc)
if title0:
lines.append(f"标题: {title0}")
if content_url:
lines.append(f"链接: {content_url}")
if location:
lines.append(f"位置: {location}")
if media_list:
lines.append("媒体:")
for idx0, media0 in enumerate(media_list, start=1):
m = media0 if isinstance(media0, dict) else {}
mtype = str(m.get("type") or "").strip() or "-"
mid = str(m.get("id") or "").strip()
murl = str(m.get("url") or "").strip()
mthumb = str(m.get("thumb") or "").strip()
media_parts = [f"- [{idx0}] type={mtype}"]
if mid:
media_parts.append(f"id={mid}")
if murl:
media_parts.append(f"url={murl}")
if mthumb and mthumb != murl:
media_parts.append(f"thumb={mthumb}")
lines.append(" ".join(media_parts))
if likes:
like_names = [str(x or "").strip() for x in likes if str(x or "").strip()]
if like_names:
lines.append("点赞: " + "".join(like_names))
if comments:
lines.append("评论:")
for idx0, comment0 in enumerate(comments, start=1):
comment = comment0 if isinstance(comment0, dict) else {}
cn = _clean_name(comment.get("nickname") or comment.get("displayName") or comment.get("username") or "") or "未知"
refn = _clean_name(comment.get("refNickname") or comment.get("refUsername") or comment.get("refUserName") or "")
text = str(comment.get("content") or "").strip()
prefix = f"- [{idx0}] {cn}"
if refn:
prefix += f" 回复 {refn}"
if text:
prefix += f": {text}"
lines.append(prefix)
return "\n".join(lines)
def _render_user_text(*, username: str, display_name: str, post_count: int, posts: list[dict[str, Any]]) -> str:
header = [
"朋友圈导出",
f"联系人: {display_name or username}",
f"用户名: {username}",
f"条目数: {post_count}",
"",
]
body: list[str] = []
for idx0, post0 in enumerate(posts, start=1):
body.append(_render_post_text(post0, idx0))
body.append("")
return "\n".join(header + body).rstrip() + "\n"
def render_media_block(*, zf: zipfile.ZipFile, post: dict[str, Any]) -> str:
media = post.get("media") if isinstance(post.get("media"), list) else []
if not media:
@@ -1331,51 +1492,6 @@ class SnsExportManager:
likes = post.get("likes") if isinstance(post.get("likes"), list) else []
comments = post.get("comments") if isinstance(post.get("comments"), list) else []
def guess_official_name_from_title(title: str) -> str:
t0 = str(title or "").strip()
if not t0:
return ""
m = re.search(r"[《「【](.+?)[》」】]", t0)
return str(m.group(1) or "").strip() if m and m.group(1) else ""
def format_moment_type_label(p: dict[str, Any]) -> str:
try:
t = int(p.get("type") or 0)
except Exception:
t = 0
if t == 3:
off = p.get("official") if isinstance(p.get("official"), dict) else {}
st0 = off.get("serviceType") if isinstance(off, dict) else None
try:
st = int(st0) if st0 not in (None, "") else None
except Exception:
st = None
prefix = "服务号" if st == 1 else "公众号"
name = str(off.get("displayName") or "").strip() if isinstance(off, dict) else ""
if not name:
name = guess_official_name_from_title(str(p.get("title") or ""))
return f"{prefix}·{name}" if name else prefix
if t == 28:
ff = p.get("finderFeed") if isinstance(p.get("finderFeed"), dict) else {}
name = str(ff.get("nickname") or "").strip() if isinstance(ff, dict) else ""
return f"视频号·{name}" if name else "视频号"
if t in (5, 42):
name0 = str(p.get("sourceName") or "").strip()
if name0:
return name0
url0 = str(p.get("contentUrl") or "").strip()
if not url0:
ml0 = p.get("media") if isinstance(p.get("media"), list) else []
m0 = ml0[0] if (ml0 and isinstance(ml0[0], dict)) else {}
url0 = str(m0.get("url") or "").strip()
if url0:
# host+path (no query) as a readable fallback label.
s = re.sub(r"^https?://", "", url0.strip(), flags=re.I)
s = s.split("#", 1)[0].split("?", 1)[0].rstrip("/")
return s or ("音乐" if t == 42 else "外部分享")
return "音乐" if t == 42 else "外部分享"
return ""
def format_finder_feed_card_text(p: dict[str, Any]) -> str:
title0 = str(p.get("title") or "").strip()
if title0:
@@ -1426,15 +1542,15 @@ class SnsExportManager:
f'style="background-color:#4B5563">{fallback}</div></div>'
)
moment_label = format_moment_type_label(post)
moment_label = _format_moment_type_label(post)
try:
post_type = int(post.get("type") or 1)
except Exception:
post_type = 1
out: list[str] = []
out.append(f'<div class="bg-white rounded-sm px-4 py-4 mb-3" id="{_esc_attr(pid)}">')
out.append('<div class="flex items-start gap-3">')
out.append(f'<div class="wse-sns-post bg-white rounded-sm px-4 py-4 mb-3" id="{_esc_attr(pid)}">')
out.append('<div class="wse-sns-post-row flex items-start gap-3">')
out.append(avatar_html)
out.append('<div class="flex-1 min-w-0">')
out.append(f'<div class="text-sm font-medium leading-5 text-[#576b95]">{_esc_text(display)}</div>')
@@ -1558,6 +1674,7 @@ class SnsExportManager:
'<svg class="w-6 h-6 text-white" fill="currentColor" viewBox="0 0 24 24"><path d="M8 5v14l11-7z"/></svg>'
)
out.append("</div></div></div>")
out.append("</div>")
else:
out.append(render_media_block(zf=zf, post=post))
@@ -1635,7 +1752,7 @@ class SnsExportManager:
avatar_arc = export_avatar_to_zip(zf=zf, username=username, display_name=display_name)
out: list[str] = []
out.append('<div class="relative w-full mb-12 -mt-4 bg-white">')
out.append('<div class="wse-sns-cover relative w-full -mt-4">')
out.append('<div class="h-64 w-full bg-[#333333] relative overflow-hidden">')
if cover_arc:
out.append(
@@ -1644,7 +1761,7 @@ class SnsExportManager:
)
out.append("</div>")
out.append('<div class="absolute right-4 -bottom-6 flex items-end gap-4">')
out.append('<div class="absolute right-4 flex items-end gap-4" style="bottom:-12px; z-index:2;">')
out.append(
f'<div class="text-white font-bold text-xl mb-7 drop-shadow-md">{_esc_text(display_name or username)}</div>'
)
@@ -1667,22 +1784,24 @@ class SnsExportManager:
try:
with zipfile.ZipFile(str(tmp_zip), mode="w", compression=zipfile.ZIP_DEFLATED) as zf:
css_payload = _load_ui_css_bundle(ui_public_dir=ui_public_dir, report=report) + "\n\n" + _SNS_EXPORT_CSS_PATCH
zf.writestr("assets/wechat-sns-export.css", css_payload)
written.add("assets/wechat-sns-export.css")
css_href = "assets/wechat-sns-export.css"
if export_format == "html":
css_payload = _load_ui_css_bundle(ui_public_dir=ui_public_dir, report=report) + "\n\n" + _SNS_EXPORT_CSS_PATCH
zf.writestr(css_href, css_payload)
written.add(css_href)
repo_root = Path(__file__).resolve().parents[2]
wxemoji_src: Optional[Path] = None
if ui_public_dir is not None:
cand = Path(ui_public_dir) / "wxemoji"
if cand.is_dir():
wxemoji_src = cand
if wxemoji_src is None:
cand = repo_root / "frontend" / "public" / "wxemoji"
if cand.is_dir():
wxemoji_src = cand
if wxemoji_src is not None:
_zip_write_tree(zf=zf, src_dir=wxemoji_src, dest_prefix="wxemoji", written=written)
repo_root = Path(__file__).resolve().parents[2]
wxemoji_src: Optional[Path] = None
if ui_public_dir is not None:
cand = Path(ui_public_dir) / "wxemoji"
if cand.is_dir():
wxemoji_src = cand
if wxemoji_src is None:
cand = repo_root / "frontend" / "public" / "wxemoji"
if cand.is_dir():
wxemoji_src = cand
if wxemoji_src is not None:
_zip_write_tree(zf=zf, src_dir=wxemoji_src, dest_prefix="wxemoji", written=written)
if scope == "all":
users = _load_sns_users(account_dir)
@@ -1691,19 +1810,37 @@ class SnsExportManager:
order = {u: i for i, u in enumerate(target_usernames)}
users.sort(key=lambda x: order.get(str(x.get("username") or ""), 10**9))
total_posts_est = 0
for user_item in users:
try:
total_posts_est += max(0, int(user_item.get("postCount") or 0))
except Exception:
continue
with self._lock:
job.progress.users_total = len(users)
job.progress.posts_total = total_posts_est
job.progress.posts_exported = 0
job.progress.current_user_posts_total = 0
job.progress.current_user_posts_done = 0
user_pages: list[dict[str, Any]] = []
css_href = "assets/wechat-sns-export.css"
user_outputs: list[dict[str, Any]] = []
exported_at = datetime.now().isoformat(timespec="seconds")
for i, u in enumerate(users):
should_cancel()
uname = str(u.get("username") or "").strip()
display = _clean_name(u.get("displayName")) or uname
try:
post_count_est = max(0, int(u.get("postCount") or 0))
except Exception:
post_count_est = 0
safe_uname = _safe_name(uname, max_len=80) or hashlib.md5(uname.encode("utf-8", errors="ignore")).hexdigest()[:12]
with self._lock:
job.progress.current_username = uname
job.progress.current_display_name = display
job.progress.current_user_posts_total = post_count_est
job.progress.current_user_posts_done = 0
posts_all: list[dict[str, Any]] = []
cover_data: Optional[dict[str, Any]] = None
@@ -1728,132 +1865,252 @@ class SnsExportManager:
if not bool(resp.get("hasMore")):
break
post_parts: list[str] = []
for p in posts_all:
should_cancel()
post_parts.append(render_post_html(zf=zf, post=p))
actual_post_count = len(posts_all)
if actual_post_count != post_count_est:
with self._lock:
job.progress.posts_exported += 1
job.progress.posts_total = max(
job.progress.posts_exported,
max(0, job.progress.posts_total + (actual_post_count - post_count_est)),
)
job.progress.current_user_posts_total = actual_post_count
else:
with self._lock:
job.progress.current_user_posts_total = actual_post_count
safe_uname = _safe_name(uname, max_len=80) or hashlib.md5(uname.encode("utf-8", errors="ignore")).hexdigest()[:12]
page_name = f"sns_{safe_uname}.html"
title = f"朋友圈导出 - {display}"
back_link = (
'<a href="index.html" class="text-sm text-[#576b95] hover:underline">← 返回</a>'
if scope == "all"
else ""
)
cover_html = render_cover_header_html(zf=zf, username=uname, display_name=display, cover_data=cover_data)
page_html = "\n".join(
[
"<!doctype html>",
"<html>",
"<head>",
'<meta charset="utf-8" />',
'<meta name="viewport" content="width=device-width, initial-scale=1" />',
f"<title>{_esc_text(title)}</title>",
f'<link rel="stylesheet" href="{_esc_attr(css_href)}" />',
"</head>",
'<body style="background-color:#EDEDED">',
'<div class="min-h-screen" style="background-color:#EDEDED">',
'<div class="max-w-2xl mx-auto px-4 py-4">',
cover_html,
('<div class="flex items-center justify-between mb-4">' + back_link + (f'<div class="text-xs text-gray-500 truncate">{_esc_text(uname)}</div>' if uname else "") + "</div>") if back_link else "",
"".join(post_parts),
"</div>",
"</div>",
"</body>",
"</html>",
"",
]
)
zf.writestr(page_name, page_html)
written.add(page_name)
output_name = ""
if export_format == "html":
post_parts: list[str] = []
for p in posts_all:
should_cancel()
post_parts.append(render_post_html(zf=zf, post=p))
with self._lock:
job.progress.posts_exported += 1
job.progress.current_user_posts_done += 1
user_pages.append(
output_name = f"sns_{safe_uname}.html"
title = f"朋友圈导出 - {display}"
back_link = (
'<a href="index.html" class="text-sm text-[#576b95] hover:underline">← 返回</a>'
if scope == "all"
else ""
)
cover_html = render_cover_header_html(zf=zf, username=uname, display_name=display, cover_data=cover_data)
page_html = "\n".join(
[
"<!doctype html>",
"<html>",
"<head>",
'<meta charset="utf-8" />',
'<meta name="viewport" content="width=device-width, initial-scale=1" />',
f"<title>{_esc_text(title)}</title>",
f'<link rel="stylesheet" href="{_esc_attr(css_href)}" />',
"</head>",
'<body style="background-color:#EDEDED">',
'<div class="min-h-screen" style="background-color:#EDEDED">',
'<div class="wse-sns-page max-w-2xl mx-auto px-4 py-4">',
cover_html,
('<div class="flex items-center justify-between mb-4">' + back_link + (f'<div class="text-xs text-gray-500 truncate">{_esc_text(uname)}</div>' if uname else "") + "</div>") if back_link else "",
'<div class="wse-sns-post-list">' + "".join(post_parts) + "</div>",
"</div>",
"</div>",
"</body>",
"</html>",
"",
]
)
zf.writestr(output_name, page_html)
written.add(output_name)
elif export_format == "json":
exported_posts: list[dict[str, Any]] = []
for p in posts_all:
should_cancel()
exported_posts.append(_build_post_json_record(p))
with self._lock:
job.progress.posts_exported += 1
job.progress.current_user_posts_done += 1
output_name = f"sns_{safe_uname}.json"
json_payload: dict[str, Any] = {
"exportedAt": exported_at,
"exportId": job.export_id,
"account": account_dir.name,
"scope": scope,
"format": export_format,
"username": uname,
"displayName": display,
"postCount": actual_post_count,
"posts": exported_posts,
}
if isinstance(cover_data, dict) and cover_data:
json_payload["cover"] = _json_safe(cover_data)
zf.writestr(output_name, json.dumps(json_payload, ensure_ascii=False, indent=2))
written.add(output_name)
else:
for _idx0, _post0 in enumerate(posts_all, start=1):
should_cancel()
with self._lock:
job.progress.posts_exported += 1
job.progress.current_user_posts_done += 1
output_name = f"sns_{safe_uname}.txt"
zf.writestr(
output_name,
_render_user_text(
username=uname,
display_name=display,
post_count=actual_post_count,
posts=posts_all,
),
)
written.add(output_name)
user_outputs.append(
{
"username": uname,
"displayName": display,
"postCount": int(u.get("postCount") or 0),
"page": page_name,
"postCount": actual_post_count,
"entry": output_name,
}
)
with self._lock:
job.progress.users_done = i + 1
job.progress.current_user_posts_done = actual_post_count
if scope == "all":
rows: list[str] = []
for u in user_pages:
uname = str(u.get("username") or "").strip()
display = _clean_name(u.get("displayName")) or uname
pc = int(u.get("postCount") or 0)
href = str(u.get("page") or "").strip()
avatar_arc = export_avatar_to_zip(zf=zf, username=uname, display_name=display)
if avatar_arc:
avatar_html = (
'<div class="w-8 h-8 rounded-md overflow-hidden bg-gray-300 flex-shrink-0">'
f'<img src="{_esc_attr(avatar_arc)}" class="w-full h-full object-cover" '
f'alt="{_esc_attr(display or uname)}" loading="lazy" referrerpolicy="no-referrer" />'
"</div>"
if export_format == "html":
if scope == "all":
rows: list[str] = []
for u in user_outputs:
uname = str(u.get("username") or "").strip()
display = _clean_name(u.get("displayName")) or uname
pc = int(u.get("postCount") or 0)
href = str(u.get("entry") or "").strip()
avatar_arc = export_avatar_to_zip(zf=zf, username=uname, display_name=display)
if avatar_arc:
avatar_html = (
'<div class="w-8 h-8 rounded-md overflow-hidden bg-gray-300 flex-shrink-0">'
f'<img src="{_esc_attr(avatar_arc)}" class="w-full h-full object-cover" '
f'alt="{_esc_attr(display or uname)}" loading="lazy" referrerpolicy="no-referrer" />'
"</div>"
)
else:
fallback = _esc_text((display or uname or "")[:1] or "")
avatar_html = (
'<div class="w-8 h-8 rounded-md overflow-hidden bg-gray-300 flex-shrink-0">'
'<div class="w-full h-full flex items-center justify-center text-white text-xs font-bold" '
f'style="background-color:#4B5563">{fallback}</div></div>'
)
rows.append(
'<a class="px-3 py-2 text-sm cursor-pointer flex items-center gap-2 border-b border-gray-100 hover:bg-gray-50" '
f'href="{_esc_attr(href)}">'
f"{avatar_html}"
'<div class="flex-1 min-w-0">'
f'<div class="truncate">{_esc_text(display)}</div>'
f'<div class="text-[11px] text-gray-400 truncate">{_esc_text(uname)} · {pc} 条</div>'
"</div></a>"
)
else:
fallback = _esc_text((display or uname or "")[:1] or "")
avatar_html = (
'<div class="w-8 h-8 rounded-md overflow-hidden bg-gray-300 flex-shrink-0">'
'<div class="w-full h-full flex items-center justify-center text-white text-xs font-bold" '
f'style="background-color:#4B5563">{fallback}</div></div>'
)
rows.append(
'<a class="px-3 py-2 text-sm cursor-pointer flex items-center gap-2 border-b border-gray-100 hover:bg-gray-50" '
f'href="{_esc_attr(href)}">'
f"{avatar_html}"
'<div class="flex-1 min-w-0">'
f'<div class="truncate">{_esc_text(display)}</div>'
f'<div class="text-[11px] text-gray-400 truncate">{_esc_text(uname)} · {pc} 条</div>'
"</div></a>"
)
index_html = "\n".join(
[
"<!doctype html>",
"<html>",
"<head>",
'<meta charset="utf-8" />',
'<meta name="viewport" content="width=device-width, initial-scale=1" />',
"<title>朋友圈导出</title>",
f'<link rel="stylesheet" href="{_esc_attr(css_href)}" />',
"</head>",
'<body style="background-color:#EDEDED">',
'<div class="min-h-screen" style="background-color:#EDEDED">',
'<div class="max-w-2xl mx-auto px-4 py-4">',
'<div class="mb-4 flex items-center justify-between">',
'<div class="text-sm font-semibold text-gray-700">朋友圈联系人</div>',
f'<div class="text-xs text-gray-500">{len(user_pages)} 人</div>',
"</div>",
'<div class="bg-white rounded-sm overflow-hidden border border-gray-200">',
"".join(rows),
"</div>",
"</div>",
"</div>",
"</body>",
"</html>",
"",
]
)
zf.writestr("index.html", index_html)
written.add("index.html")
else:
only_page = user_pages[0]["page"] if user_pages else ""
if only_page:
index_html = (
"<!doctype html><html><head>"
'<meta charset="utf-8" />'
f'<meta http-equiv="refresh" content="0; url={_esc_attr(only_page)}" />'
"</head><body></body></html>"
index_html = "\n".join(
[
"<!doctype html>",
"<html>",
"<head>",
'<meta charset="utf-8" />',
'<meta name="viewport" content="width=device-width, initial-scale=1" />',
"<title>朋友圈导出</title>",
f'<link rel="stylesheet" href="{_esc_attr(css_href)}" />',
"</head>",
'<body style="background-color:#EDEDED">',
'<div class="min-h-screen" style="background-color:#EDEDED">',
'<div class="max-w-2xl mx-auto px-4 py-4">',
'<div class="mb-4 flex items-center justify-between">',
'<div class="text-sm font-semibold text-gray-700">朋友圈联系人</div>',
f'<div class="text-xs text-gray-500">{len(user_outputs)} 人</div>',
"</div>",
'<div class="bg-white rounded-sm overflow-hidden border border-gray-200">',
"".join(rows),
"</div>",
"</div>",
"</div>",
"</body>",
"</html>",
"",
]
)
zf.writestr("index.html", index_html)
written.add("index.html")
else:
only_page = user_outputs[0]["entry"] if user_outputs else ""
if only_page:
index_html = (
"<!doctype html><html><head>"
'<meta charset="utf-8" />'
f'<meta http-equiv="refresh" content="0; url={_esc_attr(only_page)}" />'
"</head><body></body></html>"
)
zf.writestr("index.html", index_html)
written.add("index.html")
elif export_format == "json":
zf.writestr(
"index.json",
json.dumps(
{
"exportedAt": exported_at,
"exportId": job.export_id,
"account": account_dir.name,
"scope": scope,
"format": export_format,
"users": user_outputs,
},
ensure_ascii=False,
indent=2,
),
)
written.add("index.json")
else:
lines = [
"朋友圈导出",
f"导出时间: {exported_at}",
f"账号: {account_dir.name}",
f"范围: {'全部联系人' if scope == 'all' else '指定联系人'}",
f"格式: {export_format}",
"",
]
for item in user_outputs:
lines.append(
f"- {item.get('displayName') or item.get('username') or ''} "
f"({item.get('username') or ''}) · {int(item.get('postCount') or 0)} 条 -> {item.get('entry') or ''}"
)
zf.writestr("index.txt", "\n".join(lines).rstrip() + "\n")
written.add("index.txt")
zf.writestr(
"manifest.json",
json.dumps(
{
"schemaVersion": 1,
"exportedAt": exported_at,
"exportId": job.export_id,
"account": account_dir.name,
"scope": scope,
"format": export_format,
"options": {
"useCache": use_cache,
},
"stats": {
"users": len(user_outputs),
"postsExported": job.progress.posts_exported,
"postsTotal": job.progress.posts_total,
"mediaCopied": job.progress.media_copied,
"mediaMissing": job.progress.media_missing,
},
"entries": user_outputs,
},
ensure_ascii=False,
indent=2,
),
)
written.add("manifest.json")
try:
zf.writestr("export_report.json", json.dumps(report, ensure_ascii=False, indent=2))
@@ -1881,8 +2138,9 @@ class SnsExportManager:
if job.status != "cancelled":
job.status = "done"
job.finished_at = time.time()
job.progress.current_user_posts_done = job.progress.current_user_posts_total
return tmp_zip
return final_out
SNS_EXPORT_MANAGER = SnsExportManager()
Generated
+1 -1
View File
@@ -872,7 +872,7 @@ wheels = [
[[package]]
name = "wechat-decrypt-tool"
version = "1.3.0"
version = "1.7.10"
source = { editable = "." }
dependencies = [
{ name = "aiofiles" },