From 94aaee8d9b8451871a1d4b7bcb1746d55bca807a Mon Sep 17 00:00:00 2001 From: Cjsah Date: Sun, 16 Jun 2024 10:51:12 +0800 Subject: [PATCH] resolve refresh --- modules/web/src/api/axios.js | 19 +++++++++++++++---- modules/web/src/views/BookShelf.vue | 13 ++++--------- 2 files changed, 19 insertions(+), 13 deletions(-) diff --git a/modules/web/src/api/axios.js b/modules/web/src/api/axios.js index 352ce6f60..469deab9d 100644 --- a/modules/web/src/api/axios.js +++ b/modules/web/src/api/axios.js @@ -1,14 +1,25 @@ import axios from "axios"; const SECOND = 1000; - -export const baseUrl = () => { - return localStorage.getItem("remoteIp"); -}; +const remoteIp = ref(localStorage.getItem("remoteIp")); const ajax = axios.create({ // 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/views/BookShelf.vue b/modules/web/src/views/BookShelf.vue index 7c2fe2999..384c39e1d 100644 --- a/modules/web/src/views/BookShelf.vue +++ b/modules/web/src/views/BookShelf.vue @@ -91,7 +91,7 @@ import githubUrl from "@/assets/imgs/github.png"; import { useLoading } from "@/hooks/loading"; import { Search } from "@element-plus/icons-vue"; import API from "@api"; -import ajax from "@/api/axios.js"; +import { baseUrl, setRemoteIp } from "@/api/axios.js"; const store = useBookStore(); const { connectStatus, connectType, newConnect, shelf } = storeToRefs(store); @@ -164,15 +164,15 @@ const searchBook = () => { }; const ipInput = reactive({ - ip: "", + ip: baseUrl(), disable: true, }); const toggleIpConfig = () => { - ipInput.ip = localStorage.getItem("remoteIp"); + ipInput.ip = baseUrl(); ipInput.disable = !ipInput.disable; }; const setIP = () => { - localStorage.setItem("remoteIp", ipInput.ip); + setRemoteIp(ipInput.ip); ipInput.disable = true; loadShelf(); }; @@ -212,11 +212,6 @@ const toDetail = (bookUrl, bookName, bookAuthor, chapterIndex, chapterPos) => { onMounted(() => { //获取最近阅读书籍 let readingRecentStr = localStorage.getItem("readingRecent"); - ipInput.ip = localStorage.getItem("remoteIp"); - ajax.interceptors.request.use((config) => { - config.baseURL = ipInput.ip; - return config; - }); if (readingRecentStr != null) { readingRecent.value = JSON.parse(readingRecentStr); if (typeof readingRecent.value.chapterIndex == "undefined") {