refactor: 创建统一 API 封装层,前端不再直接调用 invoke

- 新增 src/api.ts,按职责分为 MusicApi/AudioApi/DeviceApi/DownloadApi/AppApi 五个命名空间
- 替换 15 个文件中所有 invoke 调用为 API 层方法
- 后端接口变更只需修改 api.ts 一处,便于后期迭代维护
This commit is contained in:
2026-05-29 22:02:42 +08:00
parent 68f29c8ea8
commit e40f82cc51
19 changed files with 302 additions and 116 deletions

View File

@ -76,7 +76,7 @@
<script setup lang="ts">
import { ref, computed, onMounted, onActivated, onBeforeUnmount, watch } from 'vue';
import { invoke } from '@tauri-apps/api/core';
import { MusicApi, DownloadApi } from '../api';
import { usePlayerStore } from '../stores/player';
import { useDownload } from '../composables/useDownload';
import { useSettingsStore } from '../stores/settings';
@ -129,7 +129,7 @@ async function refresh() {
loading.value = true;
pageCacheInvalidate('localMusic');
try {
const list = await invoke<LocalSong[]>('list_local_songs', { downloadPath: settings.downloadPath || null });
const list = await DownloadApi.listLocalSongs(settings.downloadPath || null);
songs.value = list;
pageCacheSet('localMusic', list);
fetchMissingCovers();
@ -145,7 +145,7 @@ async function fetchMissingCovers() {
if (missing.length === 0) return;
const ids = [...new Set(missing.map(s => s.id))];
try {
const jsonStr: string = await invoke('get_song_detail', { id: JSON.stringify(ids) });
const jsonStr: string = await MusicApi.getSongDetail(JSON.stringify(ids));
const data = JSON.parse(jsonStr);
const detailMap = new Map<number, string>();
for (const s of data.songs || []) {
@ -194,7 +194,7 @@ function confirmDelete(song: LocalSong) {
async function doDelete() {
if (!deleteTarget.value) return;
try {
await invoke('delete_local_song', { query: { id: deleteTarget.value.id, filename: deleteTarget.value.filename, downloadPath: settings.downloadPath || null } });
await DownloadApi.deleteLocalSong({ id: deleteTarget.value.id, filename: deleteTarget.value.filename, downloadPath: settings.downloadPath || null });
songs.value = songs.value.filter(s => s.id !== deleteTarget.value!.id);
download.localSongIds.delete(deleteTarget.value.id);
showToast('已删除', 'success');