Newer
Older
urbanLifeline_YanAn / src / utils / util.js
@zhangqy zhangqy on 17 Oct 7 KB 首页修改
  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. /**
  32. * 获取uuid
  33. */
  34. export function getUUID() {
  35. return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, (c) => {
  36. return (c === "x" ? (Math.random() * 16) | 0 : "r&0x3" | "0x8").toString(
  37. 16
  38. );
  39. });
  40. }
  41. /**
  42. * 树形数据转换
  43. * @param {*} data
  44. */
  45. export function treeDataTranslate(data) {
  46. const arr = [];
  47. let obj = {};
  48. data.forEach((item) => {
  49. const tmp = {
  50. ...item,
  51. };
  52. if (tmp.children) {
  53. tmp.children = treeDataTranslate(tmp.children);
  54. obj = {
  55. label: tmp.name,
  56. key: tmp.id,
  57. children: tmp.children,
  58. };
  59. } else {
  60. obj = {
  61. label: tmp.name,
  62. key: tmp.id,
  63. };
  64. }
  65. arr.push(obj);
  66. });
  67. return arr;
  68. }
  69. /**
  70. * 去掉多余的children
  71. * @param {*} data
  72. */
  73. export function deleteChildren(data) {
  74. console.log(data);
  75. const arr = [];
  76. let obj = {};
  77. data.forEach((item) => {
  78. const tmp = {
  79. ...item,
  80. };
  81. if (tmp.children.length > 0) {
  82. tmp.children = deleteChildren(tmp.children);
  83. obj = {
  84. label: tmp.areaName,
  85. value: tmp.areaCode,
  86. children: tmp.children,
  87. };
  88. } else {
  89. obj = {
  90. label: tmp.areaName,
  91. value: tmp.areaCode,
  92. };
  93. }
  94. arr.push(obj);
  95. });
  96. return arr;
  97. }
  98. /**
  99. * 时间格式化
  100. * @param {*} time
  101. */
  102. export function formatDate(
  103. time = +new Date(),
  104. dateType = "YYYY-MM-DD hh:mm:ss"
  105. ) {
  106. const timeStamp = +new Date(time);
  107. // 如果转换成数字出错
  108. if (!timeStamp) {
  109. return time;
  110. }
  111. let str;
  112. // 得到时间字符串
  113. const dateStr = new Date(timeStamp);
  114. str = dateType.replace("YYYY", dateStr.getFullYear());
  115. str = str.replace(
  116. "MM",
  117. (dateStr.getMonth() + 1 < 10 ? "0" : "") + (dateStr.getMonth() + 1)
  118. );
  119. str = str.replace(
  120. "DD",
  121. (dateStr.getDate() < 10 ? "0" : "") + dateStr.getDate()
  122. );
  123. str = str.replace(
  124. "hh",
  125. (dateStr.getHours() < 10 ? "0" : "") + dateStr.getHours()
  126. );
  127. str = str.replace(
  128. "mm",
  129. (dateStr.getMinutes() < 10 ? "0" : "") + dateStr.getMinutes()
  130. );
  131. str = str.replace(
  132. "ss",
  133. (dateStr.getSeconds() < 10 ? "0" : "") + dateStr.getSeconds()
  134. );
  135.  
  136. return str;
  137. }
  138. /**
  139. * 下载 blob 流数据
  140. * @param {*} data 流数据
  141. * @param {*} fileName
  142. */
  143. export function downloadBlob(data, fileName) {
  144. // 1.解析 blob 数据 生成 url
  145. const blob = new Blob([data], {
  146. type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet; charset=utf-8",
  147. });
  148. const url = window.URL.createObjectURL(blob);
  149. // 2.创建下载链接+自动下载
  150. const link = document.createElement("a");
  151. link.href = url;
  152. let name = decodeURIComponent(fileName);
  153. link.download = name;
  154. document.body.appendChild(link);
  155. link.click();
  156. window.URL.revokeObjectURL(link);
  157. }
  158. /**
  159. * 表单重置
  160. * @param {*} formObj 表单数据
  161. *
  162. */
  163. export function resetForm(formObj) {
  164. let obj = Object.keys(formObj).forEach((key) => (formObj[key] = null));
  165. return obj;
  166. }
  167.  
  168. export function getDom(key) {
  169. return document.querySelector(key);
  170. }
  171.  
  172. /**
  173. * 转换接口字符串加上括号
  174. * @param {*} formObj 表单数据
  175. *
  176. */
  177. export function convertString(str) {
  178. return str && str.indexOf("/") === -1 ? `(${str})` : "";
  179. }
  180.  
  181. /**
  182. * 判断日期是否过期
  183. * @param {*} formObj 日期字符串
  184. *
  185. */
  186. export function isDateExpired(date) {
  187. var now = new Date();
  188. var date = new Date(date);
  189. if (now.getTime() > date.getTime()) {
  190. return true;
  191. } else {
  192. return false;
  193. }
  194. }
  195.  
  196. //echar自适应设置
  197. export function nowSize(val, initWidth) {
  198. let nowClientWidth = document.documentElement.clientWidth
  199. return val * (nowClientWidth / initWidth);
  200. }
  201.  
  202. //通过id获取对应的名称
  203. export const getChineseFun = (data, key, arrId, name) => {
  204. if (data && data.length > 0 && arrId) {
  205. let arr = arrId.split(",");
  206. let nameArr = [];
  207. for (let i = 0; i < arr.length; i++) {
  208. for (let j = 0; j < data.length; j++) {
  209. if (data[j][key] == arr[i]) {
  210. nameArr.push(data[j][name]);
  211. }
  212. }
  213. }
  214. return nameArr.join(",");
  215. }
  216. };
  217. /**
  218. * 通过key找到在列表中对应的显示
  219. * @param {Object} obj
  220. * @param obj.dataList 数据列表
  221. * @param obj.value 数据的值对应的字段名称 例如 'value'
  222. * @param obj.label 数据的说明对应的字段名称 例如 'label'
  223. * @param obj.data 当前传入的数据值
  224. * @return name 返回当前传入值在数组中对应的名字
  225. */
  226. export const getDataName = (obj) => {
  227. let name = obj.data;
  228. if (Array.isArray(obj.dataList) && obj.dataList.length > 0) {
  229. for (let i = 0; i < obj.dataList.length; i++) {
  230. if (obj.dataList[i][obj.value] == obj.data) {
  231. name = obj.dataList[i][obj.label];
  232. }
  233. }
  234. }
  235. return name;
  236. };
  237. /**
  238. * 递归寻找当前级
  239. */
  240. export const findCurrentTreeObj = (data, target, value) => {
  241. for (let i = 0; i < data.length; i++) {
  242. if (data[i][target] == value) {
  243. return data[i];
  244. } else {
  245. if (data[i].children && data[i].children.length > 0) {
  246. let item = data[i].children;
  247. let res = findCurrentTreeObj(item, target, value);
  248. if (res) {
  249. return res;
  250. }
  251. }
  252. }
  253. }
  254. };
  255.  
  256. // 手机号前3后4
  257. export function phonefromdata(val) {
  258. if (val) {
  259. let regPhoneNum = val.replace(/(\d{3})(\d{4})/, "$1 **** "); //添加空格
  260. return regPhoneNum;
  261. }
  262. }
  263. /**
  264. * 浏览器判断是否全屏
  265. */
  266. export const fullscreenToggel = () => {
  267. if (fullscreenEnable()) {
  268. // exitFullScreen();
  269. } else {
  270. reqFullScreen();
  271. }
  272. };
  273. /**
  274. * esc监听全屏
  275. */
  276. export const listenfullscreen = (callback) => {
  277. function listen() {
  278. callback();
  279. }
  280.  
  281. document.addEventListener("fullscreenchange", function () {
  282. listen();
  283. });
  284. document.addEventListener("mozfullscreenchange", function () {
  285. listen();
  286. });
  287. document.addEventListener("webkitfullscreenchange", function () {
  288. listen();
  289. });
  290. document.addEventListener("msfullscreenchange", function () {
  291. listen();
  292. });
  293. };
  294. /**
  295. * 浏览器判断是否全屏
  296. */
  297. export const fullscreenEnable = () => {
  298. var isFullscreen =
  299. document.isFullScreen ||
  300. document.mozIsFullScreen ||
  301. document.webkitIsFullScreen;
  302. return isFullscreen;
  303. };
  304.  
  305. /**
  306. * 浏览器全屏
  307. */
  308. export const reqFullScreen = () => {
  309. if (document.documentElement.requestFullScreen) {
  310. document.documentElement.requestFullScreen();
  311. } else if (document.documentElement.webkitRequestFullScreen) {
  312. document.documentElement.webkitRequestFullScreen();
  313. } else if (document.documentElement.mozRequestFullScreen) {
  314. document.documentElement.mozRequestFullScreen();
  315. }
  316. };
  317. /**
  318. * 浏览器退出全屏
  319. */
  320. export const exitFullScreen = () => {
  321. if (document.documentElement.requestFullScreen) {
  322. document.exitFullScreen();
  323. } else if (document.documentElement.webkitRequestFullScreen) {
  324. document.webkitCancelFullScreen();
  325. } else if (document.documentElement.mozRequestFullScreen) {
  326. document.mozCancelFullScreen();
  327. }
  328. };
  329. /**
  330. * <p> 获取加载图片地址 </p>
  331. * examples: bg/bg.png
  332. * @param imgName: string
  333. */
  334. export const requireTuChengImg = (imgName) => {
  335. return new URL(`../assets/images/Sponge_screen/TuCheng/${imgName}.png`, import.meta.url).href;
  336. };