UNPKG

@bos-alpha/progress

Version:

进度管理

158 lines (157 loc) 4.38 kB
export function calcSize(size) { return size / 100 + 'rem'; } /** * 获取本地 */ export function getItem(name) { var value = localStorage.getItem(name); if (!!value) { try { return JSON.parse(value); } catch (err) { return value; } } return ''; } /** * 存储本地 */ export function setItem(name, value) { var values = (typeof value === 'object' ? JSON.stringify(value) : value); localStorage.setItem(name, values); } /** * 删除本地 */ export function removeItem(name) { localStorage.removeItem(name); } /** * 清空所有本地数据 * */ export function clearItem() { localStorage.clear(); } /** * 生成随机数 */ export function uuid() { function S4() { return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1); } return S4() + S4() + S4() + S4() + S4() + S4() + S4() + S4(); } /** * 转换base64 * */ export function getBase64(file) { return new Promise(function (resolve, reject) { var reader = new FileReader(); reader.readAsDataURL(file); reader.onload = function () { return resolve(reader.result); }; reader.onerror = function (error) { return reject(error); }; }); } /** * 简易路由跳转 * @param path 路径 */ export var redirect = function (path) { window.location.pathname = path; }; /** * object序列化 * @param obj object */ export var obj2Param = function (obj) { return Object.entries(obj) .map(function (_a) { var key = _a[0], value = _a[1]; return "".concat(key, "=").concat(encodeURIComponent(value)); }) .join('&'); }; /** * 文件下载(导出) * @param url 下载链接 * @param fileName 文件名 */ export var linkClick = function (url, fileName) { if (fileName === void 0) { fileName = 'download'; } var tempLink = document.createElement('a'); tempLink.style.display = 'none'; tempLink.href = url; tempLink.setAttribute('download', fileName); if (typeof tempLink.download === 'undefined') { tempLink.setAttribute('target', '_blank'); } document.body.appendChild(tempLink); tempLink.click(); document.body.removeChild(tempLink); window.URL.revokeObjectURL(url); }; // 创建下载 二进制文件 export var createDownload = function (_a) { var data = _a.data, _b = _a.filename, filename = _b === void 0 ? 'user.xls' : _b; var blob = new Blob([data], { type: 'application/octet-stream' }); // @ts-ignore if (typeof window.navigator.msSaveBlob !== 'undefined') { // @ts-ignore window.navigator.msSaveBlob(blob, filename); } else { var blobURL = window.URL.createObjectURL(blob); var tempLink = document.createElement('a'); tempLink.style.display = 'none'; tempLink.href = blobURL; tempLink.setAttribute('download', filename); if (typeof tempLink.download === 'undefined') { tempLink.setAttribute('target', '_blank'); } document.body.appendChild(tempLink); tempLink.click(); document.body.removeChild(tempLink); window.URL.revokeObjectURL(blobURL); } }; // 加载静态资源js和css export var loadJsCss = function (type, id, src) { return new Promise(function (resolve, reject) { var staticEle = document.querySelector("#".concat(id)); if (staticEle) { resolve(id); return; } if (type === 'css') { var cssNode = document.createElement('link'); cssNode.id = id; cssNode.href = src; cssNode.rel = 'stylesheet'; cssNode.onload = function () { resolve(id); }; cssNode.onerror = function () { reject(); }; document.head.appendChild(cssNode); } else { var scriptNode = document.createElement('script'); scriptNode.id = id; scriptNode.src = src; scriptNode.type = 'text/javascript'; scriptNode.onload = function () { resolve(id); }; scriptNode.onerror = function () { reject(); }; document.head.appendChild(scriptNode); } }); };