fix(modules/web): fix legado remoteURl parse

This commit is contained in:
Xwite
2024-10-09 09:09:58 +08:00
parent fbf6b72a07
commit 87fb73fdd7
3 changed files with 54 additions and 19 deletions
+1 -1
View File
@@ -5,7 +5,7 @@ const SECOND = 1000;
const ajax = axios.create({ const ajax = axios.create({
baseURL: baseURL:
import.meta.env.VITE_API || import.meta.env.VITE_API ||
localStorage.getItem("remoteIp") || localStorage.getItem("remoteOrigin") ||
location.origin, location.origin,
timeout: 120 * SECOND, timeout: 120 * SECOND,
}); });
+15 -5
View File
@@ -8,12 +8,22 @@ let legado_http_origin;
let legado_webSocket_origin; let legado_webSocket_origin;
const setLeagdoHttpUrl = (http_url) => { const setLeagdoHttpUrl = (http_url) => {
let legado_webSocket_port; let legado_webSocket_port, url;
const { protocol, hostname, port } = new URL(http_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")) if (!protocol.startsWith("http"))
throw new Error("unexpect protocol:" + http_url); throw new Error("unexpect protocol: " + http_url);
ajax.defaults.baseURL = http_url; ajax.defaults.baseURL = origin;
legado_http_origin = http_url; //持久化
localStorage.setItem("remoteOrigin", origin);
legado_http_origin = origin;
if (port !== "") { if (port !== "") {
legado_webSocket_port = Number(port) + 1; legado_webSocket_port = Number(port) + 1;
} else { } else {
+38 -13
View File
@@ -50,7 +50,7 @@
size="large" size="large"
class="setting-connect" class="setting-connect"
:class="{ 'no-point': newConnect }" :class="{ 'no-point': newConnect }"
@click="setIP" @click="setLegadoRetmoteUrl"
> >
{{ connectStatus }} {{ connectStatus }}
</el-tag> </el-tag>
@@ -175,35 +175,60 @@ export default defineComponent({
const connectStatus = computed(() => store.connectStatus); const connectStatus = computed(() => store.connectStatus);
const connectType = computed(() => store.connectType); const connectType = computed(() => store.connectType);
const newConnect = computed(() => store.newConnect); const newConnect = computed(() => store.newConnect);
const setIP = () => { const setLegadoRetmoteUrl = () => {
ElMessageBox.prompt( ElMessageBox.prompt(
"请输入 IP 和端口 ( 如:127.0.0.1:9527 或者通过内网穿透的地址)", "请输入 后端地址 ( 如:http://127.0.0.1:9527 或者通过内网穿透的地址)",
"提示", "提示",
{ {
confirmButtonText: "确定", confirmButtonText: "确定",
cancelButtonText: "取消", cancelButtonText: "取消",
inputPattern: inputPlaceholder: API.legado_http_origin,
/^[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])$/, inputValidator: (url) => {
inputErrorMessage: "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) => { beforeClose: (action, instance, done) => {
if (action === "confirm") { if (action === "confirm") {
store.setNewConnect(true); store.setNewConnect(true);
instance.confirmButtonLoading = true; instance.confirmButtonLoading = true;
instance.confirmButtonText = "校验中……"; instance.confirmButtonText = "校验中……";
// instance.inputValue // instance.inputValue
const ip = instance.inputValue; const url = instance.inputValue;
API.testLeagdoHttpUrlConnection("http://" + ip) API.testLeagdoHttpUrlConnection(url)
//API.getBookShelf() //API.getBookShelf()
.then(function (configStr) { .then(function (configStr) {
saveReadConfig(configStr); saveReadConfig(configStr);
instance.confirmButtonLoading = false; instance.confirmButtonLoading = false;
store.setConnectType("success"); store.setConnectType("success");
store.setConnectStatus("已连接 " + ip); store.setConnectStatus("已连接 " + url);
store.clearSearchBooks(); store.clearSearchBooks();
store.setNewConnect(false); store.setNewConnect(false);
API.setLeagdoHttpUrl("http://" + ip); API.setLeagdoHttpUrl(url);
//持久化
localStorage.setItem("remoteIp", ip);
fetchBookShelfData(); fetchBookShelfData();
done(); done();
}) })
@@ -344,7 +369,7 @@ export default defineComponent({
}); });
}); });
return { return {
setIP, setLegadoRetmoteUrl,
isNight, isNight,
connectStatus, connectStatus,
connectType, connectType,