/*
* @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 = '';
const onlyImportMatchBillsCompilations = ['广东@中山', '浙江@杭州', '广西@广西'];
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 || '请选择导入文件');
});
// 导入确认事件
const excludeAreas = ['山东', '广西'];
const contactMap = {
'山东': '王洪生18660778328',
'广西': '陈叶婷15607807379',
};
$('#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 '请选择有效地区。';
}
if (excludeAreas.includes(parentArea) && userMobile !== '15919278500') {
const message = `尊敬的用户:
感谢您一直以来对纵横的信任和支持,为了更好地坚守初心,持续为您提供高质量的产品与服务,纵横公路养护云造价已升级为大司空云计价。
请联系当地客服免费升级账号,使用大司空云计价继续导出导入电子招投标文件。
当地服务热线:${contactMap[parentArea] || '王洪生18660778328'}
全国服务热线:0756-3850888,企业QQ:800003850
请点链接进入大司空云计价:https://dsk.smartcost.com.cn`;
window.alert(message);
return;
}
const curAreaKey = `${parentArea}@${subArea}`;
if (!(importData && areaKey && areaKey === curAreaKey)) {
areaKey = curAreaKey;
await STD_INTERFACE.loadScriptByArea(areaKey, STD_INTERFACE.ScriptType.IMPORT);
// const onlyImportMatchBills = areaKey === '广东@中山';
const onlyImportMatchBills = onlyImportMatchBillsCompilations.includes(areaKey);
importData = await INTERFACE_EXPORT_BASE.extractImportData(INTERFACE_IMPORT.entry, file, areaKey, false, onlyImportMatchBills, INTERFACE_IMPORT.handleAfterImport);
}
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();
});