Skip to content

Commit

Permalink
fix: 某些情况下认证失败后不能再认证的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
汪航洋 committed Dec 27, 2023
1 parent 4f2d097 commit f17c249
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 44 deletions.
16 changes: 8 additions & 8 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -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); // 用户主题
Expand All @@ -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);
Expand Down Expand Up @@ -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;
Expand All @@ -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
Expand All @@ -102,7 +102,7 @@ function App() {
theme={{ algorithm: isDark ? theme.darkAlgorithm : undefined }}
locale={zhCN}
>
{gostInfo ? <Manage /> : <Home />}
{info ? <Manage /> : <Home />}
</ConfigProvider>
</Ctx.Provider>
);
Expand Down
24 changes: 12 additions & 12 deletions src/Pages/Manage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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);
Expand All @@ -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 {
Expand All @@ -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();
};
Expand All @@ -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" }}>
Expand All @@ -102,7 +102,7 @@ const Manage = () => {
<Select placeholder="快捷操作"></Select>
</Space>
</Col>
<Col>{gostInfo.addr}</Col>
<Col>{info.addr}</Col>
<Col>
<Space>
<Space.Compact>
Expand Down Expand Up @@ -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);
}
}}
>
Expand Down
4 changes: 2 additions & 2 deletions src/api/local.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -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(
Expand Down
8 changes: 4 additions & 4 deletions src/uitls/require.ts
Original file line number Diff line number Diff line change
@@ -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(
Expand Down
44 changes: 26 additions & 18 deletions src/uitls/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,32 +20,34 @@ 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);
await login(server);
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]);
Expand All @@ -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) => {
Expand All @@ -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);
};

Expand Down

0 comments on commit f17c249

Please sign in to comment.