diff --git a/modules/web/.env.development b/modules/web/.env.development deleted file mode 100644 index 03aca1a74..000000000 --- a/modules/web/.env.development +++ /dev/null @@ -1 +0,0 @@ -VITE_API=http://192.168.10.11:1122 diff --git a/modules/web/.env.production b/modules/web/.env.production deleted file mode 100644 index b182964c3..000000000 --- a/modules/web/.env.production +++ /dev/null @@ -1 +0,0 @@ -VITE_API= diff --git a/modules/web/README.md b/modules/web/README.md index 02407cc80..91e5d7a7b 100644 --- a/modules/web/README.md +++ b/modules/web/README.md @@ -12,9 +12,8 @@ | Edge ≥ 79 | Firefox ≥ 78 | Chrome ≥ 64 | Safari ≥ 12 | ## 开发 -> 需要阅读app提供后端服务,开发前修改环境变量`VITE_API`为阅读web服务地址 +> 需要阅读app提供后端服务 ```bash -echo "VITE_API=http://:" > .env.development pnpm dev ``` diff --git a/modules/web/src/api/axios.js b/modules/web/src/api/axios.js index cb2c50f6b..469deab9d 100644 --- a/modules/web/src/api/axios.js +++ b/modules/web/src/api/axios.js @@ -1,10 +1,25 @@ import axios from "axios"; const SECOND = 1000; +const remoteIp = ref(localStorage.getItem("remoteIp")); const ajax = axios.create({ - baseURL: import.meta.env.VITE_API || location.origin, + // baseURL: import.meta.env.VITE_API || location.origin, timeout: 120 * SECOND, }); +ajax.interceptors.request.use((config) => { + config.baseURL = remoteIp.value; + return config; +}); + export default ajax; + +export const setRemoteIp = (ip) => { + remoteIp.value = ip; + localStorage.setItem("remoteIp", ip); +}; + +export const baseUrl = () => { + return remoteIp.value; +}; diff --git a/modules/web/src/api/index.js b/modules/web/src/api/index.js index aeb9f1838..c71f876dd 100644 --- a/modules/web/src/api/index.js +++ b/modules/web/src/api/index.js @@ -1,10 +1,13 @@ -import ajax from "./axios"; +import ajax, { baseUrl } 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.origin); +const getUrl = () => { + const { hostname, port } = new URL(baseUrl()); + return `${hostname}:${Number(port) + 1}`; +}; const isSourecEditor = /source/i.test(location.href); const APIExceptionHandler = (error) => { @@ -29,7 +32,7 @@ const saveBookProgressWithBeacon = (bookProgress) => { if (!bookProgress) return; // 常规请求可能会被取消 使用Fetch keep-alive 或者 navigator.sendBeacon navigator.sendBeacon( - `${import.meta.env.VITE_API || location.origin}/saveBookProgress`, + `${baseUrl()}/saveBookProgress`, JSON.stringify(bookProgress), ); }; @@ -56,7 +59,7 @@ const search = ( /** @type {() => void} */ onFinish, ) => { // webSocket - const url = `ws://${hostname}:${Number(port) + 1}/searchBook`; + const url = `ws://${getUrl()}/searchBook`; const socket = new WebSocket(url); socket.onopen = () => { @@ -100,7 +103,7 @@ const debug = ( /** @type {() => void} */ onFinish, ) => { // webSocket - const url = `ws://${hostname}:${Number(port) + 1}/${ + const url = `ws://${getUrl()}/${ isBookSource ? "bookSource" : "rssSource" }Debug`; diff --git a/modules/web/src/auto-imports.d.ts b/modules/web/src/auto-imports.d.ts index 964ac4fd0..567fdd4b1 100644 --- a/modules/web/src/auto-imports.d.ts +++ b/modules/web/src/auto-imports.d.ts @@ -82,5 +82,6 @@ declare global { // for type re-export declare global { // @ts-ignore - export type { Component, ComponentPublicInstance, ComputedRef, InjectionKey, PropType, Ref, VNode, WritableComputedRef } from 'vue' + export type { Component, ComponentPublicInstance, ComputedRef, ExtractDefaultPropTypes, ExtractPropTypes, ExtractPublicPropTypes, InjectionKey, PropType, Ref, VNode, WritableComputedRef } from 'vue' + import('vue') } diff --git a/modules/web/src/components/BookItems.vue b/modules/web/src/components/BookItems.vue index 333d4a969..c8543882c 100644 --- a/modules/web/src/components/BookItems.vue +++ b/modules/web/src/components/BookItems.vue @@ -50,15 +50,14 @@