define(['html!templates/menu/baseCalc/shpAGeojsonMutualism', 'css!styles/tools/toolPublicPopup', 'css!styles/menu/baseCalc/shpAGeojsonMutualism', ], function (tplLayout) { /** * 模块状态,用于存储模块的状态 例如:收起,关闭 * @type {Object} */ var status = { initialized: false//是否初始化 }; /** * 模块数据 用于数据存储和外部调用 * @type {Object} * 数据存放 */ var modValue = { data: {}, }; /** * 初始化并订阅事件 * @return {[type]} [description] */ function open(data) { if (!status.initialized) { setLayout(); bindEvent(); subscribe(); status.initialized = true; } else { // 重置 reset(); } modValue.data = data; $("#shpAGeojsonMutualismModal .title").text(data.label); ONEMAP.C.publisher.publish({ modName: 'shpAGeojsonMutualism' }, 'baseCalc:active'); }; function setLayout() { $('body').append(tplLayout); //拖拽 $("#shpAGeojsonMutualismModal .popup-ct").dragmove($('#shpAGeojsonMutualismModal')); }; /** * 事件绑定 */ function bindEvent() { $("#shpAGeojsonMutualismModal .close").bind('click', function () { close(); }); $("#shpAGeojsonMutualismModal .sure").bind('click', function () { getFile(); }); // $("#shpAGeojsonMutualismModal .openFunc").bind('click', function () { // openFunc(); // }); // $("#shpAGeojsonMutualismModal .closeFunc").bind('click', function () { // closeFunc(); // }); }; // function openFunc() { // addMapClickEvent(); // } // function closeFunc() { // removeMapClickEvent(); // } /** * 注册监听 * @type {Function} */ function subscribe() { ONEMAP.C.publisher.subscribe(remove, 'baseCalc:active'); }; /** * 关闭模块 * @return {[type]} [description] */ function remove(options) { if (options.modName != 'shpAGeojsonMutualism') { close(); } else { $("#shpAGeojsonMutualismModal").show(); } } function reset() { $("#shpAGeojsonMutualismForm .outputResult table tr").empty("") // removeMapClickEvent(); } function close() { $("#shpAGeojsonMutualismModal").hide(); reset(); } /** * 获取转换后结果 * @private */ function getFile() { let file = $("#shpAGeojsonMutualismForm .uploadFile")[0].files[0] let selectValue = $("#shpAGeojsonMutualismForm .changeBefore").val() let selectValue2 = $("#shpAGeojsonMutualismForm .changeAfter").val() let fileType = $("#shpAGeojsonMutualismForm .changeAfterFileType").val() let encodingFormat = $("#shpAGeojsonMutualismForm .encodingFormat").val() let form = new FormData(); form.append("file", file) form.append("selectValue", selectValue) form.append("selectValue2", selectValue2) form.append("fileType", fileType) form.append("outCharsetStr", encodingFormat) // $.ajax({ // // cache: true, // type: "post", // url: "/proxy_ceshiproxy/geoCoordinate", // data: form, // // async: false, // processData: false, // !!!重要必须有该字段 // contentType: false, // !!!重要必须有该字段 // success: function (result) { // //转换为blob对象,通过a标签来实现文件下载 // const blob = new Blob([result], { type: 'application/zip' }) // const a = document.createElement('a') // a.href = URL.createObjectURL(blob) // a.download = '转换后的坐标.zip' // a.click() // // if (data.code == 200) { // // debugger; // // // setOutputResult(data.content) // // } // }, // error: function (error) { // console.log(error) // } // }); // return; let xhr = new XMLHttpRequest(); xhr.open("POST", "/proxy_ceshiproxy/geoCoordinate"); xhr.send(form); xhr.responseType = 'blob'; const divElement = document.createElement('div'); divElement.id = "shpAGeojsonLoading"; divElement.style.width = "100%"; divElement.style.height = "100%"; divElement.style.display = "absolute"; document.body.appendChild(divElement); var load = new Load({ el: '#shpAGeojsonLoading',//绑定元素容器 model: "manual", // hide:'#app',//隐藏元素选择器 在加载完成前将该选择器下的元素全部进行隐藏 加载完成后再进行显示 起到防止滚动的作用 // bindClass:'.load',//如果只想预加载有指定类名的图片就写这 不加则默认是自动预加载网页中的所有img // wait:10000,//加载超时时间 默认10000毫秒 // animateTime:'1.5s',//动画淡入淡出时间 默认1.5s 建议不要超过delTime的时间 否则动画未加载完元素就会被彻底删除 // delTime:3000,//完全删除动画元素时间(包括容器) 默认3000毫秒 // callback:()=>{}// 加载完成回调事件 }) xhr.onload = function () { // if (this.status == 200) { setTimeout(() => { load.loadOut(); }, 1000); let blob = this.response; if (blob && blob.size > 0) { let fileName = getFileNameFromResponse(xhr.getResponseHeader("content-disposition")); // 创建一个临时链接并模拟点击进行下载 let a = document.createElement('a'); a.href = window.URL.createObjectURL(blob); a.download = fileName; a.click(); } else { setTimeout(() => { load.loadOut(); }, 1000); console.error("下载失败"); } } }; // 根据响应头获取文件名 function getFileNameFromResponse(contentDisposition) { var matchResult = /fileName[^;=\n]*=((['"]).*?\2|[^;\n]*)/.exec(contentDisposition); if (matchResult != null && matchResult[1]) { return decodeURIComponent(matchResult[1].replace(/['"]/g, "")); } return "download"; } return ONEMAP.M.shpAGeojsonMutualismForm = { open: open, }; })