Initial commit

This commit is contained in:
JustSong
2023-04-22 20:39:27 +08:00
committed by GitHub
commit ab1f8a2bf4
87 changed files with 6933 additions and 0 deletions

13
web/src/helpers/api.js Normal file
View File

@@ -0,0 +1,13 @@
import { showError } from './utils';
import axios from 'axios';
export const API = axios.create({
baseURL: process.env.REACT_APP_SERVER ? process.env.REACT_APP_SERVER : '',
});
API.interceptors.response.use(
(response) => response,
(error) => {
showError(error);
}
);

View File

@@ -0,0 +1,10 @@
export function authHeader() {
// return authorization header with jwt token
let user = JSON.parse(localStorage.getItem('user'));
if (user && user.token) {
return { 'Authorization': 'Bearer ' + user.token };
} else {
return {};
}
}

View File

@@ -0,0 +1,3 @@
import { createBrowserHistory } from 'history';
export const history = createBrowserHistory();

4
web/src/helpers/index.js Normal file
View File

@@ -0,0 +1,4 @@
export * from './history';
export * from './auth-header';
export * from './utils';
export * from './api';

99
web/src/helpers/utils.js Normal file
View File

@@ -0,0 +1,99 @@
import { toast } from 'react-toastify';
import { toastConstants } from '../constants';
export function isAdmin() {
let user = localStorage.getItem('user');
if (!user) return false;
user = JSON.parse(user);
return user.role >= 10;
}
export function isRoot() {
let user = localStorage.getItem('user');
if (!user) return false;
user = JSON.parse(user);
return user.role >= 100;
}
export async function copy(text) {
let okay = true;
try {
await navigator.clipboard.writeText(text);
} catch (e) {
okay = false;
console.error(e);
}
return okay;
}
export function isMobile() {
return window.innerWidth <= 600;
}
let showErrorOptions = { autoClose: toastConstants.ERROR_TIMEOUT };
let showSuccessOptions = { autoClose: toastConstants.SUCCESS_TIMEOUT };
let showInfoOptions = { autoClose: toastConstants.INFO_TIMEOUT };
let showNoticeOptions = { autoClose: false };
if (isMobile()) {
showErrorOptions.position = 'top-center';
// showErrorOptions.transition = 'flip';
showSuccessOptions.position = 'top-center';
// showSuccessOptions.transition = 'flip';
showInfoOptions.position = 'top-center';
// showInfoOptions.transition = 'flip';
showNoticeOptions.position = 'top-center';
// showNoticeOptions.transition = 'flip';
}
export function showError(error) {
console.error(error);
if (error.message) {
if (error.name === 'AxiosError') {
switch (error.message) {
case 'Request failed with status code 429':
toast.error('错误:请求次数过多,请稍后再试!', showErrorOptions);
break;
case 'Request failed with status code 500':
toast.error('错误:服务器内部错误,请联系管理员!', showErrorOptions);
break;
case 'Request failed with status code 405':
toast.info('本站仅作演示之用,无服务端!');
break;
default:
toast.error('错误:' + error.message, showErrorOptions);
}
return;
}
toast.error('错误:' + error.message, showErrorOptions);
} else {
toast.error('错误:' + error, showErrorOptions);
}
}
export function showSuccess(message) {
toast.success(message, showSuccessOptions);
}
export function showInfo(message) {
toast.info(message, showInfoOptions);
}
export function showNotice(message) {
toast.info(message, showNoticeOptions);
}
export function openPage(url) {
window.open(url);
}
export function removeTrailingSlash(url) {
if (url.endsWith('/')) {
return url.slice(0, -1);
} else {
return url;
}
}