feat: 支持软件内登录

This commit is contained in:
imsyy
2024-12-11 10:38:15 +08:00
parent 3b07f7346f
commit a4d4cd5f70
4 changed files with 93 additions and 3 deletions

View File

@@ -24,6 +24,7 @@ import fs from "fs/promises";
import log from "../main/logger";
import Store from "electron-store";
import fg from "fast-glob";
import openLoginWin from "./loginWin";
// 注册 ipcMain
const initIpcMain = (
@@ -535,6 +536,9 @@ const initWinIpcMain = (
// 开始下载更新
ipcMain.on("start-download-update", () => startDownloadUpdate());
// 新建窗口
ipcMain.on("open-login-web", () => openLoginWin(win!));
};
// lyric

58
electron/main/loginWin.ts Normal file
View File

@@ -0,0 +1,58 @@
import { BrowserWindow, MenuItemConstructorOptions, Menu, session, dialog } from "electron";
const openLoginWin = (mainWin: BrowserWindow) => {
const loginSession = session.fromPartition("login-win");
const loginWin = new BrowserWindow({
parent: mainWin,
width: 1280,
height: 800,
center: true,
modal: true,
// resizable: false,
// movable: false,
// minimizable: false,
// maximizable: false,
webPreferences: {
nodeIntegration: false,
contextIsolation: true,
partition: "login-win",
},
});
// 打开网易云
loginWin.loadURL("https://music.163.com/#/login");
// 阻止新窗口创建
loginWin.webContents.setWindowOpenHandler(() => {
return { action: "deny" };
});
// 菜单栏
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();
},
},
];
const menu = Menu.buildFromTemplate(menuTemplate);
loginWin.setMenu(menu);
};
export default openLoginWin;

View File

@@ -6,7 +6,7 @@
</template>
可在官方的
<n-a href="https://music.163.com/" target="_blank">网页端</n-a>
和客户端的控制台中获取只需要 Cookie 中的 <code>MUSIC_U</code> 字段即可例如
或点击下方的自动获取只需要 Cookie 中的 <code>MUSIC_U</code> 字段即可例如
<code>MUSIC_U=00C7...;</code><br />请注意必须以 <code>;</code> 结束
</n-alert>
<n-input
@@ -15,12 +15,16 @@
type="textarea"
placeholder="请输入 Cookie"
/>
<n-button type="primary" @click="login">登录</n-button>
<n-flex class="menu">
<n-button v-if="isElectron" type="primary" @click="openWeb">自动获取</n-button>
<n-button type="primary" @click="login">登录</n-button>
</n-flex>
</div>
</template>
<script setup lang="ts">
import type { LoginType } from "@/types/main";
import { isElectron } from "@/utils/helper";
const emit = defineEmits<{
close: [];
@@ -29,6 +33,11 @@ const emit = defineEmits<{
const cookie = ref<string>();
// 开启窗口
const openWeb = () => {
window.electron.ipcRenderer.send("open-login-web");
};
// Cookie 登录
const login = async () => {
if (!cookie.value) {
@@ -53,6 +62,18 @@ const login = async () => {
console.error("Cookie 登录出错:", error);
}
};
onMounted(() => {
if (isElectron) {
window.electron.ipcRenderer.on("send-cookies", (_, value) => {
console.log(typeof value);
if (!value) return;
cookie.value = value;
login();
});
}
});
</script>
<style lang="scss" scoped>
@@ -73,5 +94,13 @@ const login = async () => {
margin: 4px 0;
font-family: auto;
}
.menu {
margin-top: 20px;
.n-button {
width: auto;
flex: 1;
margin: 0;
}
}
}
</style>

View File

@@ -36,7 +36,6 @@ export const isLogin = (): 0 | 1 | 2 => {
// 退出登录
export const toLogout = async () => {
const dataStore = useDataStore();
// 退出登录
await logout();
// 去除 cookie
removeCookie("MUSIC_U");