// Midway后端API // 使用相对路径,通过Vite代理转发到后端 const BASE_URL = '' async function request(path, options = {}) { // 使用相对路径,Vite会通过代理转发到后端 const url = path.startsWith('/') ? path : `/${path}` const { method = 'GET', body, params = {} } = options // 构建URL,添加查询参数 let requestUrl = url if (method === 'GET' && params && Object.keys(params).length > 0) { const searchParams = new URLSearchParams() Object.entries(params).forEach(([k, v]) => { if (v !== undefined && v !== null && v !== '') { searchParams.append(k, v) } }) const queryString = searchParams.toString() if (queryString) { requestUrl += `?${queryString}` } } const config = { method, headers: { 'Content-Type': 'application/json', }, } if (body && method !== 'GET') { config.body = JSON.stringify(body) } const res = await fetch(requestUrl, config) if (!res.ok) throw new Error(`HTTP ${res.status}`) const data = await res.json() if (!data.success) { throw new Error(data.message || '请求失败') } return data.data } /** * 搜索歌曲 */ export async function searchSongs(keywords, limit = 20) { return await request('/api/music/search', { method: 'GET', params: { keywords, limit } }) } /** * 获取歌曲详情 */ export async function getSongDetail(id) { return await request(`/api/music/song/${id}/detail`, { method: 'GET' }) } /** * 获取歌曲播放URL */ export async function getSongUrlV1(id, level = 'standard') { const data = await request(`/api/music/song/${id}/url`, { method: 'GET', params: { level } }) return data.url || '' } /** * 获取播放队列 */ export async function getQueue() { return await request('/api/music/queue', { method: 'GET' }) } /** * 添加到播放队列 */ export async function addToQueue(song) { const result = await request('/api/music/queue/add', { method: 'POST', body: { song } }) return result } /** * 播放下一首 */ export async function playNext() { const result = await request('/api/music/queue/play-next', { method: 'POST' }) return result } /** * 清空队列 */ export async function clearQueue() { return await request('/api/music/queue/clear', { method: 'POST' }) } /** * 从队列移除歌曲 */ export async function removeFromQueue(songId) { return await request('/api/music/queue/remove', { method: 'POST', body: { songId } }) } /** * 获取歌词 */ export async function getLyric(id) { const data = await request(`/api/music/song/${id}/lyric`, { method: 'GET' }) return data || { raw: '', tlyric: '' } } /** * 设置当前播放歌曲 */ export async function setPlaybackSong(songId) { return await request('/api/music/playback/set-song', { method: 'POST', body: { songId } }) } /** * 播放控制 - 播放 */ export async function play() { return await request('/api/music/playback/play', { method: 'POST' }) } /** * 播放控制 - 暂停 */ export async function pause() { return await request('/api/music/playback/pause', { method: 'POST' }) } /** * 播放控制 - 跳转时间 */ export async function seek(time) { return await request('/api/music/playback/seek', { method: 'POST', body: { time } }) } /** * 获取当前播放状态 */ export async function getPlaybackState() { return await request('/api/music/playback/state', { method: 'GET' }) } export function getBaseUrl() { // 返回空字符串,使用相对路径通过代理 return '' }