You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
171 lines
4.2 KiB
171 lines
4.2 KiB
import request from '@/utils/request';
|
|
import type { ApiResult, PageResult } from '@/api';
|
|
import type { FileRecord, FileRecordParam } from './model';
|
|
import { FILE_SERVER, SERVER_API_URL } from '@/config/setting';
|
|
import { getCache } from '@/api/system/cache';
|
|
|
|
/**
|
|
* 上传文件
|
|
*/
|
|
export async function uploadFile(file: File) {
|
|
const formData = new FormData();
|
|
formData.append('file', file);
|
|
let url = FILE_SERVER + '/api/file/upload';
|
|
const uploadMethod = localStorage.getItem('uploadMethod');
|
|
if (!uploadMethod) {
|
|
getCache('setting:upload:').then((res) => {
|
|
localStorage.setItem('uploadMethod', res?.uploadMethod);
|
|
});
|
|
}
|
|
if (uploadMethod == 'oss') {
|
|
url = SERVER_API_URL + '/oss/upload';
|
|
}
|
|
if (uploadMethod == 'cos') {
|
|
url = '/cos/upload';
|
|
}
|
|
if (uploadMethod == 'kodo') {
|
|
url = '/kodo/upload';
|
|
}
|
|
const res = await request.post<ApiResult<FileRecord>>(url, formData);
|
|
if (res.data.code === 0 && res.data.data) {
|
|
return res.data.data;
|
|
}
|
|
return Promise.reject(new Error(res.data.message));
|
|
}
|
|
|
|
/**
|
|
* 上传 base64 文件
|
|
* @param base64 文件数据
|
|
* @param fileName 文件名称
|
|
*/
|
|
export async function uploadBase64File(base64: string, fileName?: string) {
|
|
const formData = new FormData();
|
|
formData.append('base64', base64);
|
|
if (fileName) {
|
|
formData.append('fileName', fileName);
|
|
}
|
|
const res = await request.post<ApiResult<FileRecord>>(
|
|
SERVER_API_URL + '/file/upload/base64',
|
|
formData
|
|
);
|
|
if (res.data.code === 0 && res.data.data) {
|
|
return res.data.data;
|
|
}
|
|
return Promise.reject(new Error(res.data.message));
|
|
}
|
|
|
|
/**
|
|
* 上传证书
|
|
*/
|
|
export async function uploadCert(file: File) {
|
|
const formData = new FormData();
|
|
formData.append('file', file);
|
|
const res = await request.post<ApiResult<FileRecord>>(
|
|
SERVER_API_URL + '/file/upload',
|
|
formData
|
|
);
|
|
if (res.data.code === 0 && res.data.data) {
|
|
return res.data.data;
|
|
}
|
|
return Promise.reject(new Error(res.data.message));
|
|
}
|
|
|
|
/**
|
|
* 分页查询文件上传记录
|
|
*/
|
|
export async function pageFiles(params: FileRecordParam) {
|
|
const res = await request.get<ApiResult<PageResult<FileRecord>>>(
|
|
SERVER_API_URL + '/file/page',
|
|
{ params }
|
|
);
|
|
if (res.data.code === 0) {
|
|
return res.data.data;
|
|
}
|
|
return Promise.reject(new Error(res.data.message));
|
|
}
|
|
|
|
/**
|
|
* 根据id查询文件
|
|
*/
|
|
export async function getFile(id: number) {
|
|
const res = await request.get<ApiResult<FileRecord>>(
|
|
SERVER_API_URL + '/file/' + id
|
|
);
|
|
if (res.data.code === 0 && res.data.data) {
|
|
return res.data.data;
|
|
}
|
|
return Promise.reject(new Error(res.data.message));
|
|
}
|
|
|
|
/**
|
|
* 删除文件
|
|
*/
|
|
export async function removeFile(id?: number) {
|
|
const res = await request.delete<ApiResult<unknown>>(
|
|
SERVER_API_URL + '/file/remove/' + id
|
|
);
|
|
if (res.data.code === 0) {
|
|
return res.data.message;
|
|
}
|
|
return Promise.reject(new Error(res.data.message));
|
|
}
|
|
|
|
/**
|
|
* 批量删除文件
|
|
*/
|
|
export async function removeFiles(data: (number | undefined)[]) {
|
|
const res = await request.delete<ApiResult<unknown>>(
|
|
SERVER_API_URL + '/file/remove/batch',
|
|
{
|
|
data
|
|
}
|
|
);
|
|
if (res.data.code === 0) {
|
|
return res.data.message;
|
|
}
|
|
return Promise.reject(new Error(res.data.message));
|
|
}
|
|
|
|
/**
|
|
* 添加文件
|
|
*/
|
|
export async function addFiles(data: FileRecord) {
|
|
const res = await request.post<ApiResult<unknown>>(
|
|
SERVER_API_URL + '/file',
|
|
data
|
|
);
|
|
if (res.data.code === 0) {
|
|
return res.data.message;
|
|
}
|
|
return Promise.reject(new Error(res.data.message));
|
|
}
|
|
|
|
/**
|
|
* 修改文件
|
|
*/
|
|
export async function updateFiles(data: FileRecord) {
|
|
const res = await request.put<ApiResult<unknown>>(
|
|
SERVER_API_URL + '/file',
|
|
data
|
|
);
|
|
if (res.data.code === 0) {
|
|
return res.data.message;
|
|
}
|
|
return Promise.reject(new Error(res.data.message));
|
|
}
|
|
|
|
/**
|
|
* 上传阿里云OSS
|
|
*/
|
|
export async function uploadOss(file: File) {
|
|
const formData = new FormData();
|
|
formData.append('file', file);
|
|
const res = await request.post<ApiResult<FileRecord>>(
|
|
SERVER_API_URL + '/oss/upload',
|
|
formData
|
|
);
|
|
if (res.data.code === 0 && res.data.data) {
|
|
return res.data.data;
|
|
}
|
|
return Promise.reject(new Error(res.data.message));
|
|
}
|