utils.js 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. import { formatDate } from "@vueuse/shared";
  2. export const isLegadoUrl = (/** @type {string} */ url) =>
  3. /,\s*\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. //返回阅读代理的图片链接 已经代理的或者dataurl返回传入值
  14. if (!isLegadoUrl(src)) {
  15. return src;
  16. }
  17. return (
  18. (import.meta.env.VITE_API || location.origin) +
  19. "/image?path=" +
  20. encodeURIComponent(src) +
  21. "&url=" +
  22. encodeURIComponent(sessionStorage.getItem("bookUrl")) +
  23. "&width=" +
  24. useBookStore().config.readWidth
  25. );
  26. }
  27. // @ts-ignore
  28. export const dateFormat = (/** @type {number} */ t) => {
  29. let time = new Date().getTime();
  30. let offset = Math.floor((time - t) / 1000);
  31. let str = "";
  32. if (offset <= 30) {
  33. str = "刚刚";
  34. } else if (offset < 60) {
  35. str = offset + "秒前";
  36. } else if (offset < 3600) {
  37. str = Math.floor(offset / 60) + "分钟前";
  38. } else if (offset < 86400) {
  39. str = Math.floor(offset / 3600) + "小时前";
  40. } else if (offset < 2592000) {
  41. str = Math.floor(offset / 86400) + "天前";
  42. } else {
  43. str = formatDate(new Date(t), "YYYY-MM-DD");
  44. }
  45. return str;
  46. };