1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253 |
- import React from 'react';
- import Redirect from 'umi/redirect';
- import { apiUrl } from '@/utils/sldconfig';
- export default ({ children }) => {
- let cur_time = new Date().getTime();
- let start_time = localStorage.getItem('time');
- if (typeof start_time == 'undefined' || start_time == null) {
- return <Redirect to="/user/login"/>;
- } else {
- if (cur_time - start_time > 58 * 60 * 1000) {
- //用户token过期之后重新根据refresh_token获取token(58分钟,token的有效期是60分钟)
- let param = new FormData();
- param.append('grant_type', 'refresh_token');
- param.append('refresh_token', localStorage.getItem('sld_refresh_token'));
- fetch(apiUrl + 'v3/adminLogin/oauth/token', {
- credentials: 'include',
- headers: {
- Authorization: 'Basic YWRtaW46YWRtaW4=',
- },
- method: 'POST',
- body: param,
- }).then(response => response.json())
- .then(res => {
- if (res.state == 200) {
- localStorage.setItem('sld_token', res.data.access_token);
- localStorage.setItem('sld_refresh_token', res.data.refresh_token);
- } else {
- return <Redirect to="/user/login"/>;
- }
- });
- } else if (cur_time - start_time > ((24 * 60 * 15 - 2) * 60 * 1000)) {
- //用户登陆过期之后直接跳转登录页面(14天23小时58分钟,refresh_token的有效期是15天)
- return <Redirect to="/user/login"/>;
- }
- }
- //如果redirect在用户拥有的路由内,则跳转,否则跳转第一个页面
- if (localStorage.sld_all_routes != undefined) {
- let all_routes = JSON.parse(localStorage.sld_all_routes);
- let contain_redirect_flag = false;
- for (let i = 0; i < all_routes.length; i++) {
- if (children.props.location.pathname.indexOf("/manage_product/") >-1 || children.props.location.pathname.indexOf(all_routes[i]) > -1) {
- contain_redirect_flag = true;
- break;
- }
- }
- if (!contain_redirect_flag) {
- return <Redirect to={all_routes[0]}/>;
- }
- }
- return children;
- };
|