ipink-util
Version:
util.js
2 lines (1 loc) • 6 kB
JavaScript
;Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const e=require("./toast.js");function getLocalFilePath(e){if(0===e.indexOf("_www")||0===e.indexOf("_doc")||0===e.indexOf("_documents")||0===e.indexOf("_downloads"))return e;if(0===e.indexOf("file://"))return e;if(0===e.indexOf("/storage/emulated/0/"))return e;if(0===e.indexOf("/")){var n=plus.io.convertAbsoluteFileSystem(e);if(n!==e)return n;e=e.substr(1)}return"_www/"+e}function dataUrlToBase64(e){var n=e.split(",");return n[n.length-1]}var n=0;exports.base64ToPath=function base64ToPath(e){return new Promise((function(t,i){if("object"==typeof window&&"document"in window){const n=e.split(",");for(var o=n[0].match(/:(.*?);/)[1],a=atob(n[1]),r=a.length,s=new Uint8Array(r);r--;)s[r]=a.charCodeAt(r);return t((window.URL||window.webkitURL).createObjectURL(new Blob([s],{type:o})))}var u=e.split(",")[0].match(/data\:\S+\/(\S+);/),c="";u?c=u[1]:t({t:"base64 error"});var f=function getNewFileId(){return Date.now()+String(n++)}()+"."+c;if("object"!=typeof plus)if("object"==typeof wx&&wx.canIUse("getFileSystemManager")){m=wx.env.USER_DATA_PATH+"/"+f;wx.getFileSystemManager().writeFile({i:m,data:dataUrlToBase64(e),encoding:"base64",o:function(){t(m)},u:function(e){i(e)}})}else i(new Error("not support"));else{var l="_doc",w="uniapp_temp",m=l+"/"+w+"/"+f;if(!function biggerThan(e,n){for(var t=e.split("."),i=n.split("."),o=!1,a=0;a<i.length;a++){var r=+t[a]-+i[a];if(0!==r){o=r>0;break}}return o}("Android"===plus.os.name?"1.9.9.80627":"1.9.9.80472",plus.runtime.innerVersion))return void plus.io.resolveLocalFileSystemURL(l,(function(n){n.getDirectory(w,{create:!0,l:!1},(function(n){n.getFile(f,{create:!0,l:!1},(function(n){n.m((function(n){n.p=function(){t(m)},n.onerror=i,n.seek(0),n.v(dataUrlToBase64(e))}),i)}),i)}),i)}),i);var p=new plus.nativeObj.Bitmap(f);p.h(e,(function(){p.save(m,{},(function(){p.clear(),t(m)}),(function(e){p.clear(),i(e)}))}),(function(e){p.clear(),i(e)}))}}))},exports.chooseImage=function chooseImage(n){let{count:t=1,T:i=1,P:o="image",j:a,S:r=!1}=n||{};return new Promise((async n=>{if(a=a||["png","PNG","JPG","jpg","jpeg"],window&&window.AlipayJSBridge)await AlipayJSBridge.call("chooseImage",{count:t,T:2==i?["album"]:3==i?["camera"]:["album","camera"]},(async function(t){const i=t.F||t.I;let r=[];if("string"==typeof i)try{r=JSON.parse(i)}catch(s){}if(r=r.length?r:t._||[],!r.length)return n([]);if("image"==o){if((t.A||[]).findIndex((e=>{var n=e.type;const t=(a||[]).map((e=>"image/"+e)).find((e=>e==n));if(n&&!t)return e}))>=0)return e.B("选择的文件格式不正确,请重新选择!"),n([])}n(r)}));else{if(!uni||!uni.chooseImage)return n([]);uni.chooseImage({count:t,j:a,T:2==i?["album"]:3==i?["camera"]:["album","camera"],N:["compressed"],o:e=>{let t=e.I;try{"string"==typeof t&&(t=JSON.parse(t))}catch(i){t=[]}n(t)},u(t){r&&e.B(t&&t.J||"上传失败!"),n([])}})}}))},exports.chooseOtherFile=function chooseOtherFile(n){let{count:t=1,j:i,type:o="all",T:a,S:r=!1}=n||{};return i=i||[".csv",".xlsx"],new Promise((n=>{const s={count:t,type:o,T:2==a?["album"]:3==a?["camera"]:["album","camera"],j:i,u:t=>{r&&e.B(t&&t.J||"上传失败!"),n([])}};wx&&wx.chooseMessageFile?wx.chooseMessageFile({...s,o(e){n(e&&e.I||(e&&e.A&&e.A[0]?[e.A[0].path]:[]))}}):uni&&uni.chooseFile?uni.chooseFile({...s,o:function(e){n(e.I?"string"==typeof e.I?[e.I]:e.I:[])}}):(e.B("请在「APP」内进行操作, 未安装请先前往下载!"),n([]))}))},exports.chooseVideo=function chooseVideo(n){let{T:t=1,L:i,camera:o,M:a,S:r=!1}=n||{};return uni&&uni.chooseVideo?new Promise((n=>{uni.chooseVideo({T:2==t?["album"]:3==t?["camera"]:["album","camera"],L:i,camera:o,M:a,o(e){n([e.O])},u(t){n([]),r&&e.B(t&&t.J||"上传失败!")}})})):Promise.resolve([])},exports.U=function getImageInfo(e){const n=uni||wx;return n&&n.U?new Promise((t=>{n.U({src:e,o:e=>{t(e)},u:e=>{t(e)}})})):Promise.resolve({t:"当前环境暂不支持!"})},exports.getLocalFilePath=getLocalFilePath,exports.pathToBase64=async function pathToBase64(e,n,t,i){return i=i||300,new Promise((async function(o,a){if(window&&window.AlipayJSBridge){var r=new Image;return r.crossOrigin="anonymous",r.src=e,void(r.onload=await(function(){var n=document.createElement("CANVAS"),t=n.getContext("2d");n.height=r.height,n.width=r.width,t.drawImage(r,0,0);try{var i=n.toDataURL("image/png");return n=null,o({D:i,file:e})}catch(a){return n=null,o({D:"",file:e})}}))}if("object"==typeof window&&"document"in window){if("function"==typeof FileReader){var s=new XMLHttpRequest;return s.open("GET",e,!0),s.responseType="blob",s.onload=function(){if(200===this.status){let n=new FileReader;n.onload=function(n){o({D:n.target.result,file:e})},n.onerror=a,n.readAsDataURL(this.response)}},s.onerror=a,void s.send()}var u=document.createElement("canvas"),c=u.getContext("2d"),f=new Image;return f.src=e,f.onload=await(function(){u.width=f.width,u.height=f.height,c.drawImage(f,0,0),o({D:u.toDataURL(),file:e}),u.height=u.width=0}),f.onerror=a,f.src=e,void(u=null)}if("object"!=typeof plus)if("object"==typeof wx&&wx.canIUse("getFileSystemManager")){if(wx&&wx.getFileSystemManager)return void wx.getFileSystemManager().readFile({i:e,encoding:"base64",o:function(n){o({D:"data:image/png;base64,"+n.data,file:e,info:n})},u:function(n){o({D:"",file:e,G:n})}});if(my&&my.createCanvasContext){const a=my.createCanvasContext("canvas");if(n&&n.width){n.width,n.height;my.getSystemInfoSync().windowWidth,n.width;n.height,a.drawImage(e,0,0,n.width,n.height)}else a.drawImage(e,0,0,t||my.getSystemInfoSync().windowWidth,i);a.draw(!1,(()=>{a.toDataURL({}).then((n=>{o({D:n,file:e})})).catch((n=>{o({D:"",file:e,G:n})}))}))}}else o({D:"",file:e});else await plus.io.resolveLocalFileSystemURL(getLocalFilePath(e),(function(n){n.file((function(n){var t=new plus.io.FileReader;t.onload=function(n){o({D:n.target.result,file:e})},t.onerror=function(n){o({D:"",file:e})},t.readAsDataURL(n)}),(function(n){o({D:"",file:e})}))}),(function(n){o({D:"",file:e})}))}))};