This commit is contained in:
felinae98
2021-09-24 15:48:53 +08:00
parent 31c5e283ba
commit dce58580f2
16 changed files with 370 additions and 20 deletions
+7 -3
View File
@@ -1,9 +1,13 @@
import axios from "axios";
import { GlobalConf } from "../utils/type";
const baseUrl = '/hk_reporter/api/'
import { GlobalConf, TokenResp } from "../utils/type";
import { baseUrl } from './utils';
export async function getGlobalConf(): Promise<GlobalConf> {
const res = await axios.get<GlobalConf>(`${baseUrl}global_conf`);
return res.data;
}
export async function auth(token: string): Promise<TokenResp> {
const res = await axios.get<TokenResp>(`${baseUrl}auth`, {params: {token}});
return res.data;
}
+21
View File
@@ -0,0 +1,21 @@
import axios from "axios";
// import { useContext } from 'react';
// import { LoginContext } from "../utils/context";
export const baseUrl = '/hk_reporter/api/'
// const loginStatus = useContext(LoginContext);
axios.interceptors.request.use(function (config) {
if (config.url && config.url.startsWith(baseUrl) && config.url !== `${baseUrl}auth`
&& config.url !== `${baseUrl}global_conf`) {
const token = sessionStorage.getItem('token');
if (token) {
config.headers['Authorization'] = `Bearer ${token}`;
} else {
throw new axios.Cancel('User not login');
}
}
return config;
}, function (error) {
return Promise.reject(error);
})