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:
@@ -0,0 +1,114 @@
|
||||
import { readFileSync, writeFileSync } from "node:fs";
|
||||
import { join } from "node:path";
|
||||
|
||||
import { app } from "electron/main";
|
||||
|
||||
export interface DesktopAppSettings {
|
||||
openAtLogin: boolean;
|
||||
keepRunningInBackground: boolean;
|
||||
}
|
||||
|
||||
export type DesktopAppSettingKey = keyof DesktopAppSettings;
|
||||
|
||||
const DEFAULT_DESKTOP_APP_SETTINGS: DesktopAppSettings = {
|
||||
openAtLogin: false,
|
||||
keepRunningInBackground: true,
|
||||
};
|
||||
|
||||
let cachedSettings: DesktopAppSettings = { ...DEFAULT_DESKTOP_APP_SETTINGS };
|
||||
|
||||
function settingsPath(): string {
|
||||
return join(app.getPath("userData"), "desktop-settings.json");
|
||||
}
|
||||
|
||||
function normalizeSettings(input: unknown): DesktopAppSettings {
|
||||
if (!input || typeof input !== "object") {
|
||||
return { ...DEFAULT_DESKTOP_APP_SETTINGS };
|
||||
}
|
||||
|
||||
const candidate = input as Partial<Record<DesktopAppSettingKey, unknown>>;
|
||||
return {
|
||||
openAtLogin:
|
||||
typeof candidate.openAtLogin === "boolean"
|
||||
? candidate.openAtLogin
|
||||
: DEFAULT_DESKTOP_APP_SETTINGS.openAtLogin,
|
||||
keepRunningInBackground:
|
||||
typeof candidate.keepRunningInBackground === "boolean"
|
||||
? candidate.keepRunningInBackground
|
||||
: DEFAULT_DESKTOP_APP_SETTINGS.keepRunningInBackground,
|
||||
};
|
||||
}
|
||||
|
||||
function readPersistedSettings(): DesktopAppSettings {
|
||||
try {
|
||||
return normalizeSettings(JSON.parse(readFileSync(settingsPath(), "utf8")));
|
||||
} catch {
|
||||
return { ...DEFAULT_DESKTOP_APP_SETTINGS };
|
||||
}
|
||||
}
|
||||
|
||||
function persistSettings(settings: DesktopAppSettings): void {
|
||||
try {
|
||||
writeFileSync(settingsPath(), JSON.stringify(settings), "utf8");
|
||||
} catch (error) {
|
||||
console.warn("[desktop-settings] persist failed", error);
|
||||
}
|
||||
}
|
||||
|
||||
function readSystemOpenAtLogin(fallback: boolean): boolean {
|
||||
try {
|
||||
return app.getLoginItemSettings().openAtLogin;
|
||||
} catch (error) {
|
||||
console.warn("[desktop-settings] read login item failed", error);
|
||||
return fallback;
|
||||
}
|
||||
}
|
||||
|
||||
function applyOpenAtLogin(openAtLogin: boolean): boolean {
|
||||
try {
|
||||
app.setLoginItemSettings({
|
||||
openAtLogin,
|
||||
openAsHidden: false,
|
||||
});
|
||||
return readSystemOpenAtLogin(openAtLogin);
|
||||
} catch (error) {
|
||||
console.warn("[desktop-settings] apply login item failed", error);
|
||||
return openAtLogin;
|
||||
}
|
||||
}
|
||||
|
||||
export function initDesktopAppSettings(): DesktopAppSettings {
|
||||
cachedSettings = readPersistedSettings();
|
||||
cachedSettings = {
|
||||
...cachedSettings,
|
||||
openAtLogin: applyOpenAtLogin(cachedSettings.openAtLogin),
|
||||
};
|
||||
persistSettings(cachedSettings);
|
||||
return getDesktopAppSettings();
|
||||
}
|
||||
|
||||
export function getDesktopAppSettings(): DesktopAppSettings {
|
||||
cachedSettings = {
|
||||
...cachedSettings,
|
||||
openAtLogin: readSystemOpenAtLogin(cachedSettings.openAtLogin),
|
||||
};
|
||||
return { ...cachedSettings };
|
||||
}
|
||||
|
||||
export function setDesktopAppSetting(
|
||||
key: DesktopAppSettingKey,
|
||||
value: boolean,
|
||||
): DesktopAppSettings {
|
||||
const next: DesktopAppSettings = {
|
||||
...cachedSettings,
|
||||
[key]: value,
|
||||
};
|
||||
|
||||
if (key === "openAtLogin") {
|
||||
next.openAtLogin = applyOpenAtLogin(value);
|
||||
}
|
||||
|
||||
cachedSettings = next;
|
||||
persistSettings(cachedSettings);
|
||||
return getDesktopAppSettings();
|
||||
}
|
||||
Reference in New Issue
Block a user