| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175 | const AliOss = (function (){    const setting = {        hint: true    };    let client, uploadInfo;    const downloadFile = function (url, filename) {        axios.get(url, {responseType: 'blob' }).then(res => {            saveAs(res.data, filename);        });    };    const downloadFileSync = function(url) {        return new Promise((resolve, reject) => {            axios.get(url, {responseType: 'blob'}).then(res => {                resolve(res.data);            }).catch(err => {                reject(err);            });        })    };    const zipFiles = function (files, filename = '打包.zip', successCallback, errorCallback) {        const zip = new JSZip();        const download = [], fails = [];        files.forEach(f => {            download.push(downloadFileSync(f.filepath).then(data => {                if (setting.hint) {                    toastr.success(`文件 “${f.filename + f.fileext}” 下载成功...`);                }                zip.file(f.filename + f.fileext, data, {binary: true});            }).catch(err => {                fails.push(f);            }));        });        Promise.all(download).then(() => {            toastr.clear();            if (fails.length < files.length) {                if (setting.hint) {                    toastr.success('所有文件下载成功,压缩中...');                }                zip.generateAsync({ type: "blob" }).then(content => {                    saveAs(content, filename);                    successCallback && successCallback(fails);                });            } else {                errorCallback && errorCallback(fails);            }        })    };    const setSetting = function(data) {        if (!data) return;        setting.hint = data.hint || setting.hint;    };    const stopUpload = async function() {        if (!client) toastr.warning('当前没有上传中的大文件');        client.cancel();        uploadInfo.isStop = true;        if (uploadInfo.stopObj) uploadInfo.stopObj.attr('disabled', 'disabled');    };    const resumeUpload = async function() {        if (!client || !uploadInfo.abortCheckpoint) return;        if (uploadInfo.stopObj) uploadInfo.stopObj.removeAttr('disabled');        if (uploadInfo.resumeObj) uploadInfo.resumeObj.attr('disabled', 'disabled');        try {            const result = await client.multipartUpload(uploadInfo.filepath, uploadInfo.file, {                checkpoint: uploadInfo.abortCheckpoint,                progress: (p, cpt, res) => {                    uploadInfo.abortCheckpoint = cpt;                    // 刷新进度条                    if (uploadInfo.progressObj) {                        uploadInfo.progressObj.attr('aria-valuenow', p * 100);                        uploadInfo.progressObj.width((p * 100) + '%');                    }                }            });            return await endUploadBigFile();        } catch (e) {            if (uploadInfo.resumeObj) uploadInfo.resumeObj.removeAttr('disabled');        }    };    const resetRelaObj = function() {        if (uploadInfo.progressObj) {            uploadInfo.progressObj.attr('aria-valuenow', 0);            uploadInfo.progressObj.width('0%');        }        if (uploadInfo.stopObj) {            uploadInfo.stopObj.show();            uploadInfo.stopObj.removeAttr('disabled');            uploadInfo.stopObj.bind('click', function() {                stopUpload();            });        }        if (uploadInfo.resumeObj) {            uploadInfo.resumeObj.show();            uploadInfo.resumeObj.attr('disabled', 'disabled');            uploadInfo.resumeObj.bind('click', function() {                resumeUpload();            });        }    };    const clearRelaObj = function() {        if (uploadInfo.stopObj) {            uploadInfo.stopObj.attr('disabled', 'disabled');            uploadInfo.stopObj.unbind('click');        }        if (uploadInfo.resumeObj) {            uploadInfo.resumeObj.attr('disabled', 'disabled');            uploadInfo.resumeObj.unbind('click');        }    };    const beginUploadBigFile = async function () {        const beingData = JSON.parse(JSON.stringify(uploadInfo.defaultData));        beingData.type = 'begin';        beingData.fileInfo = uploadInfo.fileInfo;        const info = await postDataAsync(uploadInfo.url, beingData, false);        uploadInfo.filepath = info.filepath;        uploadInfo.filename = info.filename;        if (!info.oss) throw '授权信息错误';        uploadInfo.oss = info.oss;    };    const endUploadBigFile = async function() {        client = null;        const endData = JSON.parse(JSON.stringify(uploadInfo.defaultData));        endData.type = 'end';        endData.filepath = uploadInfo.filename;        endData.fileInfo = uploadInfo.fileInfo;        clearRelaObj();        const uploadRes = await postDataAsync(uploadInfo.url, endData, false);        if (uploadInfo.callback) {            uploadInfo.callback(uploadRes);        }        uploadInfo = {};        return uploadRes;    };    const uploadBigFile = async function (file, url, data, relaObj, callback) {        uploadInfo = { url, file, ...relaObj, callback };        uploadInfo.fileInfo = { filename: file.name, filesize: file.size, type: file.type, lastModified: file.lastModified };        uploadInfo.defaultData = data;        await beginUploadBigFile();        try {            client = new OSS(uploadInfo.oss);            resetRelaObj();            const result = await client.multipartUpload(uploadInfo.filepath, uploadInfo.file, {                progress: (p, cpt, res) => {                    uploadInfo.abortCheckpoint = cpt;                    // 刷新进度条                    if (uploadInfo.progressObj) {                        uploadInfo.progressObj.attr('aria-valuenow', p * 100);                        uploadInfo.progressObj.width((p * 100) + '%');                    }                }            });            return await endUploadBigFile();        } catch (err) {            if (uploadInfo.resumeObj) uploadInfo.resumeObj.removeAttr('disabled');            if (uploadInfo.stopObj) uploadInfo.stopObj.attr('disabled', 'disabled');        }    };    return { downloadFile, downloadFileSync, zipFiles, setSetting, uploadBigFile, resumeUpload }})();
 |