index.js 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /*
  2. * @Descripttion: 招投标数据接口
  3. * @Author: vian
  4. * @Date: 2020-08-17 15:07:31
  5. */
  6. // 用于导出的挂载变量,各地区对外接口需要作覆盖它。
  7. // eg: 导出接口A时,加载了scriptA,此时scriptA的对外接口INTERFACE_EXPORT = { entry }
  8. // 导出接口B时,加载了scriptB,此时scriptB的对外接口INTERFACE_EXPORT = { entry }
  9. let INTERFACE_EXPORT = {};
  10. // 用于导入的挂载变量,同上
  11. let INTERFACE_IMPORT = {};
  12. const STD_INTERFACE = (() => {
  13. 'use strict';
  14. // 地区配置,key为地区,value为该地区接口的js文件名。注意:相同地区的导入导出接口js文件名称应相同。
  15. const config = {
  16. '安徽省@马鞍山': 'anhui_maanshan.js',
  17. };
  18. /**
  19. * 动态加载script
  20. * 由于后续的接口可能会非常多,一次性加载所有的接口文件完全没必要,而且不可控,很容易导致初次加载速度变慢。
  21. * 在选定相关地区后,再根据地区对应的script路径,动态加载script
  22. * 不需要缓存,缓存可能会影响正常操作的性能。而且导入导出时动态获取接口文件的需要的时间,客户是不可感知的。
  23. * @param {String} path - 需要加载的scipt路径
  24. * @return {Promise}
  25. */
  26. function loadScript(path) {
  27. return new Promise((resolve, reject) => {
  28. const body = document.getElementsByTagName('body')[0];
  29. const script = document.createElement('script');
  30. script.src = path;
  31. script.type = 'text/javascript';
  32. body.appendChild(script);
  33. script.onload = script.onreadystatechange = function () { // ie、ff触发事件不同,都写上
  34. if (!this.readyState || this.readyState === 'loaded' || this.readyState === 'complete') {
  35. script.onload = script.onreadystatechange = null;
  36. cache[path] = 1;
  37. resolve();
  38. }
  39. };
  40. script.onerror = function () {
  41. reject('script加载失败,请稍后重试。');
  42. };
  43. });
  44. }
  45. const ScriptType = {
  46. EXPORT: 'export',
  47. IMPORT: 'import',
  48. };
  49. let curArea = '';
  50. /**
  51. * 根据地区和脚本类型加载脚本
  52. * @param {String} area - 地区选项 eg: '安徽省@马鞍山'
  53. * @param {Number} scriptType - 脚本类型
  54. * @return {Void}
  55. */
  56. async function loadScriptByArea(area, scriptType) {
  57. if (area === curArea) {
  58. return;
  59. }
  60. curArea = area;
  61. const path = config[area];
  62. if (!path) {
  63. throw new Error(`[${area}]不存在有效script配置。`);
  64. }
  65. const fullPath = `/web/building_saas/standard_interface/${scriptType}/${path}`;
  66. await loadScript(fullPath);
  67. }
  68. return {
  69. ScriptType,
  70. loadScriptByArea
  71. }
  72. })();