diff --git a/src/App.tsx b/src/App.tsx index 66f3703..724529c 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -1,7 +1,7 @@ import { useState, useEffect, useRef, useMemo, useCallback } from "react"; import { ConfigProvider, theme } from "antd"; import Ctx from "./uitls/ctx"; -import { init, logout, useGolstCofnig } from "./uitls/server"; +import { init, logout, useInfo } from "./uitls/server"; import * as API from "./api"; import Home from "./Pages/Home"; import "./App.css"; @@ -12,7 +12,7 @@ import Manage from "./Pages/Manage"; import { ServerComm } from "./api/local"; function App() { - const gostInfo = useGolstCofnig(); + const info = useInfo(); const [gostConfig, setGostConfig] = useState(null); const [localConfig, setLocalConfig] = useState(null); const [userTheme, setUserTheme] = useState(null); // 用户主题 @@ -25,7 +25,7 @@ function App() { // console.log("update"); const [l1, l2] = await Promise.all([ API.getConfig(), - slef.current.updateLocalConfig(useGolstCofnig.get()?.addr), + slef.current.updateLocalConfig(useInfo.get()?.addr), ]); setGostConfig(l1); setLocalConfig(l2); @@ -59,7 +59,7 @@ function App() { const localUpdate = async () => { // console.log("localUpdate"); return setLocalConfig( - await slef.current.updateLocalConfig(useGolstCofnig.get()?.addr) + await slef.current.updateLocalConfig(useInfo.get()?.addr) ); }; const update = slef.current.update; @@ -80,15 +80,15 @@ function App() { }, []); useEffect(() => { - if (gostInfo) { + if (info) { slef.current.update().then(([data]) => { setGostConfig(data); - document.title = gostInfo.addr.replace(/^(https?:)?\/\//, ""); + document.title = info.addr.replace(/^(https?:)?\/\//, ""); }); } else { document.title = slef.current.defaultTitle; } - }, [gostInfo]); + }, [info]); return ( - {gostInfo ? : } + {info ? : } ); diff --git a/src/Pages/Manage.tsx b/src/Pages/Manage.tsx index 0f6a89a..5b730af 100644 --- a/src/Pages/Manage.tsx +++ b/src/Pages/Manage.tsx @@ -12,7 +12,7 @@ import { Space, Switch, } from "antd"; -import { logout, saveLocal, useGolstCofnig } from "../uitls/server"; +import { logout, saveLocal, useInfo } from "../uitls/server"; import { download, jsonFormat } from "../uitls"; import Ctx from "../uitls/ctx"; import * as API from "../api"; @@ -44,7 +44,7 @@ const colSpan1 = { }; const Manage = () => { - const gostInfo = useGolstCofnig()!; + const info = useInfo()!; const { gostConfig } = useContext(Ctx); const [show, setShow] = useState(false); const [loading, setLoading] = useState(false); @@ -60,7 +60,7 @@ const Manage = () => { const onSave = (ref.current.onSave = async () => { try { setLoading(true); - const { saveFormat, savePath } = useGolstCofnig.get() || {}; + const { saveFormat, savePath } = useInfo.get() || {}; await API.saveCofnig(saveFormat, savePath); setIsSaved(true); } finally { @@ -70,12 +70,12 @@ const Manage = () => { const update = () => { setIsSaved(false); - if (!useGolstCofnig.get()?.autoSave) return; + if (!useInfo.get()?.autoSave) return; return onSave(); }; const onApiUpdate = async (reqConfig: any) => { setIsSaved(false); - if (!useGolstCofnig.get()?.autoSave) return; + if (!useInfo.get()?.autoSave) return; if (reqConfig.url === API.apis.config) return; return onSave(); }; @@ -87,7 +87,7 @@ const Manage = () => { configEvent.off("apiUpdate", onApiUpdate); }; }, []); - console.log('gostInfo', gostInfo) + console.log('gostInfo', info) return ( @@ -102,7 +102,7 @@ const Manage = () => { - {gostInfo.addr} + {info.addr} @@ -140,15 +140,15 @@ const Manage = () => { footer={false} >
{ console.log(v,vs) - Object.assign(gostInfo, v); - useGolstCofnig.set(gostInfo); - if (gostInfo.isLocal) { - saveLocal(gostInfo.addr, gostInfo); + Object.assign(info, v); + useInfo.set(info); + if (info.isLocal) { + saveLocal(info.addr, info); } }} > diff --git a/src/api/local.ts b/src/api/local.ts index a0173ea..1356f3b 100644 --- a/src/api/local.ts +++ b/src/api/local.ts @@ -1,6 +1,6 @@ import { getIdb, updatedIdb } from "./db"; import type * as Gost from "./types"; -import { getGost } from "../uitls/server"; +import { getInfo } from "../uitls/server"; import { configEvent } from "../uitls/events"; const localCache = "localCache"; const savedServer = "savedServer"; @@ -13,7 +13,7 @@ export class GostCommit { this.type = type; } private get key() { - return getGost()?.addr; + return getInfo()?.addr; } private _getIdb = () => { return getIdb( diff --git a/src/uitls/require.ts b/src/uitls/require.ts index 173c948..846aac8 100644 --- a/src/uitls/require.ts +++ b/src/uitls/require.ts @@ -1,13 +1,13 @@ import axios from "axios"; -import { getGost } from "./server"; +import { getInfo } from "./server"; import { message } from "antd"; import { configEvent } from "./events"; const require = axios.create(); require.interceptors.request.use((config) => { - const gost = getGost(); - config.baseURL = gost?.addr; - config.auth = gost?.auth; + const info = getInfo(); + config.baseURL = info?.addr; + config.auth = info?.auth; return config; }); require.interceptors.response.use( diff --git a/src/uitls/server.ts b/src/uitls/server.ts index 7bb48e2..eb4a0da 100644 --- a/src/uitls/server.ts +++ b/src/uitls/server.ts @@ -20,19 +20,27 @@ export type GostApiConfig = { isLocal?: boolean | null; }; -export const useGolstCofnig = getUseValue(); +export const useInfo = getUseValue(); Object.defineProperty(window, gostServerKey, { - get: useGolstCofnig.get, - set: useGolstCofnig.set, + get: useInfo.get, + set: useInfo.set, }); -export const getGost = (): GostApiConfig | null => useGolstCofnig.get(); +export const getInfo = (): GostApiConfig | null => useInfo.get(); export const init = async () => { // 内存 if (window[gostServerKey]) return true; - // sessionStorage + // 本地保存 + const query = qs.parse(location.search, { ignoreQueryPrefix: true }); + if (query.use) { + window[uselocalServerKey] = query.use; + window.history.replaceState(null, "", location.pathname); + logout(); // 清理sessionStorage + } + + // 刷新(会话保持) const serverJson = sessionStorage.getItem(gostServerKey); if (serverJson) { const server = JSON.parse(serverJson); @@ -40,12 +48,6 @@ export const init = async () => { return true; } - const query = qs.parse(location.search, { ignoreQueryPrefix: true }); - if (query.use) { - window[uselocalServerKey] = query.use; - window.history.replaceState(null, "", location.pathname); - } - // 本地保存的服务器信息 if (window[uselocalServerKey]) { const server = await getLocal(window[uselocalServerKey]); @@ -62,9 +64,13 @@ export const init = async () => { const verify = async (arg: GostApiConfig) => { const baseUrl = arg.addr.replace(/\/+$/, ""); - return axios.get(baseUrl + "/config", { - auth: arg.auth, - }); + return axios + .get(baseUrl + "/config", { + auth: arg.auth, + }) + .catch((error) => { + throw "verify error"; + }); }; export const login = async (arg: GostApiConfig, save?: false) => { @@ -74,18 +80,20 @@ export const login = async (arg: GostApiConfig, save?: false) => { window.sessionStorage.setItem(gostServerKey, JSON.stringify(arg)); if (save) { arg.isLocal = true; - window[gostServerKey] = arg + window[gostServerKey] = arg; await saveLocal(arg.addr, arg); } } catch (e: any) { - // console.log(e); - message.error(e?.message || "链接失败"); + if (e === "verify error") { + logout(); + message.error(e?.message || "连接失败"); + } throw e; } }; export const logout = async () => { - useGolstCofnig.set(null); + useInfo.set(null); window.sessionStorage.removeItem(gostServerKey); };