This commit is contained in:
2024-02-25 08:27:01 +08:00
commit 20c1fa08dc
279 changed files with 78489 additions and 0 deletions

105
src/middleware/Api.ts Normal file
View File

@@ -0,0 +1,105 @@
import axios from "axios";
import Auth from "./Auth";
import i18next from "../i18n";
export const baseURL = "/api/v3";
export const getBaseURL = () => {
return baseURL;
};
export const getPreviewURL = (
isShare: boolean,
shareID: any,
fileID: any,
path: any
): string => {
return (
getBaseURL() +
(isShare
? "/share/preview/" +
shareID +
(path !== "" ? "?path=" + encodeURIComponent(path) : "")
: "/file/preview/" + fileID)
);
};
// eslint-disable-next-line @typescript-eslint/ban-ts-ignore
// @ts-ignore
const instance = axios.create({
baseURL: getBaseURL(),
withCredentials: true,
});
export class AppError extends Error {
constructor(message: string | undefined, public code: any, error: any) {
super(message);
this.code = code;
if (i18next.exists(`errors.${code}`, { ns: "common" })) {
this.message = i18next.t(`errors.${code}`, {
ns: "common",
message,
});
} else if (i18next.exists(`vasErrors.${code}`, { ns: "common" })) {
this.message = i18next.t(`vasErrors.${code}`, {
ns: "common",
message,
});
} else if (i18next.exists(`errors.${code}`, { ns: "dashboard" })) {
this.message = i18next.t(`errors.${code}`, {
ns: "dashboard",
message,
});
} else {
this.message =
message || i18next.t("unknownError", { ns: "common" });
}
this.message +=
error && !this.message.includes(error) ? ` (${error})` : "";
this.stack = new Error().stack;
}
}
instance.interceptors.response.use(
function (response: any) {
response.rawData = response.data;
response.data = response.data.data;
if (
response.rawData.code !== undefined &&
response.rawData.code !== 0 &&
response.rawData.code !== 203
) {
// Login expired
if (response.rawData.code === 401) {
Auth.signout();
window.location.href =
"/login?redirect=" +
encodeURIComponent(
window.location.pathname + window.location.search
);
}
// Non-admin
if (response.rawData.code === 40008) {
window.location.href = "/home";
}
// Not binding mobile phone
if (response.rawData.code === 40010) {
window.location.href = "/setting?modal=phone";
}
throw new AppError(
response.rawData.msg,
response.rawData.code,
response.rawData.error
);
}
return response;
},
function (error) {
return Promise.reject(error);
}
);
export default instance;

52
src/middleware/Auth.ts Normal file
View File

@@ -0,0 +1,52 @@
const Auth = {
isAuthenticated: false,
authenticate(cb: any) {
Auth.SetUser(cb);
Auth.isAuthenticated = true;
},
GetUser() {
return JSON.parse(localStorage.getItem("user") || "null");
},
SetUser(newUser: any) {
localStorage.setItem("user", JSON.stringify(newUser));
},
Check(): boolean {
if (Auth.isAuthenticated) {
return true;
}
if (localStorage.getItem("user") !== null) {
return !Auth.GetUser().anonymous;
}
return false;
},
signout() {
Auth.isAuthenticated = false;
const oldUser = Auth.GetUser();
oldUser.id = 0;
localStorage.setItem("user", JSON.stringify(oldUser));
},
SetPreference(key: string, value: any) {
let preference = JSON.parse(
localStorage.getItem("user_preference") || "{}"
);
preference = preference == null ? {} : preference;
preference[key] = value;
localStorage.setItem("user_preference", JSON.stringify(preference));
},
GetPreference(key: string): any | null {
const preference = JSON.parse(
localStorage.getItem("user_preference") || "{}"
);
if (preference && preference[key] !== undefined) {
return preference[key];
}
return null;
},
GetPreferenceWithDefault(key: string, defaultVal: any): any {
return Auth.GetPreference(key) !== null
? Auth.GetPreference(key)
: defaultVal;
},
};
export default Auth;

View File

@@ -0,0 +1,25 @@
import React from "react";
import Auth from "./Auth";
import { Route, Redirect } from "react-router-dom";
function AuthRoute({ children, ...rest }) {
return (
<Route
{...rest}
render={({ location }) =>
Auth.Check(rest.isLogin) ? (
children
) : (
<Redirect
to={{
pathname: "/login",
state: { from: location },
}}
/>
)
}
/>
);
}
export default AuthRoute;

98
src/middleware/Init.js Normal file
View File

@@ -0,0 +1,98 @@
import API from "./Api";
import Auth from "./Auth";
import pathHelper from "../utils/page";
import {
changeViewMethod,
setSiteConfig,
toggleSnackbar,
} from "../redux/explorer";
import i18next from "../i18n";
import { msDocPreviewSuffix, setWopiExts } from "../config";
const initUserConfig = (siteConfig) => {
if (siteConfig.user !== undefined && !siteConfig.user.anonymous) {
const themes = JSON.parse(siteConfig.themes);
const user = siteConfig.user;
delete siteConfig.user;
//更换用户自定配色
if (
user["preferred_theme"] !== "" &&
themes[user["preferred_theme"]] !== undefined
) {
siteConfig.theme = themes[user["preferred_theme"]];
}
// 更新登录态
Auth.authenticate(user);
}
if (siteConfig.user !== undefined && siteConfig.user.anonymous) {
Auth.SetUser(siteConfig.user);
}
return siteConfig;
};
export const InitSiteConfig = (rawStore) => {
// 从缓存获取默认配置
const configCache = JSON.parse(localStorage.getItem("siteConfigCache"));
if (configCache != null) {
rawStore.siteConfig = configCache;
}
// 检查是否有path参数
const url = new URL(window.location.href);
const c = url.searchParams.get("path");
rawStore.navigator.path = c === null ? "/" : c;
// 初始化用户个性配置
rawStore.siteConfig = initUserConfig(rawStore.siteConfig);
// 更改站点标题
document.title = rawStore.siteConfig.title;
return rawStore;
};
export async function UpdateSiteConfig(store) {
API.get("/site/config")
.then(function (response) {
const themes = JSON.parse(response.data.themes);
response.data.theme = themes[response.data.defaultTheme];
response.data = initUserConfig(response.data);
store.dispatch(setSiteConfig(response.data));
localStorage.setItem(
"siteConfigCache",
JSON.stringify(response.data)
);
// 更新 office WOPI 预览后缀
if (response.data.wopi_exts) {
setWopiExts(response.data.wopi_exts);
}
// 偏爱的列表样式
const preferListMethod = Auth.GetPreference("view_method");
if (preferListMethod) {
store.dispatch(changeViewMethod(preferListMethod));
} else {
if (pathHelper.isSharePage(window.location.pathname)) {
store.dispatch(
changeViewMethod(response.data.share_view_method)
);
} else {
store.dispatch(
changeViewMethod(response.data.home_view_method)
);
}
}
})
.catch(function (error) {
store.dispatch(
toggleSnackbar(
"top",
"right",
i18next.t("errLoadingSiteConfig", { ns: "common" }) +
error.message,
"error"
)
);
});
}

View File

@@ -0,0 +1,25 @@
import React from "react";
import Auth from "./Auth";
import { Route, Redirect } from "react-router-dom";
function NoAuthRoute({ children, ...rest }) {
return (
<Route
{...rest}
render={({ location }) =>
!Auth.Check(rest.isLogin) ? (
children
) : (
<Redirect
to={{
pathname: "/home",
state: { from: location },
}}
/>
)
}
/>
);
}
export default NoAuthRoute;