fix: 某些情况下认证失败后不能再认证的问题

This commit is contained in:
cnwhy
2023-12-27 18:24:47 +08:00
parent b222ea4f1e
commit 0aaada76a2
5 changed files with 52 additions and 44 deletions
+8 -8
View File
@@ -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<any>(null);
const [localConfig, setLocalConfig] = useState<any>(null);
const [userTheme, setUserTheme] = useState<any>(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 (
<Ctx.Provider
@@ -102,7 +102,7 @@ function App() {
theme={{ algorithm: isDark ? theme.darkAlgorithm : undefined }}
locale={zhCN}
>
{gostInfo ? <Manage /> : <Home />}
{info ? <Manage /> : <Home />}
</ConfigProvider>
</Ctx.Provider>
);
+12 -12
View File
@@ -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 (
<Layout style={{ height: "100vh", overflow: "hidden" }}>
<Layout.Header style={{ color: "#FFF" }}>
@@ -102,7 +102,7 @@ const Manage = () => {
<Select placeholder="快捷操作"></Select>
</Space>
</Col>
<Col>{gostInfo.addr}</Col>
<Col>{info.addr}</Col>
<Col>
<Space>
<Space.Compact>
@@ -140,15 +140,15 @@ const Manage = () => {
footer={false}
>
<Form
initialValues={gostInfo}
initialValues={info}
layout="horizontal"
labelCol={{span:4}}
onValuesChange={(v, vs) => {
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);
}
}}
>
+2 -2
View File
@@ -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<T = any> {
this.type = type;
}
private get key() {
return getGost()?.addr;
return getInfo()?.addr;
}
private _getIdb = () => {
return getIdb(
+4 -4
View File
@@ -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(
+26 -18
View File
@@ -20,19 +20,27 @@ export type GostApiConfig = {
isLocal?: boolean | null;
};
export const useGolstCofnig = getUseValue<GostApiConfig | null>();
export const useInfo = getUseValue<GostApiConfig | null>();
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);
};