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';
import { CurrentUser } from '@/models/user';
import HeaderDropdown from '../HeaderDropdown';
import styles from './index.less';
import { logOut } from '@/utils/log';
export interface GlobalHeaderRightProps extends Partial<ConnectProps> {
currentUser?: CurrentUser;
......@@ -19,40 +20,8 @@ class AvatarDropdown extends React.Component<GlobalHeaderRightProps> {
if (key === 'logout') {
const { dispatch } = this.props;
if (dispatch) {
// dispatch({
// 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',
// });
logOut();
}
return;
}
......
......@@ -79,7 +79,7 @@ const BasicLayout: React.FC<BasicLayoutProps> = (props) => {
* constructor
*/
useEffect(() => {
console.log(login);
// console.log(login);
if (dispatch) {
// console.log('Maple');
dispatch({
......
......@@ -6,6 +6,8 @@ import { ConnectState } from '@/models/connect';
import { CurrentUser } from '@/models/user';
import { getCookie } from '@/utils/method';
import { message } from 'antd';
import moment from 'moment';
import { logOut } from '@/utils/log';
interface SecurityLayoutProps extends ConnectProps {
loading?: boolean;
......@@ -24,28 +26,6 @@ class SecurityLayout extends React.Component<SecurityLayoutProps, SecurityLayout
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() {
const { children, loading } = this.props;
// You can replace it to your authentication rule (such as check token exists)
......@@ -103,15 +83,41 @@ class SecurityLayout extends React.Component<SecurityLayoutProps, SecurityLayout
{ path: '/AccountManagement/account/Add', title: '添加账号', key: '65' },
];
const permission = JSON.parse(localStorage.getItem('permission') || '[]');
// 用户重新打开需要重新登录
const tokenLogin = JSON.parse(sessionStorage.getItem('token') || '[]');
if (tokenLogin.length == 0) {
message.warning('Login Expired !');
return <Redirect to={`/user/login`} />;
let time = localStorage.getItem('loginTime');
const tokenLogin = time ? JSON.parse(time) : '';
if (history.location.pathname == '/') {
// const isLogin = currentUser && currentUser.name;//isLogin为关键点
var isLogin = false;
if (getCookie('token').length != 0) {
isLogin = true;
}
const queryString = stringify({
redirect: window.location.href,
});
if (!isLogin && loading) {
return <PageLoading />;
}
if (!isLogin && window.location.pathname !== '/user/login') {
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;
});
......@@ -122,23 +128,6 @@ class SecurityLayout extends React.Component<SecurityLayoutProps, SecurityLayout
}
}
// const isLogin = currentUser && currentUser.name;//isLogin为关键点
var isLogin = false;
if (getCookie('token').length != 0) {
isLogin = true;
}
const queryString = stringify({
redirect: window.location.href,
});
if (!isLogin && loading) {
// console.log("页面加载")
return <PageLoading />;
}
if (!isLogin && window.location.pathname !== '/user/login') {
return <Redirect to={`/user/login?${queryString}`} />;
}
return children;
}
}
......
......@@ -7,6 +7,9 @@ import { getPageQuery } from '@/utils/utils';
import { setCookie, getCookie } from '@/utils/method';
import { message } from 'antd';
import moment from 'moment';
export interface StateType {
status?: 'ok' | 'error';
type?: string;
......@@ -67,9 +70,8 @@ const Model: LoginModelType = {
effects: {
*login({ payload }, { call, put }) {
const response = yield call(AccountLogin, payload);
console.log(response);
if (response.status === 'resetPwd') {
console.log('跳转到修改密码页面');
// console.log('跳转到修改密码页面');
yield put({
type: 'resetPwd',
result: true,
......@@ -83,12 +85,9 @@ const Model: LoginModelType = {
payload: response,
});
// Login successfully response.status === 'ok'
console.log('用户名密码登录成功');
console.log(response);
message.success('Login Successfully!', 3);
const userMessage = yield call(AccountCheckLogin, payload); //请求时间网络
console.log(userMessage);
console.log(userMessage.token);
setCookie('token', userMessage.token);
setCookie('id', userMessage.userModel.id);
setCookie('name', userMessage.userModel.tosUserName);
......@@ -96,9 +95,9 @@ const Model: LoginModelType = {
// 更新用户信息
localStorage.setItem('userInfo', JSON.stringify(userMessage));
localStorage.setItem('permission', JSON.stringify(userMessage.permission));
console.log('获取到Token:' + getCookie('token'));
// 设置 session 登录 token
sessionStorage.setItem('token', JSON.stringify(userMessage.token));
// console.log('获取到Token:' + getCookie('token'));
// 设置 登录的时间
localStorage.setItem('loginTime', JSON.stringify(moment().format('YYYY-MM-DD HH:mm:ss')));
yield put({
type: 'saveToken',
......
// import {message } from 'antd';
export const printf = (playload: any,resp:any) => {
console.log("请求错误码:"+"("+playload.index+")" + resp.error_code)
console.log(playload)
}
/*
* @Author: your name
* @Date: 2020-11-19 16:54:53
* @LastEditTime: 2020-12-10 17:29:08
* @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