81 lines
2.9 KiB
TypeScript
81 lines
2.9 KiB
TypeScript
import { post } from '@/lib/request'
|
|
import { USE_MOCK, delay, paginate, mockId } from '@/mock'
|
|
import type { SystemOss, StorageConfig } from '../system'
|
|
import type { PageResult } from '@/lib/request'
|
|
|
|
const BASE_URL = '/file'
|
|
|
|
const mockFiles: SystemOss[] = [
|
|
{ id: '1', name: 'avatar.jpg', key: 'avatars/1.jpg', url: 'https://images.unsplash.com/photo-1535713875002-d1d0cf377fde?w=800&q=80', suffix: 'jpg', createdAt: new Date().toISOString(), updatedAt: new Date().toISOString() },
|
|
{ id: '2', name: 'bg.png', key: 'bg/1.png', url: 'https://images.unsplash.com/photo-1451187580459-43490279c0fa?w=800&q=80', suffix: 'png', createdAt: new Date().toISOString(), updatedAt: new Date().toISOString() },
|
|
]
|
|
|
|
export async function getFileList(params: { current: number; pageSize: number; keyword?: string }) {
|
|
if (USE_MOCK) {
|
|
await delay()
|
|
let list = [...mockFiles]
|
|
if (params.keyword) list = list.filter(f => f.name.includes(params.keyword!))
|
|
return paginate(list, params.current, params.pageSize)
|
|
}
|
|
return post<PageResult<SystemOss>>(`${BASE_URL}/list`, params)
|
|
}
|
|
|
|
export async function uploadFile(_file: File) {
|
|
if (USE_MOCK) {
|
|
await delay(1000)
|
|
const newFile: SystemOss = {
|
|
id: mockId(), name: _file.name, key: `upload/${_file.name}`,
|
|
url: URL.createObjectURL(_file), suffix: _file.name.split('.').pop() || '',
|
|
createdAt: new Date().toISOString(), updatedAt: new Date().toISOString()
|
|
}
|
|
mockFiles.unshift(newFile)
|
|
return { file: newFile }
|
|
}
|
|
const formData = new FormData()
|
|
formData.append('file', _file)
|
|
return post<{ file: SystemOss }>(`${BASE_URL}/upload`, formData)
|
|
}
|
|
|
|
export async function deleteFile(ids: string[]) {
|
|
if (USE_MOCK) {
|
|
await delay()
|
|
const idSet = new Set(ids)
|
|
for (let i = mockFiles.length - 1; i >= 0; i--) {
|
|
if (idSet.has(mockFiles[i].id)) mockFiles.splice(i, 1)
|
|
}
|
|
return null
|
|
}
|
|
return post<null>(`${BASE_URL}/delete`, { ids })
|
|
}
|
|
|
|
// ==================== Storage Config APIs ====================
|
|
const CONFIG_URL = '/file/config'
|
|
|
|
export async function getStorageConfigList(params: { current: number; pageSize: number; type?: string; name?: string }) {
|
|
if (USE_MOCK) {
|
|
await delay()
|
|
return paginate([], params.current, params.pageSize)
|
|
}
|
|
return post<PageResult<StorageConfig>>(`${CONFIG_URL}/list`, params)
|
|
}
|
|
|
|
export async function createStorageConfig(data: Partial<StorageConfig>) {
|
|
if (USE_MOCK) return delay()
|
|
return post<null>(`${CONFIG_URL}/create`, data)
|
|
}
|
|
|
|
export async function updateStorageConfig(data: Partial<StorageConfig>) {
|
|
if (USE_MOCK) return delay()
|
|
return post<null>(`${CONFIG_URL}/update`, data)
|
|
}
|
|
|
|
export async function deleteStorageConfig(ids: string[]) {
|
|
if (USE_MOCK) return delay()
|
|
return post<null>(`${CONFIG_URL}/delete`, { ids })
|
|
}
|
|
|
|
export async function setDefaultStorageConfig(id: string) {
|
|
if (USE_MOCK) return delay()
|
|
return post<null>(`${CONFIG_URL}/setDefault`, { id })
|
|
}
|