123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208 |
- import request from "./../core/request.js";
- const {
- chooseImage,
- chooseVideo,
- qiniuUpload,
- urlUpload
- } = require("./utils");
- import {
- mergeConfig
- } from "./../core/utils.js";
- export default class fileUpload extends request {
- constructor(props) {
- // 调用实现父类的构造函数
- super(props);
- }
- //七牛云上传图片
- 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);
- }
- }
- }
- }
|