diff --git a/src/App.vue b/src/App.vue index abcf1e7..c920d03 100644 --- a/src/App.vue +++ b/src/App.vue @@ -25,8 +25,8 @@ import Navbar from "./components/Navbar.vue"; import Player from "./components/Player.vue"; import GlobalEvents from "vue-global-events"; -const electron = window.require('electron') -const ipcRenderer = electron.ipcRenderer +const electron = window.require("electron"); +const ipcRenderer = electron.ipcRenderer; export default { name: "App", @@ -35,44 +35,44 @@ export default { Player, GlobalEvents, }, - created(){ + created() { // listens to the main process 'changeRouteTo' event and changes the route from // inside this Vue instance, according to what path the main process requires. // responds to Menu click() events at the main process and changes the route accordingly. - ipcRenderer.on('changeRouteTo', (event, path) => { - console.log(event) - this.$router.push(path) - }) - ipcRenderer.on('play', () => { - this.$refs.player.play() - }) - ipcRenderer.on('next', () => { - this.$refs.player.next() - }) - ipcRenderer.on('previous', () => { - this.$refs.player.previous() - }) - ipcRenderer.on('increaseVolume', () => { + ipcRenderer.on("changeRouteTo", (event, path) => { + console.log(event); + this.$router.push(path); + }); + ipcRenderer.on("play", () => { + this.$refs.player.play(); + }); + ipcRenderer.on("next", () => { + this.$refs.player.next(); + }); + ipcRenderer.on("previous", () => { + this.$refs.player.previous(); + }); + ipcRenderer.on("increaseVolume", () => { if (this.$refs.player.volume + 0.1 >= 1) { - return this.$refs.player.volume = 1 + return (this.$refs.player.volume = 1); } - this.$refs.player.volume += 0.1 - }) - ipcRenderer.on('decreaseVolume', () => { + this.$refs.player.volume += 0.1; + }); + ipcRenderer.on("decreaseVolume", () => { if (this.$refs.player.volume - 0.1 <= 0) { - return this.$refs.player.volume = 0 + return (this.$refs.player.volume = 0); } - this.$refs.player.volume -= 0.1 - }) - ipcRenderer.on('like', () => { - this.$refs.player.likeCurrentSong() - }) - ipcRenderer.on('repeat', () => { - this.$refs.player.repeat() - }) - ipcRenderer.on('shuffle', () => { - this.$refs.player.shuffle() - }) + this.$refs.player.volume -= 0.1; + }); + ipcRenderer.on("like", () => { + this.$refs.player.likeCurrentSong(); + }); + ipcRenderer.on("repeat", () => { + this.$refs.player.repeat(); + }); + ipcRenderer.on("shuffle", () => { + this.$refs.player.shuffle(); + }); }, methods: { play(e) { diff --git a/src/background.js b/src/background.js index 01f1f53..7f5787f 100644 --- a/src/background.js +++ b/src/background.js @@ -1,6 +1,6 @@ "use strict"; -import path from 'path' +import path from "path"; // import { autoUpdater } from "electron-updater" import { app, @@ -23,27 +23,27 @@ let win; protocol.registerSchemesAsPrivileged([ { scheme: "app", privileges: { secure: true, standard: true } }, ]); -const iconString = path.join(__static, "img/icons/apple-touch-icon.png") +const iconString = path.join(__static, "img/icons/apple-touch-icon.png"); -let bounceId = app.dock.bounce() -app.dock.setIcon(iconString) +let bounceId = app.dock.bounce(); +app.dock.setIcon(iconString); function createWindow() { - const touchbar = require('./electron/touchbar.js') - const tray = require('./electron/tray.js') - const createMenu = require('./electron/menu.js') - tray.on('click', function () { + const touchbar = require("./electron/touchbar.js"); + const tray = require("./electron/tray.js"); + const createMenu = require("./electron/menu.js"); + tray.on("click", function () { if (win.isVisible()) { - win.hide() + win.hide(); } else { - win.show() + win.show(); } - }) + }); win = new BrowserWindow({ width: 1440, height: 768, icon: iconString, - titleBarStyle: 'default', + titleBarStyle: "default", webPreferences: { webSecurity: false, nodeIntegration: true, @@ -52,13 +52,13 @@ function createWindow() { }); try { - createMenu(win) - win.setTouchBar(touchbar) - win.setAutoHideCursor(true) - app.dock.cancelBounce(bounceId) + createMenu(win); + win.setTouchBar(touchbar); + win.setAutoHideCursor(true); + app.dock.cancelBounce(bounceId); // autoUpdater.checkForUpdatesAndNotify() } catch (error) { - console.log(error) + console.log(error); } if (process.env.WEBPACK_DEV_SERVER_URL) { @@ -98,7 +98,7 @@ app.on("activate", () => { // Some APIs can only be used after this event occurs. app.on("ready", async () => { // 启动 api 服务器 - require('./electron/services.js') + require("./electron/services.js"); if (isDevelopment && !process.env.IS_TEST) { // Install Vue Devtools try { diff --git a/src/electron/menu.js b/src/electron/menu.js index a50d2f5..2eb3b7d 100644 --- a/src/electron/menu.js +++ b/src/electron/menu.js @@ -1,178 +1,177 @@ -const { app, Menu } = require('electron') +const { app, Menu } = require("electron"); // import { autoUpdater } from "electron-updater" // const version = app.getVersion(); -const isMac = process.platform === 'darwin' +const isMac = process.platform === "darwin"; function createMenu(win) { - let menu = null + let menu = null; const template = [ - ...(isMac ? [{ - label: app.name, - submenu: [ - { role: 'about' }, - { type: 'separator' }, - { role: 'services' }, - { type: 'separator' }, - { type: 'separator' }, - { - label: 'Preferences...', - accelerator: (() => isMac ? 'CmdOrCtrl+,' : 'Ctrl+,')(), - click: () => { - win.webContents.send("changeRouteTo", "/settings") + ...(isMac + ? [ + { + label: app.name, + submenu: [ + { role: "about" }, + { type: "separator" }, + { role: "services" }, + { type: "separator" }, + { type: "separator" }, + { + label: "Preferences...", + accelerator: (() => (isMac ? "CmdOrCtrl+," : "Ctrl+,"))(), + click: () => { + win.webContents.send("changeRouteTo", "/settings"); + }, + role: "preferences", + }, + { type: "separator" }, + { role: "hide" }, + { role: "hideothers" }, + { role: "unhide" }, + { type: "separator" }, + { role: "quit" }, + ], }, - role: 'preferences' - }, - { type: 'separator' }, - { role: 'hide' }, - { role: 'hideothers' }, - { role: 'unhide' }, - { type: 'separator' }, - { role: 'quit' } - ] - }] : []), + ] + : []), { - label: 'Edit', + label: "Edit", submenu: [ - { role: 'undo' }, - { role: 'redo' }, - { type: 'separator' }, - { role: 'cut' }, - { role: 'copy' }, - { role: 'paste' }, - ...(isMac ? [ - { role: 'delete' }, - { role: 'selectAll' }, - { type: 'separator' }, - { - label: 'Speech', - submenu: [ - { role: 'startspeaking' }, - { role: 'stopspeaking' } + { role: "undo" }, + { role: "redo" }, + { type: "separator" }, + { role: "cut" }, + { role: "copy" }, + { role: "paste" }, + ...(isMac + ? [ + { role: "delete" }, + { role: "selectAll" }, + { type: "separator" }, + { + label: "Speech", + submenu: [{ role: "startspeaking" }, { role: "stopspeaking" }], + }, ] - } - ] : [ - { role: 'delete' }, - { type: 'separator' }, - { role: 'selectAll' } - ]) - ] + : [{ role: "delete" }, { type: "separator" }, { role: "selectAll" }]), + ], }, { - label: 'Controls', + label: "Controls", submenu: [ { - label: 'Play', - accelerator: 'Space', + label: "Play", + accelerator: "Space", click: () => { - win.webContents.send("play") + win.webContents.send("play"); }, }, { - label: 'Next', - accelerator: 'CmdOrCtrl+Right', + label: "Next", + accelerator: "CmdOrCtrl+Right", click: () => { - win.webContents.send("next") + win.webContents.send("next"); }, }, { - label: 'Previous', - accelerator: 'CmdOrCtrl+Left', + label: "Previous", + accelerator: "CmdOrCtrl+Left", click: () => { - win.webContents.send("previous") + win.webContents.send("previous"); }, }, { - label: 'Increase Volume', - accelerator: 'CmdOrCtrl+Up', + label: "Increase Volume", + accelerator: "CmdOrCtrl+Up", click: () => { - win.webContents.send("increaseVolume") + win.webContents.send("increaseVolume"); }, }, { - label: 'Decrease Volume', - accelerator: 'CmdOrCtrl+Down', + label: "Decrease Volume", + accelerator: "CmdOrCtrl+Down", click: () => { - win.webContents.send("decreaseVolume") + win.webContents.send("decreaseVolume"); }, }, { - label: 'Like', - accelerator: 'CmdOrCtrl+L', + label: "Like", + accelerator: "CmdOrCtrl+L", click: () => { - win.webContents.send("like") + win.webContents.send("like"); }, }, { - label: 'Repeat', - accelerator: 'Alt+R', + label: "Repeat", + accelerator: "Alt+R", click: () => { - win.webContents.send("repeat") + win.webContents.send("repeat"); }, }, { - label: 'Shuffle', - accelerator: 'Alt+S', + label: "Shuffle", + accelerator: "Alt+S", click: () => { - win.webContents.send("shuffle") + win.webContents.send("shuffle"); }, - } - ] + }, + ], }, { - label: 'Window', + label: "Window", submenu: [ - { role: 'minimize' }, - { role: 'zoom' }, - { role: 'reload' }, - { role: 'forcereload' }, - { role: 'toggledevtools' }, - { type: 'separator' }, - { role: 'togglefullscreen' }, - ...(isMac ? [ - { type: 'separator' }, - { role: 'front' }, - { type: 'separator' }, - { - role: 'window', - id: 'window', - label: 'Yes Play Music', - type: 'checkbox', - checked: true, - click: () => { - const current = menu.getMenuItemById('window') - if (current.checked === false) { - win.hide() - } else { - win.show() - } - }, - } - ] : [ - { role: 'close' } - ]) - ] + { role: "minimize" }, + { role: "zoom" }, + { role: "reload" }, + { role: "forcereload" }, + { role: "toggledevtools" }, + { type: "separator" }, + { role: "togglefullscreen" }, + ...(isMac + ? [ + { type: "separator" }, + { role: "front" }, + { type: "separator" }, + { + role: "window", + id: "window", + label: "Yes Play Music", + type: "checkbox", + checked: true, + click: () => { + const current = menu.getMenuItemById("window"); + if (current.checked === false) { + win.hide(); + } else { + win.show(); + } + }, + }, + ] + : [{ role: "close" }]), + ], }, { - label: 'Help', + label: "Help", submenu: [ { - label: 'Github', + label: "Github", click: async () => { - const { shell } = require('electron') - await shell.openExternal('https://github.com/qier222/YesPlayMusic') - } + const { shell } = require("electron"); + await shell.openExternal("https://github.com/qier222/YesPlayMusic"); + }, }, { - label: 'Electron', + label: "Electron", click: async () => { - const { shell } = require('electron') - await shell.openExternal('https://electronjs.org') - } + const { shell } = require("electron"); + await shell.openExternal("https://electronjs.org"); + }, }, - ] - } - ] + ], + }, + ]; // for window // if (process.platform === "win32") { // template.push({ @@ -194,10 +193,8 @@ function createMenu(win) { // ], // }); // } - menu = Menu.buildFromTemplate(template) - Menu.setApplicationMenu(menu) + menu = Menu.buildFromTemplate(template); + Menu.setApplicationMenu(menu); } -module.exports = createMenu - - +module.exports = createMenu; diff --git a/src/electron/services.js b/src/electron/services.js index f449b07..7436c3a 100644 --- a/src/electron/services.js +++ b/src/electron/services.js @@ -1,57 +1,57 @@ const express = require("express"); -const bodyParser = require('body-parser') -const cache = require('../../napi/util/apicache').middleware -const fileUpload = require('express-fileupload') +const bodyParser = require("body-parser"); +const cache = require("../../napi/util/apicache").middleware; +const fileUpload = require("express-fileupload"); import routes from "../../napi/routes"; // Integrate API -const app = express() +const app = express(); // CORS & Preflight request app.use((req, res, next) => { - if (req.path !== '/' && !req.path.includes('.')) { + if (req.path !== "/" && !req.path.includes(".")) { res.set({ - 'Access-Control-Allow-Credentials': true, - 'Access-Control-Allow-Origin': req.headers.origin || '*', - 'Access-Control-Allow-Headers': 'X-Requested-With,Content-Type', - 'Access-Control-Allow-Methods': 'PUT,POST,GET,DELETE,OPTIONS', - 'Content-Type': 'application/json; charset=utf-8', - }) + "Access-Control-Allow-Credentials": true, + "Access-Control-Allow-Origin": req.headers.origin || "*", + "Access-Control-Allow-Headers": "X-Requested-With,Content-Type", + "Access-Control-Allow-Methods": "PUT,POST,GET,DELETE,OPTIONS", + "Content-Type": "application/json; charset=utf-8", + }); } - req.method === 'OPTIONS' ? res.status(204).end() : next() -}) + req.method === "OPTIONS" ? res.status(204).end() : next(); +}); // cookie parser app.use((req, res, next) => { - req.cookies = {} - ;(req.headers.cookie || '').split(/\s*;\s*/).forEach((pair) => { - let crack = pair.indexOf('=') - if (crack < 1 || crack == pair.length - 1) return + req.cookies = {}; + (req.headers.cookie || "").split(/\s*;\s*/).forEach((pair) => { + let crack = pair.indexOf("="); + if (crack < 1 || crack == pair.length - 1) return; req.cookies[ decodeURIComponent(pair.slice(0, crack)).trim() - ] = decodeURIComponent(pair.slice(crack + 1)).trim() - }) - next() -}) + ] = decodeURIComponent(pair.slice(crack + 1)).trim(); + }); + next(); +}); // body parser -app.use(bodyParser.json()) -app.use(bodyParser.urlencoded({ extended: false })) +app.use(bodyParser.json()); +app.use(bodyParser.urlencoded({ extended: false })); -app.use(fileUpload()) +app.use(fileUpload()); // cache -app.use(cache('2 minutes', (req, res) => res.statusCode === 200)) +app.use(cache("2 minutes", (req, res) => res.statusCode === 200)); // router -Object.keys(routes).forEach(route => { - app.use(route, routes[route]) -}) +Object.keys(routes).forEach((route) => { + app.use(route, routes[route]); +}); -const port = process.env.PORT || 10754 -const host = process.env.HOST || '127.0.0.1' +const port = process.env.PORT || 10754; +const host = process.env.HOST || "127.0.0.1"; app.server = app.listen(port, host, () => { - console.log(`server running @ http://${host ? host : 'localhost'}:${port}`) -}) + console.log(`server running @ http://${host ? host : "localhost"}:${port}`); +}); diff --git a/src/electron/touchbar.js b/src/electron/touchbar.js index 6633adb..5c9f37f 100644 --- a/src/electron/touchbar.js +++ b/src/electron/touchbar.js @@ -1,96 +1,92 @@ -const { - app, - BrowserWindow, - TouchBar -} = require('electron') +const { app, BrowserWindow, TouchBar } = require("electron"); -const { TouchBarLabel, TouchBarButton, TouchBarSpacer } = TouchBar +const { TouchBarLabel, TouchBarButton, TouchBarSpacer } = TouchBar; -let spinning = false +let spinning = false; // Reel labels -const reel1 = new TouchBarLabel() -const reel2 = new TouchBarLabel() -const reel3 = new TouchBarLabel() +const reel1 = new TouchBarLabel(); +const reel2 = new TouchBarLabel(); +const reel3 = new TouchBarLabel(); // Spin result label -const result = new TouchBarLabel() +const result = new TouchBarLabel(); // Spin button const spin = new TouchBarButton({ - label: '🎰 Spin', - backgroundColor: '#7851A9', + label: "🎰 Spin", + backgroundColor: "#7851A9", click: () => { // Ignore clicks if already spinning if (spinning) { - return + return; } - spinning = true - result.label = '' + spinning = true; + result.label = ""; - let timeout = 10 - const spinLength = 4 * 1000 // 4 seconds - const startTime = Date.now() + let timeout = 10; + const spinLength = 4 * 1000; // 4 seconds + const startTime = Date.now(); const spinReels = () => { - updateReels() + updateReels(); - if ((Date.now() - startTime) >= spinLength) { - finishSpin() + if (Date.now() - startTime >= spinLength) { + finishSpin(); } else { // Slow down a bit on each spin - timeout *= 1.1 - setTimeout(spinReels, timeout) + timeout *= 1.1; + setTimeout(spinReels, timeout); } - } + }; - spinReels() - } -}) + spinReels(); + }, +}); const getRandomValue = () => { - const values = ['🍒', '💎', '7️⃣', '🍊', '🔔', '⭐', '🍇', '🍀'] - return values[Math.floor(Math.random() * values.length)] -} + const values = ["🍒", "💎", "7️⃣", "🍊", "🔔", "⭐", "🍇", "🍀"]; + return values[Math.floor(Math.random() * values.length)]; +}; const updateReels = () => { - reel1.label = getRandomValue() - reel2.label = getRandomValue() - reel3.label = getRandomValue() -} + reel1.label = getRandomValue(); + reel2.label = getRandomValue(); + reel3.label = getRandomValue(); +}; const finishSpin = () => { - const uniqueValues = new Set([reel1.label, reel2.label, reel3.label]).size + const uniqueValues = new Set([reel1.label, reel2.label, reel3.label]).size; if (uniqueValues === 1) { // All 3 values are the same - result.label = '💰 Jackpot!' - result.textColor = '#FDFF00' + result.label = "💰 Jackpot!"; + result.textColor = "#FDFF00"; } else if (uniqueValues === 2) { // 2 values are the same - result.label = '😍 Winner!' - result.textColor = '#FDFF00' + result.label = "😍 Winner!"; + result.textColor = "#FDFF00"; } else { // No values are the same - result.label = '🙁 Spin Again' - result.textColor = null + result.label = "🙁 Spin Again"; + result.textColor = null; } - spinning = false -} + spinning = false; +}; const touchBar = new TouchBar({ items: [ spin, - new TouchBarSpacer({ size: 'large' }), + new TouchBarSpacer({ size: "large" }), reel1, - new TouchBarSpacer({ size: 'small' }), + new TouchBarSpacer({ size: "small" }), reel2, - new TouchBarSpacer({ size: 'small' }), + new TouchBarSpacer({ size: "small" }), reel3, - new TouchBarSpacer({ size: 'large' }), - result - ] -}) + new TouchBarSpacer({ size: "large" }), + result, + ], +}); // let window @@ -104,4 +100,4 @@ const touchBar = new TouchBar({ // window.setTouchBar(touchBar) // }) -module.exports = touchBar \ No newline at end of file +module.exports = touchBar; diff --git a/src/electron/tray.js b/src/electron/tray.js index 13100c5..8ddd536 100644 --- a/src/electron/tray.js +++ b/src/electron/tray.js @@ -1,12 +1,12 @@ -const path = require('path') -const { Menu, Tray } = require('electron') +const path = require("path"); +const { Menu, Tray } = require("electron"); -let tray = null +let tray = null; -const macIcon = path.join(__static, "img/icons/menu.png") -const winIcon = path.join(__static, "img/icons/icon.ico") +const macIcon = path.join(__static, "img/icons/menu.png"); +const winIcon = path.join(__static, "img/icons/icon.ico"); -tray = new Tray(macIcon) +tray = new Tray(macIcon); // Temporary no need for menu. // const contextMenu = Menu.buildFromTemplate([ @@ -21,4 +21,4 @@ tray = new Tray(macIcon) // Call this again for Linux because we modified the context menu // tray.setContextMenu(contextMenu) -module.exports = tray \ No newline at end of file +module.exports = tray; diff --git a/src/locale/lang/en.js b/src/locale/lang/en.js index 659d2f2..e11fd2c 100644 --- a/src/locale/lang/en.js +++ b/src/locale/lang/en.js @@ -36,7 +36,7 @@ export default { artist: "Artist", videos: "Music Videos", following: "Following", - follow: "Follow" + follow: "Follow", }, album: { released: "Released", diff --git a/src/locale/lang/zh-CN.js b/src/locale/lang/zh-CN.js index 4839267..b3331a5 100644 --- a/src/locale/lang/zh-CN.js +++ b/src/locale/lang/zh-CN.js @@ -33,7 +33,7 @@ export default { artist: "歌手", videos: "个MV", following: "已关注", - follow: "关注" + follow: "关注", }, album: { released: "发行于", diff --git a/src/registerServiceWorker.js b/src/registerServiceWorker.js index b8a8c97..8c6d0d3 100644 --- a/src/registerServiceWorker.js +++ b/src/registerServiceWorker.js @@ -2,7 +2,10 @@ import { register } from "register-service-worker"; -if (process.env.NODE_ENV === "production" && process.env.IS_ELECTRON === 'undefined') { +if ( + process.env.NODE_ENV === "production" && + process.env.IS_ELECTRON === "undefined" +) { register(`${process.env.BASE_URL}service-worker.js`, { ready() { console.log( diff --git a/src/utils/request.js b/src/utils/request.js index e35bb11..e5fac75 100644 --- a/src/utils/request.js +++ b/src/utils/request.js @@ -1,11 +1,11 @@ import axios from "axios"; -let baseURL = '' +let baseURL = ""; // Web 和 Electron 跑在不同端口避免同时启动时冲突 if (process.env.IS_ELECTRON) { - baseURL = process.env.VUE_APP_ELECTRON_API_URL + baseURL = process.env.VUE_APP_ELECTRON_API_URL; } else { - baseURL = process.env.VUE_APP_NETEASE_API_URL + baseURL = process.env.VUE_APP_NETEASE_API_URL; } const service = axios.create({