index.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /**
  2. * Created by PanJiaChen on 16/11/18.
  3. */
  4. import config from "@/config";
  5. import {isString} from "v-calendar/src/utils/_";
  6. /**
  7. * Parse the time to string
  8. * @param {(Object|string|number)} time
  9. * @param {string} cFormat
  10. * @returns {string}
  11. */
  12. export function parseTime(time, cFormat) {
  13. if (arguments.length === 0) {
  14. return null
  15. }
  16. const format = cFormat || '{y}-{m}-{d} {h}:{i}:{s}'
  17. let date
  18. if (typeof time === 'object') {
  19. date = time
  20. } else {
  21. if (typeof time === 'string' && /^[0-9]+$/.test(time)) {
  22. time = parseInt(time)
  23. }
  24. if (typeof time === 'number' && time.toString().length === 10) {
  25. time = time * 1000
  26. }
  27. date = new Date(time)
  28. }
  29. const formatObj = {
  30. y: date.getFullYear(),
  31. m: date.getMonth() + 1,
  32. d: date.getDate(),
  33. h: date.getHours(),
  34. i: date.getMinutes(),
  35. s: date.getSeconds(),
  36. a: date.getDay()
  37. }
  38. const time_str = format.replace(/{(y|m|d|h|i|s|a)+}/g, (result, key) => {
  39. let value = formatObj[key]
  40. // Note: getDay() returns 0 on Sunday
  41. if (key === 'a') {
  42. return ['日', '一', '二', '三', '四', '五', '六'][value]
  43. }
  44. if (result.length > 0 && value < 10) {
  45. value = '0' + value
  46. }
  47. return value || 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 d.getMonth() + 1 + '月' + d.getDate() + '日' + d.getHours() + '时' + d.getMinutes() + '分'
  79. }
  80. }
  81. /**
  82. * @param {string} url
  83. * @returns {Object}
  84. */
  85. export function param2Obj(url) {
  86. const search = url.split('?')[1]
  87. if (!search) {
  88. return {}
  89. }
  90. return JSON.parse(
  91. '{"' +
  92. decodeURIComponent(search).replace(/"/g, '\\"').replace(/&/g, '","').replace(/=/g, '":"').replace(/\+/g, ' ') +
  93. '"}'
  94. )
  95. }
  96. /**
  97. * @param {string} url
  98. * @returns {string}
  99. * */
  100. //根据当前环境拼接图片地址
  101. export function imgUrl(url){
  102. if(!url) return "";
  103. return process.env.NODE_ENV === "development"
  104. ? config.baseUrl + url
  105. : window.origin + url;
  106. }