Files
vs100/src/router/index.js

75 lines
2.1 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { createRouter, createWebHistory } from 'vue-router';
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: '/',
name: 'BattleRanking',
component: BattleRanking,
meta: { title: '百日大战排行榜' }
},
{
path: '/admin',
name: 'AdminPanel',
component: AdminPanel,
meta: { title: '管理员面板' }
},
{
// 404路由未匹配路径重定向到首页
path: '/:pathMatch(.*)*',
redirect: '/'
}
];
// 创建路由实例
const router = createRouter({
history: createWebHistory(),
routes
});
// 路由守卫:页面切换时控制音乐状态
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();
});
export default router;