index.js 3.5 KB

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