| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980 |
- /*
- * @Descripttion: 招投标数据接口
- * @Author: vian
- * @Date: 2020-08-17 15:07:31
- */
- // 用于导出的挂载变量,各地区对外接口需要作覆盖它。
- // eg: 导出接口A时,加载了scriptA,此时scriptA的对外接口INTERFACE_EXPORT = { entry }
- // 导出接口B时,加载了scriptB,此时scriptB的对外接口INTERFACE_EXPORT = { entry }
- let INTERFACE_EXPORT = {};
- // 用于导入的挂载变量,同上
- let INTERFACE_IMPORT = {};
- const STD_INTERFACE = (() => {
- 'use strict';
- // 地区配置,key为地区,value为该地区接口的js文件名。注意:相同地区的导入导出接口js文件名称应相同。
- const config = {
- '安徽省@马鞍山': 'anhui_maanshan.js',
- };
- /**
- * 动态加载script
- * 由于后续的接口可能会非常多,一次性加载所有的接口文件完全没必要,而且不可控,很容易导致初次加载速度变慢。
- * 在选定相关地区后,再根据地区对应的script路径,动态加载script
- * 不需要缓存,缓存可能会影响正常操作的性能。而且导入导出时动态获取接口文件的需要的时间,客户是不可感知的。
- * @param {String} path - 需要加载的scipt路径
- * @return {Promise}
- */
- function loadScript(path) {
- return new Promise((resolve, reject) => {
- const body = document.getElementsByTagName('body')[0];
- const script = document.createElement('script');
- script.src = path;
- script.type = 'text/javascript';
- body.appendChild(script);
- script.onload = script.onreadystatechange = function () { // ie、ff触发事件不同,都写上
- if (!this.readyState || this.readyState === 'loaded' || this.readyState === 'complete') {
- script.onload = script.onreadystatechange = null;
- cache[path] = 1;
- resolve();
- }
- };
- script.onerror = function () {
- reject('script加载失败,请稍后重试。');
- };
- });
- }
- const ScriptType = {
- EXPORT: 'export',
- IMPORT: 'import',
- };
- let curArea = '';
- /**
- * 根据地区和脚本类型加载脚本
- * @param {String} area - 地区选项 eg: '安徽省@马鞍山'
- * @param {Number} scriptType - 脚本类型
- * @return {Void}
- */
- async function loadScriptByArea(area, scriptType) {
- if (area === curArea) {
- return;
- }
- curArea = area;
- const path = config[area];
- if (!path) {
- throw new Error(`[${area}]不存在有效script配置。`);
- }
- const fullPath = `/web/building_saas/standard_interface/${scriptType}/${path}`;
- await loadScript(fullPath);
- }
- return {
- ScriptType,
- loadScriptByArea
- }
- })();
|