index.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  1. <template>
  2. <div class="layer-list-panel" :class="{ 'fix-layer-list-panel': leftPanelHide }">
  3. <div class="layer-list-panel-title">
  4. <div class="title">
  5. <img src="@/assets/image/common/layerIcon.png" alt="" />
  6. <span style="margin-left: 0.08rem">资源列表</span>
  7. </div>
  8. <div @click="onExpandChange" style="cursor: pointer">
  9. <img v-show="isExpanded" src="@/assets/image/common/arrow-up.png" style="width: 14px; height: 12px" />
  10. <img v-show="!isExpanded" src="@/assets/image/common/arrow-up.png" style="transform: rotate(180deg); width: 14px; height: 12px" />
  11. </div>
  12. </div>
  13. <div class="layer-list-panel-content" v-show="isExpanded">
  14. <el-tree ref="treeRef" :data="treeData" show-checkbox node-key="id" :props="defaultProps" @check="onCheckChange" :default-expanded-keys="['1']">
  15. <template #default="{ node, data }">
  16. <span class="custom-tree-node">
  17. <span>{{ node.label }}</span>
  18. <i v-if="data.loading" class="el-icon-loading" style="margin-left: 4px; color: #409eff"></i>
  19. <i v-if="data.error" class="el-icon-warning" style="margin-left: 4px; color: #f56c6c" :title="data.error"></i>
  20. </span>
  21. </template>
  22. </el-tree>
  23. </div>
  24. </div>
  25. </template>
  26. <script>
  27. import * as mars3d from 'mars3d'
  28. let layerCache = {}
  29. export default {
  30. name: 'LayerListView',
  31. data() {
  32. return {
  33. leftPanelHide: false,
  34. isExpanded: true,
  35. defaultProps: { children: 'children', label: 'label' },
  36. treeData: [
  37. {
  38. id: '1',
  39. label: '综合信息',
  40. children: [
  41. {
  42. id: '1-1',
  43. label: '生态区',
  44. children: [
  45. { id: '1-1-1', label: '提防背河坡脚线', meta: { type: 'polyline', url: '/geojson/提防背河坡脚线.geojson' } },
  46. { id: '1-1-2', label: '河道管理范围线', meta: { type: 'polyline', url: '/geojson/河道管理范围线.geojson' } },
  47. { id: '1-1-3', label: '一二级管控区界限', meta: { type: 'polyline', url: '/geojson/一二级管控区界限.geojson' } },
  48. { id: '1-1-4', label: '生态区界限', meta: { type: 'polyline', url: '/geojson/生态区界限.geojson' } },
  49. { id: '1-1-5', label: '城市核心区', meta: { type: 'polyline', url: '/geojson/城市核心区.geojson' } },
  50. { id: '1-1-6', label: '农村区段', meta: { type: 'polyline', url: '/geojson/农村区段.geojson' } },
  51. { id: '1-1-8', label: '城市核心区(右岸)', meta: { type: 'polyline', url: '/geojson/城市核心区右岸.geojson' } },
  52. { id: '1-1-9', label: '农村区段(右岸)', meta: { type: 'polyline', url: '/geojson/农村区段右岸.geojson' } }
  53. ]
  54. },
  55. { id: '1-2', label: '监测设备' },
  56. { id: '1-3', label: '入河排水(污)口' },
  57. { id: '1-4', label: '河道管理站' }
  58. ]
  59. },
  60. {
  61. id: '2',
  62. label: '水文监测',
  63. children: [
  64. { id: '2-1', label: '水文监测点1', meta: { type: 'point', url: '/geojson/sw1.geojson' } },
  65. { id: '2-2', label: '水文监测点2', meta: { type: 'point', url: '/geojson/sw2.geojson' }}
  66. ]
  67. },
  68. {
  69. id: '3',
  70. label: '采砂区',
  71. children: [
  72. { id: '3-1', label: '兴平市宜空采砂区', meta: { type: 'polygon', url: '/geojson/宜空采砂区.geojson' } },
  73. { id: '3-2', label: '兴平市团结采砂区', meta: { type: 'polygon', url: '/geojson/团结采砂区.geojson' } },
  74. { id: '3-3', label: '兴平市汤坊龙兴1区采砂区', meta: { type: 'polygon', url: '/geojson/坊龙兴1区采砂区.geojson' } }
  75. ]
  76. },
  77. {
  78. id: '4',
  79. label: '防汛应急预案',
  80. type: 'plan',
  81. children: [
  82. { id: '4-1', type: 'plan', label: '2025年度渭河兴平段水灾害防御工作方案' },
  83. { id: '4-2', type: 'plan', label: '2025年度渭河水灾害防御工作方案' },
  84. { id: '4-3', type: 'plan', label: '陕西省防汛应急预案' }
  85. ]
  86. }
  87. ],
  88. mainMenu: '',
  89. checkedNodes: []
  90. }
  91. },
  92. created() {
  93. this.mainMenu = this.$route.params.menu
  94. },
  95. mounted() {
  96. this.$globalEventBus.$on('toggleLeftPanel', (val) => {
  97. this.leftPanelHide = val
  98. })
  99. this.$globalEventBus.$on('closePlanDialog', (data) => {
  100. this.checkedNodes = []
  101. const ids = data.map((item) => item.id)
  102. this.cancelCheckNode(ids)
  103. })
  104. },
  105. watch: {
  106. mainMenu: {
  107. handler(val) {
  108. if (val === 'hydrologicInfo') {
  109. this.handleData(true)
  110. } else {
  111. this.handleData(false)
  112. }
  113. }
  114. }
  115. },
  116. methods: {
  117. //关闭防汛预案弹窗后取消选中
  118. cancelCheckNode(ids) {
  119. const currentCheckedKeys = this.$refs.treeRef.getCheckedKeys(true)
  120. const newArray = currentCheckedKeys.filter((item) => !ids.includes(item))
  121. this.$refs.treeRef.setCheckedKeys(newArray)
  122. },
  123. onExpandChange() {
  124. this.isExpanded = !this.isExpanded
  125. },
  126. async onCheckChange(node, checkData) {
  127. const { checkedKeys, checkedNodes } = checkData
  128. const id = node.id
  129. const isChecked = checkedKeys.includes(id)
  130. // 维护选中的预案节点
  131. this.checkedNodes = checkedNodes.filter((item) => item.type === 'plan' && !item.children)
  132. // ================== 选中 ==================
  133. if (isChecked) {
  134. if (node.type === 'plan') {
  135. this.$globalEventBus.$emit('showPlanDialog', { list: this.checkedNodes })
  136. return
  137. }
  138. if (node.children?.length) {
  139. // 递归加载所有叶子节点
  140. await this.loadAllLeafLayers(node.children)
  141. return
  142. }
  143. if (node.meta?.url) {
  144. if (layerCache[id]) {
  145. layerCache[id].show = true
  146. } else {
  147. await this.loadGeoJsonLayer(node)
  148. }
  149. }
  150. }
  151. // ================== 取消选中 ==================
  152. else {
  153. if (node.type === 'plan') {
  154. this.$globalEventBus.$emit('showPlanDialog', { list: this.checkedNodes })
  155. return
  156. }
  157. if (node.children?.length) {
  158. // 递归删除所有叶子节点
  159. this.removeAllLeafLayers(node.children)
  160. return
  161. }
  162. this.removeLayer(id)
  163. }
  164. },
  165. // 批量加载叶子节点
  166. async loadAllLeafLayers(children) {
  167. for (const child of children) {
  168. if (child.children?.length) {
  169. await this.loadAllLeafLayers(child.children)
  170. } else if (child.meta?.url) {
  171. if (layerCache[child.id]) {
  172. layerCache[child.id].show = true
  173. } else {
  174. await this.loadGeoJsonLayer(child)
  175. }
  176. }
  177. }
  178. },
  179. // 批量删除叶子节点
  180. removeAllLeafLayers(children) {
  181. for (const child of children) {
  182. if (child.children?.length) {
  183. this.removeAllLeafLayers(child.children)
  184. } else {
  185. this.removeLayer(child.id)
  186. }
  187. }
  188. },
  189. // 删除图层的通用方法
  190. removeLayer(id) {
  191. if (layerCache[id]) {
  192. try {
  193. window.map.removeLayer(layerCache[id])
  194. } catch (e) {
  195. console.warn(`removeLayer failed: ${id}`, e)
  196. }
  197. delete layerCache[id]
  198. }
  199. },
  200. // 加载GeoJson图层的通用方法
  201. async loadGeoJsonLayer(node) {
  202. const id = node.id
  203. if (layerCache[id]) {
  204. layerCache[id].show = true
  205. return
  206. }
  207. this.$set(node, 'loading', true)
  208. this.$set(node, 'error', '')
  209. try {
  210. const layer = new mars3d.layer.GeoJsonLayer({
  211. id,
  212. name: node.label,
  213. url: node.meta.url,
  214. clampToGround: true,
  215. symbol: this.getStyleByName(node.label),
  216. flyTo: true
  217. })
  218. window.map.addLayer(layer)
  219. this.bindEvent(layer)
  220. layerCache[id] = layer
  221. // 父子关联
  222. const parent = this.$refs.treeRef.getNode(id).parent
  223. if (parent?.data?.id) {
  224. layer.options.parentId = parent.data.id
  225. }
  226. } catch (e) {
  227. this.$set(node, 'error', e.message || '加载失败')
  228. } finally {
  229. this.$set(node, 'loading', false)
  230. }
  231. },
  232. getStyleByName(name) {
  233. if (name === '生态区界限') {
  234. return { type: 'polyline', styleOptions: { color: '#0c5b0f', width: 2 } }
  235. } else if (name === '提防背河坡脚线') {
  236. return { type: 'polyline', styleOptions: { color: '#c53632', width: 2 } }
  237. } else if (name === '河道管理范围线') {
  238. return { type: 'polyline', styleOptions: { color: '#12641c', width: 2 } }
  239. } else if (name === '一二级管控区界限') {
  240. return {
  241. type: 'polyline',
  242. styleOptions: { width: 2, materialType: mars3d.MaterialType.PolylineDash, materialOptions: { color: '#12641c', dashLength: 60 } }
  243. }
  244. } else if (name === '城市核心区' || name === '农村区段') {
  245. return {
  246. type: 'polygon',
  247. styleOptions: {
  248. color: '#194830',
  249. opacity: 0.6,
  250. outline: true,
  251. outlineWidth: 1,
  252. outlineColor: '#57e0a9'
  253. }
  254. }
  255. } else if (name === '城市核心区(右岸)' || name === '农村区段(右岸)') {
  256. return {
  257. type: 'polygon',
  258. styleOptions: {
  259. color: '#368d63',
  260. opacity: 0.3,
  261. outline: true,
  262. outlineWidth: 1,
  263. outlineColor: '#57e0a9'
  264. }
  265. }
  266. } else if (name.indexOf('水文') > -1) {
  267. return {
  268. type: 'billboard',
  269. styleOptions: {
  270. image: require('./image/水文站.png'),
  271. scale: 1,
  272. clampToGround: true,
  273. horizontalOrigin: this.Cesium.HorizontalOrigin.CENTER,
  274. verticalOrigin: this.Cesium.VerticalOrigin.BOTTOM,
  275. pixelOffset: new this.Cesium.Cartesian2(0, -6), // 偏移量
  276. distanceDisplayCondition: new this.Cesium.DistanceDisplayCondition(0.0, 500000) // 按视距显示
  277. }
  278. }
  279. } else if (name.indexOf('采砂区') > -1) {
  280. return {
  281. type: 'polygon',
  282. styleOptions: {
  283. materialType: this.mars3d.MaterialType.Grid,
  284. materialOptions: {
  285. color: '#cc9648',
  286. cellAlpha: 0.5
  287. },
  288. outline: true,
  289. outlineWidth: 1,
  290. outlineColor: '#cc9648'
  291. }
  292. }
  293. }
  294. },
  295. // 处理水文站数据
  296. handleData(isShow) {
  297. const nodeIds = ['2-1', '2-2']
  298. this.$nextTick(() => {
  299. nodeIds.forEach((id) => {
  300. const node = this.$refs.treeRef.getNode(id)
  301. if (!node) return
  302. // 直接修改 checked 状态
  303. this.$refs.treeRef.setChecked(node, isShow)
  304. // 手动触发 check 事件
  305. this.$refs.treeRef.$emit(
  306. 'check',
  307. node.data, // 选中的节点数据
  308. {
  309. checkedKeys: this.$refs.treeRef.getCheckedKeys(),
  310. checkedNodes: this.$refs.treeRef.getCheckedNodes()
  311. }
  312. )
  313. })
  314. })
  315. },
  316. // 绑定点击事件
  317. bindEvent(layer) {
  318. const _that = this
  319. if (layer.id === '2-1' || layer.id === '2-2') {
  320. layer.on(mars3d.EventType.click, function (event) {
  321. _that.$globalEventBus.$emit('clickWaterStation', event)
  322. })
  323. }
  324. }
  325. },
  326. destroyed() {
  327. this.$globalEventBus.$off('closePlanDialog')
  328. this.$globalEventBus.$off('toggleLeftPanel')
  329. // 把图层上绑定的事件也清掉
  330. Object.values(layerCache).forEach((l) => l.off('click'))
  331. }
  332. }
  333. </script>
  334. <style lang="scss" scoped>
  335. .layer-list-panel {
  336. position: absolute;
  337. top: px-to-rem(80);
  338. left: px-to-rem(480);
  339. width: px-to-rem(240);
  340. padding: px-to-rem(10);
  341. background: #1b2535;
  342. border-radius: 6px;
  343. opacity: 0.85;
  344. z-index: 1000;
  345. transition: left 0.3s ease-in-out;
  346. .layer-list-panel-title {
  347. margin-bottom: px-to-rem(10);
  348. height: px-to-rem(30);
  349. line-height: px-to-rem(30);
  350. display: flex;
  351. justify-content: space-between;
  352. align-items: center;
  353. .title {
  354. display: flex;
  355. justify-content: center;
  356. align-items: center;
  357. font-size: px-to-rem(18);
  358. color: #eaf3fe;
  359. }
  360. }
  361. .layer-list-panel-content {
  362. margin: px-to-rem(-10);
  363. padding: px-to-rem(10);
  364. max-height: px-to-rem(340);
  365. overflow: auto;
  366. :deep(.el-tree) {
  367. background: transparent;
  368. color: #fff;
  369. .el-tree-node__content {
  370. height: unset;
  371. }
  372. .el-tree-node__content:hover,
  373. .el-upload-list__item:hover,
  374. .el-tree-node:focus > .el-tree-node__content {
  375. background-color: transparent;
  376. height: unset;
  377. }
  378. .el-tree-node__label {
  379. white-space: wrap;
  380. }
  381. }
  382. .custom-tree-node {
  383. font-size: px-to-rem(16);
  384. overflow: hidden;
  385. white-space: nowrap;
  386. text-overflow: ellipsis;
  387. }
  388. }
  389. }
  390. .fix-layer-list-panel {
  391. left: px-to-rem(20);
  392. }
  393. </style>