modules 添加web

This commit is contained in:
Xwite
2023-04-07 20:27:20 +08:00
parent 3447e46176
commit 57e2c3a418
88 changed files with 5784 additions and 0 deletions

View File

@@ -0,0 +1,10 @@
import axios from "axios";
const SECOND = 1000;
const ajax = axios.create({
baseURL: import.meta.env.VITE_API || location.origin,
timeout: 5 * SECOND,
});
export default ajax;

View File

@@ -0,0 +1,131 @@
import ajax from "./axios";
import { ElMessage } from "element-plus/es";
/** https://github.com/gedoor/legado/tree/master/app/src/main/java/io/legado/app/api */
/** https://github.com/gedoor/legado/tree/master/app/src/main/java/io/legado/app/web */
const { hostname, port } = new URL(import.meta.env.VITE_API || location.href);
const isSourecEditor = /source/i.test(location.href);
const APIExceptionHandler = (error) => {
if (isSourecEditor) {
ElMessage({
message: "后端错误检查网络或者阅读app",
type: "error",
});
}
throw error;
};
ajax.interceptors.response.use((response) => response, APIExceptionHandler);
// Http
const getReadConfig = () => ajax.get("/getReadConfig");
const saveReadConfig = (config) => ajax.post("/saveReadConfig", config);
const saveBookProcess = (bookProgress) =>
ajax.post("/saveBookProgress", bookProgress);
const getBookShelf = () => ajax.get("/getBookshelf");
const getChapterList = (/** @type {string} */ bookUrl) =>
ajax.get("/getChapterList?url=" + encodeURIComponent(bookUrl));
const getBookContent = (
/** @type {string} */ bookUrl,
/** @type {number} */ chapterIndex
) =>
ajax.get(
"/getBookContent?url=" +
encodeURIComponent(bookUrl) +
"&index=" +
chapterIndex
);
const search = (
/** @type {string} */ searchKey,
/** @type {(data: string) => void} */ onReceive,
/** @type {() => void} */ onFinish
) => {
// webSocket
const url = `ws://${hostname}:${Number(port) + 1}/searchBook`;
const socket = new WebSocket(url);
socket.onopen = () => {
socket.send(`{"key":"${searchKey}"}`);
};
socket.onmessage = ({ data }) => onReceive(data);
socket.onclose = () => {
onFinish();
};
};
const saveBook = (book) => ajax.post("/saveBook", book);
const deleteBook = (book) => ajax.post("/deleteBook", book);
const isBookSource = /bookSource/i.test(location.href);
// Http
const getSources = () =>
isBookSource ? ajax.get("getBookSources") : ajax.get("getRssSources");
const saveSource = (data) =>
isBookSource
? ajax.post("saveBookSource", data)
: ajax.post("saveRssSource", data);
const saveSources = (data) =>
isBookSource
? ajax.post("saveBookSources", data)
: ajax.post("saveRssSources", data);
const deleteSource = (data) =>
isBookSource
? ajax.post("deleteBookSources", data)
: ajax.post("deleteRssSources", data);
const debug = (
/** @type {string} */ sourceUrl,
/** @type {string} */ searchKey,
/** @type {(data: string) => void} */ onReceive,
/** @type {() => void} */ onFinish
) => {
// webSocket
const url = `ws://${hostname}:${Number(port) + 1}/${
isBookSource ? "bookSource" : "rssSource"
}Debug`;
const socket = new WebSocket(url);
socket.onopen = () => {
socket.send(`{"tag":"${sourceUrl}", "key":"${searchKey}"}`);
};
socket.onmessage = ({ data }) => onReceive(data);
socket.onclose = () => {
ElMessage({
message: "调试已关闭!",
type: "info",
});
onFinish();
};
};
export default {
getReadConfig,
saveReadConfig,
saveBookProcess,
getBookShelf,
getChapterList,
getBookContent,
search,
saveBook,
deleteBook,
getSources,
saveSources,
saveSource,
deleteSource,
debug,
};