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
+17 -4
View File
@@ -1,9 +1,11 @@
import React, { useContext, useEffect, useState } from 'react';
import { HashRouter as Router, Route, Switch } from 'react-router-dom';
import './App.css';
import { LoginContext, loginContextDefault, GlobalConfContext } from './utils/context';
import { LoginStatus, GlobalConf } from './utils/type';
import { Admin } from './pages/admin';
import { getGlobalConf } from './api/config';
import { Auth } from './pages/auth';
import 'antd/dist/antd.css';
@@ -15,7 +17,9 @@ function LoginSwitch() {
return (
<div>
not login
<button onClick={() => save({login: true, type: 'admin', name: ''})}>1</button>
<button onClick={() => save({
login: true, type: 'admin', name: '', id: '123', token: ''
})}>1</button>
</div>
)
}
@@ -35,9 +39,18 @@ function App() {
}, []);
return (
<LoginContext.Provider value={{login: loginStatus, save}}>
<GlobalConfContext.Provider value={globalConf}>
<LoginSwitch />
</GlobalConfContext.Provider>
<GlobalConfContext.Provider value={globalConf}>
<Router>
<Switch>
<Route path="/auth/:code">
<Auth />
</Route>
<Route path="/admin/">
<LoginSwitch />
</Route>
</Switch>
</Router>
</GlobalConfContext.Provider>
</LoginContext.Provider>
);
}
+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);
})
+27
View File
@@ -0,0 +1,27 @@
import React, {useContext, useEffect, useState} from "react";
import { useParams } from "react-router";
import { auth } from '../api/config';
import { LoginContext } from '../utils/context';
import { Redirect } from 'react-router-dom'
interface AuthParam {
code: string
}
export function Auth() {
const { code } = useParams<AuthParam>();
const [ content, contentUpdate ] = useState(<div>Logining...</div>);
const { save } = useContext(LoginContext);
useEffect(() => {
const loginFun = async () => {
const resp = await auth(code);
if (resp.status === 200) {
save({login: true, type: resp.type, name: resp.name, id: resp.id, token: resp.token});
contentUpdate(_ => <Redirect to={{pathname: '/admin'}} />);
sessionStorage.setItem('token', resp.token);
} else {
contentUpdate(_ => <div></div>);
}
}
loginFun();
}, [])
return content;
}
+4 -1
View File
@@ -5,7 +5,10 @@ export const loginContextDefault: LoginContextType = {
login: {
login: false,
type: '',
name: ''
name: '',
id: '123',
// groups: [],
token: ''
},
save: () => {}
};
+21 -5
View File
@@ -1,7 +1,15 @@
interface QQGroup {
id: string,
name: string,
}
export interface LoginStatus {
login: boolean
type: String
name: String
type: string
name: string
id: string
// groups: Array<QQGroup>
token: string
}
export type LoginContextType = {
@@ -10,10 +18,10 @@ export type LoginContextType = {
}
export interface SubscribeConfig {
platform: String
target?: String
platform: string
target?: string
catetories: Array<number>
tags: Array<String>
tags: Array<string>
}
export interface GlobalConf {
@@ -27,3 +35,11 @@ export interface PlatformConfig {
platformName: string,
hasTarget: boolean
}
export interface TokenResp {
status: number,
token: string,
type: string,
id: string
name: string
}