UNPKG

ipink-util

Version:

util.js

2 lines (1 loc) 11.5 kB
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const e=require("./is.js"),t=require("./get.js"),n=require("./env.js"),r=require("./config.js"),i=require("./toast.js"),o=require("./cache.js"),s="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/",u=new Array(-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,62,-1,-1,-1,63,52,53,54,55,56,57,58,59,60,61,-1,-1,-1,-1,-1,-1,-1,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,-1,-1,-1,-1,-1,-1,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,-1,-1,-1,-1,-1);function base64decode(e){if(""==e||null==e||null==e)return"";let t,n,r,i,o=0,s=e.length,c="";for(;o<s;){do{t=u[255&e.charCodeAt(o++)]}while(o<s&&-1==t);if(-1==t)break;do{n=u[255&e.charCodeAt(o++)]}while(o<s&&-1==n);if(-1==n)break;c+=String.fromCharCode(t<<2|(48&n)>>4);do{if(r=255&e.charCodeAt(o++),61==r)return c;r=u[r]}while(o<s&&-1==r);if(-1==r)break;c+=String.fromCharCode((15&n)<<4|(60&r)>>2);do{if(i=255&e.charCodeAt(o++),61==i)return c;i=u[i]}while(o<s&&-1==i);if(-1==i)break;c+=String.fromCharCode((3&r)<<6|i)}return c}function fillStr(t,n,r){if(!e.t(t)&&!e.i(t))return t;e.i(t)&&(t=""+t);for(var i=0;i<r;i++)t+=n;return t}let c=Math.PI;function getRad(e){return e*c/180}const a=["EAN_8","EAN_13","CODE_25","CODE_39","CODE_128","UPC_A","UPC_E"];exports.BAR_CODE_TYPE=a,exports.allSettled=function allSettled(e){return Promise.all(function handlerPromise(e){return e.map((e=>e.then((e=>e),(e=>e))))}(e))},exports.backMainAppHome=(e=1)=>{if("undefined"!=typeof document&&r.o){const t=n.u();if(n.p.l===t&&"undefined"!=typeof wx)return wx.miniProgram.navigateBack({delta:e}),!0;if(n.p.m===t&&"undefined"!=typeof my)return my.navigateBack({delta:e}),!0;if(n.p.h===t&&"undefined"!=typeof swan)return swan.webView.navigateBack({delta:e}),!0;if(sdk)return"undefined"!=typeof uni?uni.webView?.navigateBack({delta:e}):wx.webView.navigateBack({delta:e}),!0;"undefined"!=typeof uni?uni.navigateBack({delta:e}):"undefined"!=typeof wx&&wx.navigateBack({delta:e})}return!0},exports.base64decode=base64decode,exports.base64encode=function base64encode(e){let t,n,r,i="",o=0,u=e.length;for(;o<u;){if(t=255&e.charCodeAt(o++),o==u){i+=s.charAt(t>>2),i+=s.charAt((3&t)<<4),i+="==";break}if(n=e.charCodeAt(o++),o==u){i+=s.charAt(t>>2),i+=s.charAt((3&t)<<4|(240&n)>>4),i+=s.charAt((15&n)<<2),i+="=";break}r=e.charCodeAt(o++),i+=s.charAt(t>>2),i+=s.charAt((3&t)<<4|(240&n)>>4),i+=s.charAt((15&n)<<2|(192&r)>>6),i+=s.charAt(63&r)}return i},exports.buildHierarchyArray=function buildHierarchyArray(e,t){let n=[],r={};return e.forEach((e=>{r[e.id]={...e,children:[]}})),e.forEach((e=>{let i=e[t];i&&i?r[i].children.push(r[e.id]):n.push(r[e.id])})),n},exports.checkWhiteScreen=(e,t=".content")=>{try{e.M((()=>{uni.createSelectorQuery().in(e).select(t).boundingClientRect((e=>{e&&0!=e.height||window.location.reload()})).exec()}))}catch(n){}},exports.compareVersion=(e,t)=>{const n=e.split("."),r=t.split("."),i=Math.max(n.length,r.length);for(;n.length<i;)n.push("0");for(;r.length<i;)r.push("0");for(let o=0;o<i;o++){const e=parseInt(n[o]),t=parseInt(r[o]);if(e>t)return 1;if(e<t)return-1}return 0},exports.converNumber=(e,t,n=1)=>{let r=(e=""+e).split(".")[0],i=e.split(".")[1]||"",o=i.length,s=r.length;if("en"===t){let e=s<=4?"K":s<=6?"W":s<=12?"M":"B",t=(+r/{S:1e3,k:1e4,v:1e6,A:1e9}[e]).toFixed(n),i=t.split(".")[1]||"";return i.length>n&&(t=t.split(".")[0]+i.slice(0,n)),t+e}if("cn"===t){let e=s<=5?" ":s<=11?"万":"亿",t=(+r/{C:1,T:1e4,j:1e8}[e]).toFixed(n),i=(""+t).split(".")[1]||"";return i.length>n&&(t=(""+t).split(".")[0]+i.slice(0,n)),t+e}if("sp"===t){i=o<=n?fillStr(i,"0",n-o):i.slice(0,n);let e=0,t=[];for(let n=r.length-1;n>=0;n--)e%3==0&&0!==e&&t.unshift(","),t.unshift(r[n]),e++;return t.join("")+"."+i}return""+e},exports.copyValue=function copyValue(t,n){const{R:r,O:o,B:s}=n||{};if("undefined"!=typeof document&&window&&window.getSelection){let n="";(e.J()||e.P()||e.q()||e.D())&&(n="ios");try{if("ios"==n){window.getSelection().removeAllRanges();let e=document.createElement("input");document.body.appendChild(e),e.style.opacity=0,e.value=t;let n=document.createRange();n.selectNode(e),window.getSelection().addRange(n),e.select(),e.setSelectionRange(0,e.value.length),document.execCommand("copy"),window.getSelection().removeAllRanges(),document.body.removeChild(e)}else{var u=document.createElement("input");u.value=t,document.body.appendChild(u),u.select(),document.execCommand("Copy"),u.remove()}r&&i.N(r),o&&o()}catch(c){s&&s(c)}}else try{let e={data:t,O:()=>{r&&i.N(r),o&&o()},B:e=>s&&s(e)};"undefined"!=typeof uni?uni.setClipboardData(e):wx.setClipboardData(e)}catch(a){s&&s(a)}},exports.debounce=function debounce(e,t,n){if(!t||t<=0)return e;let r;return function(){if(r&&clearTimeout(r),n){const n=!r;r=setTimeout((()=>{r=null}),t),n&&e.apply(this,arguments)}else r=setTimeout((()=>{e.apply(this,arguments)}),t)}},exports.desensitization=(e,t=3,n=t)=>{if(!e||e.length<=t+n)return e;n=e.length-n;let r=e.slice(0,t),i=e.slice(n);return r+e.substring(t,Math.abs(n)).replace(/[\s\S]/gi,"*")+i},exports.fillStr=fillStr,exports.findTargetAndRemove=function findTargetAndRemove(t,n,r){const{key:i="id",type:o="unshift"}=r||{},s=t.findIndex((t=>{if((e.t(t)||e.i(t))&&t===n)return t;const r=n;return t[i]==r[i]?t:void 0}));if(-1==s)t[o](n);else{const e=t.splice(s,1);t[o](1==Object.keys(n).length?e:n)}return t},exports.firstCharCase=e=>e.slice(0,1).toUpperCase()+e.slice(1).toLowerCase(),exports.flattenArray=function flattenArray(e){let t=[];return e.forEach((e=>{Array.isArray(e)?t=t.concat(flattenArray(e)):t.push(e)})),t},exports.formatTree=function formatTree(e,t,n){t=t||"id",n=n||"pid";const r={};for(var i=0;i<e.length;i++){const o=e[i],s=o[n],u=o[t];s||(r[u]={...o,children:[]})}for(i=0;i<e.length;i++){const t=e[i],o=t[n];t[o]&&r[o]&&r[o].children&&r[o].children.push(e[i])}return Object.values(r)},exports.getGreatCircleDistance=function getGreatCircleDistance(e,t,n,r){let i=getRad(t),o=getRad(r),s=i-o,u=getRad(e)-getRad(n),c=2*Math.asin(Math.sqrt(Math.pow(Math.sin(s/2),2)+Math.cos(i)*Math.cos(o)*Math.pow(Math.sin(u/2),2)));return c*=6378137,c=Math.round(1e4*c)/1e4,c},exports.getLocation=async t=>{const{I:s,O:u,B:c,complete:a,_:d=!1,K:f=!1,L:l}=t||{};return new Promise((async t=>{if(f){const n=o.Storage.getItem("IsRefuseGetLocation");if(n&&e.$(n))return s&&s(!1,{}),t(null)}let d=!1,p=null;if("undefined"!=typeof document&&r.o)try{let e=n.u();if(e==n.p.G||e==n.p.m)return d=!0,void(window.AlipayJSBridge&&AlipayJSBridge.call("getCurrentLocation",{U:"$s"},(e=>{e.error?(s&&s(),11==e.error?AlipayJSBridge.call("showAuthGuide",{U:"$s",W:"LBS"},(function(t){c&&c({...e,H:e.V})})):(c&&c({...e,H:e.V}),i.N(e.V)),a&&a(),t(null)):(s&&s(e),u&&u(e),a&&a(e),t(e))})));if(e==n.p.F||e==n.p.l)return l&&await l(),void wx.getLocation({type:"gcj02",O:e=>{s&&s(e),u&&u(e),a&&a(e),t(e)},B:e=>{s&&s(),c&&c(e),a&&a(),t(null)},cancel:function(e){if(s&&s(),c&&c({H:e.H||"用户拒绝授权获取地理位置信息"}),a&&a(),t(null),f){confirm("用户拒绝授权获取地理位置,今日是否不再提示!")&&o.Storage.setItem("IsRefuseGetLocation",new Date)}}});if(e==n.p.h&&"undefined"!=typeof swan&&swan.getLocation)return void swan.getLocation({type:"gcj02",O:e=>{s&&s(e),u&&u(e),a&&a(e),t(e)},B:e=>{s&&s(),c&&c(e),a&&a(),t(null)}})}catch(y){p={...y||{},H:y?.H||y?.message||"获取失败"}}try{if(r.X()){let n="gcj02";return n="undefined"!=typeof document&&d?"wgs84":"gcj02",e.Y()&&(n="wgs84"),void uni.getLocation({type:n,Z:1e3,O:e=>{s&&s(e),u&&u(e),a&&a(e),t(e)},B:e=>{s&&s(),c&&c(e),a&&a(),t(null)}})}}catch(y){p={...y||{},H:y?.H||y?.message||"获取失败"}}s&&s(),c&&c({...p||{},H:p?.H||"获取失败"}),a&&a(),t(null)}))},exports.getRefInfo=(e,t,n)=>(n||(n={size:!0,rect:!0,ee:!0}),r.X()?new Promise((r=>{e||r(null);try{uni.createSelectorQuery().in(t).select(e).fields(n,(t=>{r(t?{...t,te:e}:null)})).exec()}catch(i){r(null)}})):Promise.resolve(null)),exports.hasKey=function hasKey(e,t){return Object.prototype.hasOwnProperty.call(e,t)},exports.navigateBack=function navigateBack(e){if(e=e||1,"undefined"!=typeof window&&window.history)return void window.history.go(Number("-"+e));let t=r.X();try{t&&("undefined"!=typeof uni?uni.navigateBack({delta:e}):wx.navigateBack({delta:e}))}catch(n){}},exports.postMessage=function postMessage(e){const t=n.u();return new Promise((i=>{if("undefined"!=typeof document&&r.o)try{const{type:r,ne:o={},O:s,B:u}=e||{},c={data:{type:r,ne:o}},callBack=()=>{i({ok:!0}),s({ok:!0})};switch(t){case n.p.m:my.postMessage({type:r,ne:o}),my.onMessage=e=>{const{ok:t,result:n,type:r}=e;t?("function"==typeof s&&s(e),i(e)):("function"==typeof u&&u(e),i({...e||{},H:e?.H||e?.message||"获取信息失败"}))};break;case n.p.F:case n.p.l:wx.miniProgram.postMessage(c),callBack();break;case n.p.h:swan.webView.postMessage(c),callBack();break;case n.p.re:tt.miniProgram.postMessage(c),callBack();break;default:window.postMessage(c),callBack()}}catch(o){i({ok:!1,...o||{}})}else i({ok:!0,H:"非浏览器环境调用"})}))},exports.scanCode=t=>{const{O:r,ie:o,oe:s={}}=t||{},u={code:""};return new Promise((async t=>{try{if(navigator.userAgent.indexOf("AlipayClient")>=0)e.Y()?(my.onMessage=e=>{let n={code:JSON.parse(e.se).scanCode};r&&r(n),t(n)},my.postMessage({messageType:"scan"})):window.AlipayJSBridge&&AlipayJSBridge?AlipayJSBridge.call("scan",{ue:["qrCode","barCode"]},(e=>{let n={code:""};e.ce&&(n.code=e.ce),e.ae&&(n.code=e.ae),r&&r(n),t(n)}),(e=>{i.N("识别失败: "+JSON.stringify(e)),r&&r(u),t(u)})):(i.N("扫码调用失败"),r&&r(u),t(u));else if(navigator.userAgent.indexOf("MicroMessenger")>=0&&"undefined"!=typeof wx){if(n.de())return wx.miniProgram.postMessage({data:{type:"scan",ne:{ie:o}}}),r&&r(u),t(u);wx.config({debug:0,...s||{},fe:["scanQRCode"]}),wx.ready((()=>{wx.scanQRCode({le:1,ue:["qrCode","barCode","datamatrix"],O:e=>{let n=e.pe.indexOf(",");try{a.find((t=>t.toLocaleUpperCase()==e.pe.slice(0,n)))&&(e.pe=e.pe.slice(n+1))}catch(o){}let i={code:e.pe};r&&r(i),t(i)},B:e=>{i.N(JSON.stringify(e)),r&&r(u),t(u)}})}))}else uni.scanCode({O:function(e){let n={code:e.result};r&&r(n),t(n)},B(){i.N("当前环境暂不支持扫码!"),r&&r(u),t(u)}})}catch(c){i.N(c?.H||c?.message||JSON.stringify(c)),r&&r(u),t(u)}}))},exports.setTitleName=(e="")=>{if("undefined"!=typeof window&&"undefined"!=typeof document){try{t.ye().indexOf("base64=1")>-1&&(e=decodeURIComponent(base64decode(e)))}catch(i){e=""}const r=n.u();if(n.p.m===r)return void(document.title=e);if(n.p.G===r){const setAlipayTitle=()=>{AlipayJSBridge.call("setTitle",{title:e})};return void(window.AlipayJSBridge&&AlipayJSBridge?setAlipayTitle():document.addEventListener("AlipayJSBridgeReady",setAlipayTitle,!1))}}if(r.X()){let t={title:e,B:()=>{"undefined"!=typeof document&&(document.title=e)}};"undefined"!=typeof uni?uni.setNavigationBarTitle(t):wx.setNavigationBarTitle(t)}},exports.throttle=function throttle(e,t){let n,r;return function(...i){let o=(new Date).getTime();if(r||(r=o),o-r>t)clearTimeout(n),n=r=o=void 0,e.apply(this,i);else{if(n)return;n=setTimeout((()=>{e.apply(this,i),n=r=o=void 0}),t)}}},exports.uuid=function uuid(e="-"){let t=(new Date).getTime();return`xxxxxxxx${e}xxxx${e}4xxx${e}yxxx${e}xxxxxxxxxxxx`.replace(/[xy]/g,(function(e){var n=(t+16*Math.random())%16|0;return t=Math.floor(t/16),("x"==e?n:3&n|8).toString(16)}))};