index.wxs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. var WEEK = ['周日', '周一', '周二', '周三', '周四', '周五', '周六']
  2. function datePolyfill(val) {
  3. return val < 10 ? '0' + val : val
  4. }
  5. /**
  6. * 时间戳解析
  7. * @param ts 单位:ms
  8. * @param type
  9. * @returns {string}
  10. */
  11. function formatTs(ts, type) {
  12. var date = getDate(ts)
  13. var obj = {
  14. YYYY: date.getFullYear(),
  15. MM: datePolyfill(date.getMonth() + 1),
  16. DD: datePolyfill(date.getDate()),
  17. HH: datePolyfill(date.getHours()),
  18. mm: datePolyfill(date.getMinutes()),
  19. ss: datePolyfill(date.getSeconds()),
  20. week: WEEK[date.getDay()]
  21. }
  22. if (isNaN(ts)) {
  23. return ''
  24. }
  25. if (type === 'hh:mm:ss') {
  26. return obj.HH + ':' + obj.mm + ':' + obj.ss
  27. }
  28. if (type === 'MM.DD hh:mm') {
  29. return obj.MM + '.' + obj.DD + ' ' + obj.HH + ':' + obj.mm
  30. }
  31. if (type === 'YYYY.MM.DD hh:mm week') {
  32. return obj.YYYY + '.' + obj.MM + '.' + obj.DD + ' ' + obj.HH + ':' + obj.mm + ' ' + obj.week
  33. }
  34. if (type === 'YYYY-MM-DD hh:mm') {
  35. return obj.YYYY + '-' + obj.MM + '-' + obj.DD + ' ' + obj.HH + ':' + obj.mm
  36. }
  37. if (type === 'hh:mm zh') {
  38. return obj.HH + '小时' + obj.mm + '分'
  39. }
  40. }
  41. function formatNumber(n) {
  42. n = n.toString()
  43. return n[1] ? n : '0' + n
  44. }
  45. function cutDownTime(ts, type) {
  46. if (ts < 0) {
  47. return ['0', '00', '00', '00']
  48. }
  49. ts = parseInt(ts)
  50. var D = Math.floor(ts / 24 / 60 / 60)
  51. // 小时位
  52. var h = Math.floor((ts - D * 24 * 60 * 60) / 3600)
  53. // 分钟位
  54. var m = Math.floor((ts - D * 24 * 3600 - h * 3600) / 60)
  55. // 秒位
  56. var s = ts - D * 24 * 3600 - h * 3600 - m * 60
  57. if (type === 'hh:mm:ss') {
  58. return formatNumber(D * 24 + h) + ':' + formatNumber(m) + ':' + formatNumber(s)
  59. }
  60. if (type === 'hh:mm zh') {
  61. return (D * 24 + h) + '小时' + m + '分'
  62. }
  63. return [D, formatNumber(h), formatNumber(m), formatNumber(s)]
  64. }
  65. function computedFormOrderCarCount(list) {
  66. var result = 0
  67. for (var i = 0; i < list.length; i++) {
  68. result += list[i].car_num
  69. }
  70. return result
  71. }
  72. function fen2Yuan(num) {
  73. return isNaN(num) ? '' : (num * 0.01).toFixed(2) * 1
  74. }
  75. function yuan2Fen(num) {
  76. if (isNaN(num)) {
  77. return ''
  78. }
  79. var amount = num.toString()
  80. var index = amount.indexOf('.')
  81. var arr = amount.split('.')
  82. var result = arr[0] * 100
  83. if (index > -1) {
  84. var temp = arr[1].split('')
  85. for (var i = 0; i < temp.length; i++) {
  86. if (i === 0) {
  87. result += temp[i] * 10
  88. } else {
  89. result += temp[i] * 1
  90. }
  91. }
  92. }
  93. return result
  94. }
  95. function fen2YuanAndJiao(num) {
  96. if (isNaN(num)) {
  97. return ''
  98. }
  99. var temp = (num * 0.01).toFixed(2) * 1
  100. var val = temp.toString().split('.')
  101. return {
  102. yuan: val[0],
  103. jiao: val[1] ? '.' + val[1] : ''
  104. }
  105. }
  106. function mToKm(distance) {
  107. if (isNaN(distance)) {
  108. return ''
  109. }
  110. return Math.ceil(distance / 1000) + 'KM'
  111. }
  112. function imgFilter(src) {
  113. return src.indexOf('http') > -1
  114. ? src
  115. : 'https://bashi-1311374120.cos.ap-shanghai.myqcloud.com/wxMini/image' + src
  116. }
  117. function formatProductLable(str) {
  118. if (!str) {
  119. return []
  120. }
  121. return str.split(',')
  122. }
  123. module.exports = {
  124. formatTs: formatTs,
  125. computedFormOrderCarCount: computedFormOrderCarCount,
  126. fen2Yuan: fen2Yuan,
  127. yuan2Fen: yuan2Fen,
  128. fen2YuanAndJiao: fen2YuanAndJiao,
  129. mToKm: mToKm,
  130. cutDownTime: cutDownTime,
  131. imgFilter: imgFilter,
  132. formatProductLable: formatProductLable
  133. }