index.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. import ajax from "./axios";
  2. import { ElMessage } from "element-plus/es";
  3. /** https://github.com/gedoor/legado/tree/master/app/src/main/java/io/legado/app/api */
  4. /** https://github.com/gedoor/legado/tree/master/app/src/main/java/io/legado/app/web */
  5. const { hostname, port } = new URL(import.meta.env.VITE_API || location.origin);
  6. const isSourecEditor = /source/i.test(location.href);
  7. const APIExceptionHandler = (error) => {
  8. if (isSourecEditor) {
  9. ElMessage({
  10. message: "后端错误,检查网络或者阅读app",
  11. type: "error",
  12. });
  13. }
  14. throw error;
  15. };
  16. ajax.interceptors.response.use((response) => response, APIExceptionHandler);
  17. // Http
  18. const getReadConfig = () => ajax.get("/getReadConfig");
  19. const saveReadConfig = (config) => ajax.post("/saveReadConfig", config);
  20. const saveBookProgress = (bookProgress) =>
  21. ajax.post("/saveBookProgress", bookProgress);
  22. const saveBookProgressWithBeacon = (bookProgress) => {
  23. if (!bookProgress) return;
  24. // 常规请求可能会被取消 使用Fetch keep-alive 或者 navigator.sendBeacon
  25. navigator.sendBeacon(
  26. `${import.meta.env.VITE_API || location.origin}/saveBookProgress`,
  27. JSON.stringify(bookProgress)
  28. );
  29. };
  30. const getBookShelf = () => ajax.get("/getBookshelf");
  31. const getChapterList = (/** @type {string} */ bookUrl) =>
  32. ajax.get("/getChapterList?url=" + encodeURIComponent(bookUrl));
  33. const getBookContent = (
  34. /** @type {string} */ bookUrl,
  35. /** @type {number} */ chapterIndex
  36. ) =>
  37. ajax.get(
  38. "/getBookContent?url=" +
  39. encodeURIComponent(bookUrl) +
  40. "&index=" +
  41. chapterIndex
  42. );
  43. const search = (
  44. /** @type {string} */ searchKey,
  45. /** @type {(data: string) => void} */ onReceive,
  46. /** @type {() => void} */ onFinish
  47. ) => {
  48. // webSocket
  49. const url = `ws://${hostname}:${Number(port) + 1}/searchBook`;
  50. const socket = new WebSocket(url);
  51. socket.onopen = () => {
  52. socket.send(`{"key":"${searchKey}"}`);
  53. };
  54. socket.onmessage = ({ data }) => onReceive(data);
  55. socket.onclose = () => {
  56. onFinish();
  57. };
  58. };
  59. const saveBook = (book) => ajax.post("/saveBook", book);
  60. const deleteBook = (book) => ajax.post("/deleteBook", book);
  61. const isBookSource = /bookSource/i.test(location.href);
  62. // Http
  63. const getSources = () =>
  64. isBookSource ? ajax.get("getBookSources") : ajax.get("getRssSources");
  65. const saveSource = (data) =>
  66. isBookSource
  67. ? ajax.post("saveBookSource", data)
  68. : ajax.post("saveRssSource", data);
  69. const saveSources = (data) =>
  70. isBookSource
  71. ? ajax.post("saveBookSources", data)
  72. : ajax.post("saveRssSources", data);
  73. const deleteSource = (data) =>
  74. isBookSource
  75. ? ajax.post("deleteBookSources", data)
  76. : ajax.post("deleteRssSources", data);
  77. const debug = (
  78. /** @type {string} */ sourceUrl,
  79. /** @type {string} */ searchKey,
  80. /** @type {(data: string) => void} */ onReceive,
  81. /** @type {() => void} */ onFinish
  82. ) => {
  83. // webSocket
  84. const url = `ws://${hostname}:${Number(port) + 1}/${
  85. isBookSource ? "bookSource" : "rssSource"
  86. }Debug`;
  87. const socket = new WebSocket(url);
  88. socket.onopen = () => {
  89. socket.send(JSON.stringify({ tag: sourceUrl, key: searchKey }));
  90. };
  91. socket.onmessage = ({ data }) => onReceive(data);
  92. socket.onclose = () => {
  93. ElMessage({
  94. message: "调试已关闭!",
  95. type: "info",
  96. });
  97. onFinish();
  98. };
  99. };
  100. export default {
  101. getReadConfig,
  102. saveReadConfig,
  103. saveBookProgress,
  104. saveBookProgressWithBeacon,
  105. getBookShelf,
  106. getChapterList,
  107. getBookContent,
  108. search,
  109. saveBook,
  110. deleteBook,
  111. getSources,
  112. saveSources,
  113. saveSource,
  114. deleteSource,
  115. debug,
  116. };