promise.js 3.5 KB

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