From b2c3ecc5bab6e5b37b38ff8b6cab5a026362c213 Mon Sep 17 00:00:00 2001 From: Chen Date: Thu, 4 Dec 2025 20:23:18 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=20=E6=96=B0=E5=90=AF?= =?UTF-8?q?=E5=8A=A8=E9=A6=96=E6=AC=A1=E5=8A=A0=E8=BD=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/src/routes/library.js | 48 +++++++++++++-------------- backend/utils/config.js | 9 ++---- frontend/src/App.jsx | 61 +++++++++++++++++++++++++++++++++-- 3 files changed, 85 insertions(+), 33 deletions(-) diff --git a/backend/src/routes/library.js b/backend/src/routes/library.js index 11d6007..6919901 100644 --- a/backend/src/routes/library.js +++ b/backend/src/routes/library.js @@ -41,6 +41,30 @@ router.post('/', }) ); +/** + * 更新偏好设置(必须在 /:id 之前,否则会被 /:id 匹配) + * PUT /api/library/preferences + * Body: { preferences } + */ +router.put('/preferences', asyncHandler(async (req, res) => { + const result = libraryService.updatePreferences(req.body); + res.json({ success: true, data: result }); +})); + +/** + * 更新主题(必须在 /:id 之前) + * PUT /api/library/theme + * Body: { theme } + */ +router.put('/theme', + validateRequired(['theme']), + asyncHandler(async (req, res) => { + const { theme } = req.body; + const result = libraryService.updateTheme(theme); + res.json({ success: true, data: result }); + }) +); + /** * 更新素材库 * PUT /api/library/:id @@ -72,30 +96,6 @@ router.post('/:id/set-current', asyncHandler(async (req, res) => { res.json({ success: true, data: result }); })); -/** - * 更新偏好设置 - * PUT /api/library/preferences - * Body: { preferences } - */ -router.put('/preferences', asyncHandler(async (req, res) => { - const result = libraryService.updatePreferences(req.body); - res.json({ success: true, data: result }); -})); - -/** - * 更新主题 - * PUT /api/library/theme - * Body: { theme } - */ -router.put('/theme', - validateRequired(['theme']), - asyncHandler(async (req, res) => { - const { theme } = req.body; - const result = libraryService.updateTheme(theme); - res.json({ success: true, data: result }); - }) -); - /** * 获取素材库统计 * GET /api/library/:id/stats diff --git a/backend/utils/config.js b/backend/utils/config.js index b573adc..e4fa34c 100644 --- a/backend/utils/config.js +++ b/backend/utils/config.js @@ -13,21 +13,16 @@ const CONFIG_CACHE_TTL = 5000; // 5秒缓存 function getConfigDir() { // Check if running on fnOS if (process.env.TRIM_PKGVAR) { - console.log('📁 配置目录 (fnOS): ' + process.env.TRIM_PKGVAR); return process.env.TRIM_PKGVAR; } // Windows if (process.platform === 'win32') { - const dir = path.join(process.env.APPDATA || os.homedir(), 'FlyPic'); - console.log('📁 配置目录 (Windows): ' + dir); - return dir; + return path.join(process.env.APPDATA || os.homedir(), 'FlyPic'); } // Linux/Mac - const dir = path.join(os.homedir(), '.flypic'); - console.log('📁 配置目录 (Linux/Mac): ' + dir); - return dir; + return path.join(os.homedir(), '.flypic'); } /** diff --git a/frontend/src/App.jsx b/frontend/src/App.jsx index 4bfc43b..4bb2b04 100644 --- a/frontend/src/App.jsx +++ b/frontend/src/App.jsx @@ -21,6 +21,8 @@ function App() { const [isDraggingLeft, setIsDraggingLeft] = useState(false); const [isDraggingRight, setIsDraggingRight] = useState(false); const [isMobile, setIsMobile] = useState(false); + const [isConnecting, setIsConnecting] = useState(true); // 启动加载状态 + const [connectionError, setConnectionError] = useState(null); const containerRef = useRef(null); useEffect(() => { @@ -174,8 +176,12 @@ function App() { } }; - const loadLibraries = async () => { + const loadLibraries = async (retryCount = 0) => { + const maxRetries = 5; + const retryDelay = 1000; // 1秒 + try { + setConnectionError(null); const response = await libraryAPI.getAll(); const data = response.data || response; @@ -208,8 +214,21 @@ function App() { }).catch(() => { }); } + // 连接成功 + setIsConnecting(false); + } catch (error) { - console.error('❌ 加载素材库失败:', error.message); + console.error(`❌ 加载素材库失败 (${retryCount + 1}/${maxRetries}):`, error.message); + + if (retryCount < maxRetries - 1) { + // 重试 + setConnectionError(`连接服务器中... (${retryCount + 1}/${maxRetries})`); + setTimeout(() => loadLibraries(retryCount + 1), retryDelay); + } else { + // 重试次数用完 + setIsConnecting(false); + setConnectionError('无法连接到服务器,请检查后端是否启动'); + } } }; @@ -340,6 +359,44 @@ function App() { }; }, [isDraggingLeft, isDraggingRight]); + // 启动加载界面(移动端和桌面端共用) + if (isConnecting) { + return ( +
+
+
+
+ {connectionError || '正在连接服务器...'} +
+
+
+ ); + } + + // 连接失败界面 + if (connectionError) { + return ( +
+
+
⚠️
+
+ {connectionError} +
+ +
+
+ ); + } + // 移动端布局 if (isMobile) { return (