Commit 31ac0822 authored by cellee's avatar cellee

Signed-off-by: cellee <893264950@qq.com>

parent c74234e0
...@@ -7,6 +7,7 @@ import { ConnectState } from '@/models/connect'; ...@@ -7,6 +7,7 @@ import { ConnectState } from '@/models/connect';
import { CurrentUser } from '@/models/user'; import { CurrentUser } from '@/models/user';
import HeaderDropdown from '../HeaderDropdown'; import HeaderDropdown from '../HeaderDropdown';
import styles from './index.less'; import styles from './index.less';
import { logOut } from '@/utils/log';
export interface GlobalHeaderRightProps extends Partial<ConnectProps> { export interface GlobalHeaderRightProps extends Partial<ConnectProps> {
currentUser?: CurrentUser; currentUser?: CurrentUser;
...@@ -19,40 +20,8 @@ class AvatarDropdown extends React.Component<GlobalHeaderRightProps> { ...@@ -19,40 +20,8 @@ class AvatarDropdown extends React.Component<GlobalHeaderRightProps> {
if (key === 'logout') { if (key === 'logout') {
const { dispatch } = this.props; const { dispatch } = this.props;
if (dispatch) { if (dispatch) {
// dispatch({ logOut();
// type: 'user/deleteCurrent',
// });
// 清掉所有localStorage缓存
// localStorage.clear();
// 只用清掉当前账户的 localStorage
localStorage.removeItem('userInfo');
// 清掉本域名所有cookie
let keys = document.cookie.match(/[^ =;]+(?==)/g);
if (keys) {
for (var i = keys.length; i--; ) {
document.cookie = keys[i] + '=0;path=/;expires=' + new Date(0).toUTCString();
document.cookie =
keys[i] +
'=0;path=/;domain=' +
document.domain +
';expires=' +
new Date(0).toUTCString();
document.cookie =
keys[i] + '=0;path=/;domain=ratingdog.cn;expires=' + new Date(0).toUTCString();
}
}
// 在返回登录页
// 刷新一下,避免 dva 的数据还存在
window.location.pathname = '/';
// dispatch({
// type: 'login/logout',
// });
} }
return; return;
} }
......
...@@ -79,7 +79,7 @@ const BasicLayout: React.FC<BasicLayoutProps> = (props) => { ...@@ -79,7 +79,7 @@ const BasicLayout: React.FC<BasicLayoutProps> = (props) => {
* constructor * constructor
*/ */
useEffect(() => { useEffect(() => {
console.log(login); // console.log(login);
if (dispatch) { if (dispatch) {
// console.log('Maple'); // console.log('Maple');
dispatch({ dispatch({
......
...@@ -6,6 +6,8 @@ import { ConnectState } from '@/models/connect'; ...@@ -6,6 +6,8 @@ import { ConnectState } from '@/models/connect';
import { CurrentUser } from '@/models/user'; import { CurrentUser } from '@/models/user';
import { getCookie } from '@/utils/method'; import { getCookie } from '@/utils/method';
import { message } from 'antd'; import { message } from 'antd';
import moment from 'moment';
import { logOut } from '@/utils/log';
interface SecurityLayoutProps extends ConnectProps { interface SecurityLayoutProps extends ConnectProps {
loading?: boolean; loading?: boolean;
...@@ -24,28 +26,6 @@ class SecurityLayout extends React.Component<SecurityLayoutProps, SecurityLayout ...@@ -24,28 +26,6 @@ class SecurityLayout extends React.Component<SecurityLayoutProps, SecurityLayout
token: '', token: '',
}; };
// componentDidMount() {
// console.log('componentDidMount触发了23333333');
// this.setState({
// isReady: true,
// });
// const { dispatch, login } = this.props;
// console.log('安全登录中');
// console.log(login);
// if (dispatch && !!login?.userName && !!login?.password) {
// dispatch({
// type: 'user/fetchCurrent',
// playload: {
// userName: login.userName,
// password: login.password,
// },
// });
// } else {
// history.push('/user/login');
// }
// }
render() { render() {
const { children, loading } = this.props; const { children, loading } = this.props;
// You can replace it to your authentication rule (such as check token exists) // You can replace it to your authentication rule (such as check token exists)
...@@ -103,42 +83,51 @@ class SecurityLayout extends React.Component<SecurityLayoutProps, SecurityLayout ...@@ -103,42 +83,51 @@ class SecurityLayout extends React.Component<SecurityLayoutProps, SecurityLayout
{ path: '/AccountManagement/account/Add', title: '添加账号', key: '65' }, { path: '/AccountManagement/account/Add', title: '添加账号', key: '65' },
]; ];
const permission = JSON.parse(localStorage.getItem('permission') || '[]');
// 用户重新打开需要重新登录 // 用户重新打开需要重新登录
const tokenLogin = JSON.parse(sessionStorage.getItem('token') || '[]'); let time = localStorage.getItem('loginTime');
if (tokenLogin.length == 0) { const tokenLogin = time ? JSON.parse(time) : '';
message.warning('Login Expired !');
return <Redirect to={`/user/login`} />;
}
let perObj = perList.filter((obj) => {
return obj.path === location.pathname;
});
if (perObj.length !== 0) {
if (permission.indexOf(perObj[0].key) < 0) {
history.push('/403');
}
}
if (history.location.pathname == '/') {
// const isLogin = currentUser && currentUser.name;//isLogin为关键点 // const isLogin = currentUser && currentUser.name;//isLogin为关键点
var isLogin = false; var isLogin = false;
if (getCookie('token').length != 0) { if (getCookie('token').length != 0) {
isLogin = true; isLogin = true;
} }
const queryString = stringify({ const queryString = stringify({
redirect: window.location.href, redirect: window.location.href,
}); });
if (!isLogin && loading) { if (!isLogin && loading) {
// console.log("页面加载")
return <PageLoading />; return <PageLoading />;
} }
if (!isLogin && window.location.pathname !== '/user/login') { if (!isLogin && window.location.pathname !== '/user/login') {
return <Redirect to={`/user/login?${queryString}`} />; return <Redirect to={`/user/login?${queryString}`} />;
} }
return children;
} else if (tokenLogin && moment().diff(moment(tokenLogin), 'minutes') < 30) {
// 刷新时间
localStorage.setItem('loginTime', JSON.stringify(moment().format('YYYY-MM-DD HH:mm:ss')));
} else {
// 退出登录
message.warning('Expired login !');
setTimeout(function () {
logOut();
}, 1000);
return false;
}
// 在判断权限
const permission = JSON.parse(localStorage.getItem('permission') || '[]');
let perObj = perList.filter((obj) => {
return obj.path === location.pathname;
});
if (perObj.length !== 0) {
if (permission.indexOf(perObj[0].key) < 0) {
history.push('/403');
}
}
return children; return children;
} }
} }
......
...@@ -7,6 +7,9 @@ import { getPageQuery } from '@/utils/utils'; ...@@ -7,6 +7,9 @@ import { getPageQuery } from '@/utils/utils';
import { setCookie, getCookie } from '@/utils/method'; import { setCookie, getCookie } from '@/utils/method';
import { message } from 'antd'; import { message } from 'antd';
import moment from 'moment';
export interface StateType { export interface StateType {
status?: 'ok' | 'error'; status?: 'ok' | 'error';
type?: string; type?: string;
...@@ -67,9 +70,8 @@ const Model: LoginModelType = { ...@@ -67,9 +70,8 @@ const Model: LoginModelType = {
effects: { effects: {
*login({ payload }, { call, put }) { *login({ payload }, { call, put }) {
const response = yield call(AccountLogin, payload); const response = yield call(AccountLogin, payload);
console.log(response);
if (response.status === 'resetPwd') { if (response.status === 'resetPwd') {
console.log('跳转到修改密码页面'); // console.log('跳转到修改密码页面');
yield put({ yield put({
type: 'resetPwd', type: 'resetPwd',
result: true, result: true,
...@@ -83,12 +85,9 @@ const Model: LoginModelType = { ...@@ -83,12 +85,9 @@ const Model: LoginModelType = {
payload: response, payload: response,
}); });
// Login successfully response.status === 'ok' // Login successfully response.status === 'ok'
console.log('用户名密码登录成功');
console.log(response);
message.success('Login Successfully!', 3); message.success('Login Successfully!', 3);
const userMessage = yield call(AccountCheckLogin, payload); //请求时间网络 const userMessage = yield call(AccountCheckLogin, payload); //请求时间网络
console.log(userMessage);
console.log(userMessage.token);
setCookie('token', userMessage.token); setCookie('token', userMessage.token);
setCookie('id', userMessage.userModel.id); setCookie('id', userMessage.userModel.id);
setCookie('name', userMessage.userModel.tosUserName); setCookie('name', userMessage.userModel.tosUserName);
...@@ -96,9 +95,9 @@ const Model: LoginModelType = { ...@@ -96,9 +95,9 @@ const Model: LoginModelType = {
// 更新用户信息 // 更新用户信息
localStorage.setItem('userInfo', JSON.stringify(userMessage)); localStorage.setItem('userInfo', JSON.stringify(userMessage));
localStorage.setItem('permission', JSON.stringify(userMessage.permission)); localStorage.setItem('permission', JSON.stringify(userMessage.permission));
console.log('获取到Token:' + getCookie('token')); // console.log('获取到Token:' + getCookie('token'));
// 设置 session 登录 token // 设置 登录的时间
sessionStorage.setItem('token', JSON.stringify(userMessage.token)); localStorage.setItem('loginTime', JSON.stringify(moment().format('YYYY-MM-DD HH:mm:ss')));
yield put({ yield put({
type: 'saveToken', type: 'saveToken',
......
// import {message } from 'antd'; /*
* @Author: your name
export const printf = (playload: any,resp:any) => { * @Date: 2020-11-19 16:54:53
console.log("请求错误码:"+"("+playload.index+")" + resp.error_code) * @LastEditTime: 2020-12-10 17:29:08
console.log(playload) * @LastEditors: Please set LastEditors
} * @Description: In User Settings Edit
* @FilePath: \tostumi\src\utils\log.ts
*/
import { message } from 'antd';
export const printf = (playload: any, resp: any) => {
console.log('请求错误码:' + '(' + playload.index + ')' + resp.error_code);
console.log(playload);
};
// 退出登录
export const logOut = () => {
localStorage.removeItem('userInfo');
localStorage.removeItem('loginTime');
localStorage.removeItem('permission');
// 清掉本域名所有cookie
let keys = document.cookie.match(/[^ =;]+(?==)/g);
if (keys) {
for (var i = keys.length; i--; ) {
document.cookie = keys[i] + '=0;path=/;expires=' + new Date(0).toUTCString();
document.cookie =
keys[i] + '=0;path=/;domain=' + document.domain + ';expires=' + new Date(0).toUTCString();
document.cookie =
keys[i] + '=0;path=/;domain=ratingdog.cn;expires=' + new Date(0).toUTCString();
}
}
window.location.pathname = '/user/login';
};
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment