初始化

This commit is contained in:
2024-07-10 13:50:01 +08:00
commit e30966c6c5
1073 changed files with 143076 additions and 0 deletions
@@ -0,0 +1,171 @@
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));
}
@@ -0,0 +1,47 @@
import { PageParam } from '@/api';
/**
* 文件上传记录
*/
export interface FileRecord {
// id
id: number;
// 文件名称
name?: string;
// 文件存储路径
path?: string;
// 文件大小
length?: number;
// 文件类型
contentType?: string;
// 上传人id
createUserId?: number;
// 上传时间
createTime?: string;
// 描述
comments?: string;
// 文件访问地址
url?: string;
// 文件缩略图访问地址
thumbnail?: string;
// 文件下载地址
downloadUrl?: string;
// 上传人账号
createUsername?: string;
// 上传人名称
createNickname?: string;
}
/**
* 文件上传记录查询参数
*/
export interface FileRecordParam extends PageParam {
name?: string;
path?: string;
contentType?: string;
createNickname?: string;
count?: number;
page?: number;
// 商户编号
merchantCode?: string;
}