ipink-util
Version:
util.js
2 lines (1 loc) • 4.13 kB
JavaScript
;Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const e=require("./get.js"),r=require("./is.js"),t=require("./env.js"),n=require("./config.js"),o=require("./toast.js"),i=require("./navigation.js"),s=/[!'()*]/g,encodeReserveReplacer=e=>"%"+e.charCodeAt(0).toString(16),u=/%2C/g,encode=e=>encodeURIComponent(e).replace(s,encodeReserveReplacer).replace(u,",");function decode(e){try{return decodeURIComponent(e)}catch(r){}return e}function parseQuery(e){const r={};return(e=e.trim().replace(/^(\?|#|&)/,""))?(e.split("&").forEach((e=>{const t=e.replace(/\+/g," ").split("=")||[],n=decode(t.shift()||""),o=t.length>0?t.join("="):null;void 0===r[n]?r[n]=o:Array.isArray(r[n])?r[n].push(o):r[n]=[r[n],o]})),r):r}function stringifyQuery(e){const r=e?Object.keys(e).map((r=>{const t=e[r];if(void 0===t)return"";if(null===t)return encode(r);if(Array.isArray(t)){const e=[];return t.forEach((t=>{void 0!==t&&(null===t?e.push(encode(r)):"object"==typeof t?e.push(encode(r)+"="+JSON.stringify(t)):e.push(encode(r)+"="+t))})),e.join("&")}return encode(r)+"="+t})).filter((e=>e.length>0)).join("&"):"";return r?`?${r}`:""}const l=stringifyQuery;function delUrlParams(e,t=[]){const n=e.split("?"),o=n[1];if(!o)return e;const i={},s=o.split("&");for(let r=0;r<s.length;r++){let[e,t]=s[r].split("=");i[e]=t}r.t(t)&&(t=[t]);for(let r=0;r<t.length;r++)delete i[t[r]];let u=JSON.stringify(i).replace(/[\"\{\}]/g,"").replace(/\:/g,"=").replace(/\,/g,"&");return n[0]+"?"+u}const a=delUrlParams;const getParamsByScheme=(e,r)=>{const n={[t.i.o]:16,[t.i.u]:18},i={[t.i.o]:"alipays:",[t.i.u]:"wx:"};let s,u={};if(e.startsWith(i[r])){var parseQuery2=e=>e.replace(/^.*?\?/,"").split("&").map((e=>{var r=e.includes("=")?e.indexOf("="):e.length;return[e.slice(0,r),e.slice(r+1)].map(decodeURIComponent)}));for(var[l,a]of parseQuery2(e)){if("appId"==l){if(a.length!=n[r])return void(s=`! 非 ${n[r]} 位 appId '${a}'`)}else if("page"==l)l="path";else{if("query"!=l||r!==t.i.o){s=`! 不支持参数 '${l}' `;continue}var f={};for(var[c,p]of parseQuery2(a))f[c]=p;a=f}u[l]=a}}else s=`! 非 ${i[r]} 开头`;if(s)try{o.l(s)}catch(d){}return u.p?u:""};exports.addNextUrl=(r,t)=>{t=t||{};let n="";n=e.v(),t.m&&(n=encodeURIComponent(a(n,t.m)));let o=t.key||"nextUrl";return-1==r.indexOf(o)&&(r=`${r}${r.indexOf("?")>-1?"&":"?"}${o}=${n}`),r},exports.decode=decode,exports.delUrlParams=delUrlParams,exports.deleteQuery=a,exports.getParamsByScheme=getParamsByScheme,exports.getQueryByUrl=r=>parseQuery((r=r||e.v()||"").split("?")[1]||""),exports.getQueryMember=function getQueryMember(r,t){return parseQuery((t=t||e.v()||"").split("?")[1]||"")[r]},exports.getUrlByQuery=l,exports.jump=(e,r=1)=>{let o,s=n.j(),u=!1;if(s&&(o="undefined"!=typeof uni?2==r?uni.redirectTo:3==r?uni.switchTab:4==r?uni.reLaunch:uni.navigateTo:2==r?wx.redirectTo:3==r?wx.switchTab:4==r?wx.reLaunch:wx.navigateTo),s&&e.startsWith("/"))o({url:e}),u=!0;else if(s&&e.startsWith("tel://")){const[r,t]=e.split("//");let n=t||r;n.indexOf("?")>-1&&(n=n.split("?")[0]),"undefined"!=typeof uni?uni.makePhoneCall({R:n}):wx.makePhoneCall({R:n}),u=!0}else{if(n.h&&"undefined"!=typeof document){const o=t.U();if(e.startsWith("http://")||e.startsWith("https://"))4==r&&(window.location.replace(e),window.location.reload()),n.h.location.href=e,u=!0;else if(e.startsWith("alipays://"))if(t.i.o===o){const r=getParamsByScheme(e,t.i.o);r&&Object.keys(r).length?(i.$({...r,O:e=>{console.log("Navigation.navigateToMiniProgram.ERROR.alipays: ",e)}}),u=!0):u=!1}else n.h&&window.AlipayJSBridge&&(window.location.href=e,u=!0);else if(e.startsWith("wx://")){if(t.i.u===o||t.i.q===o){const r=getParamsByScheme(e,t.i.u);r&&Object.keys(r).length?(i.$({...r,O:e=>{console.log("Navigation.navigateToMiniProgram.ERROR.alipays: ",e)}}),u=!0):u=!1}}else e.startsWith("weixin://")?t.i.u!==o&&t.i.q!==o||(window.location.href=e,u=!0):u=!1;return u}s&&(e.startsWith("https://")||e.startsWith("http://")?(o({url:n.C.I+"?url="+encodeURIComponent(e),O:()=>{"undefined"!=typeof plus&&plus.runtime.openURL(e)}}),u=!0):u=!1)}return u},exports.parseQuery=parseQuery,exports.stringifyQuery=stringifyQuery;