filters.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. // 数字转汉字方法
  2. export const numToZh = num => {
  3. if (!num && num !== 0) return ''
  4. const chnNumChar = ['零', '一', '二', '三', '四', '五', '六', '七', '八', '九']
  5. const chnUnitSection = ['', '万', '亿', '万亿', '亿亿']
  6. const chnUnitChar = ['', '十', '百', '千']
  7. const numToChn = num => {
  8. const index = num.toString().indexOf('.')
  9. if (index !== -1) {
  10. const str = num.toString().slice(index)
  11. let a = '点'
  12. for (let i = 1; i < str.length; i++) {
  13. a += chnNumChar[parseInt(str[i])]
  14. }
  15. return a
  16. } else {
  17. return ''
  18. }
  19. }
  20. // 定义在每个小节的内部进行转化的方法,其他部分则与小节内部转化方法相同
  21. const sectionToChinese = section => {
  22. let str = ''
  23. let chnstr = ''
  24. let zero = false
  25. let count = 0 // zero为是否进行补零, 第一次进行取余由于为个位数,默认不补零
  26. while (section > 0) {
  27. var v = section % 10 // 对数字取余10,得到的数即为个位数
  28. if (v === 0) {
  29. // 如果数字为零,则对字符串进行补零
  30. if (zero) {
  31. zero = false // 如果遇到连续多次取余都是0,那么只需补一个零即可
  32. chnstr = chnNumChar[v] + chnstr
  33. }
  34. } else {
  35. zero = true // 第一次取余之后,如果再次取余为零,则需要补零
  36. str = chnNumChar[v]
  37. str += chnUnitChar[count]
  38. chnstr = str + chnstr
  39. }
  40. count++
  41. section = Math.floor(section / 10)
  42. }
  43. return chnstr
  44. }
  45. const a = numToChn(num)
  46. num = Math.floor(num)
  47. let unitPos = 0
  48. let strIns = ''
  49. let chnStr = ''
  50. let needZero = false
  51. if (num === 0) {
  52. return chnNumChar[0]
  53. }
  54. while (num > 0) {
  55. const section = num % 10000
  56. if (needZero) {
  57. chnStr = chnNumChar[0] + chnStr
  58. }
  59. strIns = sectionToChinese(section)
  60. strIns += section !== 0 ? chnUnitSection[unitPos] : chnUnitSection[0]
  61. chnStr = strIns + chnStr
  62. needZero = section < 1000 && section > 0
  63. num = Math.floor(num / 10000)
  64. unitPos++
  65. }
  66. let result = chnStr + a
  67. const temp = result.split('')
  68. if (temp[0] === '一' && temp[1] === '十') {
  69. temp.splice(0, 1)
  70. result = temp.join('')
  71. }
  72. return result
  73. }
  74. // 活动类型
  75. export const seasonType = type => {
  76. return type ? '密训营' : '实战营'
  77. }
  78. // 时间戳转换
  79. export function dateFormatter(timestamp, fmt) {
  80. fmt = fmt || "yyyy-MM-dd";
  81. if (typeof timestamp === "string") {
  82. timestamp = timestamp.replace(/-/g, '/')
  83. }
  84. const $this = new Date(timestamp);
  85. const o = {
  86. "M+": $this.getMonth() + 1,
  87. "d+": $this.getDate(),
  88. "h+": $this.getHours(),
  89. "m+": $this.getMinutes(),
  90. "s+": $this.getSeconds(),
  91. "q+": Math.floor(($this.getMonth() + 3) / 3),
  92. S: $this.getMilliseconds()
  93. };
  94. if (/(y+)/.test(fmt)) {
  95. fmt = fmt.replace(
  96. RegExp.$1,
  97. ($this.getFullYear() + "").substr(4 - RegExp.$1.length)
  98. );
  99. }
  100. for (var k in o) {
  101. if (new RegExp("(" + k + ")").test(fmt)) {
  102. fmt = fmt.replace(
  103. RegExp.$1,
  104. RegExp.$1.length === 1 ?
  105. o[k] :
  106. ("00" + o[k]).substr(("" + o[k]).length)
  107. );
  108. }
  109. }
  110. return fmt;
  111. }
  112. // 倒计时
  113. export const countDown = (s1, s2) => {
  114. const _c = n => n < 10 ? `0${n}` : n
  115. let max = Math.max(s1, s2)
  116. let min = Math.min(s1, s2)
  117. const _x = Math.floor((max - min) / 1000)
  118. let d = Math.floor(_x / 86400)
  119. let h = Math.floor((_x % 86400) / 3600)
  120. let m = Math.floor((_x - 86400 * d - h * 3600) / 60)
  121. let s = Math.floor(_x - 86400 * d - h * 3600 - m * 60)
  122. return `${_c(d)}天${_c(h)}时${_c(m)}分${_c(s)}秒`
  123. }
  124. export const getName = value => {
  125. return name.length > 10 ? name.slice(0, 10) + '...' : name;
  126. }
  127. // 活动类型
  128. export const activityType = t => {
  129. t = Number(t)
  130. return t ? '密训营' : '实战营'
  131. }