mirror of
https://github.com/imsyy/SPlayer.git
synced 2025-11-25 11:29:26 +08:00
🦄 refactor: 主进程重构
修复导航栏不及时响应窗口状态 修复 thumb 展示异常
This commit is contained in:
100
electron/main/windows/login-window.ts
Normal file
100
electron/main/windows/login-window.ts
Normal file
@@ -0,0 +1,100 @@
|
||||
import { app, BrowserWindow, session } from "electron";
|
||||
import { createWindow } from "./index";
|
||||
import { join } from "path";
|
||||
|
||||
class LoginWindow {
|
||||
private win: BrowserWindow | null = null;
|
||||
private loginTimer: NodeJS.Timeout | null = null;
|
||||
private loginSession: Electron.Session | null = null;
|
||||
|
||||
constructor() {}
|
||||
|
||||
private getLoginSession(): Electron.Session {
|
||||
if (!this.loginSession) {
|
||||
this.loginSession = session.fromPartition("persist:login");
|
||||
}
|
||||
return this.loginSession;
|
||||
}
|
||||
// 事件绑定
|
||||
private event(mainWin: BrowserWindow): void {
|
||||
if (!this.win) return;
|
||||
// 阻止新窗口创建
|
||||
this.win.webContents.setWindowOpenHandler(() => ({ action: "deny" }));
|
||||
// 加载完成后显示并开始轮询登录状态
|
||||
this.win.webContents.once("did-finish-load", () => {
|
||||
this.win?.show();
|
||||
this.loginTimer = setInterval(() => this.checkLogin(mainWin), 1000);
|
||||
this.win?.on("closed", () => {
|
||||
if (this.loginTimer) clearInterval(this.loginTimer);
|
||||
this.loginTimer = null;
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
// 检查是否已登录
|
||||
private async checkLogin(mainWin: BrowserWindow) {
|
||||
if (!this.win) return;
|
||||
try {
|
||||
this.win.webContents.executeJavaScript(
|
||||
"document.title = '登录网易云音乐( 若遇到无响应请关闭后重试 )'",
|
||||
);
|
||||
// 判断 MUSIC_U
|
||||
const MUSIC_U = await this.getLoginSession().cookies.get({ name: "MUSIC_U" });
|
||||
if (MUSIC_U && MUSIC_U.length > 0) {
|
||||
if (this.loginTimer) clearInterval(this.loginTimer);
|
||||
this.loginTimer = null;
|
||||
const value = `MUSIC_U=${MUSIC_U[0].value};`;
|
||||
// 发送回主进程
|
||||
mainWin?.webContents.send("send-cookies", value);
|
||||
this.win.destroy();
|
||||
this.win = null;
|
||||
}
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
}
|
||||
}
|
||||
|
||||
// 创建登录窗口
|
||||
async create(mainWin: BrowserWindow): Promise<BrowserWindow | null> {
|
||||
await app.whenReady();
|
||||
const loginSession = this.getLoginSession();
|
||||
// 清理登录会话存储
|
||||
await loginSession.clearStorageData({
|
||||
storages: ["cookies", "localstorage"],
|
||||
});
|
||||
|
||||
this.win = createWindow({
|
||||
parent: mainWin,
|
||||
title: "登录网易云音乐( 若遇到无响应请关闭后重试 )",
|
||||
width: 1280,
|
||||
height: 800,
|
||||
center: true,
|
||||
autoHideMenuBar: true,
|
||||
webPreferences: {
|
||||
preload: join(__dirname, "../preload/index.mjs"),
|
||||
sandbox: false,
|
||||
webSecurity: false,
|
||||
allowRunningInsecureContent: true,
|
||||
spellcheck: false,
|
||||
nodeIntegration: true,
|
||||
nodeIntegrationInWorker: true,
|
||||
contextIsolation: false,
|
||||
session: loginSession,
|
||||
},
|
||||
});
|
||||
|
||||
if (!this.win) return null;
|
||||
// 加载登录地址
|
||||
this.win.loadURL("https://music.163.com/#/login/");
|
||||
// 绑定事件
|
||||
this.event(mainWin);
|
||||
return this.win;
|
||||
}
|
||||
|
||||
// 获取窗口
|
||||
getWin(): BrowserWindow | null {
|
||||
return this.win;
|
||||
}
|
||||
}
|
||||
|
||||
export default new LoginWindow();
|
||||
Reference in New Issue
Block a user