util.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. import Moment from 'moment'
  2. import { env } from '../static/config'
  3. export default {
  4. stopPrevent (event) {
  5. let e = event || window.event
  6. if (e.preventDefault) {
  7. e.preventDefault()
  8. } else {
  9. window.event.returnValue = false // IE
  10. }
  11. },
  12. // arrayFilter: 数组去重
  13. arrayFilter (array) {
  14. let newAray = []
  15. for (let i = 0, len = array.length - 1; i <= len; i++) {
  16. if (newAray.indexOf(array[i]) < 0) {
  17. newAray.push(array[i])
  18. }
  19. }
  20. return newAray
  21. },
  22. /**
  23. *
  24. * @param start 开始展示的字符
  25. * @param end 结束字符展示位置
  26. * @param target 目标字符
  27. * @param length
  28. * @returns {string|string}
  29. */
  30. replaceStr (target, start, end, length) {
  31. let str = ''
  32. if (start) {
  33. str = target.substr(start, length) + '***'
  34. } else if (end) {
  35. str = '***' + target.substr(end, length)
  36. } else {
  37. str = target.substr(0, 1) + '***' + target.substr(target.length - 1, 1)
  38. }
  39. return str
  40. },
  41. /**
  42. *
  43. * @param date 日期
  44. * @param format 显示的格式
  45. * @returns {string}
  46. */
  47. formatDate (date, format) {
  48. if (date) {
  49. return Moment(date).format(format)
  50. }
  51. },
  52. padLeftZero (str) {
  53. return ('00' + str).substr(str.length)
  54. },
  55. /**
  56. * 格式化价格(保留两位小数)
  57. * @param price 价格
  58. * @returns {string}
  59. */
  60. currencyPrice (price) {
  61. return parseFloat(price).toFixed(2)
  62. },
  63. /**
  64. * 是否是数值
  65. * @param value 要判断的参数值
  66. * @returns {boolean}
  67. */
  68. isNumber (value) {
  69. let reg = new RegExp('^[0-9]*$')
  70. if (!reg.test(value)) {
  71. return false
  72. }
  73. return true
  74. },
  75. /**
  76. *
  77. * @param beginAt 开始时间(时间戳)
  78. * @param endAt 结束时间(时间戳)
  79. * @returns {number}
  80. */
  81. activityStatus (beginAt, endAt) {
  82. let status = -1 // (0: 未开始;1: 进行中;2: 已过期)
  83. let timestamp = Date.parse(new Date()) / 1000
  84. if (beginAt > timestamp) {
  85. status = 0
  86. } else if (timestamp > beginAt && timestamp < endAt) {
  87. status = 1
  88. } else if (timestamp > endAt) {
  89. status = 2
  90. }
  91. return status
  92. },
  93. /**
  94. * 把秒数换为*天*时*分*秒的时间格式
  95. * @param interval 时间间隔(单位为s)
  96. * @returns {string|*}
  97. */
  98. formatTimeInterval (interval) {
  99. let format = null
  100. let day = parseInt(interval / 60.0 / 60.0 / 24.0)
  101. let hour = parseInt(interval / 60 / 60 % 24)
  102. let minute = parseInt(interval / 60 % 60)
  103. let second = interval % 60
  104. format = day + ' 天 ' + hour + ' 时 ' + minute + ' 分 ' + second + ' 秒'
  105. return format
  106. },
  107. /**
  108. * 获取未读消息数
  109. * @param zhiManager
  110. * @param scoped
  111. * @param key
  112. */
  113. getunreadCount (zhiManager, scoped, key) {
  114. zhiManager.on('unread.count', function (data) {
  115. })
  116. zhiManager.on('receivemessage', function (ret) {
  117. scoped.key = ret
  118. })
  119. },
  120. /**
  121. *
  122. * @param timestamp 时间戳转化为日期
  123. * @returns {string}
  124. */
  125. timestampToTime (timestamp) {
  126. var date = new Date(timestamp * 1000) // 时间戳为10位需*1000,时间戳为13位的话不需乘1000
  127. var Y = date.getFullYear() + '-'
  128. var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-'
  129. var D = date.getDate() + ' '
  130. var h = date.getHours() + ':'
  131. var m = date.getMinutes() + ':'
  132. var s = date.getSeconds()
  133. return Y + M + D + h + m + s
  134. },
  135. getUrlKey: function (url, name) {
  136. return decodeURIComponent((new RegExp('[?|&]' + name + '=' + '([^&;]+?)(&|#|;|$)').exec(url) || [, ''])[1].replace(/\+/g, '%20')) || null
  137. },
  138. }