diff --git a/modules/web/src/api/axios.js b/modules/web/src/api/axios.js index fe1a9a235..740d5cf32 100644 --- a/modules/web/src/api/axios.js +++ b/modules/web/src/api/axios.js @@ -5,7 +5,7 @@ const SECOND = 1000; const ajax = axios.create({ baseURL: import.meta.env.VITE_API || - localStorage.getItem("remoteIp") || + localStorage.getItem("remoteOrigin") || location.origin, timeout: 120 * SECOND, }); diff --git a/modules/web/src/api/index.js b/modules/web/src/api/index.js index cf424ce78..49f4438e4 100644 --- a/modules/web/src/api/index.js +++ b/modules/web/src/api/index.js @@ -8,12 +8,22 @@ let legado_http_origin; let legado_webSocket_origin; const setLeagdoHttpUrl = (http_url) => { - let legado_webSocket_port; - const { protocol, hostname, port } = new URL(http_url); + let legado_webSocket_port, url; + try { + url = new URL(http_url); + } catch (e) { + if (localStorage.getItem("remoteOrigin") == http_url) { + localStorage.removeItem("remoteOrigin"); + } + throw new Error("Fail to parse Leagdo remoteOrigin: " + e); + } + const { protocol, hostname, port, origin } = url; if (!protocol.startsWith("http")) - throw new Error("unexpect protocol:" + http_url); - ajax.defaults.baseURL = http_url; - legado_http_origin = http_url; + throw new Error("unexpect protocol: " + http_url); + ajax.defaults.baseURL = origin; + //持久化 + localStorage.setItem("remoteOrigin", origin); + legado_http_origin = origin; if (port !== "") { legado_webSocket_port = Number(port) + 1; } else { diff --git a/modules/web/src/views/BookShelf.vue b/modules/web/src/views/BookShelf.vue index 500e65040..1a775a545 100644 --- a/modules/web/src/views/BookShelf.vue +++ b/modules/web/src/views/BookShelf.vue @@ -50,7 +50,7 @@ size="large" class="setting-connect" :class="{ 'no-point': newConnect }" - @click="setIP" + @click="setLegadoRetmoteUrl" > {{ connectStatus }} @@ -175,35 +175,60 @@ export default defineComponent({ const connectStatus = computed(() => store.connectStatus); const connectType = computed(() => store.connectType); const newConnect = computed(() => store.newConnect); - const setIP = () => { + const setLegadoRetmoteUrl = () => { ElMessageBox.prompt( - "请输入 IP 和端口 ( 如:127.0.0.1:9527 或者通过内网穿透的地址)", + "请输入 后端地址 ( 如:http://127.0.0.1:9527 或者通过内网穿透的地址)", "提示", { confirmButtonText: "确定", cancelButtonText: "取消", - inputPattern: - /^[a-zA-Z0-9][-a-zA-Z0-9]{0,62}(\.[a-zA-Z0-9][-a-zA-Z0-9]{0,62})+\.?:([1-9]|[1-9][0-9]|[1-9][0-9][0-9]|[1-9][0-9][0-9][0-9]|[1-6][0-5][0-5][0-3][0-5])$/, - inputErrorMessage: "url 形式不正确", + inputPlaceholder: API.legado_http_origin, + inputValidator: (url) => { + try { + const { + origin, + protocol, + username, + password, + pathname, + search, + hash, + } = new URL(url); + console.log(new URL(url)); + if (origin == API.legado_http_origin) + return "请输入非当前远程链接"; + if (!protocol.startsWith("http")) return `不支持协议${protocol}`; + if ( + pathname !== "/" || + search !== "" || + hash !== "" || + password !== "" || + username !== "" + ) + return `目前仅支持输入${origin}`; + } catch (e) { + console.warn(e); + return "URL解析失败,请重新输入"; + } + return true; + }, beforeClose: (action, instance, done) => { if (action === "confirm") { store.setNewConnect(true); instance.confirmButtonLoading = true; instance.confirmButtonText = "校验中……"; // instance.inputValue - const ip = instance.inputValue; - API.testLeagdoHttpUrlConnection("http://" + ip) + const url = instance.inputValue; + API.testLeagdoHttpUrlConnection(url) //API.getBookShelf() .then(function (configStr) { saveReadConfig(configStr); instance.confirmButtonLoading = false; store.setConnectType("success"); - store.setConnectStatus("已连接 " + ip); + store.setConnectStatus("已连接 " + url); store.clearSearchBooks(); store.setNewConnect(false); - API.setLeagdoHttpUrl("http://" + ip); - //持久化 - localStorage.setItem("remoteIp", ip); + API.setLeagdoHttpUrl(url); fetchBookShelfData(); done(); }) @@ -344,7 +369,7 @@ export default defineComponent({ }); }); return { - setIP, + setLegadoRetmoteUrl, isNight, connectStatus, connectType,