promise.js 4.4 KB

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