feat: 优化桌面歌词功能 添加歌词进度 优化歌词页面样式

This commit is contained in:
alger
2024-12-06 23:50:44 +08:00
parent 8870390770
commit edf5c77ea0
10 changed files with 771 additions and 217 deletions
+11
View File
@@ -0,0 +1,11 @@
module.exports = {
// 开发环境配置
development: {
mainPort: 4488,
lyricPort: 4488,
},
// 生产环境配置
production: {
distPath: '../dist',
},
};
+8 -4
View File
@@ -1,5 +1,6 @@
const { BrowserWindow } = require('electron');
const path = require('path');
const config = require('./config');
let lyricWindow = null;
@@ -10,13 +11,16 @@ const createWin = () => {
frame: false,
show: false,
transparent: true,
hasShadow: false,
webPreferences: {
nodeIntegration: true,
nodeIntegration: false,
contextIsolation: true,
preload: `${__dirname}/preload.js`,
contextIsolation: false,
webSecurity: false,
},
});
};
const loadLyricWindow = (ipcMain) => {
ipcMain.on('open-lyric', () => {
if (lyricWindow) {
@@ -28,9 +32,9 @@ const loadLyricWindow = (ipcMain) => {
createWin();
if (process.env.NODE_ENV === 'development') {
lyricWindow.webContents.openDevTools({ mode: 'detach' });
lyricWindow.loadURL('http://localhost:4678/#/lyric');
lyricWindow.loadURL(`http://localhost:${config.development.lyricPort}/#/lyric`);
} else {
const distPath = path.resolve(__dirname, '../dist');
const distPath = path.resolve(__dirname, config.production.distPath);
lyricWindow.loadURL(`file://${distPath}/index.html#/lyric`);
}
+14 -12
View File
@@ -1,5 +1,6 @@
const { contextBridge, ipcRenderer, app } = require('electron');
const { contextBridge, ipcRenderer } = require('electron');
// 主进程通信
contextBridge.exposeInMainWorld('electronAPI', {
minimize: () => ipcRenderer.send('minimize-window'),
maximize: () => ipcRenderer.send('maximize-window'),
@@ -11,18 +12,19 @@ contextBridge.exposeInMainWorld('electronAPI', {
sendLyric: (data) => ipcRenderer.send('send-lyric', data),
});
const electronHandler = {
// 存储相关
contextBridge.exposeInMainWorld('electron', {
ipcRenderer: {
setStoreValue: (key, value) => {
ipcRenderer.send('setStore', key, value);
setStoreValue: (key, value) => ipcRenderer.send('setStore', key, value),
getStoreValue: (key) => ipcRenderer.sendSync('getStore', key),
on: (channel, func) => {
ipcRenderer.on(channel, (event, ...args) => func(...args));
},
getStoreValue(key) {
const resp = ipcRenderer.sendSync('getStore', key);
return resp;
once: (channel, func) => {
ipcRenderer.once(channel, (event, ...args) => func(...args));
},
send: (channel, data) => {
ipcRenderer.send(channel, data);
},
},
app,
};
contextBridge.exposeInMainWorld('electron', electronHandler);
});
+69
View File
@@ -0,0 +1,69 @@
const { app, BrowserWindow } = require('electron');
const axios = require('axios');
const fs = require('fs');
const path = require('path');
const AdmZip = require('adm-zip');
class Updater {
constructor(mainWindow) {
this.mainWindow = mainWindow;
this.updateUrl = 'http://your-server.com/update'; // 更新服务器地址
this.version = app.getVersion();
}
// 检查更新
async checkForUpdates() {
try {
const response = await axios.get(`${this.updateUrl}/check`, {
params: {
version: this.version,
},
});
if (response.data.hasUpdate) {
await this.downloadUpdate(response.data.downloadUrl);
}
} catch (error) {
console.error('检查更新失败:', error);
}
}
// 下载更新
async downloadUpdate(downloadUrl) {
try {
const response = await axios({
url: downloadUrl,
method: 'GET',
responseType: 'arraybuffer',
});
const tempPath = path.join(app.getPath('temp'), 'update.zip');
fs.writeFileSync(tempPath, response.data);
await this.extractUpdate(tempPath);
} catch (error) {
console.error('下载更新失败:', error);
}
}
// 解压更新
async extractUpdate(zipPath) {
try {
const zip = new AdmZip(zipPath);
const targetPath = path.join(__dirname, '../dist'); // 前端文件目录
// 解压文件
zip.extractAllTo(targetPath, true);
// 删除临时文件
fs.unlinkSync(zipPath);
// 刷新页面
this.mainWindow.webContents.reload();
} catch (error) {
console.error('解压更新失败:', error);
}
}
}
module.exports = Updater;