Skip to content

Commit

Permalink
✨ feat: 支持软件内登录
Browse files Browse the repository at this point in the history
  • Loading branch information
imsyy committed Dec 11, 2024
1 parent 3b07f73 commit a4d4cd5
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 3 deletions.
4 changes: 4 additions & 0 deletions electron/main/ipcMain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = (
Expand Down Expand Up @@ -535,6 +536,9 @@ const initWinIpcMain = (

// 开始下载更新
ipcMain.on("start-download-update", () => startDownloadUpdate());

// 新建窗口
ipcMain.on("open-login-web", () => openLoginWin(win!));
};

// lyric
Expand Down
58 changes: 58 additions & 0 deletions electron/main/loginWin.ts
Original file line number Diff line number Diff line change
@@ -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;
33 changes: 31 additions & 2 deletions src/components/Modal/Login/LoginCookie.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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: [];
Expand All @@ -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) {
Expand All @@ -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>
Expand All @@ -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>
1 change: 0 additions & 1 deletion src/utils/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ export const isLogin = (): 0 | 1 | 2 => {
// 退出登录
export const toLogout = async () => {
const dataStore = useDataStore();
// 退出登录
await logout();
// 去除 cookie
removeCookie("MUSIC_U");
Expand Down

0 comments on commit a4d4cd5

Please sign in to comment.