- 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 getImageUrlForPublic = (name) => {
- return new URL(`../assets/images/oneMap/${name}.png`, import.meta.url).href;
- };
- /**
- *动态设置天气img Src 属性
- *@param {string} name:图片名
- */
- export const getImageUrlForWeather = (name) => {
- return new URL(`../assets/images/weather/${name}.png`, import.meta.url).href;
- };
-
- /**
- * 获取uuid
- */
- export function getUUID() {
- return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, (c) => {
- return (c === "x" ? (Math.random() * 16) | 0 : "r&0x3" | "0x8").toString(
- 16
- );
- });
- }
- /**
- * 树形数据转换
- * @param {*} data
- */
- export function treeDataTranslate(data) {
- const arr = [];
- let obj = {};
- data.forEach((item) => {
- const tmp = {
- ...item,
- };
- if (tmp.children) {
- tmp.children = treeDataTranslate(tmp.children);
- obj = {
- label: tmp.name,
- key: tmp.id,
- children: tmp.children,
- };
- } else {
- obj = {
- label: tmp.name,
- key: tmp.id,
- };
- }
- arr.push(obj);
- });
- return arr;
- }
- /**
- * 去掉多余的children
- * @param {*} data
- */
- export function deleteChildren(data) {
- console.log(data);
- const arr = [];
- let obj = {};
- data.forEach((item) => {
- const tmp = {
- ...item,
- };
- if (tmp.children.length > 0) {
- tmp.children = deleteChildren(tmp.children);
- obj = {
- label: tmp.areaName,
- value: tmp.areaCode,
- children: tmp.children,
- };
- } else {
- obj = {
- label: tmp.areaName,
- value: tmp.areaCode,
- };
- }
- arr.push(obj);
- });
- return arr;
- }
- /**
- * 时间格式化
- * @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;
- }
- /**
- * 下载 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;
- let name = decodeURIComponent(fileName);
- link.download = name;
- document.body.appendChild(link);
- link.click();
- window.URL.revokeObjectURL(link);
- }
- /**
- * 表单重置
- * @param {*} formObj 表单数据
- *
- */
- export function resetForm(formObj) {
- let obj = Object.keys(formObj).forEach((key) => (formObj[key] = null));
- return obj;
- }
-
- export function getDom(key) {
- return document.querySelector(key);
- }
-
- /**
- * 转换接口字符串加上括号
- * @param {*} formObj 表单数据
- *
- */
- export function convertString(str) {
- return str && str.indexOf("/") === -1 ? `(${str})` : "";
- }
-
- /**
- * 判断日期是否过期
- * @param {*} formObj 日期字符串
- *
- */
- export function isDateExpired(date) {
- var now = new Date();
- var date = new Date(date);
- if (now.getTime() > date.getTime()) {
- return true;
- } else {
- return false;
- }
- }
-
- //echar自适应设置
- export function nowSize(val, initWidth) {
- let nowClientWidth = document.documentElement.clientWidth
- return val * (nowClientWidth / initWidth);
- }
-
- //通过id获取对应的名称
- export const getChineseFun = (data, key, arrId, name) => {
- if (data && data.length > 0 && arrId) {
- let arr = arrId.split(",");
- let nameArr = [];
- for (let i = 0; i < arr.length; i++) {
- for (let j = 0; j < data.length; j++) {
- if (data[j][key] == arr[i]) {
- nameArr.push(data[j][name]);
- }
- }
- }
- return nameArr.join(",");
- }
- };
- /**
- * 通过key找到在列表中对应的显示
- * @param {Object} obj
- * @param obj.dataList 数据列表
- * @param obj.value 数据的值对应的字段名称 例如 'value'
- * @param obj.label 数据的说明对应的字段名称 例如 'label'
- * @param obj.data 当前传入的数据值
- * @return name 返回当前传入值在数组中对应的名字
- */
- export const getDataName = (obj) => {
- let name = obj.data;
- if (Array.isArray(obj.dataList) && obj.dataList.length > 0) {
- for (let i = 0; i < obj.dataList.length; i++) {
- if (obj.dataList[i][obj.value] == obj.data) {
- name = obj.dataList[i][obj.label];
- }
- }
- }
- return name;
- };
- /**
- * 递归寻找当前级
- */
- export const findCurrentTreeObj = (data, target, value) => {
- for (let i = 0; i < data.length; i++) {
- if (data[i][target] == value) {
- return data[i];
- } else {
- if (data[i].children && data[i].children.length > 0) {
- let item = data[i].children;
- let res = findCurrentTreeObj(item, target, value);
- if (res) {
- return res;
- }
- }
- }
- }
- };
-
- // 手机号前3后4
- export function phonefromdata(val) {
- if (val) {
- let regPhoneNum = val.replace(/(\d{3})(\d{4})/, "$1 **** "); //添加空格
- return regPhoneNum;
- }
- }
- /**
- * 浏览器判断是否全屏
- */
- export const fullscreenToggel = () => {
- if (fullscreenEnable()) {
- // exitFullScreen();
- } else {
- reqFullScreen();
- }
- };
- /**
- * esc监听全屏
- */
- export const listenfullscreen = (callback) => {
- function listen() {
- callback();
- }
-
- document.addEventListener("fullscreenchange", function () {
- listen();
- });
- document.addEventListener("mozfullscreenchange", function () {
- listen();
- });
- document.addEventListener("webkitfullscreenchange", function () {
- listen();
- });
- document.addEventListener("msfullscreenchange", function () {
- listen();
- });
- };
- /**
- * 浏览器判断是否全屏
- */
- export const fullscreenEnable = () => {
- var isFullscreen =
- document.isFullScreen ||
- document.mozIsFullScreen ||
- document.webkitIsFullScreen;
- return isFullscreen;
- };
-
- /**
- * 浏览器全屏
- */
- export const reqFullScreen = () => {
- if (document.documentElement.requestFullScreen) {
- document.documentElement.requestFullScreen();
- } else if (document.documentElement.webkitRequestFullScreen) {
- document.documentElement.webkitRequestFullScreen();
- } else if (document.documentElement.mozRequestFullScreen) {
- document.documentElement.mozRequestFullScreen();
- }
- };
- /**
- * 浏览器退出全屏
- */
- export const exitFullScreen = () => {
- if (document.documentElement.requestFullScreen) {
- document.exitFullScreen();
- } else if (document.documentElement.webkitRequestFullScreen) {
- document.webkitCancelFullScreen();
- } else if (document.documentElement.mozRequestFullScreen) {
- document.mozCancelFullScreen();
- }
- };
- /**
- * <p> 获取加载图片地址 </p>
- * examples: bg/bg.png
- * @param imgName: string
- */
- export const requireTuChengImg = (imgName) => {
- return new URL(`../assets/images/Sponge_screen/TuCheng/${imgName}.png`, import.meta.url).href;
- };