utils.js 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. import { formatDate } from "@vueuse/shared";
  2. export const isLegadoUrl = (/** @type {string} */ url) =>
  3. /,\s*\{/.test(url) ||
  4. !(
  5. url.startsWith("http") ||
  6. url.startsWith("data:") ||
  7. url.startsWith("blob:")
  8. );
  9. /**
  10. * @param {string} src
  11. */
  12. export function getImageFromLegado(src) {
  13. return (
  14. (import.meta.env.VITE_API || location.origin) +
  15. "/image?path=" +
  16. encodeURIComponent(src) +
  17. "&url=" +
  18. encodeURIComponent(sessionStorage.getItem("bookUrl")) +
  19. "&width=" +
  20. useBookStore().config.readWidth
  21. );
  22. }
  23. // @ts-ignore
  24. export const dateFormat = (/** @type {number} */ t) => {
  25. let time = new Date().getTime();
  26. let offset = Math.floor((time - t) / 1000);
  27. let str = "";
  28. if (offset <= 30) {
  29. str = "刚刚";
  30. } else if (offset < 60) {
  31. str = offset + "秒前";
  32. } else if (offset < 3600) {
  33. str = Math.floor(offset / 60) + "分钟前";
  34. } else if (offset < 86400) {
  35. str = Math.floor(offset / 3600) + "小时前";
  36. } else if (offset < 2592000) {
  37. str = Math.floor(offset / 86400) + "天前";
  38. } else {
  39. str = formatDate(new Date(t), "YYYY-MM-DD");
  40. }
  41. return str;
  42. };