until.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. export default {
  2. isType(obj) {
  3. //类型判断
  4. var regexp = /\s(\w+)\]/;
  5. var result = regexp.exec(Object.prototype.toString.call(obj))[1];
  6. return result;
  7. },
  8. dateFormat(time) {
  9. const t = new Date(time * 1000);
  10. // 日期格式
  11. const format = 'Y-m-d h:i:s';
  12. const year = t.getFullYear();
  13. // 由于 getMonth 返回值会比正常月份小 1
  14. const month = t.getMonth() + 1;
  15. const day = t.getDate();
  16. const hours = t.getHours();
  17. const minutes = t.getMinutes();
  18. const seconds = t.getSeconds();
  19. const hash = {
  20. Y: year,
  21. m: month,
  22. d: day,
  23. h: hours,
  24. i: minutes,
  25. s: seconds,
  26. };
  27. return format.replace(/\w/g, (o) => {
  28. return hash[o];
  29. });
  30. },
  31. replaceDate(str, flag) {
  32. //时间戳转换
  33. if (str) {
  34. str = str.toString().length == 10 ? str * 1000 : str;
  35. let now = str ? new Date(str) : new Date(),
  36. y = now.getFullYear(),
  37. m = now.getMonth() + 1,
  38. d = now.getDate();
  39. if (flag) {
  40. return y + '-' + (m < 10 ? '0' + m : m) + '-' + (d < 10 ? '0' + d : d);
  41. }
  42. return (
  43. y +
  44. '-' +
  45. (m < 10 ? '0' + m : m) +
  46. '-' +
  47. (d < 10 ? '0' + d : d) +
  48. ' ' +
  49. now.toTimeString().substr(0, 8)
  50. );
  51. } else {
  52. return null;
  53. }
  54. },
  55. replaceDateNoHMS(str) {
  56. //时间戳转换
  57. if (str) {
  58. str = str.toString().length == 10 ? str * 1000 : str;
  59. let now = str ? new Date(str) : new Date(),
  60. y = now.getFullYear(),
  61. m = now.getMonth() + 1,
  62. d = now.getDate();
  63. return y + '-' + (m < 10 ? '0' + m : m) + '-' + (d < 10 ? '0' + d : d);
  64. } else {
  65. return null;
  66. }
  67. },
  68. //时间转换
  69. setDate(time) {
  70. let data = '';
  71. if (Object.prototype.toString.call(time) === '[object Date]') {
  72. data = new Date(time).toLocaleDateString().replace(/\//g, '-');
  73. }
  74. return data;
  75. },
  76. uniquesObjs(obj) {
  77. //去除统一数组中相同的对象
  78. let uniques = [];
  79. let stringify = {};
  80. for (let i = 0; i < obj.length; i++) {
  81. let keys = Object.keys(obj[i]);
  82. keys.sort(function(a, b) {
  83. return Number(a) - Number(b);
  84. });
  85. let str = '';
  86. for (let j = 0; j < keys.length; j++) {
  87. str += JSON.stringify(keys[j]);
  88. str += JSON.stringify(obj[i][keys[j]]);
  89. }
  90. if (!stringify.hasOwnProperty(str)) {
  91. uniques.push(obj[i]);
  92. stringify[str] = true;
  93. }
  94. }
  95. uniques = uniques;
  96. return uniques;
  97. },
  98. throttle(func, wait) {
  99. //节流
  100. let timer = null;
  101. let fn = function() {
  102. if (!timer) {
  103. let args = Array.from(arguments);
  104. timer = setTimeout(() => {
  105. func.apply(this, args);
  106. timer = null;
  107. }, wait);
  108. }
  109. };
  110. return fn;
  111. },
  112. shake(func, t) {
  113. //防抖
  114. let fn = function() {
  115. let args = Array.from(arguments);
  116. clearTimeout(func.id);
  117. func.id = setTimeout(() => {
  118. func.apply(this, args);
  119. }, t);
  120. };
  121. return fn;
  122. },
  123. objToParams(obj) {
  124. let result = '';
  125. for (let i in obj) {
  126. result += `&${i}=${obj[i]}`;
  127. }
  128. result = '?' + result.substr(1, result.length - 1);
  129. return result;
  130. },
  131. computedForm(array, header) {
  132. const result = array.reduce((pre, cur) => {
  133. const data = header.filter(
  134. (row) => cur.is_show && row.serverName == cur.key
  135. );
  136. if (data && data.length == 1) {
  137. return pre.concat(data);
  138. } else {
  139. return pre;
  140. }
  141. }, []);
  142. return result;
  143. },
  144. computedHeader(array, header) {
  145. const result = array.reduce((pre, cur) => {
  146. const data = header.filter((row) => (cur.is_show && row.key == cur.key) && (row.title = cur.value));
  147. if (data && data.length == 1) {
  148. return pre.concat(data);
  149. } else {
  150. return pre;
  151. }
  152. }, []);
  153. return result;
  154. },
  155. };