feat(desktop-client): add settings window and switch default API to production

- Open a dedicated settings BrowserWindow with general / login / diagnostics / about tabs
- Persist openAtLogin and keepRunningInBackground via desktop-settings.json; hide main window instead of quitting when background mode is on
- Default API base URL now points to api.shengxintui.com; LoginView and SettingsView share a single constant
- Restore wangyihao session cookies on probe and silent refresh, and accept the legacy http://mp.163.com console origin
- Drop the standalone diagnostics view in favor of the settings tab

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-04-30 22:00:01 +08:00
parent 40ef60eae2
commit baff51295a
13 changed files with 1110 additions and 226 deletions
+113 -2
View File
@@ -16,6 +16,12 @@ import {
startDesktopHealthIndicator,
syncDesktopHealthIndicator,
} from "./app-issue-indicator";
import {
getDesktopAppSettings,
initDesktopAppSettings,
setDesktopAppSetting,
type DesktopAppSettingKey,
} from "./app-settings";
import { bindPublishAccount, openPublishAccountConsole } from "./account-binder";
import { installObservedGlobalFetch, registerObservedRequestRendererTarget } from "./network-observer";
import { initProcessMetricsSampler } from "./process-metrics";
@@ -67,10 +73,13 @@ process.on("uncaughtException", (error) => {
let mainWindow: ElectronBrowserWindow | null = null;
let loginWindow: ElectronBrowserWindow | null = null;
let settingsWindow: ElectronBrowserWindow | null = null;
let mainRendererContents: ElectronWebContents | null = null;
let quitReleaseInFlight = false;
const forceCloseWindows = new WeakSet<ElectronBrowserWindow>();
type WindowMode = "login" | "main";
type RendererWindowMode = WindowMode | "settings";
type WindowModeSource = "auth-state" | "login" | "logout";
interface WindowSizePreset {
@@ -91,6 +100,14 @@ const WINDOW_SIZES: Record<WindowMode, WindowSizePreset> = {
main: { width: 1320, height: 860, minWidth: 900, minHeight: 600, resizable: true },
};
const SETTINGS_WINDOW_SIZE: WindowSizePreset = {
width: 920,
height: 640,
minWidth: 760,
minHeight: 520,
resizable: true,
};
function windowModePath(): string {
return join(app.getPath("userData"), "window-mode.json");
}
@@ -155,11 +172,23 @@ function currentLoginWindow(): ElectronBrowserWindow | null {
return loginWindow;
}
function currentSettingsWindow(): ElectronBrowserWindow | null {
if (!settingsWindow) {
return null;
}
if (settingsWindow.isDestroyed()) {
settingsWindow = null;
return null;
}
return settingsWindow;
}
function currentActiveWindow(): ElectronBrowserWindow | null {
return currentMainWindow() ?? currentLoginWindow();
}
function closeWindowAfterIpcReturn(window: ElectronBrowserWindow): void {
forceCloseWindows.add(window);
setTimeout(() => {
if (!window.isDestroyed()) {
window.close();
@@ -197,6 +226,16 @@ async function ensureLoginWindow(): Promise<ElectronBrowserWindow> {
return window;
}
async function ensureSettingsWindow(): Promise<ElectronBrowserWindow> {
const existing = currentSettingsWindow();
if (existing) {
return existing;
}
const window = await createSettingsWindow();
settingsWindow = window;
return window;
}
async function ensureWindow(mode: WindowMode): Promise<ElectronBrowserWindow> {
return mode === "main" ? ensureMainWindow() : ensureLoginWindow();
}
@@ -238,6 +277,16 @@ async function switchWindowMode(mode: WindowMode, _request: WindowModeRequest =
if (previous && previous !== target && !previous.isDestroyed()) {
closeWindowAfterIpcReturn(previous);
}
const settings = currentSettingsWindow();
if (mode === "login" && settings) {
closeWindowAfterIpcReturn(settings);
}
}
async function openSettingsWindow(): Promise<void> {
const window = await ensureSettingsWindow();
await revealWindow(window);
}
function rendererURL(): string | null {
@@ -248,7 +297,7 @@ function preloadPath(): string {
return join(__dirname, "../preload/bridge.cjs");
}
async function loadRenderer(window: ElectronBrowserWindow, mode: WindowMode): Promise<void> {
async function loadRenderer(window: ElectronBrowserWindow, mode: RendererWindowMode): Promise<void> {
const devServerURL = rendererURL();
if (devServerURL) {
const url = new URL(devServerURL);
@@ -265,7 +314,7 @@ async function loadRenderer(window: ElectronBrowserWindow, mode: WindowMode): Pr
});
}
async function mountRendererWebContents(window: ElectronBrowserWindow, mode: WindowMode): Promise<void> {
async function mountRendererWebContents(window: ElectronBrowserWindow, mode: RendererWindowMode): Promise<void> {
const contents = window.webContents;
contents.setWindowOpenHandler(() => ({ action: "deny" }));
@@ -321,6 +370,18 @@ async function createAppWindow(mode: WindowMode): Promise<ElectronBrowserWindow>
}
});
window.on("close", (event) => {
if (
mode === "main"
&& !quitReleaseInFlight
&& !forceCloseWindows.has(window)
&& getDesktopAppSettings().keepRunningInBackground
) {
event.preventDefault();
window.hide();
}
});
window.on("minimize", () => {
console.warn("[desktop-main] main window minimize event", {
at: new Date().toISOString(),
@@ -345,6 +406,41 @@ async function createAppWindow(mode: WindowMode): Promise<ElectronBrowserWindow>
return window;
}
async function createSettingsWindow(): Promise<ElectronBrowserWindow> {
const initial = SETTINGS_WINDOW_SIZE;
const window = new BrowserWindow({
width: initial.width,
height: initial.height,
minWidth: initial.minWidth,
minHeight: initial.minHeight,
show: false,
title: "GEO Rankly Desktop 设置",
icon: createDesktopHealthIcon("normal"),
titleBarStyle: "hiddenInset",
trafficLightPosition: { x: 14, y: 15 },
resizable: initial.resizable,
maximizable: initial.resizable,
fullscreenable: false,
autoHideMenuBar: true,
backgroundColor: nativeTheme.shouldUseDarkColors ? "#15191a" : "#f2f4f7",
webPreferences: {
preload: preloadPath(),
sandbox: true,
contextIsolation: true,
nodeIntegration: false,
},
});
window.once("closed", () => {
if (settingsWindow === window) {
settingsWindow = null;
}
});
await mountRendererWebContents(window, "settings");
return window;
}
function flattenError(error: unknown): Error {
if (error instanceof Error) {
const copy = new Error(error.message || "ipc_handler_failed");
@@ -450,6 +546,17 @@ function registerBridgeHandlers(): void {
await shell.openExternal(url);
return null;
});
safeHandle("desktop:open-settings-window", async () => {
await openSettingsWindow();
return null;
});
ipcMain.handle("desktop:get-app-settings", () => getDesktopAppSettings());
safeHandle("desktop:set-app-setting", async (_event, key: DesktopAppSettingKey, value: boolean) => {
if (key !== "openAtLogin" && key !== "keepRunningInBackground") {
throw new Error("unsupported_app_setting");
}
return setDesktopAppSetting(key, value);
});
ipcMain.handle(
"desktop:runtime-session-sync",
(_event, session: DesktopRuntimeSessionSyncRequest | null) => {
@@ -479,6 +586,7 @@ if (!hasSingleInstanceLock) {
} else {
app.whenReady().then(async () => {
currentWindowMode = readPersistedWindowMode();
initDesktopAppSettings();
registerBridgeHandlers();
await initSessionRegistry();
initAccountHealth();
@@ -508,6 +616,9 @@ if (!hasSingleInstanceLock) {
});
app.on("window-all-closed", () => {
if (getDesktopAppSettings().keepRunningInBackground) {
return;
}
if (process.platform !== "darwin") {
app.quit();
}