2026-05-13 01:49:19 +08:00
|
|
|
|
// API 基地址:Tauri 生产环境使用绝对路径,浏览器/开发环境使用相对路径
|
|
|
|
|
|
// 判断依据:Tauri v2 在 Windows 上使用 https://tauri.localhost(protocol=https:),
|
|
|
|
|
|
// 在 macOS 上使用 tauri://localhost(protocol=tauri:),因此用 hostname 而非 protocol 判断
|
|
|
|
|
|
const BASE = window.location.hostname === 'localhost' || window.location.hostname === '127.0.0.1'
|
|
|
|
|
|
? '' // 同源请求(开发模式 localhost:1420 或浏览器)
|
|
|
|
|
|
: 'http://localhost:10801'; // Tauri 生产环境 (tauri://localhost / https://tauri.localhost)
|
2026-05-13 01:19:41 +08:00
|
|
|
|
export const API_BASE = BASE;
|
2026-05-12 23:19:37 +08:00
|
|
|
|
|
|
|
|
|
|
async function request(url, options = {}) {
|
|
|
|
|
|
const res = await fetch(`${BASE}${url}`, {
|
|
|
|
|
|
headers: { 'Content-Type': 'application/json' },
|
|
|
|
|
|
...options,
|
|
|
|
|
|
});
|
|
|
|
|
|
return res.json();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function formEncode(data) {
|
|
|
|
|
|
return Object.entries(data)
|
|
|
|
|
|
.map(([k, v]) => `${encodeURIComponent(k)}=${encodeURIComponent(v)}`)
|
|
|
|
|
|
.join('&');
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// ============ Auth ============
|
|
|
|
|
|
export async function login(username, password) {
|
|
|
|
|
|
const res = await fetch(`${BASE}/api/login`, {
|
|
|
|
|
|
method: 'POST',
|
|
|
|
|
|
headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
|
|
|
|
|
|
body: formEncode({ username, password }),
|
|
|
|
|
|
});
|
|
|
|
|
|
return res.json();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// ============ Settings ============
|
|
|
|
|
|
export async function getSettings() {
|
|
|
|
|
|
const res = await fetch(`${BASE}/api/settings`);
|
|
|
|
|
|
return res.json();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
export async function updateSettings(data) {
|
|
|
|
|
|
return request('/api/settings', {
|
|
|
|
|
|
method: 'POST',
|
|
|
|
|
|
body: JSON.stringify(data),
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// ============ Media Files ============
|
|
|
|
|
|
export async function getMediaFiles() {
|
|
|
|
|
|
const res = await fetch(`${BASE}/media`);
|
|
|
|
|
|
return res.json();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
export async function uploadFiles(files) {
|
|
|
|
|
|
for (const file of files) {
|
|
|
|
|
|
const fd = new FormData();
|
|
|
|
|
|
fd.append('file', file);
|
|
|
|
|
|
await fetch(`${BASE}/upload`, { method: 'POST', body: fd });
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
export async function addUrlMedia(url) {
|
|
|
|
|
|
return request('/api/media/add-url', {
|
|
|
|
|
|
method: 'POST',
|
|
|
|
|
|
body: JSON.stringify({ url }),
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
export async function deleteMedia(path) {
|
|
|
|
|
|
return request('/api/delete', {
|
|
|
|
|
|
method: 'POST',
|
|
|
|
|
|
body: JSON.stringify({ path }),
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// ============ Playlist ============
|
|
|
|
|
|
export async function getPlaylist() {
|
|
|
|
|
|
const res = await fetch(`${BASE}/api/playlist`);
|
|
|
|
|
|
return res.json();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
export async function addToPlaylist(path) {
|
|
|
|
|
|
return request('/api/playlist/add', {
|
|
|
|
|
|
method: 'POST',
|
|
|
|
|
|
body: JSON.stringify({ path }),
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
export async function removeFromPlaylist(path) {
|
|
|
|
|
|
return request('/api/playlist/remove', {
|
|
|
|
|
|
method: 'POST',
|
|
|
|
|
|
body: JSON.stringify({ path }),
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// ============ Playback Control ============
|
|
|
|
|
|
export async function sendControl(action) {
|
|
|
|
|
|
return request('/api/control', {
|
|
|
|
|
|
method: 'POST',
|
|
|
|
|
|
body: JSON.stringify({ action }),
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
export async function getState() {
|
|
|
|
|
|
const res = await fetch(`${BASE}/api/state`);
|
|
|
|
|
|
return res.json();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
export async function updateState(state) {
|
|
|
|
|
|
return request('/api/state', {
|
|
|
|
|
|
method: 'POST',
|
|
|
|
|
|
body: JSON.stringify({ state }),
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|