/*
 * @Author: your name
 * @Date: 2021-04-23 15:39:41
 * @LastEditTime: 2021-04-30 14:16:28
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: /react-antd-admin-template/src/router/index.js
 */
import React from "react";
import { HashRouter, Route, Switch, Redirect } from "react-router-dom";
import { connect } from "react-redux";
import { getUserInfo } from "@/store/actions";
import Layout from "@/views/layout";
import Login from "@/views/login";


const hospSign = JSON.parse(localStorage.getItem('hospSign'));
const params = hospSign?`?hospSign=${hospSign}`:'';
// console.log({hospSign});
class Router extends React.Component {
  render() {
    const { token, role, getUserInfo } = this.props;
    return (
      <HashRouter>
        <Switch>
          <Route exact path="/login" component={Login} />
          <Route
            path="/"
            render={() => {
              if (!token) {
                return <Redirect to={`/login${params}`} />;
              } else {
                if (role) {
                  return <Layout />;
                } else {
                  const userInfo = JSON.parse(localStorage.getItem("userInfo"));
                  // console.log({'来自local的userInfo':userInfo});
                  getUserInfo(userInfo).then(() => <Layout />);
                }
              }
            }}
          />
        </Switch>
      </HashRouter>
    );
  }
}

export default connect((state) => state.user, { getUserInfo })(Router);
