Foundation.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. /**
  2. * 一些常用的基础方法
  3. * unixToDate 将unix时间戳转换为指定格式
  4. * dateToUnix 将时间转unix时间戳
  5. * deepClone 对一个对象进行深拷贝
  6. * formatPrice 货币格式化
  7. * secrecyMobile 手机号隐私保护
  8. * randomString 随机生成指定长度的字符串
  9. */
  10. /**
  11. * 将数据转成tree
  12. * @param list 数据
  13. * @param parent_id 父id
  14. * @returns {*|string}
  15. */
  16. let buildTree = function buildTree(list, parent_id) {
  17. const tree = []
  18. for (let i = 0; i < list.length; i++) {
  19. if (list[i].parent_id === parent_id) {
  20. const node = {
  21. id: list[i].id,
  22. parent_id: list[i].parent_id,
  23. name: list[i].name,
  24. create_time: list[i].create_time,
  25. remark: list[i].remark,
  26. sn: list[i].sn,
  27. sort: list[i].sort,
  28. type: list[i].type,
  29. region_ids: list[i].region_ids,
  30. children: this.buildTree(list, list[i].id)
  31. }
  32. tree.push(node)
  33. }
  34. }
  35. return tree
  36. }
  37. /**
  38. * 将unix时间戳转换为指定格式
  39. * @param unix 时间戳【秒】
  40. * @param format 转换格式
  41. * @returns {*|string}
  42. */
  43. let unixToDate = function unixToDate(unix, format) {
  44. RegExp = window.RegExp
  45. if (!unix) return unix
  46. let _format = format || 'yyyy-MM-dd hh:mm:ss'
  47. let d = new Date(unix * 1000)
  48. let o = {
  49. 'M+': d.getMonth() + 1,
  50. 'd+': d.getDate(),
  51. 'h+': d.getHours(),
  52. 'm+': d.getMinutes(),
  53. 's+': d.getSeconds(),
  54. 'q+': Math.floor((d.getMonth() + 3) / 3),
  55. S: d.getMilliseconds()
  56. }
  57. if (/(y+)/.test(_format)) _format = _format.replace(RegExp.$1, (d.getFullYear() + '').substr(4 - RegExp.$1.length))
  58. for (let 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)))
  59. return _format
  60. }
  61. /**
  62. * 将时间转unix时间戳
  63. * @param date
  64. * @returns {number} 【秒】
  65. */
  66. let dateToUnix = function dateToUnix(date) {
  67. let newStr = date.replace(/:/g, '-')
  68. newStr = newStr.replace(/ /g, '-')
  69. let arr = newStr.split('-')
  70. let 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. let deepClone = 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 (let 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. let formatPrice = 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. let secrecyMobile = 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. let randomString = function randomString(length) {
  130. if (!length) length = 32
  131. let chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'
  132. let maxPos = chars.length
  133. let _string = ''
  134. for (let i = 0; i < length; i++) {
  135. _string += chars.charAt(Math.floor(Math.random() * maxPos))
  136. }
  137. return _string
  138. }
  139. /**
  140. * 计算传秒数的倒计时【天、时、分、秒】
  141. * @param seconds
  142. * @returns {{day : *, hours : *, minutes : *, seconds : *}}
  143. */
  144. let countTimeDown = function countTimeDown(seconds) {
  145. let leftTime = function(time) {
  146. if (time < 10) time = '0' + time
  147. return time + ''
  148. }
  149. return {
  150. day: leftTime(parseInt(seconds / 60 / 60 / 24, 10)),
  151. hours: leftTime(parseInt(seconds / 60 / 60 % 24, 10)),
  152. minutes: leftTime(parseInt(seconds / 60 % 60, 10)),
  153. seconds: leftTime(parseInt(seconds % 60, 10))
  154. }
  155. }
  156. /**
  157. * 计算当前时间到第二天0点的倒计时[秒]
  158. * @returns {number}
  159. */
  160. let theNextDayTime = function theNextDayTime() {
  161. let nowDate = new Date()
  162. let time = new Date(nowDate.getFullYear(), nowDate.getMonth(), nowDate.getDate() + 1, 0, 0, 0).getTime() - nowDate.getTime()
  163. return parseInt(time / 1000)
  164. }
  165. /**
  166. * 获取顶部路由路径
  167. * @param router
  168. * @param next
  169. * @returns {string}
  170. */
  171. let toFirstRoute = function getTopRoutePath(router, next) {
  172. const excludes = [
  173. "",
  174. "*",
  175. '/404',
  176. '/401',
  177. '/500',
  178. '/login',
  179. '/cashier/login',
  180. '/franchise/login'
  181. ]
  182. const paths = router.getRoutes().map(item => item.path)
  183. const path = paths.filter(item => !excludes.includes(item) && item.indexOf('/:') === -1)[0]
  184. if (typeof next === 'function') {
  185. next(path)
  186. } else {
  187. router.replace(path)
  188. }
  189. }
  190. /**
  191. * 获取顶部路由路径
  192. * @param routers
  193. * @returns {string}
  194. */
  195. let getTopRoutePath = function getTopRoutePath(routers) {
  196. let paths = []
  197. getPath(getFirstRoute(routers))
  198. function getPath(route) {
  199. paths.push(route.path)
  200. if (Array.isArray(route.children) && getFirstRoute(route.children)) {
  201. getPath(getFirstRoute(route.children))
  202. }
  203. }
  204. let path = paths.filter(item => !!item).join('/')
  205. if (path.indexOf('/') !== 0) {
  206. path = '/' + path
  207. }
  208. path = path.replaceAll('//', '/')
  209. function getFirstRoute(routers) {
  210. return routers.find(item => item.path.indexOf('/:') === -1)
  211. }
  212. return path
  213. }
  214. module.exports = {
  215. buildTree: buildTree,
  216. unixToDate: unixToDate,
  217. dateToUnix: dateToUnix,
  218. deepClone: deepClone,
  219. formatPrice: formatPrice,
  220. secrecyMobile: secrecyMobile,
  221. randomString: randomString,
  222. countTimeDown: countTimeDown,
  223. theNextDayTime: theNextDayTime,
  224. toFirstRoute: toFirstRoute,
  225. getTopRoutePath: getTopRoutePath
  226. }