2 lines
2.9 KiB
JavaScript
2 lines
2.9 KiB
JavaScript
"use strict";var e=(e,o,t)=>new Promise(((i,s)=>{var n=e=>{try{a(t.next(e))}catch(o){s(o)}},r=e=>{try{a(t.throw(e))}catch(o){s(o)}},a=e=>e.done?i(e.value):Promise.resolve(e.value).then(n,r);a((t=t.apply(e,o)).next())}));const o=require("../../../common/vendor.js"),t=require("../../api/infra/file.js"),i="chooseAndUploadFile:ok",s="chooseAndUploadFile:fail";function n(e,o){return e.tempFiles.forEach(((e,t)=>{e.name||(e.name=e.path.substring(e.path.lastIndexOf("/")+1)),o&&(e.fileType=o),e.cloudPath=Date.now()+"_"+t+e.name.substring(e.name.lastIndexOf("."))})),e.tempFilePaths||(e.tempFilePaths=e.tempFiles.map((e=>e.path))),e}function r(o,{onChooseFile:s,onUploadProgress:n}){return o.then((e=>{if(s){const o=s(e);if(void 0!==o)return Promise.resolve(o).then((o=>void 0===o?e:o))}return e})).then((e=>!1===e?{errMsg:i,tempFilePaths:[],tempFiles:[]}:e)).then((o=>e(this,null,(function*(){for(const e of o.tempFiles){const{data:o}=yield t.FileApi.uploadFile(e.path);e.url=o}return o}))))}exports.chooseAndUploadFile=function(e={type:"all"}){return"image"===e.type?r(function(e){const{count:t,sizeType:i=["original","compressed"],sourceType:r=["album","camera"],extension:a}=e;return new Promise(((e,l)=>{o.index.chooseImage({count:t,sizeType:i,sourceType:r,extension:a,success(o){e(n(o,"image"))},fail(e){l({errMsg:e.errMsg.replace("chooseImage:fail",s)})}})}))}(e),e):"video"===e.type?r(function(e){const{camera:t,compressed:i,maxDuration:r,sourceType:a=["album","camera"],extension:l}=e;return new Promise(((e,c)=>{o.index.chooseVideo({camera:t,compressed:i,maxDuration:r,sourceType:a,extension:l,success(o){const{tempFilePath:t,duration:i,size:s,height:r,width:a}=o;e(n({errMsg:"chooseVideo:ok",tempFilePaths:[t],tempFiles:[{name:o.tempFile&&o.tempFile.name||"",path:t,size:s,type:o.tempFile&&o.tempFile.type||"",width:a,height:r,duration:i,fileType:"video",cloudPath:""}]},"video"))},fail(e){c({errMsg:e.errMsg.replace("chooseVideo:fail",s)})}})}))}(e),e):r(function(e){const{count:t,extension:i}=e;return new Promise(((e,r)=>{let a=o.index.chooseFile;if(void 0!==o.wx$1&&"function"==typeof o.wx$1.chooseMessageFile&&(a=o.wx$1.chooseMessageFile),"function"!=typeof a)return r({errMsg:s+" 请指定 type 类型,该平台仅支持选择 image 或 video。"});a({type:"all",count:t,extension:i,success(o){e(n(o))},fail(e){r({errMsg:e.errMsg.replace("chooseFile:fail",s)})}})}))}(e),e)},exports.uploadCloudFiles=function(e,t=5,i){const s=(e=JSON.parse(JSON.stringify(e))).length;let n=0;const r=this;return new Promise((a=>{for(;n<t;)l();function l(){const t=n++;if(t>=s)return void(!e.find((e=>!e.url&&!e.errMsg))&&a(e));const c=e[t],u=r.files.findIndex((e=>e.uuid===c.uuid));c.url="",delete c.errMsg,o.Ws.uploadFile({filePath:c.path,cloudPath:c.cloudPath,fileType:c.fileType,onUploadProgress:e=>{e.index=u,i&&i(e)}}).then((e=>{c.url=e.fileID,c.index=u,t<s&&l()})).catch((e=>{c.errMsg=e.errMsg||e.message,c.index=u,t<s&&l()}))}}))};
|