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
+6 -6
View File
@@ -38,7 +38,6 @@
<script setup lang="ts">
import { ref, watch, onMounted, onBeforeUnmount } from 'vue';
import { invoke } from '@tauri-apps/api/core';
import { useUserStore } from './stores/user';
import { useSettingsStore, type CloseAction } from './stores/settings';
import { usePlayerStore } from './stores/player';
@@ -55,6 +54,7 @@ import { useUpdater } from './composables/useUpdater';
import { getCurrentWindow } from '@tauri-apps/api/window';
import { listen } from '@tauri-apps/api/event';
import { register, unregister } from '@tauri-apps/plugin-global-shortcut';
import { MusicApi, AudioApi, DeviceApi, AppApi } from './api';
const userStore = useUserStore();
const player = usePlayerStore();
@@ -87,9 +87,9 @@ onMounted(async () => {
if (userStore.isLoggedIn) {
player.loadLikedIds();
}
try { await invoke('stop_audio'); } catch { /* 忽略 */ }
try { await AudioApi.stopAudio(); } catch { /* 忽略 */ }
try {
const jsonStr: string = await invoke('get_login_status');
const jsonStr: string = await MusicApi.getLoginStatus();
const data = JSON.parse(jsonStr);
if (data.account || data.profile) {
const profile = data.profile || data.account;
@@ -105,7 +105,7 @@ onMounted(async () => {
if (settings.outputDevice) {
try {
await invoke('set_output_device', { device: settings.outputDevice });
await DeviceApi.setOutputDevice(settings.outputDevice);
} catch { /* 忽略 */ }
}
});
@@ -118,7 +118,7 @@ function closeWindow() {
} else if (settings.closeAction === 'minimize') {
currentWindow.hide();
} else {
invoke('exit_app');
AppApi.exitApp();
}
}
@@ -130,7 +130,7 @@ function handleCloseAction(action: CloseAction, remember: boolean) {
if (action === 'minimize') {
currentWindow.hide();
} else {
invoke('exit_app');
AppApi.exitApp();
}
}