temp: add resources

This commit is contained in:
2025-03-15 23:54:53 +08:00
parent a5bde33678
commit fd30741151
40 changed files with 2174 additions and 32 deletions

View File

@@ -0,0 +1,73 @@
import NProgress from 'nprogress';
import 'nprogress/nprogress.css';
import { toast } from 'react-toastify';
import { nanoid } from 'nanoid';
import { toastLogin } from '@/pages/message/ToastLogin';
type ConvertOpts = {
appKey?: string;
version?: string;
username?: string;
};
export const uploadFiles = async (files: File[], opts: ConvertOpts) => {
return new Promise((resolve, reject) => {
const formData = new FormData();
for (let i = 0; i < files.length; i++) {
formData.append('file', files[i], files[i].name);
}
const token = localStorage.getItem('token');
if (!token) {
toastLogin();
return;
}
const taskId = nanoid();
// 49.232.155.236:11015
// const eventSource = new EventSource('https://kevisual.silkyai.cn/api/s1/events?taskId=' + taskId);
// const eventSource = new EventSource('/api/s1/events?taskId=' + taskId);
const eventSource = new EventSource('http://49.232.155.236:11015/api/s1/events?taskId=' + taskId);
const load = toast.loading('上传中...');
NProgress.start();
eventSource.onopen = async function (event) {
console.log('eventSource.onopen', event);
const res = await fetch('/api/s1/resources/upload?taskId=' + taskId, {
method: 'POST',
body: formData,
headers: {
'task-id': taskId,
Authorization: `Bearer ${token}`,
},
}).then((response) => response.json());
console.log('upload success', res);
fetch('/api/s1/events/close?taskId=' + taskId);
eventSource.close();
NProgress.done();
toast.dismiss(load);
resolve(res);
};
// 监听服务器推送的进度更新
eventSource.onmessage = function (event) {
console.log('Progress update:', event.data);
const parseIfJson = (data: string) => {
try {
return JSON.parse(data);
} catch (e) {
return data;
}
};
const receivedData = parseIfJson(event.data);
if (typeof receivedData === 'string') return;
const progress = receivedData.progress;
console.log('progress', progress);
toast.update(load, { render: `上传中...${progress}%`, isLoading: true, autoClose: false });
if (progress) {
NProgress.set(progress);
}
};
eventSource.onerror = function (event) {
console.log('eventSource.onerror', event);
reject(event);
};
});
};