index.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. import { COMMON_SELECT,COMMON_SELECT2 } from '@/utils/select_data'
  2. import { getDictList } from '@/api/common'
  3. /**
  4. * Show plural label if time is plural number
  5. * @param {number} time
  6. * @param {string} label
  7. * @return {string}
  8. */
  9. function pluralize(time, label) {
  10. if (time === 1) {
  11. return time + label
  12. }
  13. return time + label + 's'
  14. }
  15. /**
  16. * @param {number} time
  17. */
  18. export function timeAgo(time) {
  19. const between = Date.now() / 1000 - Number(time)
  20. if (between < 3600) {
  21. return pluralize(~~(between / 60), ' minute')
  22. } else if (between < 86400) {
  23. return pluralize(~~(between / 3600), ' hour')
  24. } else {
  25. return pluralize(~~(between / 86400), ' day')
  26. }
  27. }
  28. /**
  29. * Number formatting
  30. * like 10000 => 10k
  31. * @param {number} num
  32. * @param {number} digits
  33. */
  34. export function numberFormatter(num, digits) {
  35. const si = [
  36. { value: 1e18, symbol: 'E' },
  37. { value: 1e15, symbol: 'P' },
  38. { value: 1e12, symbol: 'T' },
  39. { value: 1e9, symbol: 'G' },
  40. { value: 1e6, symbol: 'M' },
  41. { value: 1e3, symbol: 'k' }
  42. ]
  43. for (let i = 0; i < si.length; i++) {
  44. if (num >= si[i].value) {
  45. return (num / si[i].value).toFixed(digits).replace(/\.0+$|(\.[0-9]*[1-9])0+$/, '$1') + si[i].symbol
  46. }
  47. }
  48. return num.toString()
  49. }
  50. /**
  51. * 10000 => "10,000"
  52. * @param {number} num
  53. */
  54. export function toThousandFilter(num) {
  55. return (+num || 0).toString().replace(/^-?\d+/g, m => m.replace(/(?=(?!\b)(\d{3})+$)/g, ','))
  56. }
  57. /**
  58. * Upper case first char
  59. * @param {String} string
  60. */
  61. export function uppercaseFirst(string) {
  62. return string.charAt(0).toUpperCase() + string.slice(1)
  63. }
  64. /**
  65. * 保留2位小数点
  66. * @param {number} num
  67. * @returns
  68. */
  69. export function numToFixed(num) {
  70. if (!num) return '0.00'
  71. if (isNaN(Number(num))) return num
  72. num = num.toFixed(2)
  73. if (num.includes('.')) {
  74. return num.slice(0, -3).replace(/(\d)(?=(\d{3})+$)/g, '$1,') + num.slice(-3)
  75. }
  76. return num.replace(/(\d)(?=(\d{3})+$)/g, '$1,')
  77. }
  78. /**
  79. * 截取到“日”的日期
  80. * yyyy-MM-dd HH:mm:ss => yyyy-MM-dd
  81. * @param {String} date
  82. */
  83. export function dateToDayFilter(date) {
  84. if (!date) {
  85. return ''
  86. }
  87. return date.slice(0, 10)
  88. }
  89. /**
  90. * 截取到“分”的日期
  91. * yyyy-MM-dd HH:mm:ss => yyyy-MM-dd HH:mm
  92. * @param {String} date
  93. */
  94. export function dateToMinFilter(date) {
  95. if (!date) {
  96. return ''
  97. }
  98. return date.slice(0, 16)
  99. }
  100. /**
  101. * 转换成“年月”的日期
  102. * yyyy-MM-dd HH:mm:ss => yyyy年MM月
  103. */
  104. export function dateToYearMonthFilter(date) {
  105. if (!date) {
  106. return ''
  107. }
  108. return date.slice(0, 4) + '年' + date.slice(5, 7) + '月'
  109. }
  110. /**
  111. * 密码
  112. */
  113. export function passwordFilter(value) {
  114. if (value === null || value === '') return ''
  115. let password = '********'
  116. // for (let i = 0; i < value.length; i++) {
  117. // password += '*'
  118. // }
  119. return password
  120. }
  121. /**
  122. * 订单状态
  123. */
  124. export function ORDER_CURRENT_STATUS_FILTER(value) {
  125. let obj = COMMON_SELECT.ORDER_CURRENT_STATUS.find(o => o.value === value)
  126. return obj ? obj.label : ''
  127. }
  128. export function CODE_CHECK_STATUS_FILTER(value) {
  129. if(value === null || value === '') return ''
  130. let obj = COMMON_SELECT2.CODE_CHECK_STATUS.find(o => o.value == value);
  131. return obj ? obj.label : '';
  132. }