From 69c9e42a3ce1d6f78edd630e52d6d45aa30671f7 Mon Sep 17 00:00:00 2001
From: Azalea <22280294+hykilpikonna@users.noreply.github.com>
Date: Wed, 19 Nov 2025 23:07:36 +0800
Subject: [PATCH] [+] Fully implement netease import
---
src/components/status/ErrorDialog.svelte | 18 ++++++
src/lib/client.ts | 8 ++-
src/routes/import/netease/+page.svelte | 71 +++++++++++-------------
3 files changed, 56 insertions(+), 41 deletions(-)
create mode 100644 src/components/status/ErrorDialog.svelte
diff --git a/src/components/status/ErrorDialog.svelte b/src/components/status/ErrorDialog.svelte
new file mode 100644
index 0000000..122ccc5
--- /dev/null
+++ b/src/components/status/ErrorDialog.svelte
@@ -0,0 +1,18 @@
+
+
+
diff --git a/src/lib/client.ts b/src/lib/client.ts
index 2f0a2d5..2dd934b 100644
--- a/src/lib/client.ts
+++ b/src/lib/client.ts
@@ -8,7 +8,13 @@ export async function post(endpoint: string, data: any) {
headers: {
'Content-Type': 'application/json'
}
- }).then(res => res.json())
+ }).then(async res => {
+ if (res.status >= 400) throw new Error(`错误 ${res.status}:${await res.text()}`)
+ return res.json()
+ }).catch(e => {
+ console.error(e)
+ throw e
+ })
}
export const API = {
diff --git a/src/routes/import/netease/+page.svelte b/src/routes/import/netease/+page.svelte
index 6ac70a5..fcb916b 100644
--- a/src/routes/import/netease/+page.svelte
+++ b/src/routes/import/netease/+page.svelte
@@ -4,6 +4,7 @@
import Button from "../../../components/Button.svelte"
import type { NeteaseSongBrief } from "../../../shared/types"
import { API } from "../../../lib/client"
+ import ErrorDialog from "../../../components/status/ErrorDialog.svelte";
let link = $state('')
@@ -18,6 +19,8 @@
total: songs.length,
done: songs.filter(song => song.status !== 'importing').length
})
+ let error = $state('')
+ let id = $state('')
function statusToIcon(stat: string): string {
switch (stat) {
@@ -34,52 +37,34 @@
status = 'importing'
songs = []
- try {
- const data = await API.netease.startImport(link)
-
- if (data.error) {
- alert(data.error)
- status = 'error'
- return
- }
-
- const importId = data.id
- songs = data.songs
-
- pollProgress(importId)
- } catch (e) {
- console.error(e)
- status = 'error'
- alert('Failed to start import')
- }
- }
-
- async function pollProgress(id: string) {
- const interval = setInterval(async () => {
- try {
- const data = await API.netease.checkProgress(id)
-
- if (data.error) {
- clearInterval(interval)
- return
- }
-
+ API.netease.startImport(link)
+ .catch(e => error = e.message)
+ .then(data => {
+ id = data.playlistId
songs = data.songs
- if (data.done) {
- clearInterval(interval)
- status = 'success'
- }
- } catch (e) {
- console.error(e)
- clearInterval(interval)
- }
- }, 1000)
+ const interval = setInterval(() => {
+ API.netease.checkProgress(data.id)
+ .catch(e => {
+ error = e.message
+ clearInterval(interval)
+ })
+ .then(data => {
+ songs = data.songs
+ if (data.done) {
+ clearInterval(interval)
+ status = 'success'
+ }
+ })
+ }, 1000)
+ })
}