UNPKG

@hopeio/utils

Version:
2 lines (1 loc) 2.97 kB
"use strict";var u=Object.defineProperty;var d=(t,n,e)=>n in t?u(t,n,{enumerable:!0,configurable:!0,writable:!0,value:e}):t[n]=e;var i=(t,n,e)=>d(t,typeof n!="symbol"?n+"":n,e);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const m=require("localforage"),g=require("mitt"),s=require("@vueuse/core"),l=t=>new URL(location.href).searchParams.get(t),y=t=>new URLSearchParams(location.hash.split("?")[1]).get(t),h=location.hash!=="",p=t=>{var r;const e=(((r=location.href.split("?")[1])==null?void 0:r.split("&"))||[]).find(o=>o.includes(t));return(e==null?void 0:e.split("=")[1])||""},f=function(){const t=location.search,n={};return t.replace(new RegExp("([^?=&]+)(=([^&]*))?","g"),(e,r,o,a)=>n[r]=a),n};function w(t,n){const e=document.getElementsByTagName("head")[0],r=document.createElement("script");r.type="text/javascript",r.src=t,n&&typeof n=="function"&&(r.onload=function(){(!document.readyState||document.readyState==="complete")&&(n(),r.onload=null)}),e.appendChild(r)}class L{constructor(n){i(this,"storage");this.storage=n,this.storage.config({driver:[this.storage.INDEXEDDB,this.storage.LOCALSTORAGE],name:"pure-admin"})}async setItem(n,e,r=0){return new Promise((o,a)=>{this.storage.setItem(n,{data:e,expires:r?new Date().getTime()+r*60*1e3:0}).then(c=>{o(c.data)}).catch(c=>{a(c)})})}async getItem(n){return new Promise((e,r)=>{this.storage.getItem(n).then(o=>{o&&(o.expires>new Date().getTime()||o.expires===0)?e(o.data):e(null)}).catch(o=>{r(o)})})}async removeItem(n){return new Promise((e,r)=>{this.storage.removeItem(n).then(()=>{e()}).catch(o=>{r(o)})})}async clear(){return new Promise((n,e)=>{this.storage.clear().then(()=>{n()}).catch(r=>{e(r)})})}async keys(){return new Promise((n,e)=>{this.storage.keys().then(r=>{n(r)}).catch(r=>{e(r)})})}}const E=()=>new L(m),P=g();function D(t){return typeof HTMLImageElement<"u"?t instanceof HTMLImageElement:t.tagName.toLowerCase()==="img"}const I=()=>{s.useEventListener(window.document,"keydown",t=>t.key==="F12"&&t.preventDefault()),s.useEventListener(window.document,"contextmenu",t=>t.preventDefault()),s.useEventListener(window.document,"selectstart",t=>t.preventDefault()),s.useEventListener(window.document,"dragstart",t=>D(t==null?void 0:t.target)&&t.preventDefault())};function x(t="报表.xlsx"){return n=>{const e=document.createElement("a");e.download=t,e.style.display="none",e.href=URL.createObjectURL(n),document.body.appendChild(e),e.click(),URL.revokeObjectURL(e.href),document.body.removeChild(e)}}function H(t){return new Promise(n=>{const e=new XMLHttpRequest;e.open("HEAD",t,!0),e.timeout=5e3,e.onreadystatechange=()=>{e.readyState===4&&e.status!==0&&n(!0)},e.onerror=()=>{n(!1)},e.send()})}exports.addPreventDefault=I;exports.download=x;exports.dynamicLoadJs=w;exports.emitter=P;exports.getQueryByName=p;exports.getQueryByNameHash=y;exports.getQueryByNameHistory=l;exports.isHashMode=h;exports.localForage=E;exports.parseQueryString=f;exports.supportIPv6=H;