Skip to content

Commit

Permalink
fix(modules/web): fix legado remoteURl parse
Browse files Browse the repository at this point in the history
  • Loading branch information
Xwite committed Oct 9, 2024
1 parent fbf6b72 commit 87fb73f
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 19 deletions.
2 changes: 1 addition & 1 deletion modules/web/src/api/axios.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
});
Expand Down
20 changes: 15 additions & 5 deletions modules/web/src/api/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
51 changes: 38 additions & 13 deletions modules/web/src/views/BookShelf.vue
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
size="large"
class="setting-connect"
:class="{ 'no-point': newConnect }"
@click="setIP"
@click="setLegadoRetmoteUrl"
>
{{ connectStatus }}
</el-tag>
Expand Down Expand Up @@ -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();
})
Expand Down Expand Up @@ -344,7 +369,7 @@ export default defineComponent({
});
});
return {
setIP,
setLegadoRetmoteUrl,
isNight,
connectStatus,
connectType,
Expand Down

0 comments on commit 87fb73f

Please sign in to comment.