promise.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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/mr/getMr',
  39. getMr2: '/api/data/mrv2/getMr',
  40. getVersion: '/api/data/versionInfo/getVersionInfoAlls',
  41. getStaticKnowledge: '/api/data/search/getStaticKnowledge',
  42. getStaticScale: '/api/data/search/getScale',
  43. dictionaryInfo: '/api/data/dictionaryInfo/getList', //字典信息
  44. getPushSet:'/api/data/pushSet/getPushSet', //获取推送配置
  45. ruleTypeMap:{ //大数据推送参数featureType对应
  46. '22':'1,2',
  47. '11':'3',
  48. '8':'4,5'
  49. }
  50. }
  51. const imageUrlPrefix = 'http://192.168.2.121:82';
  52. // const getUrlArgObject = function(parm) {
  53. // var query = decodeURI(window.location.search);
  54. // var args = qs.parse(qs.parse(query.substr(1)));
  55. // return args[parm];//返回对象
  56. // }
  57. const getUrlArgObject = function getQueryString(name) {
  58. var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
  59. var reg_rewrite = new RegExp("(^|/)" + name + "/([^/]*)(/|$)", "i");
  60. var r = window.location.search.substr(1).match(reg);
  61. var q = window.location.pathname.substr(1).match(reg_rewrite);
  62. if (r != null) {
  63. return decodeURIComponent(r[2]);
  64. } else if (q != null) {
  65. return decodeURIComponent(q[2]);
  66. } else {
  67. return null;
  68. }
  69. }
  70. const post = function (url, data) {
  71. return new Promise((resolve, reject) => {
  72. $.ajax({
  73. method: 'post',
  74. url: url,
  75. data: JSON.stringify(data),
  76. contentType: "application/json; charset=UTF-8",
  77. beforeSend: function (xmlHttp) {
  78. xmlHttp.setRequestHeader("If-Modified-Since", "0");
  79. xmlHttp.setRequestHeader("Cache-Control", "no-cache");
  80. },
  81. success: function (res) {
  82. resolve({ data: res });
  83. },
  84. error: function (error) {
  85. reject(error);
  86. },
  87. });
  88. });
  89. }
  90. const newinConf = {
  91. width: '600', //窗口的文档显示区的宽度。以像素计。
  92. height: '600', //窗口文档显示区的高度。以像素计。
  93. left: '0', //窗口的 x 坐标。以像素计。
  94. top: '0', //窗口的 y 坐标。以像素计。
  95. openMode: "_blank" //每次都是新窗口打开为_blank,打开同一窗口填写任意字符串
  96. }
  97. const newWindowLocation = `width=${newinConf.width}, height=${newinConf.height}, left=${newinConf.left}, top=${newinConf.top} scrollbars=yes`
  98. const openNewWin = function (url) {
  99. window.open(url, newinConf.openMode, newWindowLocation)
  100. }
  101. //判断浏览器是否为Ie8
  102. const isIe8 = function () {
  103. var DEFAULT_VERSION = 8.0;
  104. var ua = navigator.userAgent.toLowerCase();
  105. var isIE = ua.indexOf("msie") > -1;
  106. var safariVersion;
  107. if (isIE) {
  108. safariVersion = ua.match(/msie ([\d.]+)/)[1];
  109. }
  110. if (safariVersion <= DEFAULT_VERSION) {
  111. return true
  112. }
  113. }
  114. const throttle = function (fn, threshhold) {
  115. var timeout
  116. var start = new Date;
  117. var threshhold = threshhold || 160
  118. return function () {
  119. var context = this, args = arguments, curr = new Date() - 0
  120. clearTimeout(timeout)//总是干掉事件回调
  121. if (curr - start >= threshhold) {
  122. // console.log("now", curr, curr - start)//注意这里相减的结果,都差不多是160左右
  123. fn.apply(context, args) //只执行一部分方法,这些方法是在某个时间段内执行一次
  124. start = curr
  125. } else {
  126. //让方法在脱离事件后也能执行一次
  127. timeout = setTimeout(function () {
  128. fn.apply(context, args)
  129. }, threshhold);
  130. }
  131. }
  132. }
  133. module.exports = {
  134. config,
  135. post,
  136. getUrlArgObject,
  137. imageUrlPrefix,
  138. throttle,
  139. openNewWin,
  140. isIe8
  141. }