chore: 支持背景音乐设置

This commit is contained in:
2025-12-15 17:44:10 +08:00
parent 4a829b7dfc
commit 5fad2f97f8
8 changed files with 890 additions and 707 deletions

View File

@@ -1,7 +1,10 @@
import { createRouter, createWebHistory } from 'vue-router';
import BattleRanking from '../views/BattleRanking.vue';
import AdminPanel from '../views/AdminPanel.vue';
import BattleRanking from '../views/BattleRanking.vue'; // 首页组件
import AdminPanel from '../views/AdminPanel.vue'; // 管理员面板组件
import { musicPlayer } from '../utils/musicPlayer'; // 音乐播放器实例
import { getMusicConfig } from '../services/configService'; // 音乐配置读取服务
// 路由配置
const routes = [
{
path: '/',
@@ -16,23 +19,56 @@ const routes = [
meta: { title: '管理员面板' }
},
{
// 捕获所有未匹配的路由,重定向到首页
// 404路由未匹配路径重定向到首页
path: '/:pathMatch(.*)*',
redirect: '/'
}
];
// 创建路由实例
const router = createRouter({
history: createWebHistory(),
routes
});
// 全局前置守卫,设置页面标题
router.beforeEach((to, from, next) => {
// 设置文档标题
// 路由守卫:页面切换时控制音乐状态
router.beforeEach(async (to, from, next) => {
// 1. 进入管理员页面:强制暂停+静音
if (to.path.startsWith('/admin')) {
musicPlayer.pause(); // 暂停音乐
musicPlayer.setMuted(true); // 强制静音(管理员页面始终无声音)
next();
return;
}
// 2. 进入首页:按配置播放/暂停
if (to.path === '/') {
try {
const musicConfig = await getMusicConfig(); // 读取音乐配置
if (musicConfig.enabled) {
// 初始化音乐路径+开关状态
musicPlayer.initMusicConfig(musicConfig.filePath, musicConfig.enabled);
musicPlayer.setMuted(false); // 首页取消静音
musicPlayer.play(); // 播放音乐
} else {
musicPlayer.pause(); // 开关关闭则暂停
}
} catch (error) {
console.error('首页音乐配置读取失败:', error);
musicPlayer.pause();
}
}
// 3. 进入其他页面:暂停音乐
if (to.path !== '/' && !to.path.startsWith('/admin')) {
musicPlayer.pause();
}
// 4. 设置页面标题(可选增强)
if (to.meta.title) {
document.title = to.meta.title;
}
next();
});