index.wxs 3.4 KB

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