index.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /*
  2. * @Author: your name
  3. * @Date: 2021-01-12 09:38:09
  4. * @LastEditTime: 2022-01-20 10:37:39
  5. * @LastEditors: Please set LastEditors
  6. * @Description: In User Settings Edit
  7. * @FilePath: \vue3-element-admin\src\utils\map.js
  8. */
  9. /**
  10. * @description:树形结构转一维数组
  11. * @param {*} nodes
  12. * @return {*}
  13. */
  14. export function jsonToArray(nodes) {
  15. let pid = -1;
  16. const toArray = (nodes) => {
  17. let r = [];
  18. if (Array.isArray(nodes)) {
  19. for (let i = 0, l = nodes.length; i < l; i++) {
  20. nodes[i].pid = pid;
  21. r.push(nodes[i]); // 取每项数据放入一个新数组
  22. if (
  23. Array.isArray(nodes[i]["children"]) &&
  24. nodes[i]["children"].length > 0
  25. ) {
  26. // 若存在children则递归调用,把数据拼接到新数组中,并且删除该children
  27. pid = nodes[i].id;
  28. r = r.concat(toArray(nodes[i]["children"]));
  29. delete nodes[i]["children"];
  30. }
  31. }
  32. }
  33. return r;
  34. };
  35. return toArray(nodes);
  36. }
  37. /**
  38. * @description:一维数组转树形结构
  39. * @param {*} treeArray
  40. * @return {*}
  41. */
  42. export function arrayToJson(treeArray) {
  43. var r = [];
  44. var tmpMap = {};
  45. for (var i = 0, l = treeArray.length; i < l; i++) {
  46. //* 以每条数据的id作为obj的key值,数据作为value值存入到一个临时对象里面
  47. tmpMap[treeArray[i]["id"]] = treeArray[i];
  48. }
  49. for (i = 0, l = treeArray.length; i < l; i++) {
  50. var key = tmpMap[treeArray[i]["pid"]];
  51. //*循环每一条数据的pid,假如这个临时对象有这个key值,就代表这个key对应的数据有children,需要Push进去
  52. //*如果这一项数据属于哪个数据的子级
  53. if (key) {
  54. // *如果这个数据没有children
  55. if (!key["children"]) {
  56. key["children"] = [];
  57. key["children"].push(treeArray[i]);
  58. //* 如果这个数据有children
  59. } else {
  60. key["children"].push(treeArray[i]);
  61. }
  62. } else {
  63. //*如果没有这个Key值,就代表找不到属于哪个数据,那就代表没有父级,直接放在最外层
  64. r.push(treeArray[i]);
  65. }
  66. }
  67. return r;
  68. }
  69. /**
  70. * @description 获取节点的所有父节点
  71. * @param {*} tree
  72. * @param {*} func
  73. * @param {*} path
  74. * @return {*}
  75. */
  76. export const treeFindPath = (tree, func, name = "id", path = []) => {
  77. if (!tree) return [];
  78. for (const data of tree) {
  79. //* 这里按照你的需求来存放最后返回的内容吧
  80. path.push(data[name]);
  81. if (func(data)) return path;
  82. if (data.children) {
  83. const findChildren = treeFindPath(data.children, func, name, path);
  84. if (findChildren.length) return findChildren;
  85. }
  86. path.pop();
  87. }
  88. return [];
  89. };
  90. /**
  91. * @description: 拆箱函数,解决tooltip显示问题
  92. * @param {*} obj
  93. * @return {*}
  94. */
  95. export const unwarp = (obj) => obj && (obj.__v_raw || obj.valueOf() || obj);
  96. /**
  97. * @description:获取所有的el-svg-icon组件名
  98. * @param {*}
  99. * @return {*}
  100. */
  101. export const icons = () => {
  102. const components = require("@element-plus/icons-vue");
  103. console.log("🚀 ~ file: map.js ~ line 107 ~ icons ~ e", components);
  104. const names = [];
  105. for (const key in components) {
  106. names.push(components[key].name);
  107. }
  108. return names;
  109. };
  110. /**
  111. * 导出表格数据到 Excel 文件
  112. * @param {Array} tableData - 表格数据
  113. * @param {Array} fieldLabels - 表头组成的数组
  114. * @param {Array} fieldKeys - 列属性名组成的数组
  115. * @param {String} fileName - 导出的文件名
  116. */
  117. export function exportExcel(tableData, fieldLabels, fieldKeys, fileName) {
  118. let dataStr = fieldLabels.toString() + '\r\n';
  119. tableData.forEach(item => {
  120. fieldKeys.forEach(key => {
  121. // 加引号是为了使换行符在单元格内正常显示
  122. dataStr += `"${item[key]}"\t,`;
  123. });
  124. dataStr += '\r\n';
  125. });
  126. // encodeURIComponent 解决中文乱码
  127. const url = "data:text/xls;charset=utf-8,\ufeff" + encodeURIComponent(dataStr);
  128. const link = document.createElement("a");
  129. link.href = url;
  130. link.download = fileName + ".xls";
  131. link.style.display = 'none';
  132. document.body.appendChild(link);
  133. link.click();
  134. document.body.removeChild(link); //释放标签
  135. }