Newer
Older
DH_Apicture / src / utils / util.js
@zhangqy zhangqy 27 days ago 6 KB first commit
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;
};
/**
 *动态设置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;
// };

// export const getImg_emergency_pipeNetwork_TuLi = (name) => {
//     const path = `/src/assets/Imgs/homePage/emergencyTuLI/${name}`;
//     const modules =
//         import.meta.globEager("/src/assets/Imgs/homePage/emergencyTuLI/*"); //批量导入静态资源
//     return modules[path].default;
// };
/**
 * 获取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  RainfallLevelFun=(rain)=>{
  let sum=Number(rain);
  let text='';
        switch (true) {
        // 江河水位
        case sum>200:
          text= '特大暴雨'
          break;
          case sum>100:
          text= '大暴雨'
          break;
          case sum>50:
          text= '暴雨'
          break;
           case sum>25:
          text= '大雨'
          break;
          case sum>10:
          text= '中雨'
          break;
          case sum>0:
          text= '小雨'
          break;
          default:
            text= '无雨'
        }
          return text
}