import mitt from 'mitt'; const bus = mitt(); export default bus; //guid export const guid = () => { const s4 = () => Math.floor((1 + Math.random()) * 0x10000) .toString(16) .substring(1); return (() => `${s4() + s4()}-${s4()}-${s4()}-${s4()}-${s4()}${s4()}${s4()}`)(); }; /** *动态设置img Src 属性 *@param {string} name:图片名 */ export const getImg = (name) => { const path = `/src/assets/Imgs/${name}`; const modules = import.meta.globEager('/src/assets/Imgs/*'); //批量导入静态资源 return modules[path].default; }; /** *动态设置img Src 属性 *@param {string} name:图片名 */ export const getLayerImg = (name) => { const path = `/src/assets/newImgs/layerIcon/${name}`; const modules = import.meta.globEager('/src/assets/newImgs/layerIcon/*'); //批量导入静态资源 return modules[path].default; }; /** *动态设置img Src 属性 *@param {string} name:图片名 */ export const getWeatherImg = (name) => { const path = `/src/assets/newImgs/weather/${name}`; const modules = import.meta.globEager('/src/assets/newImgs/weather/*'); //批量导入静态资源 return modules[path].default; }; /** *动态设置img Src 属性 *@param {string} name:图片名 */ export const getImageUrlForPublic = (name) => { return new URL(`../assets/imgs/${name}.png`, import.meta.url).href; }; /** * render 图标 * */ export function renderIcon(icon) { return () => h(NIcon, null, { default: () => h(icon), }); } /** * 时间格式化 * @param {*} time */ export function formatDate(time = +new Date(), dateType = 'YYYY-MM-DD hh:mm:ss') { const timeStamp = +new Date(time); // 如果转换成数字出错 if (!timeStamp) { return time; } let str; // 得到时间字符串 const dateStr = new Date(timeStamp); str = dateType.replace('YYYY', dateStr.getFullYear()); str = str.replace('MM', (dateStr.getMonth() + 1 < 10 ? '0' : '') + (dateStr.getMonth() + 1)); str = str.replace('DD', (dateStr.getDate() < 10 ? '0' : '') + dateStr.getDate()); str = str.replace('hh', (dateStr.getHours() < 10 ? '0' : '') + dateStr.getHours()); str = str.replace('mm', (dateStr.getMinutes() < 10 ? '0' : '') + dateStr.getMinutes()); str = str.replace('ss', (dateStr.getSeconds() < 10 ? '0' : '') + dateStr.getSeconds()); return str; } /** * 表单重置 * @param {*} formObj 表单数据 * */ export function resetForm(formObj) { let obj = Object.keys(formObj).forEach((key) => (formObj[key] = null)); return obj; } /** * 下载 blob 流数据 * @param {*} data 流数据 * @param {*} fileName */ export function downloadBlob(data, fileName) { // 1.解析 blob 数据 生成 url const blob = new Blob([data], { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8', }); const url = window.URL.createObjectURL(blob); // 2.创建下载链接+自动下载 const link = document.createElement('a'); link.href = url; link.download = fileName; document.body.appendChild(link); link.click(); window.URL.revokeObjectURL(link); }