reactor:【INFRA】文件上传 api,增加 directory 参数,去除 path 参数,并支持按照日期分目录、文件名不再使用 sha256 而是时间戳
parent
db60915de6
commit
4d308078fa
|
|
@ -1,11 +1,9 @@
|
||||||
import { baseUrl, apiPath, tenantId } from '@/sheep/config';
|
import { baseUrl, apiPath, tenantId } from '@/sheep/config';
|
||||||
import request from '@/sheep/request';
|
import request, { getAccessToken } from '@/sheep/request';
|
||||||
|
|
||||||
const FileApi = {
|
const FileApi = {
|
||||||
// 上传文件
|
// 上传文件
|
||||||
uploadFile: (file) => {
|
uploadFile: (file, directory) => {
|
||||||
// TODO 芋艿:访问令牌的接入;
|
|
||||||
const token = uni.getStorageSync('token');
|
|
||||||
uni.showLoading({
|
uni.showLoading({
|
||||||
title: '上传中',
|
title: '上传中',
|
||||||
});
|
});
|
||||||
|
|
@ -17,7 +15,10 @@ const FileApi = {
|
||||||
header: {
|
header: {
|
||||||
Accept: '*/*',
|
Accept: '*/*',
|
||||||
'tenant-id': tenantId,
|
'tenant-id': tenantId,
|
||||||
// Authorization: 'Bearer test247',
|
Authorization: 'Bearer ' + getAccessToken(),
|
||||||
|
},
|
||||||
|
formData: {
|
||||||
|
directory,
|
||||||
},
|
},
|
||||||
success: (uploadFileRes) => {
|
success: (uploadFileRes) => {
|
||||||
let result = JSON.parse(uploadFileRes.data);
|
let result = JSON.parse(uploadFileRes.data);
|
||||||
|
|
@ -42,12 +43,13 @@ const FileApi = {
|
||||||
},
|
},
|
||||||
|
|
||||||
// 获取文件预签名地址
|
// 获取文件预签名地址
|
||||||
getFilePresignedUrl: (path) => {
|
getFilePresignedUrl: (name, directory) => {
|
||||||
return request({
|
return request({
|
||||||
url: '/infra/file/presigned-url',
|
url: '/infra/file/presigned-url',
|
||||||
method: 'GET',
|
method: 'GET',
|
||||||
params: {
|
params: {
|
||||||
path,
|
name,
|
||||||
|
directory,
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
|
|
||||||
|
|
@ -175,7 +175,7 @@ function uploadCloudFiles(files, max = 5, onUploadProgress) {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
function uploadFilesFromPath(path) {
|
function uploadFilesFromPath(path, directory) {
|
||||||
// 目的:用于微信小程序,选择图片时,只有 path
|
// 目的:用于微信小程序,选择图片时,只有 path
|
||||||
return uploadFiles(
|
return uploadFiles(
|
||||||
Promise.resolve({
|
Promise.resolve({
|
||||||
|
|
@ -187,11 +187,13 @@ function uploadFilesFromPath(path) {
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
}),
|
}),
|
||||||
{},
|
{
|
||||||
|
directory,
|
||||||
|
},
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
async function uploadFiles(choosePromise, { onChooseFile, onUploadProgress }) {
|
async function uploadFiles(choosePromise, { onChooseFile, onUploadProgress, directory }) {
|
||||||
// 获取选择的文件
|
// 获取选择的文件
|
||||||
const res = await choosePromise;
|
const res = await choosePromise;
|
||||||
// 处理文件选择回调
|
// 处理文件选择回调
|
||||||
|
|
@ -212,7 +214,7 @@ async function uploadFiles(choosePromise, { onChooseFile, onUploadProgress }) {
|
||||||
const uploadPromises = files.map(async (file) => {
|
const uploadPromises = files.map(async (file) => {
|
||||||
try {
|
try {
|
||||||
// 1.1 获取文件预签名地址
|
// 1.1 获取文件预签名地址
|
||||||
const { data: presignedInfo } = await FileApi.getFilePresignedUrl(file.name);
|
const { data: presignedInfo } = await FileApi.getFilePresignedUrl(file.name, directory);
|
||||||
// 1.2 获取二进制文件对象
|
// 1.2 获取二进制文件对象
|
||||||
const fileBuffer = await readFile(file);
|
const fileBuffer = await readFile(file);
|
||||||
|
|
||||||
|
|
@ -260,6 +262,7 @@ async function uploadFiles(choosePromise, { onChooseFile, onUploadProgress }) {
|
||||||
function chooseAndUploadFile(
|
function chooseAndUploadFile(
|
||||||
opts = {
|
opts = {
|
||||||
type: 'all',
|
type: 'all',
|
||||||
|
directory: undefined,
|
||||||
},
|
},
|
||||||
) {
|
) {
|
||||||
if (opts.type === 'image') {
|
if (opts.type === 'image') {
|
||||||
|
|
@ -279,7 +282,7 @@ function createFile(vo, file) {
|
||||||
const fileVo = {
|
const fileVo = {
|
||||||
configId: vo.configId,
|
configId: vo.configId,
|
||||||
url: vo.url,
|
url: vo.url,
|
||||||
path: file.name,
|
path: vo.path,
|
||||||
name: file.name,
|
name: file.name,
|
||||||
type: file.fileType,
|
type: file.fileType,
|
||||||
size: file.size,
|
size: file.size,
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue