shpAGeojsonMutualism.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. define(['html!templates/menu/baseCalc/shpAGeojsonMutualism',
  2. 'css!styles/tools/toolPublicPopup',
  3. 'css!styles/menu/baseCalc/shpAGeojsonMutualism',
  4. ],
  5. function (tplLayout) {
  6. /**
  7. * 模块状态,用于存储模块的状态 例如:收起,关闭
  8. * @type {Object}
  9. */
  10. var status = {
  11. initialized: false//是否初始化
  12. };
  13. /**
  14. * 模块数据 用于数据存储和外部调用
  15. * @type {Object}
  16. * 数据存放
  17. */
  18. var modValue = {
  19. data: {},
  20. };
  21. /**
  22. * 初始化并订阅事件
  23. * @return {[type]} [description]
  24. */
  25. function open(data) {
  26. if (!status.initialized) {
  27. setLayout();
  28. bindEvent();
  29. subscribe();
  30. status.initialized = true;
  31. } else {
  32. // 重置
  33. reset();
  34. }
  35. modValue.data = data;
  36. $("#shpAGeojsonMutualismModal .title").text(data.label);
  37. ONEMAP.C.publisher.publish({
  38. modName: 'shpAGeojsonMutualism'
  39. }, 'baseCalc:active');
  40. };
  41. function setLayout() {
  42. $('body').append(tplLayout);
  43. //拖拽
  44. $("#shpAGeojsonMutualismModal .popup-ct").dragmove($('#shpAGeojsonMutualismModal'));
  45. };
  46. /**
  47. * 事件绑定
  48. */
  49. function bindEvent() {
  50. $("#shpAGeojsonMutualismModal .close").bind('click', function () {
  51. close();
  52. });
  53. $("#shpAGeojsonMutualismModal .sure").bind('click', function () {
  54. getFile();
  55. });
  56. // $("#shpAGeojsonMutualismModal .openFunc").bind('click', function () {
  57. // openFunc();
  58. // });
  59. // $("#shpAGeojsonMutualismModal .closeFunc").bind('click', function () {
  60. // closeFunc();
  61. // });
  62. };
  63. // function openFunc() {
  64. // addMapClickEvent();
  65. // }
  66. // function closeFunc() {
  67. // removeMapClickEvent();
  68. // }
  69. /**
  70. * 注册监听
  71. * @type {Function}
  72. */
  73. function subscribe() {
  74. ONEMAP.C.publisher.subscribe(remove, 'baseCalc:active');
  75. };
  76. /**
  77. * 关闭模块
  78. * @return {[type]} [description]
  79. */
  80. function remove(options) {
  81. if (options.modName != 'shpAGeojsonMutualism') {
  82. close();
  83. } else {
  84. $("#shpAGeojsonMutualismModal").show();
  85. }
  86. }
  87. function reset() {
  88. $("#shpAGeojsonMutualismForm .outputResult table tr").empty("")
  89. // removeMapClickEvent();
  90. }
  91. function close() {
  92. $("#shpAGeojsonMutualismModal").hide();
  93. reset();
  94. }
  95. /**
  96. * 获取转换后结果
  97. * @private
  98. */
  99. function getFile() {
  100. let file = $("#shpAGeojsonMutualismForm .uploadFile")[0].files[0]
  101. let selectValue = $("#shpAGeojsonMutualismForm .changeBefore").val()
  102. let selectValue2 = $("#shpAGeojsonMutualismForm .changeAfter").val()
  103. let fileType = $("#shpAGeojsonMutualismForm .changeAfterFileType").val()
  104. let encodingFormat = $("#shpAGeojsonMutualismForm .encodingFormat").val()
  105. let form = new FormData();
  106. form.append("file", file)
  107. form.append("selectValue", selectValue)
  108. form.append("selectValue2", selectValue2)
  109. form.append("fileType", fileType)
  110. form.append("outCharsetStr", encodingFormat)
  111. // $.ajax({
  112. // // cache: true,
  113. // type: "post",
  114. // url: "/proxy_ceshiproxy/geoCoordinate",
  115. // data: form,
  116. // // async: false,
  117. // processData: false, // !!!重要必须有该字段
  118. // contentType: false, // !!!重要必须有该字段
  119. // success: function (result) {
  120. // //转换为blob对象,通过a标签来实现文件下载
  121. // const blob = new Blob([result], { type: 'application/zip' })
  122. // const a = document.createElement('a')
  123. // a.href = URL.createObjectURL(blob)
  124. // a.download = '转换后的坐标.zip'
  125. // a.click()
  126. // // if (data.code == 200) {
  127. // // debugger;
  128. // // // setOutputResult(data.content)
  129. // // }
  130. // },
  131. // error: function (error) {
  132. // console.log(error)
  133. // }
  134. // });
  135. // return;
  136. let xhr = new XMLHttpRequest();
  137. xhr.open("POST", "/proxy_ceshiproxy/geoCoordinate");
  138. xhr.send(form);
  139. xhr.responseType = 'blob';
  140. const divElement = document.createElement('div');
  141. divElement.id = "shpAGeojsonLoading";
  142. divElement.style.width = "100%";
  143. divElement.style.height = "100%";
  144. divElement.style.display = "absolute";
  145. document.body.appendChild(divElement);
  146. var load = new Load({
  147. el: '#shpAGeojsonLoading',//绑定元素容器
  148. model: "manual",
  149. // hide:'#app',//隐藏元素选择器 在加载完成前将该选择器下的元素全部进行隐藏 加载完成后再进行显示 起到防止滚动的作用
  150. // bindClass:'.load',//如果只想预加载有指定类名的图片就写这 不加则默认是自动预加载网页中的所有img
  151. // wait:10000,//加载超时时间 默认10000毫秒
  152. // animateTime:'1.5s',//动画淡入淡出时间 默认1.5s 建议不要超过delTime的时间 否则动画未加载完元素就会被彻底删除
  153. // delTime:3000,//完全删除动画元素时间(包括容器) 默认3000毫秒
  154. // callback:()=>{}// 加载完成回调事件
  155. })
  156. xhr.onload = function () {
  157. // if (this.status == 200) {
  158. setTimeout(() => { load.loadOut(); }, 1000);
  159. let blob = this.response;
  160. if (blob && blob.size > 0) {
  161. let fileName = getFileNameFromResponse(xhr.getResponseHeader("content-disposition"));
  162. // 创建一个临时链接并模拟点击进行下载
  163. let a = document.createElement('a');
  164. a.href = window.URL.createObjectURL(blob);
  165. a.download = fileName;
  166. a.click();
  167. } else {
  168. setTimeout(() => { load.loadOut(); }, 1000);
  169. console.error("下载失败");
  170. }
  171. }
  172. };
  173. // 根据响应头获取文件名
  174. function getFileNameFromResponse(contentDisposition) {
  175. var matchResult = /fileName[^;=\n]*=((['"]).*?\2|[^;\n]*)/.exec(contentDisposition);
  176. if (matchResult != null && matchResult[1]) {
  177. return decodeURIComponent(matchResult[1].replace(/['"]/g, ""));
  178. }
  179. return "download";
  180. }
  181. return ONEMAP.M.shpAGeojsonMutualismForm = {
  182. open: open,
  183. };
  184. })