重构 更规范性的文件组织
This commit is contained in:
1 parent
59abbf30d9
commit
ea6c87ad25
53 files changed
+3274
-2110
No files matched your search
@@ -0,0 +1,103 @@
|
||||
/**
|
||||
* HTTP 客户端
|
||||
* 使用原生 Fetch API
|
||||
*/
|
||||
|
||||
const API_BASE = '/api';
|
||||
|
||||
/**
|
||||
* API 错误类
|
||||
*/
|
||||
class APIError extends Error {
|
||||
constructor(message, status, data) {
|
||||
super(message);
|
||||
this.name = 'APIError';
|
||||
this.status = status;
|
||||
this.data = data;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 发送请求
|
||||
*/
|
||||
async function request(url, options = {}) {
|
||||
const config = {
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
...options.headers
|
||||
},
|
||||
...options
|
||||
};
|
||||
|
||||
try {
|
||||
const response = await fetch(`${API_BASE}${url}`, config);
|
||||
const data = await response.json();
|
||||
|
||||
if (!response.ok) {
|
||||
throw new APIError(
|
||||
data.error?.message || data.error || 'Request failed',
|
||||
response.status,
|
||||
data
|
||||
);
|
||||
}
|
||||
|
||||
// 直接返回数据,不包装
|
||||
return data.data !== undefined ? data.data : data;
|
||||
} catch (error) {
|
||||
if (error instanceof APIError) {
|
||||
throw error;
|
||||
}
|
||||
|
||||
// 网络错误或解析错误
|
||||
throw new APIError(
|
||||
error.message || 'Network error',
|
||||
0,
|
||||
null
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* GET 请求
|
||||
*/
|
||||
function get(url, options = {}) {
|
||||
return request(url, { method: 'GET', ...options });
|
||||
}
|
||||
|
||||
/**
|
||||
* POST 请求
|
||||
*/
|
||||
function post(url, body, options = {}) {
|
||||
return request(url, {
|
||||
method: 'POST',
|
||||
body: JSON.stringify(body),
|
||||
...options
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* PUT 请求
|
||||
*/
|
||||
function put(url, body, options = {}) {
|
||||
return request(url, {
|
||||
method: 'PUT',
|
||||
body: JSON.stringify(body),
|
||||
...options
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* DELETE 请求
|
||||
*/
|
||||
function del(url, options = {}) {
|
||||
return request(url, { method: 'DELETE', ...options });
|
||||
}
|
||||
|
||||
export const api = {
|
||||
get,
|
||||
post,
|
||||
put,
|
||||
delete: del
|
||||
};
|
||||
|
||||
export { APIError };
|
||||
@@ -0,0 +1,67 @@
|
||||
/**
|
||||
* 图片 API
|
||||
*/
|
||||
|
||||
import { api } from '../client';
|
||||
|
||||
/**
|
||||
* 搜索图片
|
||||
*/
|
||||
export async function search(libraryId, params = {}, options = {}) {
|
||||
const query = new URLSearchParams({
|
||||
libraryId,
|
||||
...params
|
||||
});
|
||||
|
||||
return api.get(`/image?${query}`, options);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取图片总数
|
||||
*/
|
||||
export async function getCount(libraryId) {
|
||||
return api.get(`/image/count?libraryId=${libraryId}`);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取统计信息
|
||||
*/
|
||||
export async function getStats(libraryId) {
|
||||
return api.get(`/image/stats?libraryId=${libraryId}`);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取文件夹列表
|
||||
*/
|
||||
export async function getFolders(libraryId) {
|
||||
return api.get(`/image/folders?libraryId=${libraryId}`);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取缓存元数据
|
||||
*/
|
||||
export async function getCacheMeta(libraryId) {
|
||||
return api.get(`/image/cache-meta?libraryId=${libraryId}`);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取缩略图 URL
|
||||
* 使用分片结构,不再需要 size 参数
|
||||
*/
|
||||
export function getThumbnailUrl(libraryId, filename) {
|
||||
return `/api/image/thumbnail/${libraryId}/${filename}`;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取原图 URL
|
||||
*/
|
||||
export function getOriginalUrl(libraryId, path) {
|
||||
return `/api/image/original/${libraryId}/${path}`;
|
||||
}
|
||||
|
||||
/**
|
||||
* 在文件管理器中打开
|
||||
*/
|
||||
export async function openInExplorer(libraryId, path) {
|
||||
return api.post(`/image/${libraryId}/open-file`, { path });
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
/**
|
||||
* 素材库 API
|
||||
*/
|
||||
|
||||
import { api } from '../client';
|
||||
|
||||
/**
|
||||
* 获取所有素材库
|
||||
*/
|
||||
export async function getAll() {
|
||||
return api.get('/library');
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加素材库
|
||||
*/
|
||||
export async function add(name, path) {
|
||||
return api.post('/library', { name, path });
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新素材库
|
||||
*/
|
||||
export async function update(id, updates) {
|
||||
return api.put(`/library/${id}`, updates);
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除素材库
|
||||
*/
|
||||
export async function remove(id) {
|
||||
return api.delete(`/library/${id}`);
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除素材库(别名)
|
||||
*/
|
||||
export const deleteLibrary = remove;
|
||||
|
||||
/**
|
||||
* 设置当前素材库
|
||||
*/
|
||||
export async function setCurrent(id) {
|
||||
return api.post(`/library/${id}/set-current`);
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新偏好设置
|
||||
*/
|
||||
export async function updatePreferences(preferences) {
|
||||
return api.put('/library/preferences', preferences);
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新主题
|
||||
*/
|
||||
export async function updateTheme(theme) {
|
||||
return api.put('/library/theme', { theme });
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
/**
|
||||
* 扫描 API
|
||||
*/
|
||||
|
||||
import { api } from '../client';
|
||||
|
||||
/**
|
||||
* 全量扫描
|
||||
*/
|
||||
export async function fullScan(libraryId, wait = false) {
|
||||
return api.post('/scan/full', { libraryId, wait });
|
||||
}
|
||||
|
||||
/**
|
||||
* 增量同步
|
||||
*/
|
||||
export async function sync(libraryId, wait = false) {
|
||||
return api.post('/scan/sync', { libraryId, wait });
|
||||
}
|
||||
|
||||
/**
|
||||
* 停止扫描
|
||||
*/
|
||||
export async function stop(libraryId) {
|
||||
return api.post('/scan/stop', { libraryId });
|
||||
}
|
||||
|
||||
/**
|
||||
* 继续扫描
|
||||
*/
|
||||
export async function resume(libraryId) {
|
||||
return api.post('/scan/resume', { libraryId });
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取扫描状态
|
||||
*/
|
||||
export async function getStatus(libraryId) {
|
||||
return api.get(`/scan/status/${libraryId}`);
|
||||
}
|
||||
|
||||
/**
|
||||
* 修复文件夹路径
|
||||
*/
|
||||
export async function fixFolders(libraryId) {
|
||||
return api.post('/scan/fix-folders', { libraryId });
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
/**
|
||||
* API 统一导出
|
||||
*/
|
||||
|
||||
import * as library from './endpoints/library';
|
||||
import * as image from './endpoints/image';
|
||||
import * as scan from './endpoints/scan';
|
||||
|
||||
export const libraryAPI = library;
|
||||
export const imageAPI = image;
|
||||
export const scanAPI = scan;
|
||||
|
||||
export { api, APIError } from './client';
|
||||
|
||||
// watchAPI 空实现(已废弃)
|
||||
export const watchAPI = {
|
||||
start: () => Promise.resolve({}),
|
||||
stop: () => Promise.resolve({}),
|
||||
status: () => Promise.resolve({ watching: false }),
|
||||
list: () => Promise.resolve({ watchers: [] })
|
||||
};
|
||||
Reference in new issue
Block a user