Foundation.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /**
  2. * 一些常用的基础方法
  3. * unixToDate 将unix时间戳转换为指定格式
  4. * dateToUnix 将时间转unix时间戳
  5. * deepClone 对一个对象进行深拷贝
  6. * formatPrice 货币格式化
  7. * secrecyMobile 手机号隐私保护
  8. * randomString 随机生成指定长度的字符串
  9. */
  10. /**
  11. * 将unix时间戳转换为指定格式
  12. * @param unix 时间戳【秒】
  13. * @param format 转换格式
  14. * @returns {*|string}
  15. */
  16. export function unixToDate(unix, format) {
  17. if (!unix) return unix
  18. let _format = format || 'yyyy-MM-dd hh:mm:ss'
  19. const d = new Date(unix * 1000)
  20. const o = {
  21. 'M+': d.getMonth() + 1,
  22. 'd+': d.getDate(),
  23. 'h+': d.getHours(),
  24. 'm+': d.getMinutes(),
  25. 's+': d.getSeconds(),
  26. 'q+': Math.floor((d.getMonth() + 3) / 3),
  27. S: d.getMilliseconds()
  28. }
  29. if (/(y+)/.test(_format)) _format = _format.replace(RegExp.$1, (d.getFullYear() + '').substr(4 - RegExp.$1.length))
  30. for (const k in o) if (new RegExp('(' + k + ')').test(_format)) _format = _format.replace(RegExp.$1, (RegExp.$1.length === 1) ? (o[k]) : (('00' + o[k]).substr(('' + o[k]).length)))
  31. return _format
  32. }
  33. /**
  34. * 将时间转unix时间戳
  35. * @param date
  36. * @returns {number} 【秒】
  37. */
  38. export function dateToUnix(date) {
  39. let newStr = date.replace(/:/g, '-')
  40. newStr = newStr.replace(/ /g, '-')
  41. const arr = newStr.split('-')
  42. const datum = new Date(Date.UTC(
  43. arr[0],
  44. arr[1] - 1,
  45. arr[2],
  46. arr[3] - 8 || -8,
  47. arr[4] || 0,
  48. arr[5] || 0
  49. ))
  50. return parseInt(datum.getTime() / 1000)
  51. }
  52. /**
  53. * 对一个对象进行深拷贝
  54. * @param object
  55. * @returns {*}
  56. */
  57. export function deepClone(object) {
  58. let str
  59. let newobj = object.constructor === Array ? [] : {}
  60. if (typeof object !== 'object') {
  61. return object
  62. } else if (window.JSON) {
  63. str = JSON.stringify(object)
  64. newobj = JSON.parse(str)
  65. } else {
  66. for (const i in object) {
  67. if (object.hasOwnProperty(i)) {
  68. newobj[i] = typeof object[i] === 'object' ? deepClone(object[i]) : object[i]
  69. }
  70. }
  71. }
  72. return newobj
  73. }
  74. /**
  75. * 货币格式化
  76. * @param price
  77. * @returns {string}
  78. */
  79. export function formatPrice(price) {
  80. if (typeof price !== 'number') return price
  81. return String(Number(price).toFixed(2)).replace(/\B(?=(\d{3})+(?!\d))/g, ',')
  82. }
  83. /**
  84. * 手机号隐私保护
  85. * 隐藏中间四位数字
  86. * @param mobile
  87. * @returns {*}
  88. */
  89. export function secrecyMobile(mobile) {
  90. mobile = String(mobile)
  91. if (!/\d{11}/.test(mobile)) {
  92. return mobile
  93. }
  94. return mobile.replace(/(\d{3})(\d{4})(\d{4})/, '$1****$3')
  95. }
  96. /**
  97. * 随机生成指定长度的字符串
  98. * @param length
  99. * @returns {string}
  100. */
  101. export function randomString(length = 32) {
  102. const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'
  103. const maxPos = chars.length
  104. let _string = ''
  105. for (let i = 0; i < length; i++) {
  106. _string += chars.charAt(Math.floor(Math.random() * maxPos))
  107. }
  108. return _string
  109. }
  110. /**
  111. * 计算传秒数的倒计时【天、时、分、秒】
  112. * @param seconds
  113. * @returns {{day : *, hours : *, minutes : *, seconds : *}}
  114. */
  115. export function countTimeDown(seconds) {
  116. const leftTime = (time) => {
  117. if (time < 10) time = '0' + time
  118. return time + ''
  119. }
  120. return {
  121. day: leftTime(parseInt(seconds / 60 / 60 / 24, 10)),
  122. hours: leftTime(parseInt(seconds / 60 / 60 % 24, 10)),
  123. minutes: leftTime(parseInt(seconds / 60 % 60, 10)),
  124. seconds: leftTime(parseInt(seconds % 60, 10))
  125. }
  126. }
  127. /**
  128. * 计算当前时间到第二天0点的倒计时[秒]
  129. * @returns {number}
  130. */
  131. export function theNextDayTime() {
  132. const nowDate = new Date()
  133. const time = new Date(nowDate.getFullYear(), nowDate.getMonth(), nowDate.getDate() + 1, 0, 0, 0).getTime() - nowDate.getTime()
  134. return parseInt(time / 1000)
  135. }