From b84138c7a99a972c312ad0781ef28499adad6c3d Mon Sep 17 00:00:00 2001 From: "ethan.chen" Date: Tue, 20 May 2025 18:15:41 +0800 Subject: [PATCH] feat: enhance media management features with pagination, category selection, and improved UI elements --- src/App.svelte | 163 +++++++++++++++++++++++++++++++++++++++++++++++-- vite.config.ts | 6 ++ 2 files changed, 165 insertions(+), 4 deletions(-) diff --git a/src/App.svelte b/src/App.svelte index 1c84200..cd7cae7 100644 --- a/src/App.svelte +++ b/src/App.svelte @@ -4,12 +4,88 @@ import { writable } from 'svelte/store'; import request from './lib/request'; + // 类型定义 + interface Media { + id: number; + title: string; + type: string; + status: string; + rating?: number; + notes?: string; + platform?: string; + date?: string; + created_at: string; + updated_at: string; + } + + interface ApiResponse { + code: number; + data: T; + message: string; + } + + interface PageResponse { + list: Media[]; + total: number; + currentPage: number; + pageSize: number; + } + // 状态管理 const isAuthenticated = writable(false); const username = writable(''); const password = writable(''); const error = writable(''); + // 媒体列表状态 + const currentCategory = writable('game'); + const mediaList = writable([]); + const currentPage = writable(1); + const pageSize = writable(10); + const totalItems = writable(0); + const loading = writable(false); + + // 类别选项 + const categories = [ + { id: 'game', label: 'Games' }, + { id: 'book', label: 'Books' }, + { id: 'movie', label: 'Movies' }, + { id: 'anime', label: 'Anime' } + ]; + + // 获取媒体列表 + async function fetchMediaList() { + loading.set(true); + try { + const response = await request.get>('/media/page', { + params: { + type: $currentCategory, + currentPage: $currentPage, + pageSize: $pageSize + } + }); + if (response.data.code === 0) { + mediaList.set(response.data.data.list); + totalItems.set(response.data.data.total); + } + } catch (e: any) { + error.set(e.message || 'Failed to fetch media list'); + } finally { + loading.set(false); + } + } + + // 监听类别变化 + $: if ($isAuthenticated) { + currentPage.set(1); + fetchMediaList(); + } + + // 监听分页变化 + $: if ($isAuthenticated && $currentPage) { + fetchMediaList(); + } + // 登录处理 async function handleLogin() { try { @@ -17,15 +93,26 @@ const auth = btoa(`${$username}:${$password}`); localStorage.setItem('auth', auth); - const response = await request.get('/media/list'); + // 使用 page 接口验证登录 + const response = await request.get>('/media/page', { + params: { + type: 'game', + currentPage: 1, + pageSize: 10 + } + }); + console.log(response) if (response.code === 0) { isAuthenticated.set(true); error.set(''); + // 设置初始数据 + mediaList.set(response.data.list); + totalItems.set(response.data.total); } else { error.set(response.message || 'Invalid username or password'); localStorage.removeItem('auth'); } - } catch (e) { + } catch (e: any) { error.set(e.message || 'Connection error'); localStorage.removeItem('auth'); } @@ -94,8 +181,76 @@ {:else}
- -

Welcome, {$username}!

+
+ +
+
+
+ {#each categories as category} + + {/each} +
+
+
+ + +
+
+ {#if $loading} +
+
+
+ {:else} +
+ {#each $mediaList as media} +
+
+
+

{media.title}

+

Status: {media.status}

+ {#if media.rating} +

Rating: {media.rating}/10

+ {/if} +
+
+ {media.date ? new Date(media.date).toLocaleDateString() : 'No date'} +
+
+
+ {/each} +
+ + +
+
+ Showing {($currentPage - 1) * $pageSize + 1} to {Math.min($currentPage * $pageSize, $totalItems)} of {$totalItems} items +
+
+ + +
+
+ {/if} +
+
+
{/if} diff --git a/vite.config.ts b/vite.config.ts index 2a4a90b..6eff130 100644 --- a/vite.config.ts +++ b/vite.config.ts @@ -1,3 +1,9 @@ +/* + * @Date: 2025-05-20 17:59:45 + * @LastEditors: 陈子健 + * @LastEditTime: 2025-05-20 18:04:34 + * @FilePath: /my-score/frontend/vite.config.ts + */ import { defineConfig } from 'vite' import { svelte } from '@sveltejs/vite-plugin-svelte'