Compare commits

...

6 Commits

7 changed files with 805 additions and 18 deletions
+17 -6
View File
@@ -86,27 +86,38 @@
## 快速开始
### 1. 克隆项目
### 1. 下载并安装 EXE(Windows,推荐)
1. 打开 Release 页面(最新版):https://github.com/LifeArchiveProject/WeChatDataAnalysis/releases/latest
2. 下载 `WeChatDataAnalysis.Setup.<version>.exe` 并运行安装
3. 安装完成后启动 `WeChatDataAnalysis`
> 如果 Windows 弹出“未知发布者/更多信息”等提示,请确认下载来源为本仓库 Release 后再选择“仍要运行”。
### 2. 从源码运行(开发者/高级用户)
#### 2.1 克隆项目
```bash
git clone https://github.com/2977094657/WeChatDataAnalysis
git clone https://github.com/LifeArchiveProject/WeChatDataAnalysis.git
cd WeChatDataAnalysis
```
### 2. 安装后端依赖
#### 2.2 安装后端依赖
```bash
# 使用uv (推荐)
uv sync
```
### 3. 安装前端依赖
#### 2.3 安装前端依赖
```bash
cd frontend
npm install
```
### 4. 启动服务
#### 2.4 启动服务
#### 启动后端API服务
```bash
@@ -121,7 +132,7 @@ cd frontend
npm run dev
```
### 5. 访问应用
#### 2.5 访问应用
- 前端界面: http://localhost:3000
- API服务: http://localhost:8000
+75
View File
@@ -78,3 +78,78 @@ Function WDA_InstallDirPageLeave
FunctionEnd
!endif
!ifdef BUILD_UNINSTALLER
!include nsDialogs.nsh
!include LogicLib.nsh
Var WDA_UninstallOptionsPage
Var WDA_UninstallDeleteDataCheckbox
Var /GLOBAL WDA_DeleteUserData
!macro customUnInit
; Default: keep user data (also applies to silent uninstall / update uninstall).
StrCpy $WDA_DeleteUserData "0"
!macroend
!macro customUnWelcomePage
!insertmacro MUI_UNPAGE_WELCOME
; Optional page: allow user to choose whether to delete app data.
UninstPage custom un.WDA_UninstallOptionsCreate un.WDA_UninstallOptionsLeave
!macroend
Function un.WDA_UninstallOptionsCreate
nsDialogs::Create 1018
Pop $WDA_UninstallOptionsPage
${If} $WDA_UninstallOptionsPage == error
Abort
${EndIf}
${NSD_CreateLabel} 0u 0u 100% 24u "卸载选项:"
Pop $0
${NSD_CreateCheckbox} 0u 24u 100% 12u "同时删除用户数据(导出的聊天记录、日志、配置等)"
Pop $WDA_UninstallDeleteDataCheckbox
; Safer default: do not delete.
${NSD_Uncheck} $WDA_UninstallDeleteDataCheckbox
nsDialogs::Show
FunctionEnd
Function un.WDA_UninstallOptionsLeave
${NSD_GetState} $WDA_UninstallDeleteDataCheckbox $0
${If} $0 == ${BST_CHECKED}
StrCpy $WDA_DeleteUserData "1"
${Else}
StrCpy $WDA_DeleteUserData "0"
${EndIf}
FunctionEnd
!macro customUnInstall
; If this is an update uninstall, never delete user data.
${ifNot} ${isUpdated}
${if} $WDA_DeleteUserData == "1"
; Electron always stores user data per-user. If the app was installed for all users,
; switch to current user context to remove the correct AppData directory.
${if} $installMode == "all"
SetShellVarContext current
${endif}
RMDir /r "$APPDATA\${APP_FILENAME}"
!ifdef APP_PRODUCT_FILENAME
RMDir /r "$APPDATA\${APP_PRODUCT_FILENAME}"
!endif
; Electron may use package.json "name" for some storage (cache, indexeddb, etc.).
!ifdef APP_PACKAGE_NAME
RMDir /r "$APPDATA\${APP_PACKAGE_NAME}"
!endif
${if} $installMode == "all"
SetShellVarContext all
${endif}
${endif}
${endif}
!macroend
!endif
Binary file not shown.

After

Width:  |  Height:  |  Size: 279 KiB

+402 -6
View File
@@ -1,4 +1,13 @@
const { app, BrowserWindow, Menu, ipcMain, globalShortcut } = require("electron");
const {
app,
BrowserWindow,
Menu,
Tray,
ipcMain,
globalShortcut,
dialog,
shell,
} = require("electron");
const { spawn } = require("child_process");
const fs = require("fs");
const http = require("http");
@@ -9,6 +18,298 @@ const BACKEND_PORT = Number(process.env.WECHAT_TOOL_PORT || "8000");
const BACKEND_HEALTH_URL = `http://${BACKEND_HOST}:${BACKEND_PORT}/api/health`;
let backendProc = null;
let backendStdioStream = null;
let resolvedDataDir = null;
let mainWindow = null;
let tray = null;
let isQuitting = false;
let desktopSettings = null;
function nowIso() {
return new Date().toISOString();
}
function resolveDataDir() {
if (resolvedDataDir) return resolvedDataDir;
const fromEnv = String(process.env.WECHAT_TOOL_DATA_DIR || "").trim();
const fallback = (() => {
try {
return app.getPath("userData");
} catch {
return null;
}
})();
const chosen = fromEnv || fallback;
if (!chosen) return null;
try {
fs.mkdirSync(chosen, { recursive: true });
} catch {}
resolvedDataDir = chosen;
process.env.WECHAT_TOOL_DATA_DIR = chosen;
return chosen;
}
function getUserDataDir() {
// Backwards-compat: we historically used Electron's userData directory for runtime storage.
// Keep this name but resolve to the effective data dir (can be overridden via env).
return resolveDataDir();
}
function getExeDir() {
try {
return path.dirname(process.execPath);
} catch {
return null;
}
}
function ensureOutputLink() {
// Users often expect an `output/` folder near the installed exe. We keep the real data
// in the per-user data dir, and (when possible) create a Windows junction next to the exe.
if (!app.isPackaged) return;
const exeDir = getExeDir();
const dataDir = resolveDataDir();
if (!exeDir || !dataDir) return;
const target = path.join(dataDir, "output");
const linkPath = path.join(exeDir, "output");
// If the target doesn't exist yet, create it so the link points somewhere real.
try {
fs.mkdirSync(target, { recursive: true });
} catch {}
// If something already exists at linkPath, do not overwrite it.
try {
if (fs.existsSync(linkPath)) return;
} catch {
return;
}
try {
fs.symlinkSync(target, linkPath, "junction");
logMain(`[main] created output link: ${linkPath} -> ${target}`);
} catch (err) {
logMain(`[main] failed to create output link: ${err?.message || err}`);
}
}
function getMainLogPath() {
const dir = getUserDataDir();
if (!dir) return null;
return path.join(dir, "desktop-main.log");
}
function logMain(line) {
const p = getMainLogPath();
if (!p) return;
try {
fs.mkdirSync(path.dirname(p), { recursive: true });
fs.appendFileSync(p, `[${nowIso()}] ${line}\n`, { encoding: "utf8" });
} catch {}
}
function getDesktopSettingsPath() {
const dir = getUserDataDir();
if (!dir) return null;
return path.join(dir, "desktop-settings.json");
}
function loadDesktopSettings() {
if (desktopSettings) return desktopSettings;
const defaults = {
// 'tray' (default): closing the window hides it to the system tray.
// 'exit': closing the window quits the app.
closeBehavior: "tray",
};
const p = getDesktopSettingsPath();
if (!p) {
desktopSettings = { ...defaults };
return desktopSettings;
}
try {
if (!fs.existsSync(p)) {
desktopSettings = { ...defaults };
return desktopSettings;
}
const raw = fs.readFileSync(p, { encoding: "utf8" });
const parsed = JSON.parse(raw || "{}");
desktopSettings = { ...defaults, ...(parsed && typeof parsed === "object" ? parsed : {}) };
} catch (err) {
desktopSettings = { ...defaults };
logMain(`[main] failed to load settings: ${err?.message || err}`);
}
return desktopSettings;
}
function persistDesktopSettings() {
const p = getDesktopSettingsPath();
if (!p) return;
if (!desktopSettings) return;
try {
fs.mkdirSync(path.dirname(p), { recursive: true });
fs.writeFileSync(p, JSON.stringify(desktopSettings, null, 2), { encoding: "utf8" });
} catch (err) {
logMain(`[main] failed to persist settings: ${err?.message || err}`);
}
}
function getCloseBehavior() {
const v = String(loadDesktopSettings()?.closeBehavior || "").trim().toLowerCase();
return v === "exit" ? "exit" : "tray";
}
function setCloseBehavior(next) {
const v = String(next || "").trim().toLowerCase();
loadDesktopSettings();
desktopSettings.closeBehavior = v === "exit" ? "exit" : "tray";
persistDesktopSettings();
return desktopSettings.closeBehavior;
}
function getTrayIconPath() {
// Prefer an icon shipped in `src/` so it works both in dev and packaged (asar) builds.
const shipped = path.join(__dirname, "icon.ico");
try {
if (fs.existsSync(shipped)) return shipped;
} catch {}
// Dev fallback (not available in packaged builds).
const dev = path.resolve(__dirname, "..", "build", "icon.ico");
try {
if (fs.existsSync(dev)) return dev;
} catch {}
return null;
}
function showMainWindow() {
if (!mainWindow) return;
try {
mainWindow.setSkipTaskbar(false);
} catch {}
try {
if (mainWindow.isMinimized()) mainWindow.restore();
} catch {}
try {
mainWindow.show();
} catch {}
try {
mainWindow.focus();
} catch {}
}
function createTray() {
if (tray) return tray;
if (!app.isPackaged) return null;
const iconPath = getTrayIconPath();
if (!iconPath) {
logMain("[main] tray icon not found; disabling tray behavior");
return null;
}
try {
tray = new Tray(iconPath);
} catch (err) {
tray = null;
logMain(`[main] failed to create tray: ${err?.message || err}`);
return null;
}
try {
tray.setToolTip("WeChatDataAnalysis");
} catch {}
try {
tray.setContextMenu(
Menu.buildFromTemplate([
{
label: "显示",
click: () => showMainWindow(),
},
{
label: "退出",
click: () => {
isQuitting = true;
app.quit();
},
},
])
);
} catch {}
try {
tray.on("click", () => showMainWindow());
tray.on("double-click", () => showMainWindow());
} catch {}
return tray;
}
function destroyTray() {
if (!tray) return;
try {
tray.destroy();
} catch {}
tray = null;
}
function ensureTrayForCloseBehavior() {
const behavior = getCloseBehavior();
if (behavior === "tray") createTray();
else destroyTray();
}
function getBackendStdioLogPath(dataDir) {
return path.join(dataDir, "backend-stdio.log");
}
function attachBackendStdio(proc, logPath) {
// In packaged builds, stdout/stderr are often the only place we can see early crash
// reasons (missing DLLs, import errors) before the Python logger initializes.
try {
fs.mkdirSync(path.dirname(logPath), { recursive: true });
} catch {}
try {
backendStdioStream = fs.createWriteStream(logPath, { flags: "a" });
backendStdioStream.write(`[${nowIso()}] [main] backend stdio -> ${logPath}\n`);
} catch {
backendStdioStream = null;
return;
}
const write = (prefix, chunk) => {
if (!backendStdioStream) return;
try {
const text = Buffer.isBuffer(chunk) ? chunk.toString("utf8") : String(chunk);
backendStdioStream.write(`[${nowIso()}] ${prefix} ${text}`);
if (!text.endsWith("\n")) backendStdioStream.write("\n");
} catch {}
};
if (proc.stdout) proc.stdout.on("data", (d) => write("[backend:stdout]", d));
if (proc.stderr) proc.stderr.on("data", (d) => write("[backend:stderr]", d));
proc.on("error", (err) => write("[backend:error]", err?.stack || String(err)));
proc.on("close", (code, signal) => {
write("[backend:close]", `code=${code} signal=${signal}`);
try {
backendStdioStream?.end();
} catch {}
backendStdioStream = null;
});
}
function repoRoot() {
// desktop/src -> desktop -> repo root
@@ -27,6 +328,8 @@ function startBackend() {
...process.env,
WECHAT_TOOL_HOST: BACKEND_HOST,
WECHAT_TOOL_PORT: String(BACKEND_PORT),
// Make sure Python prints UTF-8 to stdout/stderr.
PYTHONIOENCODING: process.env.PYTHONIOENCODING || "utf-8",
};
// In packaged mode we expect to provide the generated Nuxt output dir via env.
@@ -51,9 +354,10 @@ function startBackend() {
backendProc = spawn(backendExe, [], {
cwd: env.WECHAT_TOOL_DATA_DIR,
env,
stdio: "ignore",
stdio: ["ignore", "pipe", "pipe"],
windowsHide: true,
});
attachBackendStdio(backendProc, getBackendStdioLogPath(env.WECHAT_TOOL_DATA_DIR));
} else {
backendProc = spawn("uv", ["run", "main.py"], {
cwd: repoRoot(),
@@ -67,6 +371,7 @@ function startBackend() {
backendProc = null;
// eslint-disable-next-line no-console
console.log(`[backend] exited code=${code} signal=${signal}`);
logMain(`[backend] exited code=${code} signal=${signal}`);
});
return backendProc;
@@ -124,12 +429,12 @@ async function waitForBackend({ timeoutMs }) {
function debugEnabled() {
// Enable debug helpers in dev by default; in packaged builds require explicit opt-in.
return !app.isPackaged || process.env.WECHAT_DESKTOP_DEBUG === "1";
if (!app.isPackaged) return true;
if (process.env.WECHAT_DESKTOP_DEBUG === "1") return true;
return process.argv.includes("--debug") || process.argv.includes("--devtools");
}
function registerDebugShortcuts() {
if (!debugEnabled()) return;
const toggleDevTools = () => {
const win = BrowserWindow.getFocusedWindow() || BrowserWindow.getAllWindows()[0];
if (!win) return;
@@ -186,10 +491,32 @@ function createMainWindow() {
preload: path.join(__dirname, "preload.cjs"),
contextIsolation: true,
nodeIntegration: false,
devTools: debugEnabled(),
// Allow DevTools to be opened in packaged builds (F12 / Ctrl+Shift+I).
// We still only auto-open it when debugEnabled() returns true.
devTools: true,
},
});
win.on("close", (event) => {
// In packaged builds, we default to "close -> minimize to tray" unless the user opts out.
if (!app.isPackaged) return;
if (isQuitting) return;
if (getCloseBehavior() !== "tray") return;
if (!tray) return;
try {
event.preventDefault();
win.setSkipTaskbar(true);
win.hide();
try {
tray.displayBalloon({
title: "WeChatDataAnalysis",
content: "已最小化到托盘,可从托盘图标再次打开。",
});
} catch {}
} catch {}
});
win.on("closed", () => {
stopBackend();
});
@@ -237,6 +564,53 @@ function registerWindowIpc() {
const win = getWin(event);
return !!win?.isMaximized();
});
ipcMain.handle("app:getAutoLaunch", () => {
try {
const settings = app.getLoginItemSettings();
return !!(settings?.openAtLogin || settings?.executableWillLaunchAtLogin);
} catch (err) {
logMain(`[main] getAutoLaunch failed: ${err?.message || err}`);
return false;
}
});
ipcMain.handle("app:setAutoLaunch", (_event, enabled) => {
const on = !!enabled;
try {
app.setLoginItemSettings({ openAtLogin: on });
} catch (err) {
logMain(`[main] setAutoLaunch(${on}) failed: ${err?.message || err}`);
return false;
}
try {
const settings = app.getLoginItemSettings();
return !!(settings?.openAtLogin || settings?.executableWillLaunchAtLogin);
} catch {
return on;
}
});
ipcMain.handle("app:getCloseBehavior", () => {
try {
return getCloseBehavior();
} catch (err) {
logMain(`[main] getCloseBehavior failed: ${err?.message || err}`);
return "tray";
}
});
ipcMain.handle("app:setCloseBehavior", (_event, behavior) => {
try {
const next = setCloseBehavior(behavior);
ensureTrayForCloseBehavior();
return next;
} catch (err) {
logMain(`[main] setCloseBehavior failed: ${err?.message || err}`);
return getCloseBehavior();
}
});
}
async function main() {
@@ -245,10 +619,19 @@ async function main() {
registerWindowIpc();
registerDebugShortcuts();
// Resolve/create the data dir early so we can log reliably and (optionally) place a link
// next to the installed exe for easier access.
resolveDataDir();
ensureOutputLink();
logMain(`[main] app.isPackaged=${app.isPackaged} argv=${JSON.stringify(process.argv)}`);
startBackend();
await waitForBackend({ timeoutMs: 30_000 });
const win = createMainWindow();
mainWindow = win;
ensureTrayForCloseBehavior();
const startUrl =
process.env.ELECTRON_START_URL ||
@@ -276,12 +659,25 @@ app.on("will-quit", () => {
});
app.on("before-quit", () => {
isQuitting = true;
destroyTray();
stopBackend();
});
main().catch((err) => {
// eslint-disable-next-line no-console
console.error(err);
logMain(`[main] fatal: ${err?.stack || String(err)}`);
stopBackend();
try {
const dir = getUserDataDir();
if (dir) {
dialog.showErrorBox(
"WeChatDataAnalysis 启动失败",
`启动失败:${err?.message || err}\n\n请查看日志目录:\n${dir}\n\n文件:desktop-main.log / backend-stdio.log / output\\\\logs\\\\...`
);
shell.openPath(dir);
}
} catch {}
app.quit();
});
+6 -1
View File
@@ -5,5 +5,10 @@ contextBridge.exposeInMainWorld("wechatDesktop", {
toggleMaximize: () => ipcRenderer.invoke("window:toggleMaximize"),
close: () => ipcRenderer.invoke("window:close"),
isMaximized: () => ipcRenderer.invoke("window:isMaximized"),
});
getAutoLaunch: () => ipcRenderer.invoke("app:getAutoLaunch"),
setAutoLaunch: (enabled) => ipcRenderer.invoke("app:setAutoLaunch", !!enabled),
getCloseBehavior: () => ipcRenderer.invoke("app:getCloseBehavior"),
setCloseBehavior: (behavior) => ipcRenderer.invoke("app:setCloseBehavior", String(behavior || "")),
});
+279 -4
View File
@@ -25,6 +25,23 @@
<circle v-if="!privacyMode" cx="12" cy="12" r="3" />
</svg>
</div>
<!-- 设置按钮仅桌面端 -->
<div
v-if="isDesktopEnv"
class="w-16 h-12 flex items-center justify-center cursor-pointer transition-colors text-gray-500 hover:text-gray-700"
@click="openDesktopSettings"
title="设置"
>
<svg class="w-6 h-6" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
<path
stroke-linecap="round"
stroke-linejoin="round"
d="M10.325 4.317c.426-1.756 2.924-1.756 3.35 0a1.724 1.724 0 002.573 1.066c1.543-.94 3.31.826 2.37 2.37a1.724 1.724 0 001.065 2.572c1.756.426 1.756 2.924 0 3.35a1.724 1.724 0 00-1.066 2.573c.94 1.543-.826 3.31-2.37 2.37a1.724 1.724 0 00-2.572 1.065c-.426 1.756-2.924 1.756-3.35 0a1.724 1.724 0 00-2.573-1.066c-1.543.94-3.31-.826-2.37-2.37a1.724 1.724 0 00-1.065-2.572c-1.756-.426-1.756-2.924 0-3.35a1.724 1.724 0 001.066-2.573c-.94-1.543.826-3.31 2.37-2.37.996.608 2.296.07 2.572-1.065z"
/>
<path stroke-linecap="round" stroke-linejoin="round" d="M15 12a3 3 0 11-6 0 3 3 0 016 0z" />
</svg>
</div>
</div>
</div>
@@ -1543,6 +1560,104 @@
</div>
</div>
</div>
<!-- 桌面端设置弹窗 -->
<div
v-if="desktopSettingsOpen"
class="fixed inset-0 z-50 flex items-center justify-center bg-black/40"
@click.self="closeDesktopSettings"
>
<div class="w-full max-w-md bg-white rounded-lg shadow-lg">
<div class="px-5 py-4 border-b border-gray-200 flex items-center justify-between">
<div class="text-base font-medium text-gray-900">设置</div>
<button
type="button"
class="text-gray-500 hover:text-gray-700"
@click="closeDesktopSettings"
title="关闭"
>
<svg class="w-5 h-5" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
<path stroke-linecap="round" stroke-linejoin="round" d="M6 18L18 6M6 6l12 12" />
</svg>
</button>
</div>
<div class="px-5 py-4 space-y-4">
<div class="flex items-center justify-between gap-4">
<div class="min-w-0">
<div class="text-sm font-medium text-gray-900">开机自启动</div>
<div class="text-xs text-gray-500">系统登录后自动启动桌面端</div>
</div>
<input
type="checkbox"
class="h-4 w-4"
:checked="desktopAutoLaunch"
:disabled="desktopAutoLaunchLoading"
@change="onDesktopAutoLaunchToggle"
/>
</div>
<div v-if="desktopAutoLaunchError" class="text-xs text-red-600 whitespace-pre-wrap">
{{ desktopAutoLaunchError }}
</div>
<div class="flex items-center justify-between gap-4">
<div class="min-w-0">
<div class="text-sm font-medium text-gray-900">关闭窗口行为</div>
<div class="text-xs text-gray-500">点击关闭按钮时默认最小化到托盘</div>
</div>
<select
class="text-sm px-2 py-1 rounded-md border border-gray-200"
:disabled="desktopCloseBehaviorLoading"
:value="desktopCloseBehavior"
@change="onDesktopCloseBehaviorChange"
>
<option value="tray">最小化到托盘</option>
<option value="exit">直接退出</option>
</select>
</div>
<div v-if="desktopCloseBehaviorError" class="text-xs text-red-600 whitespace-pre-wrap">
{{ desktopCloseBehaviorError }}
</div>
<div class="flex items-center justify-between gap-4">
<div class="min-w-0">
<div class="text-sm font-medium text-gray-900">启动后自动开启实时获取</div>
<div class="text-xs text-gray-500">进入聊天页后自动打开实时开关默认关闭</div>
</div>
<input
type="checkbox"
class="h-4 w-4"
:checked="desktopAutoRealtime"
@change="onDesktopAutoRealtimeToggle"
/>
</div>
<div class="flex items-center justify-between gap-4">
<div class="min-w-0">
<div class="text-sm font-medium text-gray-900">有数据时默认进入聊天页</div>
<div class="text-xs text-gray-500">有已解密账号时打开应用默认跳转到 /chat默认关闭</div>
</div>
<input
type="checkbox"
class="h-4 w-4"
:checked="desktopDefaultToChatWhenData"
@change="onDesktopDefaultToChatToggle"
/>
</div>
</div>
<div class="px-5 py-4 border-t border-gray-200 flex items-center justify-end gap-2">
<button
type="button"
class="text-sm px-3 py-2 rounded-md bg-white border border-gray-200 hover:bg-gray-50"
@click="closeDesktopSettings"
>
关闭
</button>
</div>
</div>
</div>
</div>
</template>
@@ -1607,6 +1722,145 @@ const selectedContact = ref(null)
// 隐私模式
const privacyMode = ref(false)
// 桌面端设置(仅 Electron 环境可见)
const isDesktopEnv = ref(false)
const desktopSettingsOpen = ref(false)
const DESKTOP_SETTING_AUTO_REALTIME_KEY = 'desktop.settings.autoRealtime'
const DESKTOP_SETTING_DEFAULT_TO_CHAT_KEY = 'desktop.settings.defaultToChatWhenData'
const desktopAutoRealtime = ref(false)
const desktopDefaultToChatWhenData = ref(false)
const desktopAutoLaunch = ref(false)
const desktopAutoLaunchLoading = ref(false)
const desktopAutoLaunchError = ref('')
const desktopCloseBehavior = ref('tray') // tray | exit
const desktopCloseBehaviorLoading = ref(false)
const desktopCloseBehaviorError = ref('')
const readLocalBool = (key) => {
if (!process.client) return false
try {
return localStorage.getItem(key) === 'true'
} catch {
return false
}
}
const writeLocalBool = (key, value) => {
if (!process.client) return
try {
localStorage.setItem(key, value ? 'true' : 'false')
} catch {}
}
// 尽量早读本地设置,避免首次加载联系人时拿不到 autoRealtime 选项
if (process.client) {
desktopAutoRealtime.value = readLocalBool(DESKTOP_SETTING_AUTO_REALTIME_KEY)
desktopDefaultToChatWhenData.value = readLocalBool(DESKTOP_SETTING_DEFAULT_TO_CHAT_KEY)
}
const refreshDesktopAutoLaunch = async () => {
if (!process.client || typeof window === 'undefined') return
if (!window.wechatDesktop?.getAutoLaunch) return
desktopAutoLaunchLoading.value = true
desktopAutoLaunchError.value = ''
try {
desktopAutoLaunch.value = !!(await window.wechatDesktop.getAutoLaunch())
} catch (e) {
desktopAutoLaunchError.value = e?.message || '读取开机自启动状态失败'
} finally {
desktopAutoLaunchLoading.value = false
}
}
const setDesktopAutoLaunch = async (enabled) => {
if (!process.client || typeof window === 'undefined') return
if (!window.wechatDesktop?.setAutoLaunch) return
desktopAutoLaunchLoading.value = true
desktopAutoLaunchError.value = ''
try {
desktopAutoLaunch.value = !!(await window.wechatDesktop.setAutoLaunch(!!enabled))
} catch (e) {
desktopAutoLaunchError.value = e?.message || '设置开机自启动失败'
await refreshDesktopAutoLaunch()
} finally {
desktopAutoLaunchLoading.value = false
}
}
const refreshDesktopCloseBehavior = async () => {
if (!process.client || typeof window === 'undefined') return
if (!window.wechatDesktop?.getCloseBehavior) return
desktopCloseBehaviorLoading.value = true
desktopCloseBehaviorError.value = ''
try {
const v = await window.wechatDesktop.getCloseBehavior()
desktopCloseBehavior.value = (String(v || '').toLowerCase() === 'exit') ? 'exit' : 'tray'
} catch (e) {
desktopCloseBehaviorError.value = e?.message || '读取关闭窗口行为失败'
} finally {
desktopCloseBehaviorLoading.value = false
}
}
const setDesktopCloseBehavior = async (behavior) => {
if (!process.client || typeof window === 'undefined') return
if (!window.wechatDesktop?.setCloseBehavior) return
const desired = (String(behavior || '').toLowerCase() === 'exit') ? 'exit' : 'tray'
desktopCloseBehaviorLoading.value = true
desktopCloseBehaviorError.value = ''
try {
const v = await window.wechatDesktop.setCloseBehavior(desired)
desktopCloseBehavior.value = (String(v || '').toLowerCase() === 'exit') ? 'exit' : 'tray'
} catch (e) {
desktopCloseBehaviorError.value = e?.message || '设置关闭窗口行为失败'
await refreshDesktopCloseBehavior()
} finally {
desktopCloseBehaviorLoading.value = false
}
}
const openDesktopSettings = async () => {
desktopSettingsOpen.value = true
await refreshDesktopAutoLaunch()
await refreshDesktopCloseBehavior()
}
const closeDesktopSettings = () => {
desktopSettingsOpen.value = false
}
const onDesktopAutoLaunchToggle = async (ev) => {
const checked = !!ev?.target?.checked
await setDesktopAutoLaunch(checked)
}
const onDesktopCloseBehaviorChange = async (ev) => {
const v = String(ev?.target?.value || '').trim()
await setDesktopCloseBehavior(v)
}
const onDesktopAutoRealtimeToggle = async (ev) => {
const checked = !!ev?.target?.checked
desktopAutoRealtime.value = checked
writeLocalBool(DESKTOP_SETTING_AUTO_REALTIME_KEY, checked)
if (checked) {
// 开启后尝试立即启用实时模式(不可用则静默忽略)
try {
await tryEnableRealtimeAuto()
} catch {}
}
}
const onDesktopDefaultToChatToggle = (ev) => {
const checked = !!ev?.target?.checked
desktopDefaultToChatWhenData.value = checked
writeLocalBool(DESKTOP_SETTING_DEFAULT_TO_CHAT_KEY, checked)
}
// 联系人数据
const contacts = ref([])
@@ -3406,6 +3660,9 @@ const applyRouteSelection = async () => {
// 默认选择第一个联系人
onMounted(() => {
if (process.client && typeof window !== 'undefined') {
isDesktopEnv.value = !!window.wechatDesktop
}
loadContacts()
loadSearchHistory()
})
@@ -3436,6 +3693,8 @@ const loadContacts = async () => {
} finally {
isLoadingContacts.value = false
}
await tryEnableRealtimeAuto()
}
const loadSessionsForSelectedAccount = async () => {
@@ -4546,15 +4805,18 @@ const startRealtimeStream = () => {
}
}
const toggleRealtime = async () => {
const toggleRealtime = async (opts = {}) => {
const silent = !!opts?.silent
if (!process.client || typeof window === 'undefined') return
if (!selectedAccount.value) return
if (!realtimeEnabled.value) {
await fetchRealtimeStatus()
if (!realtimeAvailable.value) {
window.alert(realtimeStatusError.value || '实时模式不可用:缺少密钥或 db_storage 路径。')
return
if (!silent) {
window.alert(realtimeStatusError.value || '实时模式不可用:缺少密钥或 db_storage 路径。')
}
return false
}
realtimeEnabled.value = true
startRealtimeStream()
@@ -4562,7 +4824,7 @@ const toggleRealtime = async () => {
if (selectedContact.value?.username) {
await refreshSelectedMessages()
}
return
return true
}
realtimeEnabled.value = false
@@ -4571,6 +4833,19 @@ const toggleRealtime = async () => {
if (selectedContact.value?.username) {
await refreshSelectedMessages()
}
return true
}
const tryEnableRealtimeAuto = async () => {
if (!process.client || typeof window === 'undefined') return
if (!isDesktopEnv.value) return
if (!desktopAutoRealtime.value) return
if (realtimeEnabled.value) return
if (!selectedAccount.value) return
try {
await toggleRealtime({ silent: true })
} catch {}
}
watch(selectedAccount, async () => {
+26 -1
View File
@@ -55,6 +55,31 @@
</template>
<script setup>
import { onMounted } from 'vue'
import { useApi } from '~/composables/useApi'
const DESKTOP_SETTING_DEFAULT_TO_CHAT_KEY = 'desktop.settings.defaultToChatWhenData'
onMounted(async () => {
if (!process.client || typeof window === 'undefined') return
if (!window.wechatDesktop) return
let enabled = false
try {
enabled = localStorage.getItem(DESKTOP_SETTING_DEFAULT_TO_CHAT_KEY) === 'true'
} catch {}
if (!enabled) return
try {
const api = useApi()
const resp = await api.listChatAccounts()
const accounts = resp?.accounts || []
if (accounts.length) {
await navigateTo('/chat', { replace: true })
}
} catch {}
})
// 开始检测并跳转到结果页面
const startDetection = async () => {
// 直接跳转到检测结果页面,让该页面处理检测
@@ -100,4 +125,4 @@ const startDetection = async () => {
linear-gradient(90deg, rgba(7, 193, 96, 0.1) 1px, transparent 1px);
background-size: 50px 50px;
}
</style>
</style>