util.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. // 是否为空
  2. const isEmpty = val => {
  3. if (val instanceof Array) {
  4. if (val.length === 0) return true;
  5. } else if (val instanceof Object) {
  6. if (!Object.keys(val).length) return true;
  7. } else {
  8. if (
  9. val === 'null' ||
  10. val === null ||
  11. val === 'undefined' ||
  12. val === undefined ||
  13. val === ''
  14. )
  15. return true;
  16. return false;
  17. }
  18. return false;
  19. };
  20. // 是否为数组
  21. const isArray = arr => {
  22. return typeof arr == 'object' && arr.constructor == Array;
  23. };
  24. // 是否为字符串
  25. const isString = str => {
  26. return typeof str == 'string' && str.constructor == String;
  27. };
  28. // 是否为对象
  29. const isObject = obj => {
  30. return typeof obj == 'object' && obj.constructor == Object;
  31. };
  32. // 是否为数字
  33. const isNumber = num => {
  34. return typeof num == 'number' && num.constructor == Number;
  35. };
  36. // 是否为日期类型
  37. const isDate = date => {
  38. return typeof date == 'object' && date.constructor == Date;
  39. };
  40. // 是否为函数
  41. const isFunction = obj => {
  42. return typeof obj == 'object' && obj.constructor == Function;
  43. };
  44. /** *
  45. * 对象参数转为url参数
  46. * @parmas query 拼接得参数对象
  47. * @parmas isFrist 为true时开头为&
  48. */
  49. export const parse = (query, isFrist = false) => {
  50. let str = Object.keys(query)
  51. .filter(key => !isEmpty(query[key]))
  52. .reduce((result, key) => {
  53. const value = query[key];
  54. // in查询特殊处理
  55. if (Array.isArray(value) && !isEmpty(value)) {
  56. return `${result}&${value.reduce(
  57. (val, cVal) => `${val ? `${val}&` : val}${key}=${cVal}`,
  58. ''
  59. )}`;
  60. }
  61. // between查询做特殊处理
  62. if (typeof value === 'object' && !isEmpty(value)) {
  63. const [start, end] = value;
  64. return `${result}&${key}[]=${start}&${key}[]=${end}`;
  65. }
  66. return `${result}&${key}=${value}`;
  67. }, '');
  68. return isFrist ? str : str.replace(/^&/, '?');
  69. };
  70. const $checkName = str => {
  71. return /^[\u4E00-\u9FA5A-Za-z\s]+(·[\u4E00-\u9FA5A-Za-z]+)*$/.test(str);
  72. };
  73. /** *
  74. * 全局时间转换
  75. * @parmas num 时间戳默认为11位时间戳
  76. * @parmas fmt 默认转换的时间格式
  77. * @parmas all 为true时time为13位时间戳
  78. */
  79. export const $formatDate = (num, fmt = 'YYYY-MM-DD HH:mm', all) => {
  80. num = all ? num : num * 1000;
  81. let date = new Date();
  82. date.setTime(num);
  83. let o = {
  84. 'M+': date.getMonth() + 1,
  85. 'D+': date.getDate(),
  86. 'h+': date.getHours() % 12 === 0 ? 12 : date.getHours() % 12,
  87. 'H+': date.getHours(),
  88. 'm+': date.getMinutes(),
  89. 's+': date.getSeconds(),
  90. 'q+': Math.floor((date.getMonth() + 3) / 3),
  91. S: date.getMilliseconds()
  92. };
  93. let week = {
  94. '0': '\u65e5',
  95. '1': '\u4e00',
  96. '2': '\u4e8c',
  97. '3': '\u4e09',
  98. '4': '\u56db',
  99. '5': '\u4e94',
  100. '6': '\u516d'
  101. };
  102. if (/(Y+)/.test(fmt)) {
  103. fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length));
  104. }
  105. if (/(E+)/.test(fmt)) {
  106. fmt = fmt.replace(
  107. RegExp.$1,
  108. (RegExp.$1.length > 1 ? (RegExp.$1.length > 2 ? '\u661f\u671f' : '\u5468') : '') +
  109. week[date.getDay() + '']
  110. );
  111. }
  112. for (let k in o) {
  113. if (new RegExp('(' + k + ')').test(fmt)) {
  114. fmt = fmt.replace(
  115. RegExp.$1,
  116. RegExp.$1.length === 1 ? o[k] : ('00' + o[k]).substr(('' + o[k]).length)
  117. );
  118. }
  119. }
  120. return fmt;
  121. };
  122. export default {
  123. isEmpty,
  124. parse,
  125. $checkName,
  126. $formatDate
  127. };