integrated_traffic_vue/src/assets/js/useAliYunUpload.js

84 lines
2.4 KiB
JavaScript
Raw Normal View History

2024-01-19 17:41:26 +08:00
import { setVideo, setVideoRefresh } from "@/request/api.js";
2024-01-20 08:48:10 +08:00
import { ElLoading, ElMessage } from "element-plus";
2024-01-19 17:41:26 +08:00
export default function useAliYunUpload({
addFileSuccess,
onUploadstarted,
onUploadSucceed,
onUploadFailed,
onUploadCanceled,
onUploadProgress,
onUploadTokenExpired,
onUploadEnd,
}) {
2024-01-20 08:48:10 +08:00
let loading;
2024-01-19 17:41:26 +08:00
const uploader = new window.AliyunUpload.Vod({
timeout: 60000,
partSize: 1048576,
parallel: 5,
retryCount: 3,
retryDuration: 2,
region: "cn-shanghai",
userId: "1663991162946873",
addFileSuccess(uploadInfo) {
addFileSuccess && addFileSuccess(uploadInfo);
},
async onUploadstarted(uploadInfo) {
onUploadstarted && onUploadstarted();
let uploadAddress;
let uploadAuth;
let videoId;
if (!uploadInfo.videoId) {
const resData = await setVideo({
Title: uploadInfo.file.name,
FileName: uploadInfo.file.name,
});
2024-02-27 09:27:50 +08:00
uploadAddress = resData.data.uploadAddress;
uploadAuth = resData.data.uploadAuth;
videoId = resData.data.videoId;
2024-01-19 17:41:26 +08:00
} else {
const resData = await setVideoRefresh({
Title: uploadInfo.file.name,
FileName: uploadInfo.file.name,
VideoId: uploadInfo.videoId,
});
uploadAddress = resData.data.uploadAddress;
uploadAuth = resData.data.uploadAuth;
videoId = resData.data.videoId;
}
uploader.setUploadAuthAndAddress(
uploadInfo,
uploadAuth,
uploadAddress,
videoId
);
2024-01-20 08:48:10 +08:00
loading = ElLoading.service({
lock: true,
text: "加载中...",
background: "rgba(0, 0, 0, 0.5)",
});
2024-01-19 17:41:26 +08:00
},
onUploadSucceed(uploadInfo) {
2024-01-20 08:48:10 +08:00
loading.close();
2024-01-19 17:41:26 +08:00
onUploadSucceed && onUploadSucceed(uploadInfo);
},
onUploadFailed(uploadInfo, code, message) {
2024-01-20 08:48:10 +08:00
ElMessage.success("上传失败,请重新上传");
2024-01-19 17:41:26 +08:00
onUploadFailed && onUploadFailed(uploadInfo, code, message);
},
onUploadCanceled(uploadInfo, code, message) {
onUploadCanceled && onUploadCanceled(uploadInfo, code, message);
},
onUploadProgress(uploadInfo, totalSize, progress) {
onUploadProgress && onUploadProgress(uploadInfo, totalSize, progress);
},
onUploadTokenExpired(uploadInfo) {
onUploadTokenExpired && onUploadTokenExpired(uploadInfo);
},
onUploadEnd(uploadInfo) {
onUploadEnd && onUploadEnd(uploadInfo);
},
});
return uploader;
}