promise.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. const qs = require('qs');
  2. const $ = require("jquery");
  3. var Promise = require('./rePromise');
  4. //重写assign方法
  5. if (typeof Object.assign != 'function') {
  6. Object.assign = function (target) {
  7. 'use strict';
  8. if (target == null) {
  9. throw new TypeError('Cannot convert undefined or null to object');
  10. }
  11. target = Object(target);
  12. for (var index = 1; index < arguments.length; index++) {
  13. var source = arguments[index];
  14. if (source != null) {
  15. for (var key in source) {
  16. if (Object.prototype.hasOwnProperty.call(source, key)) {
  17. target[key] = source[key];
  18. }
  19. }
  20. }
  21. }
  22. return target;
  23. };
  24. }
  25. window.console = window.console || (function () {
  26. var c = {}; c.log = c.warn = c.debug = c.info = c.error = c.time = c.dir = c.profile
  27. = c.clear = c.exception = c.trace = c.assert = function () { };
  28. return c;
  29. })();
  30. const config = {
  31. pushInner: '/api/data/push/push',
  32. calculate: '/api/data/calc/calculate',
  33. disclaimer: '/api/data/disclaimerInformation/getDisclaimerInformations',
  34. information: '/api/data/conceptDetail/getConceptDetail',
  35. informationMore: '/api/data/conceptDetail/getConceptDetails',
  36. pushScale: '/api/data/push/pushScale',
  37. getSysSetInfoDatas: '/api/data/sysSet/getSysSetInfoDatas',
  38. getMr: '/api/data/mrv2/getMr',
  39. getVersion: '/api/data/versionInfo/getVersionInfoAlls',
  40. getStaticKnowledge: '/api/data/search/getStaticKnowledge',
  41. getStaticScale: '/api/data/search/getScale',
  42. dictionaryInfo: '/api/data/dictionaryInfo/getList', //字典信息
  43. ruleTypeMap:{ //大数据推送参数featureType对应
  44. '22':'1,2',
  45. '11':'3',
  46. '8':'4,5'
  47. }
  48. }
  49. const imageUrlPrefix = 'http://192.168.2.241:82';
  50. // const getUrlArgObject = function(parm) {
  51. // var query = decodeURI(window.location.search);
  52. // var args = qs.parse(qs.parse(query.substr(1)));
  53. // return args[parm];//返回对象
  54. // }
  55. const getUrlArgObject = function getQueryString(name) {
  56. var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
  57. var reg_rewrite = new RegExp("(^|/)" + name + "/([^/]*)(/|$)", "i");
  58. var r = window.location.search.substr(1).match(reg);
  59. var q = window.location.pathname.substr(1).match(reg_rewrite);
  60. if (r != null) {
  61. return decodeURIComponent(r[2]);
  62. } else if (q != null) {
  63. return decodeURIComponent(q[2]);
  64. } else {
  65. return null;
  66. }
  67. }
  68. const post = function (url, data) {
  69. return new Promise((resolve, reject) => {
  70. $.ajax({
  71. method: 'post',
  72. url: url,
  73. data: JSON.stringify(data),
  74. contentType: "application/json; charset=UTF-8",
  75. beforeSend: function (xmlHttp) {
  76. xmlHttp.setRequestHeader("If-Modified-Since", "0");
  77. xmlHttp.setRequestHeader("Cache-Control", "no-cache");
  78. },
  79. success: function (res) {
  80. resolve({ data: res });
  81. },
  82. error: function (error) {
  83. reject(error);
  84. },
  85. });
  86. });
  87. }
  88. const newinConf = {
  89. width: '600', //窗口的文档显示区的宽度。以像素计。
  90. height: '600', //窗口文档显示区的高度。以像素计。
  91. left: '0', //窗口的 x 坐标。以像素计。
  92. top: '0', //窗口的 y 坐标。以像素计。
  93. openMode: "_blank" //每次都是新窗口打开为_blank,打开同一窗口填写任意字符串
  94. }
  95. const newWindowLocation = `width=${newinConf.width}, height=${newinConf.height}, left=${newinConf.left}, top=${newinConf.top} scrollbars=yes`
  96. const openNewWin = function (url) {
  97. window.open(url, newinConf.openMode, newWindowLocation)
  98. }
  99. //判断浏览器是否为Ie8
  100. const isIe8 = function () {
  101. var DEFAULT_VERSION = 8.0;
  102. var ua = navigator.userAgent.toLowerCase();
  103. var isIE = ua.indexOf("msie") > -1;
  104. var safariVersion;
  105. if (isIE) {
  106. safariVersion = ua.match(/msie ([\d.]+)/)[1];
  107. }
  108. if (safariVersion <= DEFAULT_VERSION) {
  109. return true
  110. }
  111. }
  112. const throttle = function (fn, threshhold) {
  113. var timeout
  114. var start = new Date;
  115. var threshhold = threshhold || 160
  116. return function () {
  117. var context = this, args = arguments, curr = new Date() - 0
  118. clearTimeout(timeout)//总是干掉事件回调
  119. if (curr - start >= threshhold) {
  120. // console.log("now", curr, curr - start)//注意这里相减的结果,都差不多是160左右
  121. fn.apply(context, args) //只执行一部分方法,这些方法是在某个时间段内执行一次
  122. start = curr
  123. } else {
  124. //让方法在脱离事件后也能执行一次
  125. timeout = setTimeout(function () {
  126. fn.apply(context, args)
  127. }, threshhold);
  128. }
  129. }
  130. }
  131. module.exports = {
  132. config,
  133. post,
  134. getUrlArgObject,
  135. imageUrlPrefix,
  136. throttle,
  137. openNewWin,
  138. isIe8
  139. }