util.js 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. export function timeFix() {
  2. const time = new Date()
  3. const hour = time.getHours()
  4. return hour < 9 ? '早上好' : (hour <= 11 ? '上午好' : (hour <= 13 ? '中午好' : (hour < 20 ? '下午好' : '晚上好')))
  5. }
  6. export function welcome() {
  7. const arr = ['休息一会儿吧', '准备吃什么呢?', '要不要打一把 DOTA', '我猜你可能累了']
  8. const index = Math.floor((Math.random() * arr.length))
  9. return arr[index]
  10. }
  11. /**
  12. * 触发 window.resize
  13. */
  14. export function triggerWindowResizeEvent() {
  15. const event = document.createEvent('HTMLEvents')
  16. event.initEvent('resize', true, true)
  17. event.eventType = 'message'
  18. window.dispatchEvent(event)
  19. }
  20. /**
  21. * 过滤对象中为空的属性
  22. * @param obj
  23. * @returns {*}
  24. */
  25. export function filterObj(obj) {
  26. if (!(typeof obj === 'object')) {
  27. return
  28. }
  29. for (const key in obj) {
  30. if (obj.hasOwnProperty(key) &&
  31. (obj[key] == null || obj[key] == undefined || obj[key] === '')) {
  32. delete obj[key]
  33. }
  34. }
  35. return obj
  36. }
  37. /**
  38. * 时间格式化
  39. * @param value
  40. * @param fmt
  41. * @returns {*}
  42. */
  43. export function formatDate(value, fmt) {
  44. const regPos = /^\d+(\.\d+)?$/
  45. if (regPos.test(value)) {
  46. // 如果是数字
  47. const getDate = new Date(value)
  48. const o = {
  49. 'M+': getDate.getMonth() + 1,
  50. 'd+': getDate.getDate(),
  51. 'h+': getDate.getHours(),
  52. 'm+': getDate.getMinutes(),
  53. 's+': getDate.getSeconds(),
  54. 'q+': Math.floor((getDate.getMonth() + 3) / 3),
  55. 'S': getDate.getMilliseconds()
  56. }
  57. if (/(y+)/.test(fmt)) {
  58. fmt = fmt.replace(RegExp.$1, (getDate.getFullYear() + '').substr(4 - RegExp.$1.length))
  59. }
  60. for (const k in o) {
  61. if (new RegExp('(' + k + ')').test(fmt)) {
  62. fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? (o[k]) : (('00' + o[k]).substr(('' + o[k]).length)))
  63. }
  64. }
  65. return fmt
  66. } else {
  67. // TODO
  68. value = value.trim()
  69. return value.substr(0, fmt.length)
  70. }
  71. }