import request from "./../core/request.js"; import { chooseImage, chooseVideo, qiniuUpload, urlUpload } from './utils' import { mergeConfig } from "./../core/utils.js"; export default class fileUpload extends request { constructor(props) { // 调用实现父类的构造函数 super(props); } async ossImagUpload(options = {}) { try { const files = await chooseImage(options); const data = []; for (let item of files) { let params = { size: item.size, model: 'image', } if (item.name) { params.name = item.name; params.path = item.path; } else if (item.path) { if (item.path.indexOf('/') > -1) { let names = item.path.split('/'); params.name = names[names.length - 1]; } params.path = item.path; } // 选择完成回调 options.onSelectComplete && options.onSelectComplete(files); data.push(params); } return Promise.resolve(data); } catch (err) { return Promise.reject(err); } } async ossVideoUpload(options = {}) { let files; try { files = await chooseVideo(options); let file = files[0]; let params = { size: file.size, model: 'video', } if (file.name) { params.name = file.name; params.path = file.path; } else if (file.path) { if (file.path.indexOf('/') > -1) { let names = file.path.split('/'); params.name = names[names.length - 1]; } params.path = file.path; } // 选择完成回调 options.onSelectComplete && options.onSelectComplete(files); return Promise.resolve(params); } catch (err) { return Promise.reject(err); } } //七牛云上传图片 async qnImgUpload(options = {}) { let files; try { files = await chooseImage(options); // 选择完成回调 options.onSelectComplete && options.onSelectComplete(files); } catch (err) { this.requestError && this.requestError(err); return Promise.reject(err); } if (files) { return this.qnFileUpload({ ...options, files: files }); } } //七牛云上传视频 async qnVideoUpload(options = {}) { let files; try { files = await chooseVideo(options); // 选择完成回调 options.onSelectComplete && options.onSelectComplete(files); } catch (err) { this.requestError && this.requestError(err); return Promise.reject(err); } if (files) { return this.qnFileUpload({ ...options, files: files }); } } //七牛云文件上传(支持多张上传) async qnFileUpload(options = {}) { let requestInfo; try { // 数据合并 requestInfo = { ...this.config, ...options, header: {}, method: "FILE" }; //请求前回调 if (this.requestStart) { let requestStart = this.requestStart(requestInfo); if (typeof requestStart == "object") { let changekeys = ["load", "files"]; changekeys.forEach(key => { requestInfo[key] = requestStart[key]; }); } else { throw { errMsg: "【request】请求开始拦截器未通过", statusCode: 0, data: requestInfo.data, method: requestInfo.method, header: requestInfo.header, url: requestInfo.url, } } } let requestResult = await qiniuUpload(requestInfo, this.getQnToken); return Promise.resolve(requestResult); } catch (err) { this.requestError && this.requestError(err); return Promise.reject(err); } finally { this.requestEnd && this.requestEnd(requestInfo); } } //本地服务器图片上传 async urlImgUpload() { let options = {}; if (arguments[0]) { if (typeof(arguments[0]) == "string") { options.url = arguments[0]; } else if (typeof(arguments[0]) == "object") { options = Object.assign(options, arguments[0]); } } if (arguments[1] && typeof(arguments[1]) == "object") { options = Object.assign(options, arguments[1]); } try { options.files = await chooseImage(options); // 选择完成回调 options.onSelectComplete && options.onSelectComplete(options.files); } catch (err) { this.requestError && this.requestError(err); return Promise.reject(err); } if (options.files) { return this.urlFileUpload(options); } } //本地服务器上传视频 async urlVideoUpload() { let options = {}; if (arguments[0]) { if (typeof(arguments[0]) == "string") { options.url = arguments[0]; } else if (typeof(arguments[0]) == "object") { options = Object.assign(options, arguments[0]); } } if (arguments[1] && typeof(arguments[1]) == "object") { options = Object.assign(options, arguments[1]); } try { options.files = await chooseVideo(options); // 选择完成回调 options.onSelectComplete && options.onSelectComplete(options.files); } catch (err) { this.requestError && this.requestError(err); return Promise.reject(err); } if (options.files) { return this.urlFileUpload(options); } } //本地服务器文件上传方法 async urlFileUpload() { let requestInfo = { method: "FILE" }; if (arguments[0]) { if (typeof(arguments[0]) == "string") { requestInfo.url = arguments[0]; } else if (typeof(arguments[0]) == "object") { requestInfo = Object.assign(requestInfo, arguments[0]); } } if (arguments[1] && typeof(arguments[1]) == "object") { requestInfo = Object.assign(requestInfo, arguments[1]); } if (!requestInfo.url && this.defaultUploadUrl) { requestInfo.url = this.defaultUploadUrl; } // 请求数据 // 是否运行过请求开始钩子 let runRequestStart = false; try { if (!requestInfo.url) { throw { errMsg: "【request】文件上传缺失数据url", statusCode: 0, data: requestInfo.data, method: requestInfo.method, header: requestInfo.header, url: requestInfo.url, } } // 数据合并 requestInfo = mergeConfig(this, requestInfo); // 代表之前运行到这里 runRequestStart = true; //请求前回调 if (this.requestStart) { let requestStart = this.requestStart(requestInfo); if (typeof requestStart == "object") { let changekeys = ["data", "header", "isPrompt", "load", "isFactory", "files"]; changekeys.forEach(key => { requestInfo[key] = requestStart[key]; }); } else { throw { errMsg: "【request】请求开始拦截器未通过", statusCode: 0, data: requestInfo.data, method: requestInfo.method, header: requestInfo.header, url: requestInfo.url, } } } let requestResult = await urlUpload(requestInfo, this.dataFactory); return Promise.resolve(requestResult); } catch (err) { this.requestError && this.requestError(err); return Promise.reject(err); } finally { if (runRequestStart) { this.requestEnd && this.requestEnd(requestInfo); } } } }