Skip to content

Commit

Permalink
✨ feat: 完善自动登录
Browse files Browse the repository at this point in the history
  • Loading branch information
imsyy committed Dec 12, 2024
1 parent 9accf5d commit 15b5008
Show file tree
Hide file tree
Showing 6 changed files with 145 additions and 39 deletions.
105 changes: 85 additions & 20 deletions electron/main/loginWin.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,23 @@
import { BrowserWindow, MenuItemConstructorOptions, Menu, session, dialog } from "electron";
import {
BrowserWindow,
MenuItemConstructorOptions,
Menu,
session,
dialog,
ipcMain,
} from "electron";
import icon from "../../public/icons/favicon.png?asset";

const openLoginWin = (mainWin: BrowserWindow) => {
const loginSession = session.fromPartition("login-win");
const loginWin = new BrowserWindow({
parent: mainWin,
title: "登录网易云音乐",
width: 1280,
height: 800,
center: true,
modal: true,
icon,
// resizable: false,
// movable: false,
// minimizable: false,
Expand All @@ -20,35 +30,90 @@ const openLoginWin = (mainWin: BrowserWindow) => {
});

// 打开网易云
loginWin.loadURL("https://music.163.com/#/login");
loginWin.loadURL("https://music.163.com/#/my/");

// 阻止新窗口创建
loginWin.webContents.setWindowOpenHandler(() => {
return { action: "deny" };
});

// 登录完成
const loginFinish = async () => {
if (!loginWin) return;
// 获取 Cookie
const cookies = await loginWin.webContents.session.cookies.get({ name: "MUSIC_U" });
if (!cookies?.[0]?.value) {
dialog.showMessageBox({
type: "info",
title: "登录失败",
message: "未查找到登录信息,请重试",
});
return;
}
const value = `MUSIC_U=${cookies[0].value};`;
// 发送回主进程
mainWin?.webContents.send("send-cookies", value);
await loginSession?.clearStorageData();
loginWin.close();
};

// 页面注入
// loginWin.webContents.once("did-finish-load", () => {
// const script = `
// const style = document.createElement('style');
// style.innerHTML = \`
// .login-btn {
// position: fixed;
// left: 0;
// bottom: 0;
// width: 100%;
// height: 80px;
// display: flex;
// align-items: center;
// justify-content: center;
// background-color: #242424;
// z-index: 99999;
// }

// .login-btn span {
// color: white;
// margin-right: 20px;
// }

// .login-btn button {
// border: none;
// outline: none;
// background-color: #c20c0c;
// border-radius: 25px;
// color: white;
// height: 40px;
// padding: 0 20px;
// cursor: pointer;
// }
// \`;
// document.head.appendChild(style);
// const div = document.createElement('div');
// div.className = 'login-btn';
// div.innerHTML = \`
// <span>请在登录成功后点击</span>
// <button>登录完成</button>
// \`;
// div.querySelector('button').addEventListener('click', () => {
// window.electron.ipcRenderer.send("login-success");
// });
// document.body.appendChild(div);
// `;
// loginWin.webContents.executeJavaScript(script);
// });

// 监听事件
ipcMain.on("login-success", loginFinish);

// 菜单栏
const menuTemplate: MenuItemConstructorOptions[] = [
{
label: "登录完成",
click: async () => {
if (!loginWin) return;
// 获取 Cookie
const cookies = await loginWin.webContents.session.cookies.get({ name: "MUSIC_U" });
if (!cookies?.[0]?.value) {
dialog.showMessageBox({
type: "info",
title: "登录失败",
message: "未查找到登录信息,请重试",
});
return;
}
const value = `MUSIC_U=${cookies[0].value};`;
// 发送回主进程
mainWin?.webContents.send("send-cookies", value);
await loginSession?.clearStorageData();
loginWin.close();
},
click: loginFinish,
},
];
const menu = Menu.buildFromTemplate(menuTemplate);
Expand Down
14 changes: 12 additions & 2 deletions src/components/Modal/Login/LoginCookie.vue
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,14 @@ const cookie = ref<string>();
// 开启窗口
const openWeb = () => {
window.electron.ipcRenderer.send("open-login-web");
window.$dialog.info({
title: "使用前告知",
content:
"请知悉,该功能仍旧无法确保账号的安全性!请自行决定是否使用!如遇打开窗口后页面出现白屏或者无法点击等情况,请关闭后再试。在登录完成后,请点击菜单栏中的 “登录完成” 按钮以完成登录( 通常位于窗口的左上角,macOS 位于顶部的全局菜单栏中 )",
positiveText: "我已了解",
negativeText: "取消",
onPositiveClick: () => window.electron.ipcRenderer.send("open-login-web"),
});
};
// Cookie 登录
Expand All @@ -44,8 +51,11 @@ const login = async () => {
window.$message.warning("请输入 Cookie");
return;
}
cookie.value = cookie.value.trim();
console.log(cookie.value.endsWith(";"));
// 是否为有效 Cookie
if (!cookie.value.includes("MUSIC_U") || cookie.value.endsWith(";")) {
if (!cookie.value.includes("MUSIC_U") || !cookie.value.endsWith(";")) {
window.$message.warning("请输入有效的 Cookie");
return;
}
Expand Down
37 changes: 24 additions & 13 deletions src/components/Setting/AboutSetting.vue
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,15 @@
{{ packageJson.version }}
</n-tag>
</n-flex>
<n-button type="primary" strong secondary @click="checkUpdate"> 检查更新 </n-button>
<n-button
:loading="statusStore.updateCheck"
type="primary"
strong
secondary
@click="checkUpdate"
>
{{ statusStore.updateCheck ? "检查更新中" : "检查更新" }}
</n-button>
</n-card>
<n-collapse-transition :show="!!updateData">
<n-card class="set-item update-data">
Expand Down Expand Up @@ -73,8 +81,11 @@
import type { UpdateLogType } from "@/types/main";
import { getUpdateLog, isElectron, openLink } from "@/utils/helper";
import { debounce } from "lodash-es";
import { useStatusStore } from "@/stores";
import packageJson from "@/../package.json";
const statusStore = useStatusStore();
// 社区数据
const communityData = [
{
Expand Down Expand Up @@ -102,18 +113,18 @@ const oldVersion = computed<UpdateLogType[]>(() => {
});
// 检查更新
const checkUpdate = debounce(() => {
if (!isElectron) {
window.open(packageJson.github + "/releases", "_blank");
return;
}
window.$notification.info({
title: "检查更新",
content: "正在检查更新,请稍后...",
duration: 3000,
});
window.electron.ipcRenderer.send("check-update", true);
}, 300);
const checkUpdate = debounce(
() => {
if (!isElectron) {
window.open(packageJson.github + "/releases", "_blank");
return;
}
statusStore.updateCheck = true;
window.electron.ipcRenderer.send("check-update", true);
},
300,
{ leading: true, trailing: false },
);
// 获取更新日志
const getUpdateData = async () => (updateData.value = await getUpdateLog());
Expand Down
8 changes: 7 additions & 1 deletion src/components/Setting/GeneralSetting.vue
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,13 @@
<n-text class="name">在线服务</n-text>
<n-text class="tip" :depth="3">是否开启软件的在线服务</n-text>
</div>
<n-switch class="set" :value="useOnlineService" :round="false" @update:value="modeChange" />
<n-switch
class="set"
:disabled="true"
:value="useOnlineService"
:round="false"
@update:value="modeChange"
/>
</n-card>
<n-card class="set-item">
<div class="label">
Expand Down
3 changes: 3 additions & 0 deletions src/stores/status.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ interface StatusState {
showDesktopLyric: boolean;
showPlayerComment: boolean;
personalFmMode: boolean;
updateCheck: boolean;
}

export const useStatusStore = defineStore("status", {
Expand Down Expand Up @@ -99,6 +100,8 @@ export const useStatusStore = defineStore("status", {
showDesktopLyric: false,
// 播放器评论
showPlayerComment: false,
// 更新检查
updateCheck: false,
}),
getters: {
// 播放音量图标
Expand Down
17 changes: 14 additions & 3 deletions src/utils/initIpc.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
import { isElectron } from "./helper";
import { openUpdateApp } from "./modal";
import { useMusicStore, useDataStore } from "@/stores";
import player from "./player";
import { useMusicStore, useDataStore, useStatusStore } from "@/stores";
import { toLikeSong } from "./auth";
import player from "./player";

// 关闭更新状态
const closeUpdateStatus = () => {
const statusStore = useStatusStore();
statusStore.updateCheck = false;
};

// 全局 IPC 事件
const initIpc = () => {
Expand Down Expand Up @@ -35,13 +41,18 @@ const initIpc = () => {
window.electron.ipcRenderer.on("closeDesktopLyric", () => player.toggleDesktopLyric());
// 无更新
window.electron.ipcRenderer.on("update-not-available", () => {
closeUpdateStatus();
window.$message.success("当前已是最新版本");
});
// 有更新
window.electron.ipcRenderer.on("update-available", (_, info) => openUpdateApp(info));
window.electron.ipcRenderer.on("update-available", (_, info) => {
closeUpdateStatus();
openUpdateApp(info);
});
// 更新错误
window.electron.ipcRenderer.on("update-error", (_, error) => {
console.error("Error updating:", error);
closeUpdateStatus();
window.$message.error("更新过程出现错误");
});
} catch (error) {
Expand Down

0 comments on commit 15b5008

Please sign in to comment.