Newer
Older
DH_Apicture / src / utils / util.js
@zhangqy zhangqy on 29 Nov 6 KB first commit
  1. import mitt from "mitt";
  2.  
  3. const bus = mitt();
  4. export default bus;
  5.  
  6. //guid
  7. export const guid = () => {
  8. const s4 = () =>
  9. Math.floor((1 + Math.random()) * 0x10000)
  10. .toString(16)
  11. .substring(1);
  12. return (() =>
  13. `${s4() + s4()}-${s4()}-${s4()}-${s4()}-${s4()}${s4()}${s4()}`)();
  14. };
  15.  
  16. /**
  17. *动态设置img Src 属性
  18. *@param {string} name:图片名
  19. */
  20. export const getImageUrlForPublic = (name) => {
  21. return new URL(`../assets/images/oneMap/${name}.png`, import.meta.url).href;
  22. };
  23. /**
  24. *动态设置天气img Src 属性
  25. *@param {string} name:图片名
  26. */
  27. export const getImageUrlForWeather = (name) => {
  28. return new URL(`../assets/images/weather/${name}.png`, import.meta.url).href;
  29. };
  30. /**
  31. *动态设置img Src 属性
  32. *@param {string} name:图片名
  33. */
  34. // export const getImg = (name) => {
  35. // const path = `/src/assets/Imgs/${name}`;
  36. // const modules =
  37. // import.meta.globEager("/src/assets/Imgs/*"); //批量导入静态资源
  38. // return modules[path].default;
  39. // };
  40.  
  41. // export const getImg_emergency_pipeNetwork_TuLi = (name) => {
  42. // const path = `/src/assets/Imgs/homePage/emergencyTuLI/${name}`;
  43. // const modules =
  44. // import.meta.globEager("/src/assets/Imgs/homePage/emergencyTuLI/*"); //批量导入静态资源
  45. // return modules[path].default;
  46. // };
  47. /**
  48. * 获取uuid
  49. */
  50. export function getUUID() {
  51. return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, (c) => {
  52. return (c === "x" ? (Math.random() * 16) | 0 : "r&0x3" | "0x8").toString(
  53. 16
  54. );
  55. });
  56. }
  57. /**
  58. * 树形数据转换
  59. * @param {*} data
  60. */
  61. export function treeDataTranslate(data) {
  62. const arr = [];
  63. let obj = {};
  64. data.forEach((item) => {
  65. const tmp = {
  66. ...item,
  67. };
  68. if (tmp.children) {
  69. tmp.children = treeDataTranslate(tmp.children);
  70. obj = {
  71. label: tmp.name,
  72. key: tmp.id,
  73. children: tmp.children,
  74. };
  75. } else {
  76. obj = {
  77. label: tmp.name,
  78. key: tmp.id,
  79. };
  80. }
  81. arr.push(obj);
  82. });
  83. return arr;
  84. }
  85. /**
  86. * 去掉多余的children
  87. * @param {*} data
  88. */
  89. export function deleteChildren(data) {
  90. console.log(data);
  91. const arr = [];
  92. let obj = {};
  93. data.forEach((item) => {
  94. const tmp = {
  95. ...item,
  96. };
  97. if (tmp.children.length > 0) {
  98. tmp.children = deleteChildren(tmp.children);
  99. obj = {
  100. label: tmp.areaName,
  101. value: tmp.areaCode,
  102. children: tmp.children,
  103. };
  104. } else {
  105. obj = {
  106. label: tmp.areaName,
  107. value: tmp.areaCode,
  108. };
  109. }
  110. arr.push(obj);
  111. });
  112. return arr;
  113. }
  114. /**
  115. * 时间格式化
  116. * @param {*} time
  117. */
  118. export function formatDate(
  119. time = +new Date(),
  120. dateType = "YYYY-MM-DD hh:mm:ss"
  121. ) {
  122. const timeStamp = +new Date(time);
  123. // 如果转换成数字出错
  124. if (!timeStamp) {
  125. return time;
  126. }
  127. let str;
  128. // 得到时间字符串
  129. const dateStr = new Date(timeStamp);
  130. str = dateType.replace("YYYY", dateStr.getFullYear());
  131. str = str.replace(
  132. "MM",
  133. (dateStr.getMonth() + 1 < 10 ? "0" : "") + (dateStr.getMonth() + 1)
  134. );
  135. str = str.replace(
  136. "DD",
  137. (dateStr.getDate() < 10 ? "0" : "") + dateStr.getDate()
  138. );
  139. str = str.replace(
  140. "hh",
  141. (dateStr.getHours() < 10 ? "0" : "") + dateStr.getHours()
  142. );
  143. str = str.replace(
  144. "mm",
  145. (dateStr.getMinutes() < 10 ? "0" : "") + dateStr.getMinutes()
  146. );
  147. str = str.replace(
  148. "ss",
  149. (dateStr.getSeconds() < 10 ? "0" : "") + dateStr.getSeconds()
  150. );
  151.  
  152. return str;
  153. }
  154. /**
  155. * 下载 blob 流数据
  156. * @param {*} data 流数据
  157. * @param {*} fileName
  158. */
  159. export function downloadBlob(data, fileName) {
  160. // 1.解析 blob 数据 生成 url
  161. const blob = new Blob([data], {
  162. type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet; charset=utf-8",
  163. });
  164. const url = window.URL.createObjectURL(blob);
  165. // 2.创建下载链接+自动下载
  166. const link = document.createElement("a");
  167. link.href = url;
  168. let name = decodeURIComponent(fileName);
  169. link.download = name;
  170. document.body.appendChild(link);
  171. link.click();
  172. window.URL.revokeObjectURL(link);
  173. }
  174. /**
  175. * 表单重置
  176. * @param {*} formObj 表单数据
  177. *
  178. */
  179. export function resetForm(formObj) {
  180. let obj = Object.keys(formObj).forEach((key) => (formObj[key] = null));
  181. return obj;
  182. }
  183.  
  184. export function getDom(key) {
  185. return document.querySelector(key);
  186. }
  187.  
  188. /**
  189. * 转换接口字符串加上括号
  190. * @param {*} formObj 表单数据
  191. *
  192. */
  193. export function convertString(str) {
  194. return str && str.indexOf("/") === -1 ? `(${str})` : "";
  195. }
  196.  
  197. /**
  198. * 判断日期是否过期
  199. * @param {*} formObj 日期字符串
  200. *
  201. */
  202. export function isDateExpired(date) {
  203. var now = new Date();
  204. var date = new Date(date);
  205. if (now.getTime() > date.getTime()) {
  206. return true;
  207. } else {
  208. return false;
  209. }
  210. }
  211.  
  212. //echar自适应设置
  213. export function nowSize(val,initWidth) {
  214. let nowClientWidth = document.documentElement.clientWidth
  215. return val * (nowClientWidth/initWidth);
  216. }
  217.  
  218. //通过id获取对应的名称
  219. export const getChineseFun = (data, key, arrId, name) => {
  220. if (data && data.length > 0 && arrId) {
  221. let arr = arrId.split(",");
  222. let nameArr = [];
  223. for (let i = 0; i < arr.length; i++) {
  224. for (let j = 0; j < data.length; j++) {
  225. if (data[j][key] == arr[i]) {
  226. nameArr.push(data[j][name]);
  227. }
  228. }
  229. }
  230. return nameArr.join(",");
  231. }
  232. };
  233. /**
  234. * 通过key找到在列表中对应的显示
  235. * @param {Object} obj
  236. * @param obj.dataList 数据列表
  237. * @param obj.value 数据的值对应的字段名称 例如 'value'
  238. * @param obj.label 数据的说明对应的字段名称 例如 'label'
  239. * @param obj.data 当前传入的数据值
  240. * @return name 返回当前传入值在数组中对应的名字
  241. */
  242. export const getDataName = (obj) => {
  243. let name = obj.data;
  244. if (Array.isArray(obj.dataList) && obj.dataList.length > 0) {
  245. for (let i = 0; i < obj.dataList.length; i++) {
  246. if (obj.dataList[i][obj.value] == obj.data) {
  247. name = obj.dataList[i][obj.label];
  248. }
  249. }
  250. }
  251. return name;
  252. };
  253.  
  254. // 降雨级别
  255. export const RainfallLevelFun=(rain)=>{
  256. let sum=Number(rain);
  257. let text='';
  258. switch (true) {
  259. // 江河水位
  260. case sum>200:
  261. text= '特大暴雨'
  262. break;
  263. case sum>100:
  264. text= '大暴雨'
  265. break;
  266. case sum>50:
  267. text= '暴雨'
  268. break;
  269. case sum>25:
  270. text= '大雨'
  271. break;
  272. case sum>10:
  273. text= '中雨'
  274. break;
  275. case sum>0:
  276. text= '小雨'
  277. break;
  278. default:
  279. text= '无雨'
  280. }
  281. return text
  282. }