index.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /**
  2. * Created by PanJiaChen on 16/11/18.
  3. */
  4. /**
  5. * Parse the time to string
  6. * @param {(Object|string|number)} time
  7. * @param {string} cFormat
  8. * @returns {string | null}
  9. */
  10. export function parseTime(time, cFormat) {
  11. if (arguments.length === 0 || !time) {
  12. return null
  13. }
  14. const format = cFormat || '{y}-{m}-{d} {h}:{i}:{s}'
  15. let date
  16. if (typeof time === 'object') {
  17. date = time
  18. } else {
  19. if ((typeof time === 'string')) {
  20. if ((/^[0-9]+$/.test(time))) {
  21. // support "1548221490638"
  22. time = parseInt(time)
  23. } else {
  24. // support safari
  25. // https://stackoverflow.com/questions/4310953/invalid-date-in-safari
  26. time = time.replace(new RegExp(/-/gm), '/')
  27. }
  28. }
  29. if ((typeof time === 'number') && (time.toString().length === 10)) {
  30. time = time * 1000
  31. }
  32. date = new Date(time)
  33. }
  34. const formatObj = {
  35. y: date.getFullYear(),
  36. m: date.getMonth() + 1,
  37. d: date.getDate(),
  38. h: date.getHours(),
  39. i: date.getMinutes(),
  40. s: date.getSeconds(),
  41. a: date.getDay()
  42. }
  43. const time_str = format.replace(/{([ymdhisa])+}/g, (result, key) => {
  44. const value = formatObj[key]
  45. // Note: getDay() returns 0 on Sunday
  46. if (key === 'a') { return ['日', '一', '二', '三', '四', '五', '六'][value ] }
  47. return value.toString().padStart(2, '0')
  48. })
  49. return time_str
  50. }
  51. /**
  52. * @param {number} time
  53. * @param {string} option
  54. * @returns {string}
  55. */
  56. export function formatTime(time, option) {
  57. if (('' + time).length === 10) {
  58. time = parseInt(time) * 1000
  59. } else {
  60. time = +time
  61. }
  62. const d = new Date(time)
  63. const now = Date.now()
  64. const diff = (now - d) / 1000
  65. if (diff < 30) {
  66. return '刚刚'
  67. } else if (diff < 3600) {
  68. // less 1 hour
  69. return Math.ceil(diff / 60) + '分钟前'
  70. } else if (diff < 3600 * 24) {
  71. return Math.ceil(diff / 3600) + '小时前'
  72. } else if (diff < 3600 * 24 * 2) {
  73. return '1天前'
  74. }
  75. if (option) {
  76. return parseTime(time, option)
  77. } else {
  78. return (
  79. d.getMonth() +
  80. 1 +
  81. '月' +
  82. d.getDate() +
  83. '日' +
  84. d.getHours() +
  85. '时' +
  86. d.getMinutes() +
  87. '分'
  88. )
  89. }
  90. }
  91. /**
  92. * @param {string} url
  93. * @returns {Object}
  94. */
  95. export function param2Obj(url) {
  96. const search = decodeURIComponent(url.split('?')[1]).replace(/\+/g, ' ')
  97. if (!search) {
  98. return {}
  99. }
  100. const obj = {}
  101. const searchArr = search.split('&')
  102. searchArr.forEach(v => {
  103. const index = v.indexOf('=')
  104. if (index !== -1) {
  105. const name = v.substring(0, index)
  106. const val = v.substring(index + 1, v.length)
  107. obj[name] = val
  108. }
  109. })
  110. return obj
  111. }
  112. /**
  113. * @param {string} url
  114. * @returns {Object}
  115. */
  116. export function arrToObj(arr) {
  117. const obj = {}
  118. if (Object.prototype.toString.call(arr) === '[object Array]') {
  119. if (arr.length > 0) {
  120. arr.forEach(item => {
  121. obj[item.val] = item.key
  122. })
  123. }
  124. }
  125. return obj
  126. }
  127. export function noEmpty(val) {
  128. if (val === null || val === undefined || val === '') {
  129. return false
  130. } else {
  131. return true
  132. }
  133. }
  134. export function comGetTime(date = new Date(), onlyDate = true) {
  135. const year = date.getFullYear()
  136. let month = date.getMonth() + 1
  137. month = month > 9 ? month : '0' + month
  138. let day = date.getDate()
  139. day = day > 9 ? day : '0' + day
  140. let hour = date.getHours()
  141. hour = hour > 9 ? hour : '0' + hour
  142. let minute = date.getMinutes()
  143. minute = minute > 9 ? minute : '0' + minute
  144. let second = date.getSeconds()
  145. second = second > 9 ? second : '0' + second
  146. let milli = date.getMilliseconds()
  147. milli = milli > 100 ? milli : milli > 10 ? '0' + milli : '00' + milli
  148. return onlyDate ? `${year}-${month}-${day}` : `${year}${month}${day}${hour}${minute}${second}${milli}`
  149. }
  150. export function strTrim(str) { // 删除左右两端的空格
  151. return str.replace(/(^\s*)|(\s*$)/g, '')
  152. }
  153. export function imgMarkAdd (url) { // 图片添加水印
  154. if (url && url.indexOf('_adm0') === -1) {
  155. return `${url}_adm0`
  156. } else {
  157. return url
  158. }
  159. }
  160. export function imgMarkDel (url) { // 图片删除水印
  161. if (url && url.indexOf('_adm0') > -1) {
  162. return url.substring(0, url.length - 5)
  163. } else {
  164. return url
  165. }
  166. }