index.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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.href);
  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 saveBookProcess = (bookProgress) =>
  21. ajax.post("/saveBookProgress", bookProgress);
  22. const getBookShelf = () => ajax.get("/getBookshelf");
  23. const getChapterList = (/** @type {string} */ bookUrl) =>
  24. ajax.get("/getChapterList?url=" + encodeURIComponent(bookUrl));
  25. const getBookContent = (
  26. /** @type {string} */ bookUrl,
  27. /** @type {number} */ chapterIndex
  28. ) =>
  29. ajax.get(
  30. "/getBookContent?url=" +
  31. encodeURIComponent(bookUrl) +
  32. "&index=" +
  33. chapterIndex
  34. );
  35. const search = (
  36. /** @type {string} */ searchKey,
  37. /** @type {(data: string) => void} */ onReceive,
  38. /** @type {() => void} */ onFinish
  39. ) => {
  40. // webSocket
  41. const url = `ws://${hostname}:${Number(port) + 1}/searchBook`;
  42. const socket = new WebSocket(url);
  43. socket.onopen = () => {
  44. socket.send(`{"key":"${searchKey}"}`);
  45. };
  46. socket.onmessage = ({ data }) => onReceive(data);
  47. socket.onclose = () => {
  48. onFinish();
  49. };
  50. };
  51. const saveBook = (book) => ajax.post("/saveBook", book);
  52. const deleteBook = (book) => ajax.post("/deleteBook", book);
  53. const isBookSource = /bookSource/i.test(location.href);
  54. // Http
  55. const getSources = () =>
  56. isBookSource ? ajax.get("getBookSources") : ajax.get("getRssSources");
  57. const saveSource = (data) =>
  58. isBookSource
  59. ? ajax.post("saveBookSource", data)
  60. : ajax.post("saveRssSource", data);
  61. const saveSources = (data) =>
  62. isBookSource
  63. ? ajax.post("saveBookSources", data)
  64. : ajax.post("saveRssSources", data);
  65. const deleteSource = (data) =>
  66. isBookSource
  67. ? ajax.post("deleteBookSources", data)
  68. : ajax.post("deleteRssSources", data);
  69. const debug = (
  70. /** @type {string} */ sourceUrl,
  71. /** @type {string} */ searchKey,
  72. /** @type {(data: string) => void} */ onReceive,
  73. /** @type {() => void} */ onFinish
  74. ) => {
  75. // webSocket
  76. const url = `ws://${hostname}:${Number(port) + 1}/${
  77. isBookSource ? "bookSource" : "rssSource"
  78. }Debug`;
  79. const socket = new WebSocket(url);
  80. socket.onopen = () => {
  81. socket.send(`{"tag":"${sourceUrl}", "key":"${searchKey}"}`);
  82. };
  83. socket.onmessage = ({ data }) => onReceive(data);
  84. socket.onclose = () => {
  85. ElMessage({
  86. message: "调试已关闭!",
  87. type: "info",
  88. });
  89. onFinish();
  90. };
  91. };
  92. export default {
  93. getReadConfig,
  94. saveReadConfig,
  95. saveBookProcess,
  96. getBookShelf,
  97. getChapterList,
  98. getBookContent,
  99. search,
  100. saveBook,
  101. deleteBook,
  102. getSources,
  103. saveSources,
  104. saveSource,
  105. deleteSource,
  106. debug,
  107. };