UNPKG

@hopeio/utils

Version:
146 lines (145 loc) 4.46 kB
var m = Object.defineProperty; var d = (t, n, e) => n in t ? m(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); import u from "localforage"; import h from "mitt"; import { useEventListener as a } from "@vueuse/core"; const L = (t) => new URL(location.href).searchParams.get(t), E = (t) => new URLSearchParams(location.hash.split("?")[1]).get(t), x = location.hash !== "", D = (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]) || ""; }, I = function() { const t = location.search, n = {}; return t.replace( new RegExp("([^?=&]+)(=([^&]*))?", "g"), (e, r, o, s) => n[r] = s ), n; }; function P(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 g { constructor(n) { i(this, "storage"); this.storage = n, this.storage.config({ // 首选IndexedDB作为第一驱动,不支持IndexedDB会自动降级到localStorage(WebSQL被弃用,详情看https://developer.chrome.com/blog/deprecating-web-sql) driver: [this.storage.INDEXEDDB, this.storage.LOCALSTORAGE], name: "pure-admin" }); } /** * @description 将对应键名的数据保存到离线仓库 * @param k 键名 * @param v 键值 * @param m 缓存时间(单位`分`,默认`0`分钟,永久缓存) */ async setItem(n, e, r = 0) { return new Promise((o, s) => { this.storage.setItem(n, { data: e, expires: r ? (/* @__PURE__ */ new Date()).getTime() + r * 60 * 1e3 : 0 }).then((c) => { o(c.data); }).catch((c) => { s(c); }); }); } /** * @description 从离线仓库中获取对应键名的值 * @param k 键名 */ async getItem(n) { return new Promise((e, r) => { this.storage.getItem(n).then((o) => { o && (o.expires > (/* @__PURE__ */ new Date()).getTime() || o.expires === 0) ? e(o.data) : e(null); }).catch((o) => { r(o); }); }); } /** * @description 从离线仓库中删除对应键名的值 * @param k 键名 */ async removeItem(n) { return new Promise((e, r) => { this.storage.removeItem(n).then(() => { e(); }).catch((o) => { r(o); }); }); } /** * @description 从离线仓库中删除所有的键名,重置数据库 */ async clear() { return new Promise((n, e) => { this.storage.clear().then(() => { n(); }).catch((r) => { e(r); }); }); } /** * @description 获取数据仓库中所有的key */ async keys() { return new Promise((n, e) => { this.storage.keys().then((r) => { n(r); }).catch((r) => { e(r); }); }); } } const R = () => new g(u), H = h(); function l(t) { return typeof HTMLImageElement < "u" ? t instanceof HTMLImageElement : t.tagName.toLowerCase() === "img"; } const S = () => { a( window.document, "keydown", (t) => t.key === "F12" && t.preventDefault() ), a(window.document, "contextmenu", (t) => t.preventDefault()), a(window.document, "selectstart", (t) => t.preventDefault()), a( window.document, "dragstart", (t) => l(t == null ? void 0 : t.target) && t.preventDefault() ); }; function U(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 N(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(); }); } export { S as addPreventDefault, U as download, P as dynamicLoadJs, H as emitter, D as getQueryByName, E as getQueryByNameHash, L as getQueryByNameHistory, x as isHashMode, R as localForage, I as parseQueryString, N as supportIPv6 };