UploadingData.vue 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. <template>
  2. <!-- 上传数据弹窗 -->
  3. <el-dialog title="上传数据" :visible.sync="dialogVisible" :before-close="handleClose" width="400px">
  4. <template slot="title">
  5. <div class="dialogTitle">
  6. <div class="dialogTitleIcon"></div>
  7. 请选择你要上传的文件
  8. </div>
  9. </template>
  10. <div id="uploadingBox">
  11. <el-upload
  12. class="upload-demo"
  13. drag
  14. action="https://jsonplaceholder.typicode.com/posts/"
  15. :on-preview="handlePreview"
  16. :on-remove="handleRemove"
  17. :on-success="handleSuccess"
  18. :before-remove="beforeRemove"
  19. :before-upload="beforeAvatarUpload"
  20. multiple
  21. :on-exceed="handleExceed"
  22. :file-list="fileList"
  23. >
  24. <i class="el-icon-upload"></i>
  25. <div class="el-upload__text">将文件拖到此处,或<em>点击上传</em></div>
  26. <div class="el-upload__tip" slot="tip">*仅支持Geojson格式文件,且不超过5MB</div>
  27. </el-upload>
  28. </div>
  29. <span slot="footer" class="dialog-footer">
  30. <el-button @click="clearDialogVisible()">取 消</el-button>
  31. <el-button type="primary" @click="subMitDialogVisible()">确 定</el-button>
  32. </span>
  33. </el-dialog>
  34. </template>
  35. <script>
  36. /**
  37. * 底部菜单(上传数据)组件
  38. * @author: LiuMengxiang
  39. * @Date: 2022年11月21-25日
  40. */
  41. export default {
  42. name: "UploadingData",
  43. components: {},
  44. data() {
  45. return {
  46. // 文件目标路径
  47. filePath: "",
  48. // 上传文件列表
  49. fileList: [],
  50. // 上传数据弹窗显示状态
  51. dialogVisible: false
  52. };
  53. },
  54. mounted() {
  55. // 上传数据事件监听
  56. this.$bus.$off("scsj");
  57. this.$bus.$on("scsj", () => {
  58. this.changeShowBottomMenusStatus();
  59. });
  60. },
  61. destroy() {
  62. // 当容器销毁时,需要停止监听该事件
  63. this.$bus.$off("scsj");
  64. },
  65. props: [],
  66. methods: {
  67. // 当用户点击svg底座时,切换底部菜单显示隐藏状态。
  68. changeShowBottomMenusStatus() {
  69. // 打开弹窗
  70. this.dialogVisible = true;
  71. this.$emit("changeShowBottomMenusStatus", false);
  72. },
  73. // 弹窗关闭询问
  74. handleClose() {
  75. if (this.dialogVisible) {
  76. this.clearDialogVisible();
  77. }
  78. },
  79. // 上传数据关闭
  80. clearDialogVisible() {
  81. // 关闭弹窗
  82. this.dialogVisible = false;
  83. // 修改父级菜单变量(弹窗显示状态和显示底部菜单)
  84. this.$emit("changeShowBottomMenusStatus", true);
  85. },
  86. // 上传数据表单提交
  87. subMitDialogVisible() {
  88. setTimeout(() => {
  89. if (map2DViewer.analysisGroups[`upload_layer_unique`]) {
  90. this.$message.success("数据上传成功!");
  91. map2DViewer.map.fitBounds(map2DViewer.analysisGroups[`upload_layer_unique`].getBounds());
  92. }
  93. let options = {
  94. id: "upload_layer_unique",
  95. label: "我的图层1"
  96. };
  97. this.$bus.$emit("addUploadLayerEvent", options);
  98. }, 1000);
  99. this.clearDialogVisible();
  100. },
  101. // 文件列表移除文件时
  102. handleRemove(file, fileList) {
  103. console.log("删除数据中移除文件", file, fileList);
  104. },
  105. // 点击文件列表中已上传的文件时
  106. handlePreview(file) {
  107. console.log(file);
  108. },
  109. // 文件超出个数限制时
  110. handleExceed(files, fileList) {
  111. // this.$message.warning(
  112. // `当前限制选择 1 个文件,本次选择了 ${files.length} 个文件,共选择了 ${
  113. // files.length + fileList.length
  114. // } 个文件`
  115. // );
  116. },
  117. // 删除文件之前
  118. beforeRemove(file, fileList) {
  119. this.$message.success(`文件${file.name}移除成功!`);
  120. return true;
  121. },
  122. // 文件上传成功时
  123. handleSuccess(response, file, fileList) {
  124. this.getActionData(file);
  125. },
  126. // 根据上传的文件渲染到地图中
  127. uploadRENDER(geoJson) {
  128. // 呈现数据已经成功
  129. let uniqueId = `upload_layer_unique`;
  130. if (!map2DViewer.analysisGroups[uniqueId]) {
  131. map2DViewer.analysisGroups[uniqueId] = L.featureGroup();
  132. map2DViewer.analysisGroups[uniqueId].addTo(map2DViewer.map);
  133. if (!this.$store.state.selectSelectDataMap["singlePolygon"]) {
  134. this.$store.state.selectSelectDataMap["singlePolygon"] = [];
  135. }
  136. let firstPolygon = JSON.parse(JSON.stringify(geoJson));
  137. console.log("上传文件读取到的数据", firstPolygon);
  138. firstPolygon.features.forEach((ele, index) => {
  139. let cid = `myLayer_${index}`;
  140. let geometry = JSON.stringify(ele);
  141. this.$store.state.mapMethodsCollection
  142. .get("RENDER")
  143. .addSinglePolygon(geometry, cid, "rgb(0,255,255)", uniqueId, "我的图层", "土地资源");
  144. });
  145. }
  146. },
  147. // 读取file对象内容
  148. getActionData(file) {
  149. // console.log();
  150. const __this = this;
  151. var reader = new FileReader(); // 新建一个FileReader
  152. reader.readAsText(file.raw, "UTF-8"); // 读取文件
  153. reader.onload = function (evt) {
  154. //读取文件完毕执行此函数
  155. const dataJson = JSON.parse(evt.target.result);
  156. __this.uploadRENDER(dataJson);
  157. };
  158. },
  159. // 上传文件之前
  160. beforeAvatarUpload(file) {
  161. const isJPG = file.name.indexOf(".geojson") === file.name.length - 8;
  162. const isLt5M = file.size / 1024 / 1024 < 5;
  163. if (!isJPG) {
  164. this.$message.error("仅支持Geojson格式文件!");
  165. }
  166. if (!isLt5M) {
  167. this.$message.error("上传文件大小不能超过 5MB!");
  168. }
  169. return isJPG && isLt5M;
  170. }
  171. },
  172. watch: {}
  173. };
  174. </script>
  175. <style lang="less" scoped>
  176. #uploadingBox {
  177. display: flex;
  178. .el-upload *:not(em) {
  179. color: #f2f6fc;
  180. }
  181. .el-upload__tip {
  182. color: #c0c4cc;
  183. }
  184. }
  185. </style>