This commit is contained in:
2025-09-16 21:39:30 +08:00
parent f933b30d85
commit 3585e28594
5 changed files with 100 additions and 21 deletions

Binary file not shown.

View File

@@ -1,5 +1,6 @@
import request from "../utils/request.js"; import request from "../utils/request.js";
import Method from "./Method.js"; import Method from "./Method.js";
import Qiniu from "../utils/Qiniu.js";
const system = { const system = {
getData: async (params) => { getData: async (params) => {
@@ -31,31 +32,26 @@ const system = {
}); });
}) })
}, },
uploadFile: async (file) => { getUploadToken: async () => {
const formData = new FormData();
formData.append('file', file);
return request({ return request({
UN_AES: true, UN_AES: true,
url: '/admin/upload/upload', url: `/${import.meta.env.VITE_BUILD_MODE === 'admin' ? 'admin' : 'index'}/Upload/webToken`,
method: Method.POST, method: Method.GET,
data: formData,
headers: {
'Content-Type': 'multipart/form-data; boundary=--------------------------611824495457697861278283'
}
}); });
}, },
uploadFile2: async (file) => { uploadFile: async (file) => {
const formData = new FormData(); const qiniu = await Qiniu.getInstance();
formData.append('file', file); const path = await qiniu.uploadFile(file);
return request({ return {
UN_AES: true, data: path,
url: '/index/upload/upload', }
method: Method.POST, },
data: formData, uploadFile2: async (file) => {
headers: { const qiniu = await Qiniu.getInstance();
'Content-Type': 'multipart/form-data; boundary=--------------------------611824495457697861278283' const path = await qiniu.uploadFile(file);
return {
data: path,
} }
});
}, },
} }

79
src/utils/Qiniu.js Normal file
View File

@@ -0,0 +1,79 @@
import Api from "../api/index.js";
import Method from "../api/Method.js";
import axios from "axios";
import {Message} from "@arco-design/web-vue";
/**
* TODO: 如果后面频繁出现七牛的异常,记得后面做一下异常处理
*/
class Qiniu {
static instance;
domain;
path;
token;
constructor(callback) {
this.getToken().then(() => {
if (callback) callback();
});
}
/**
* @return {Promise<Qiniu>}
*/
static async getInstance() {
return new Promise((resolve) => {
if (!Qiniu.instance) {
Qiniu.instance = new Qiniu(() => {
setTimeout(() => {
resolve(Qiniu.instance);
}, 100);
});
return;
}
resolve(Qiniu.instance);
})
}
async getToken() {
const {data} = await Api.system.getUploadToken();
this.domain = data.domain;
this.path = data.path;
this.token = data.token;
}
async uploadFile(file) {
const formData = new FormData();
formData.append('token', this.token);
formData.append('key', `${this.path}/${crypto.randomUUID()}.${this.getExt(file.name)}`);
formData.append('file', file);
try {
const {data: {key}} = await axios.request({
url: 'https://up-z2.qiniup.com',
method: Method.POST,
data: formData,
})
return `${this.domain}/${key}`;
} catch ({status, message}) {
if (status === 401) {
await this.getToken();
return await this.uploadFile(file);
} else {
Message.error(message);
}
}
}
/**
* @param filename {string}
* @return {string}
*/
getExt(filename) {
const parts = filename.split('.');
return parts.length > 1 ? parts.pop() : '';
}
}
export default Qiniu;

View File

@@ -11,6 +11,10 @@ const excludeURL = [
'/index/login/login', '/index/login/login',
'/index/task/getCommonRefund', '/index/task/getCommonRefund',
'/index/task/getDiyRefund', '/index/task/getDiyRefund',
'/index/task/getDiyRefund',
'/index/login/sendSms',
'/index/Upload/webToken',
'/admin/Upload/webToken',
]; ];
export const BASEURL = import.meta.env.MODE === 'development' ? '/baseApi' : import.meta.env.VITE_API_URL; export const BASEURL = import.meta.env.MODE === 'development' ? '/baseApi' : import.meta.env.VITE_API_URL;

File diff suppressed because one or more lines are too long