Foundation.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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. import moment from 'moment-timezone'
  17. const timeZone = process.env.timeZone
  18. export function unixToDate(unix, format) {
  19. if (!unix) return unix
  20. let _format = format || 'yyyy-MM-dd hh:mm:ss'
  21. const d = new Date(unix * 1000)
  22. const o = {
  23. 'M+': d.getMonth() + 1,
  24. 'd+': d.getDate(),
  25. 'h+': d.getHours(),
  26. 'm+': d.getMinutes(),
  27. 's+': d.getSeconds(),
  28. 'q+': Math.floor((d.getMonth() + 3) / 3),
  29. S: d.getMilliseconds()
  30. }
  31. if (/(y+)/.test(_format)) _format = _format.replace(RegExp.$1, (d.getFullYear() + '').substr(4 - RegExp.$1.length))
  32. 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)))
  33. return _format
  34. }
  35. /** 格式化时间戳 */
  36. export function unix2Date(unix, format) {
  37. if (!unix) return unix
  38. // let time = moment.tz(unix,timeZone).format(format)
  39. // console.log('time',time)
  40. // return time
  41. let _format = format || 'yyyy-MM-dd hh:mm:ss'
  42. const d = new Date(unix)
  43. const o = {
  44. 'M+': d.getMonth() + 1,
  45. 'd+': d.getDate(),
  46. 'h+': d.getHours(),
  47. 'm+': d.getMinutes(),
  48. 's+': d.getSeconds(),
  49. 'q+': Math.floor((d.getMonth() + 3) / 3),
  50. S: d.getMilliseconds()
  51. }
  52. if (/(y+)/.test(_format)) _format = _format.replace(RegExp.$1, (d.getFullYear() + '').substr(4 - RegExp.$1.length))
  53. 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)))
  54. return _format
  55. }
  56. export function unix2DateWithTimeZone(unix,format) {
  57. if (!unix) return unix
  58. let time = moment.tz(unix,timeZone).format(format)
  59. return time
  60. }
  61. /**
  62. * 将时间转unix时间戳
  63. * @param date
  64. * @returns {number} 【秒】
  65. */
  66. export function dateToUnix(date) {
  67. let newStr = date.replace(/:/g, '-')
  68. newStr = newStr.replace(/ /g, '-')
  69. const arr = newStr.split('-')
  70. const datum = new Date(Date.UTC(
  71. arr[0],
  72. arr[1] - 1,
  73. arr[2],
  74. arr[3] - 8 || -8,
  75. arr[4] || 0,
  76. arr[5] || 0
  77. ))
  78. return parseInt(datum.getTime() / 1000)
  79. }
  80. /**
  81. * 对一个对象进行深拷贝
  82. * @param object
  83. * @returns {*}
  84. */
  85. export function deepClone(object) {
  86. let str
  87. let newobj = object.constructor === Array ? [] : {}
  88. if (typeof object !== 'object') {
  89. return object
  90. } else if (window.JSON) {
  91. str = JSON.stringify(object)
  92. newobj = JSON.parse(str)
  93. } else {
  94. for (const i in object) {
  95. if (object.hasOwnProperty(i)) {
  96. newobj[i] = typeof object[i] === 'object' ? deepClone(object[i]) : object[i]
  97. }
  98. }
  99. }
  100. return newobj
  101. }
  102. /**
  103. * 货币格式化
  104. * @param price
  105. * @returns {string}
  106. */
  107. export function formatPrice(price) {
  108. if (typeof price !== 'number') return price
  109. return String(Number(price).toFixed(2)).replace(/\B(?=(\d{3})+(?!\d))/g, ',')
  110. }
  111. /**
  112. * 手机号隐私保护
  113. * 隐藏中间四位数字
  114. * @param mobile
  115. * @returns {*}
  116. */
  117. export function secrecyMobile(mobile) {
  118. mobile = String(mobile)
  119. if (!/\d{11}/.test(mobile)) {
  120. return mobile
  121. }
  122. return mobile.replace(/(\d{3})(\d{4})(\d{4})/, '$1****$3')
  123. }
  124. /**
  125. * 随机生成指定长度的字符串
  126. * @param length
  127. * @returns {string}
  128. */
  129. export function randomString(length = 32) {
  130. const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'
  131. const maxPos = chars.length
  132. let _string = ''
  133. for (let i = 0; i < length; i++) {
  134. _string += chars.charAt(Math.floor(Math.random() * maxPos))
  135. }
  136. return _string
  137. }
  138. /**
  139. * 计算传秒数的倒计时【天、时、分、秒】
  140. * @param seconds
  141. * @returns {{day : *, hours : *, minutes : *, seconds : *}}
  142. */
  143. export function countTimeDown(seconds) {
  144. const leftTime = (time) => {
  145. if (time < 10) time = '0' + time
  146. return time + ''
  147. }
  148. return {
  149. day: leftTime(parseInt(seconds / 60 / 60 / 24, 10)),
  150. hours: leftTime(parseInt(seconds / 60 / 60 % 24, 10)),
  151. minutes: leftTime(parseInt(seconds / 60 % 60, 10)),
  152. seconds: leftTime(parseInt(seconds % 60, 10))
  153. }
  154. }
  155. /**
  156. * 计算当前时间到第二天0点的倒计时[秒]
  157. * @returns {number}
  158. */
  159. export function theNextDayTime() {
  160. const nowDate = new Date()
  161. const time = new Date(nowDate.getFullYear(), nowDate.getMonth(), nowDate.getDate() + 1, 0, 0, 0).getTime() - nowDate.getTime()
  162. return parseInt(time / 1000)
  163. }
  164. export function findNodeById(data, id) {
  165. let node = null
  166. if (data!==null&&data.length > 0) {
  167. for (var i = 0; i < data.length; i++) {
  168. if (data[i].tpl_id === id) {
  169. node = data[i]
  170. break
  171. }
  172. if (data[i].children && data[i].children.length > 0) {
  173. const subresult = findNodeById(data[i].children, id)
  174. if (subresult !== null) {
  175. node = subresult
  176. break
  177. }
  178. }
  179. }
  180. }
  181. return node
  182. }