优化 新启动首次加载

This commit is contained in:
Chen committed 2025-12-04 20:23:18 +08:00
1 parent c215de731e
commit b2c3ecc5ba
3 files changed
+85 -33

No files matched your search

+24 -24
View File
@@ -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
+2 -7
View File
@@ -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');
}
/**
+59 -2
View File
@@ -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 (
<div className="flex flex-col items-center justify-center h-screen bg-gray-50 dark:bg-gray-900">
<div className="flex flex-col items-center gap-4">
<div className="w-12 h-12 border-4 border-blue-500 border-t-transparent rounded-full animate-spin"></div>
<div className="text-lg font-medium text-gray-700 dark:text-gray-300">
{connectionError || '正在连接服务器...'}
</div>
</div>
</div>
);
}
// 连接失败界面
if (connectionError) {
return (
<div className="flex flex-col items-center justify-center h-screen bg-gray-50 dark:bg-gray-900">
<div className="flex flex-col items-center gap-4 text-center px-4">
<div className="text-red-500 text-5xl">⚠️</div>
<div className="text-lg font-medium text-gray-700 dark:text-gray-300">
{connectionError}
</div>
<button
onClick={() => {
setIsConnecting(true);
setConnectionError(null);
loadLibraries(0);
}}
className="px-4 py-2 bg-blue-500 text-white rounded-lg hover:bg-blue-600 transition-colors"
>
重新连接
</button>
</div>
</div>
);
}
// 移动端布局
if (isMobile) {
return (