tree.vue 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518
  1. <template>
  2. <div class="treeData" :style="{ height: height }">
  3. <div class="treeSeach">
  4. <el-input
  5. v-model="filterText"
  6. style="height: 30px"
  7. placeholder="输入关键字过滤"
  8. :suffix-icon="Search"
  9. />
  10. <div class="treeShuax" @click="refreshFn">
  11. <img :src="tree_shuaxing" />
  12. </div>
  13. </div>
  14. <div class="treeMain">
  15. <div class="treeMainTit">
  16. <span>{{showTitle}}</span>
  17. </div>
  18. <!-- <div v-for="node in data" :key="node.id" class="treeMainMsg">
  19. <span>{{node.label}}</span>
  20. <tree-node :node="node.children" :data="data" :show-checkbox="showCheckbox"
  21. :dropdownMenu="dropdownMenu">
  22. </tree-node>
  23. </div> -->
  24. <el-tree
  25. :data="data"
  26. :props="defaultProps"
  27. :default-expanded-keys="defaultExpandedKeys"
  28. highlight-current
  29. icon="none"
  30. :filter-node-method="filterNode"
  31. ref="tree"
  32. @node-click="funCurrentChange"
  33. :show-checkbox="showCheckbox"
  34. @check="funCheckChange"
  35. node-key="id"
  36. :expand-on-click-node="true"
  37. :current-node-key="activeNode"
  38. :class="showCheckbox ? 'elTreeSty' : ''"
  39. >
  40. <template #default="{ node, data }">
  41. <div class="dashedSty">
  42. <span
  43. class="hasMore_shu"
  44. :style="
  45. node.id !==
  46. node.parent.childNodes[node.parent.childNodes.length - 1].id
  47. ? 'height: 30px'
  48. : 'height: 18px'
  49. "
  50. :styel="shuFn(node, data)"
  51. ></span>
  52. <span class="hasMore_heng"></span>
  53. </div>
  54. <el-dropdown
  55. ref="dropdown1"
  56. size="small"
  57. trigger="contextmenu"
  58. @command="funCommand"
  59. :class="
  60. !node.expanded || (node.isLeaf && !node.isCurrent)
  61. ? ''
  62. : 'eldropdownCla'
  63. "
  64. >
  65. <span
  66. class="el-dropdown-link"
  67. :class="
  68. node.isCurrent || activeNode === data.id
  69. ? 'changenodeLabel'
  70. : 'nodeLabel'
  71. "
  72. >
  73. {{ node.label }}
  74. </span>
  75. <span>{{ node.label }}</span>
  76. <template #dropdown v-if="node.label.indexOf('风电场') !== -1">
  77. <el-dropdown-menu>
  78. <el-dropdown-item
  79. class="text-[#409EFF]"
  80. v-if="dropdownMenu.includes('save')"
  81. :command="{ type: 'save', data, node }"
  82. >保存</el-dropdown-item
  83. >
  84. <el-dropdown-item
  85. class="text-[#409EFF]"
  86. v-if="
  87. data.childs &&
  88. data.childs.length &&
  89. dropdownMenu.includes('export')
  90. "
  91. :command="{ type: 'export', data, node }"
  92. >导出
  93. </el-dropdown-item>
  94. <el-dropdown-item
  95. class="text-[#F56C6C]"
  96. v-if="dropdownMenu.includes('delete')"
  97. :command="{ type: 'delete', data, node }"
  98. >删除</el-dropdown-item
  99. >
  100. </el-dropdown-menu>
  101. </template>
  102. </el-dropdown>
  103. </template>
  104. <template #empty>
  105. <div class="nodata">
  106. <img :src="nodata" alt="" />
  107. <p class="nodataText">暂无数据,敬请期待</p>
  108. </div>
  109. </template>
  110. </el-tree>
  111. </div>
  112. </div>
  113. </template>
  114. <script>
  115. import httpRequest from "@/utils/request.js";
  116. import tree_shuaxing from "@/assets/menuImg/tree_shuaxing.png";
  117. import nodata from "@/assets/images/noData.png";
  118. import TreeNode from "./treeStyle.vue";
  119. import treeJson from "./treeJson.json";
  120. import { Search } from "@element-plus/icons-vue";
  121. import { saveAs } from "file-saver";
  122. import { apiGetExportMsg } from "@/api/gengra";
  123. import { DebugCameraPrimitive } from "cesium";
  124. import DAYJS from "dayjs";
  125. export default {
  126. components: {
  127. TreeNode,
  128. },
  129. props: {
  130. data: {
  131. type: Array,
  132. default: () => {
  133. return [];
  134. },
  135. },
  136. height: {
  137. type: String,
  138. default: () => {
  139. return "";
  140. },
  141. },
  142. type: {
  143. type: String,
  144. default: () => {
  145. return "wind";
  146. },
  147. },
  148. currentNodeKey: {
  149. type: String,
  150. default: () => {
  151. return "";
  152. },
  153. },
  154. dropdownMenu: {
  155. type: Array,
  156. default: () => {
  157. return ["export", "delete"];
  158. },
  159. },
  160. showCheckbox: {
  161. type: Boolean,
  162. default: () => {
  163. return false;
  164. },
  165. },
  166. },
  167. data() {
  168. return {
  169. // treeJson: treeJson,
  170. treeJsonArr: [],
  171. filterText: "",
  172. Search: Search,
  173. nodata: nodata,
  174. tree_shuaxing: tree_shuaxing,
  175. defaultProps: {
  176. children: "children",
  177. label: "label",
  178. },
  179. activeNode: "",
  180. showTitle: "",
  181. defaultExpandedKeys: [],
  182. };
  183. },
  184. watch: {
  185. filterText(val) {
  186. this.$refs.tree.filter(val);
  187. },
  188. currentNodeKey(value) {
  189. this.activeNode = value;
  190. const node = this.getTreeItem(value, this.data?.[0]) || null;
  191. this.$refs.tree.setChecked(node, true);
  192. },
  193. data(value) {
  194. this.showTitle = value.length>0 ? value[0].label : "数据"
  195. this.defaultExpandedKeys = this.getDefaultExpandedKey([], value);
  196. },
  197. },
  198. created() {
  199. this.defaultExpandedKeys = this.getDefaultExpandedKey([], this.data || []);
  200. },
  201. mounted() {
  202. // this.treeJsonFn()
  203. },
  204. methods: {
  205. getDefaultExpandedKey(defaultExpandedKey = [], data = []) {
  206. data?.forEach((ele) => {
  207. if (/(\d{1,2})月(\d{1,2})日/.test(ele.label)) {
  208. const monthReg = new RegExp(`${DAYJS().format("MM")}月`);
  209. if (monthReg.test(ele.label)) {
  210. defaultExpandedKey.push(ele.id);
  211. }
  212. } else {
  213. if (ele?.children?.length) {
  214. return this.getDefaultExpandedKey(defaultExpandedKey, ele.children);
  215. }
  216. }
  217. });
  218. return defaultExpandedKey;
  219. },
  220. treeJsonFn() {
  221. this.treeJsonArr = this.funRepeatMap(this.treeJson.data);
  222. },
  223. funRepeatMap(arr, type = "prepare") {
  224. return arr.map((o) => {
  225. if (o.children) {
  226. const findIndex = o.children.findIndex((p) => !!p.type);
  227. if (findIndex !== -1) {
  228. o.childs = o.children;
  229. o.children = [];
  230. }
  231. }
  232. return {
  233. ...o,
  234. children: o.children ? this.funRepeatMap(o.children, type) : [],
  235. };
  236. });
  237. },
  238. getTreeItem(id = "", data = {}) {
  239. if (!id) {
  240. return {};
  241. }
  242. if (data.id === id) {
  243. return data;
  244. } else {
  245. if (data?.children?.length) {
  246. for (let i = 0; i < data.children.length; i++) {
  247. const ele = data.children[i];
  248. if (ele.id === id) {
  249. return ele;
  250. } else {
  251. if (ele.children?.length) {
  252. return this.getTreeItem(id, ele);
  253. } else {
  254. return {};
  255. }
  256. }
  257. }
  258. } else {
  259. return {};
  260. }
  261. }
  262. },
  263. shuFn(node, data) {
  264. // debugger
  265. },
  266. filterNode(value, data) {
  267. if (!value) return true;
  268. return data.label.indexOf(value) !== -1;
  269. },
  270. refreshFn() {
  271. this.$emit("refresh");
  272. },
  273. funCurrentChange(current, currentNode) {
  274. this.activeNode = current?.id || "";
  275. this.$emit("currentChange", {
  276. current,
  277. currentNode,
  278. });
  279. },
  280. async funCommand({ type, data, node }) {
  281. switch (type) {
  282. case "save":
  283. /**该保存功能目前暂用于风电场, combine页 */
  284. if (this.type !== "wind") {
  285. return false;
  286. }
  287. ElMessageBox.confirm("确认保存当前节点的拟合功率?", "保存", {
  288. confirmButtonText: "确认",
  289. cancelButtonText: "取消",
  290. type: "warning",
  291. }).then(async () => {
  292. let saveArr = [];
  293. const repeatArr = (arr, saveArr) => {
  294. for (const unit of arr) {
  295. if (unit.childs.length) {
  296. saveArr.push(...unit.childs.map((o) => o.id));
  297. } else if (unit.children.length) {
  298. repeatArr(unit.children, saveArr);
  299. }
  300. }
  301. };
  302. if (data.childs.length) {
  303. saveArr = data.childs.map((o) => o.id);
  304. } else if (data.children.length) {
  305. repeatArr(data.children, saveArr);
  306. }
  307. let res = {
  308. code: 500,
  309. };
  310. res = await httpRequest.get("/power/fitting/curve/save", {
  311. params: {
  312. ids: saveArr.join(","),
  313. },
  314. }); //删除当前节点
  315. if (res.code === 200) {
  316. ElMessage.success(res.msg);
  317. }
  318. });
  319. break;
  320. case "export":
  321. ElMessageBox.confirm("确认导出当前节点的所有数据?", "导出", {
  322. confirmButtonText: "确认",
  323. cancelButtonText: "取消",
  324. type: "warning",
  325. }).then(() => {
  326. const a = document.createElement("a");
  327. let childs = [];
  328. childs =
  329. this.type === "wind"
  330. ? data.childs.map((o) => o.id)
  331. : data.childs.map((o) => o.path);
  332. const url =
  333. this.type === "wind"
  334. ? "/data/option/download?ids="
  335. : "/export/files?filename=";
  336. a.href =
  337. // config.baseURL + url + childs.join(",");
  338. process.env.VUE_APP_GENERAT_URL + url + childs.join(",");
  339. a.download = "";
  340. a.target = "_blank";
  341. a.click();
  342. // let that = this
  343. // let childs = []
  344. // childs = this.type === 'wind' ? data.childs.map((o) => o.id) : data.childs.map(
  345. // (o) => o.path)
  346. // let params = {
  347. // ids: childs.join(",")
  348. // }
  349. // apiGetExportMsg(params).then(datas => {
  350. // let blob = new Blob([datas], {
  351. // type: `application/vnd.ms-excel;charset=utf-8`
  352. // })
  353. // saveAs(blob)
  354. // }).catch((r) => {
  355. // })
  356. });
  357. break;
  358. case "delete":
  359. ElMessageBox.confirm("确认删除当前节点的所有数据?", "删除", {
  360. confirmButtonText: "确认",
  361. cancelButtonText: "取消",
  362. type: "warning",
  363. }).then(async () => {
  364. let deleteArr = [];
  365. const repeatArr = (arr, deleteArr) => {
  366. for (const unit of arr) {
  367. if (unit.childs.length) {
  368. deleteArr.push(
  369. ...unit.childs.map((o) =>
  370. this.type === "wind" ? o.id : o.path
  371. )
  372. );
  373. } else if (unit.children.length) {
  374. repeatArr(unit.children, deleteArr);
  375. }
  376. }
  377. };
  378. if (data.childs.length) {
  379. deleteArr = data.childs.map((o) =>
  380. this.type === "wind" ? o.id : o.path
  381. );
  382. } else if (data.children.length) {
  383. repeatArr(data.children, deleteArr);
  384. }
  385. let res = {
  386. code: 500,
  387. };
  388. if (this.type === "wind") {
  389. res = await httpRequest.get("/data/option/delete", {
  390. params: {
  391. ids: deleteArr.join(","),
  392. },
  393. }); //删除当前节点
  394. } else {
  395. res = await httpRequest.delete("/delete/files", {
  396. data: {
  397. filename: deleteArr.join(","),
  398. },
  399. }); //删除当前节点
  400. }
  401. if (res.code === 200) {
  402. ElMessage.success(res.msg);
  403. this.$emit("refresh");
  404. }
  405. });
  406. break;
  407. }
  408. },
  409. funCheckChange(current, checkedNodes) {
  410. this.$emit("checkChange", {
  411. current,
  412. checkedNodes,
  413. });
  414. },
  415. },
  416. };
  417. </script>
  418. <style lang="less">
  419. .treeData {
  420. overflow-y: auto;
  421. .eldropdownCla {
  422. .changenodeLabel {
  423. }
  424. .nodeLabel {
  425. }
  426. }
  427. .treeSeach {
  428. width: 100%;
  429. display: flex;
  430. margin-bottom: 5px;
  431. .treeShuax {
  432. cursor: pointer;
  433. margin-left: 5px;
  434. height: 30px;
  435. width: 30px;
  436. background: #5473e8;
  437. border-radius: 2px;
  438. text-align: center;
  439. img {
  440. width: 25px;
  441. height: 25px;
  442. position: relative;
  443. top: 2px;
  444. left: -1px;
  445. }
  446. }
  447. }
  448. .treeMain {
  449. .treeMainTit {
  450. width: calc(100% - 10px);
  451. height: 22px;
  452. background: #1e3f9a;
  453. color: #fff;
  454. font-size: 12px;
  455. padding: 8px 0 0 10px;
  456. }
  457. .treeMainMsg {
  458. span {
  459. font-family: Microsoft YaHei;
  460. font-weight: 400;
  461. font-size: 14px;
  462. color: #121212;
  463. line-height: 30px;
  464. }
  465. }
  466. .el-tree {
  467. background: transparent;
  468. color: #fff;
  469. height: 100%;
  470. .is-current {
  471. width: 100%;
  472. background: none !important;
  473. }
  474. .el-tree__empty-block {
  475. img {
  476. margin-top: 20px;
  477. }
  478. .nodataText {
  479. line-height: 50px;
  480. font-size: 14px;
  481. color: #8c8e91;
  482. }
  483. }
  484. }
  485. }
  486. }
  487. </style>