qier222 4 years ago
commit 2ee288b9ba

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.1 KiB

@ -17,6 +17,10 @@ const isDevelopment = process.env.NODE_ENV !== "production";
// Keep a global reference of the window object, if you don't, the window will // Keep a global reference of the window object, if you don't, the window will
// be closed automatically when the JavaScript object is garbage collected. // be closed automatically when the JavaScript object is garbage collected.
let win; let win;
// eslint-disable-next-line no-unused-vars
let tray;
let willQuitApp = false;
// ipcMain // ipcMain
initIpcMain(win); initIpcMain(win);
@ -39,7 +43,7 @@ function createWindow() {
win.setMenuBarVisibility(false); win.setMenuBarVisibility(false);
if (process.platform !== "darwin") { if (process.platform !== "darwin") {
createTray(win); tray = createTray(win);
} }
if (process.env.WEBPACK_DEV_SERVER_URL) { if (process.env.WEBPACK_DEV_SERVER_URL) {
@ -60,9 +64,19 @@ function createWindow() {
e.preventDefault(); e.preventDefault();
shell.openExternal(url); shell.openExternal(url);
}); });
win.on("closed", () => { win.on("close", (e) => {
win = null; if (willQuitApp) {
/* the user tried to quit the app */
win = null;
} else {
/* the user only tried to close the window */
e.preventDefault();
win.hide();
}
}); });
// win.on("closed", () => {
// win = null;
// });
return win; return win;
} }
@ -71,7 +85,7 @@ app.on("window-all-closed", () => {
// On macOS it is common for applications and their menu bar // On macOS it is common for applications and their menu bar
// to stay active until the user quits explicitly with Cmd + Q // to stay active until the user quits explicitly with Cmd + Q
if (process.platform !== "darwin") { if (process.platform !== "darwin") {
app.quit(); // app.quit();
} }
}); });
@ -80,9 +94,16 @@ app.on("activate", () => {
// dock icon is clicked and there are no other windows open. // dock icon is clicked and there are no other windows open.
if (win === null) { if (win === null) {
createWindow(); createWindow();
} else {
win.show();
} }
}); });
/**
* 'before-quit' is emitted when Electron receives the signal to exit and wants to start closing windows
*/
app.on("before-quit", () => (willQuitApp = true));
// This method will be called when Electron has finished // This method will be called when Electron has finished
// initialization and is ready to create browser windows. // initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs. // Some APIs can only be used after this event occurs.

@ -33,8 +33,9 @@ export function initIpcMain(win) {
}); });
ipcMain.on("close", () => { ipcMain.on("close", () => {
win.close(); win.hide();
app.quit(); // win.close();
// app.quit();
}); });
ipcMain.on("minimize", () => { ipcMain.on("minimize", () => {

@ -128,6 +128,7 @@ export function createMenu(win) {
{ {
label: "Window", label: "Window",
submenu: [ submenu: [
{ role: "close" },
{ role: "minimize" }, { role: "minimize" },
{ role: "zoom" }, { role: "zoom" },
{ role: "reload" }, { role: "reload" },

@ -1,5 +1,6 @@
/* global __static */
import path from "path"; import path from "path";
import { nativeImage, Tray } from "electron"; import { app, nativeImage, Tray, Menu } from "electron";
export function createTray(win) { export function createTray(win) {
let icon = nativeImage let icon = nativeImage
@ -17,5 +18,18 @@ export function createTray(win) {
win.show(); win.show();
} }
}); });
tray.on("right-click", () => {
const contextMenu = Menu.buildFromTemplate([
{
label: "Quit",
click: () => {
app.exit();
},
},
]);
tray.popUpContextMenu(contextMenu);
});
return tray; return tray;
} }

@ -1,5 +1,5 @@
import { updateMediaSessionMetaData } from "@/utils/mediaSession"; import { updateMediaSessionMetaData } from "@/utils/mediaSession";
import { getTrackDetail, scrobble, getMP3 } from "@/api/track"; import { getTrackDetail, scrobble, getMP3 as getMP3Api } from "@/api/track";
import { isAccountLoggedIn } from "@/utils/auth"; import { isAccountLoggedIn } from "@/utils/auth";
import { updateHttps } from "@/utils/common"; import { updateHttps } from "@/utils/common";
import localforage from "localforage"; import localforage from "localforage";
@ -40,17 +40,18 @@ export default {
updateMediaSessionMetaData(track); updateMediaSessionMetaData(track);
document.title = `${track.name} · ${track.ar[0].name} - YesPlayMusic`; document.title = `${track.name} · ${track.ar[0].name} - YesPlayMusic`;
let unblockSongUrl = null;
if (track.playable === false) { if (track.playable === false) {
let res = undefined; let res = undefined;
if (process.env.IS_ELECTRON === true) { if (process.env.IS_ELECTRON === true) {
res = ipcRenderer.sendSync("unblock-music", track); res = ipcRenderer.sendSync("unblock-music", track);
} }
if (res?.url) { if (res?.url) {
commitMP3(res.url); unblockSongUrl = res.url;
} else { } else {
dispatch("nextTrack"); dispatch("nextTrack");
return;
} }
return;
} }
function commitMP3(mp3) { function commitMP3(mp3) {
@ -59,31 +60,56 @@ export default {
dispatch("nextTrack"); dispatch("nextTrack");
}); });
} }
function getMP3(id) {
return getMP3Api(id).then((data) => {
// 未知情况下会没有返回数据导致报错,增加防范逻辑
if (data.data[0]) {
const url = updateHttps(data.data[0].url);
commitMP3(url);
return url;
}
});
}
if (isAccountLoggedIn()) { if (isAccountLoggedIn()) {
if (store.state.settings.automaticallyCacheSongs === true) { if (store.state.settings.automaticallyCacheSongs === true) {
let tracks = localforage.createInstance({ let tracks = localforage.createInstance({
name: "tracks", name: "tracks",
}); });
tracks.getItem(`${track.id}`).then((t) => { tracks
if (t !== null) { .getItem(`${track.id}`)
commitMP3(URL.createObjectURL(t.mp3)); .then((t) => {
} else { if (t !== null) {
cacheTrack(`${track.id}`).then((t) => { const blob = new Blob([t.mp3]);
commitMP3(URL.createObjectURL(t.mp3)); commitMP3(URL.createObjectURL(blob));
}); } else {
} if (unblockSongUrl) {
}); commitMP3(unblockSongUrl);
cacheTrack(`${track.id}`, unblockSongUrl);
} else {
getMP3(track.id).then((url) => {
cacheTrack(`${track.id}`, url);
});
}
}
})
.catch((err) => {
console.log(err.messaeg);
if (unblockSongUrl) {
commitMP3(unblockSongUrl);
} else {
getMP3(track.id);
}
});
} else { } else {
getMP3(track.id).then((data) => { getMP3(track.id);
// 未知情况下会没有返回数据导致报错,增加防范逻辑
if (data.data[0]) {
const url = updateHttps(data.data[0].url);
commitMP3(url);
}
});
} }
} else { } else {
commitMP3(`https://music.163.com/song/media/outer/url?id=${track.id}`); commitMP3(
unblockSongUrl ||
`https://music.163.com/song/media/outer/url?id=${track.id}`
);
} }
}); });
}, },

@ -43,10 +43,11 @@ Howler.volume(store.state.player.volume);
// 防止软件第一次打开资源加载2次 // 防止软件第一次打开资源加载2次
Howler.autoUnlock = false; Howler.autoUnlock = false;
const currentTrackId = store.state?.player?.currentTrack?.id; const currentTrack = store.state?.player?.currentTrack;
if (currentTrackId) { if (currentTrack?.id) {
store.dispatch("switchTrack", { store.dispatch("switchTrack", {
id: currentTrackId, id: currentTrack.id,
sort: currentTrack.sort,
autoplay: false, autoplay: false,
}); });
} }

@ -1,11 +1,10 @@
// import axios from "axios"; import axios from "axios";
import localforage from "localforage"; import localforage from "localforage";
import { getMP3 } from "@/api/track";
export function cacheTrack(id) { export function cacheTrack(id, url) {
// let tracks = localforage.createInstance({ let tracks = localforage.createInstance({
// name: "tracks", name: "tracks",
// }); });
// TODO: limit cache songs number // TODO: limit cache songs number
// tracks.length().then(function (length) { // tracks.length().then(function (length) {
@ -17,17 +16,14 @@ export function cacheTrack(id) {
// }); // });
// TODO: cache track details // TODO: cache track details
return getMP3(id).then((data) => { return axios
// return axios .get(url, {
// .get(data.data[0].url.replace(/^http:/, "https:"), { responseType: "arraybuffer",
// responseType: "blob", })
// }) .then((response) => {
// .then((data) => { tracks.setItem(`${id}`, { mp3: response.data });
// tracks.setItem(`${id}`, { mp3: data.data }); return { mp3: response.data };
// return { mp3: data.data }; });
// });
return { mp3: data.data[0].url.replace(/^http:/, "https:") };
});
} }
export function countDBSize(dbName) { export function countDBSize(dbName) {
@ -37,11 +33,11 @@ export function countDBSize(dbName) {
let trackSizes = []; let trackSizes = [];
return db return db
.iterate((value) => { .iterate((value) => {
trackSizes.push(value.mp3.size); trackSizes.push(value.mp3.byteLength);
}) })
.then(() => { .then(() => {
return { return {
bytes: trackSizes.reduce((s1, s2) => s1 + s2), bytes: trackSizes.reduce((s1, s2) => s1 + s2, 0),
length: trackSizes.length, length: trackSizes.length,
}; };
}) })

Loading…
Cancel
Save