/* * @Descripttion: 导入接口视图相关 * @Author: vian * @Date: 2020-09-08 09:36:52 */ const IMPORT_VIEW = (() => { const { EXPORT_KIND: { BID_INVITATION } } = window.commonConstants; /** * 根据接口配置文件,设置可被接受的导入文件类型,只导入招标文件 * @param {Object} $file - 文件选择Jquery dom * @return {Void} */ function initFileAccept($file) { const set = new Set(); Object .values(INTERFACE_CONFIG) .forEach(config => { Object .entries(config.fileSuffix) .forEach(([type, suffix]) => { if (+type === BID_INVITATION) { set.add(suffix); } }); }); const accept = [...set].join(','); $file.prop('accept', accept); } // 导入相关事件监听器 let importData = null; let areaKey = ''; function importListener() { // 文件选择变更 $('#interface-import-file').change(function () { importData = null; areaKey = ''; $('#import-rename-input').val(''); $('#import-rename').hide(); const file = $(this)[0].files[0]; $('#interface-import-label').text(file && file.name || '请选择导入文件'); }); // 导入确认事件 $('#interface-import-confirm').click(async function () { try { if (STATE.importing) { return; } STATE.importing = true; const file = $('#interface-import-file')[0].files[0]; if (!file) { throw '请选择导入文件。'; } // 按照地区动态加载导入脚本 const parentArea = $('#import-parent-area').val(); const subArea = $('#import-sub-area').val(); if (!parentArea || !subArea) { throw '请选择有效地区。'; } const curAreaKey = `${parentArea}@${subArea}`; if (!(importData && areaKey && areaKey === curAreaKey)) { areaKey = curAreaKey; await STD_INTERFACE.loadScriptByArea(areaKey, STD_INTERFACE.ScriptType.IMPORT); const onlyImportMatchBills = areaKey === '广东@中山'; importData = await INTERFACE_EXPORT_BASE.extractImportData(INTERFACE_IMPORT.entry, file, areaKey, false, onlyImportMatchBills); } const constructionName = $('#import-rename').is(':visible') ? $('#import-rename-input').val() : importData.name; // 确定建设项目的名称(不允许重复) const sameDepthProjs = getProjs(projTreeObj.tree.selected); const matchedProject = sameDepthProjs.find(node => node.data.name === constructionName); if (matchedProject || !constructionName) { $('#import-rename-input').val(constructionName); $('#import-rename').show(); $('#import-rename-input').focus(); return; } importData.name = constructionName; $('#interface-import-modal').modal('hide'); $.bootstrapLoading.progressStart('导入文件', true); $("#progress_modal_body").text('正在导入接口文件,请稍候……'); //return; // 转换成File实例 const blob = new Blob([JSON.stringify(importData)], { type: 'text/plain;charset=utf-8' }); const key = `${uuid.v1()}.json`; const uploadFile = new File([blob], key); // 上传文件 await projTreeObj.getUploadToken(); await UPLOAD_CDN.uploadSync(uploadFile, key, projTreeObj.uptoken); // 下载并处理文件 await ajaxPost('/pm/import/importInterface', { key }); await importProcessChecking(key, null, (projectData) => handleProjectAfterChecking(projectData)); } catch (err) { console.log(err); alert(err); } finally { projTreeObj.uptoken = null; setTimeout(function () { STATE.importing = false; }, 500); } }); } return { importListener, initFileAccept, }; })(); $(document).ready(() => { $('#interface-import-modal').on('show.bs.modal', () => { $('#import-rename-input').val(''); $('#import-rename').hide(); $('#interface-import-file').val(''); $('#interface-import-label').text('请选择导入文件'); IMPORT_VIEW.initFileAccept($('#interface-import-file')); STD_INTERFACE.initInterfaceAreas($('#import-parent-area'), $('#import-sub-area')); }); IMPORT_VIEW.importListener(); });