diff --git a/front/.gitignore b/front/.gitignore index 4d29575..4a5ec65 100644 --- a/front/.gitignore +++ b/front/.gitignore @@ -21,3 +21,4 @@ npm-debug.log* yarn-debug.log* yarn-error.log* +.vscode \ No newline at end of file diff --git a/front/package.json b/front/package.json index fb7abb8..7626ab0 100644 --- a/front/package.json +++ b/front/package.json @@ -10,6 +10,7 @@ "babel-plugin-import": "^1.12.0", "customize-cra": "^0.2.14", "less": "^3.9.0", + "query-string": "^6.8.1", "react": "^16.8.6", "react-app-rewired": "^2.1.3", "react-dom": "^16.8.6", @@ -42,4 +43,4 @@ "devDependencies": { "less-loader": "^5.0.0" } -} \ No newline at end of file +} diff --git a/front/src/App.jsx b/front/src/App.jsx index 7116ef4..b4f3bbd 100644 --- a/front/src/App.jsx +++ b/front/src/App.jsx @@ -4,7 +4,7 @@ import { withRouter } from "react-router-dom"; import { Provider } from "react-redux"; import store from "./redux"; import Login from "./pages/public/Login"; -import Main from "./pages/Main"; +import Main from "./pages/index"; import NotFound from "./pages/public/notFound/NotFound"; class App extends Component { @@ -15,7 +15,7 @@ class App extends Component { render() { const mainStyle = { - fontSize: "0.16rem" + fontSize: "0.14rem" }; return ( diff --git a/front/src/pages/Main.jsx b/front/src/pages/Main.jsx deleted file mode 100644 index eb3f02d..0000000 --- a/front/src/pages/Main.jsx +++ /dev/null @@ -1,9 +0,0 @@ -import React from "react"; - -class Main extends React.Component { - render() { - return
这是主页
; - } -} - -export default Main; diff --git a/front/src/pages/index.jsx b/front/src/pages/index.jsx new file mode 100644 index 0000000..3ed940e --- /dev/null +++ b/front/src/pages/index.jsx @@ -0,0 +1,27 @@ +import React from "react"; +import { Button } from "antd"; +import httpUtil from "../util/httpUtil.js"; + +class Main extends React.Component { + constructor(props) { + super(props); + this.state = {}; + } + + testPriviledge = () => { + httpUtil.get("/priviledgeTest"); + }; + + render() { + return ( +
+
这是主页
+ +
+ ); + } +} + +export default Main; diff --git a/front/src/pages/main.module.less b/front/src/pages/index.module.less similarity index 100% rename from front/src/pages/main.module.less rename to front/src/pages/index.module.less diff --git a/front/src/pages/public/Login/index.jsx b/front/src/pages/public/Login/index.jsx index 89d49a9..12e61d2 100644 --- a/front/src/pages/public/Login/index.jsx +++ b/front/src/pages/public/Login/index.jsx @@ -1,13 +1,14 @@ import React, { Component } from "react"; +import queryString from "query-string"; import { Button, Input, message } from "antd"; import IconFont from "../../../components/IconFont"; import styles from "./index.module.less"; import { connect } from "react-redux"; -import { changeLoginInfo } from "../../../redux/action/LoginInfoAction"; +import { changeLoginInfo, DATA_NAME } from "../../../redux/action/loginInfoAction"; import axios from "../../../util/httpUtil"; function mapStateToProps(state) { - return {}; + return state[DATA_NAME]; } function mapDispatchToProps(dispatch) { @@ -23,6 +24,7 @@ class Login extends Component { username: "", password: "" }; + this.query = queryString.parse(window.location.search); } usernameInput = e => { @@ -40,7 +42,11 @@ class Login extends Component { window.userInfo = res.userInfo; message.success("登录成功"); this.props.updateLoginInfo(res.token, res.userInfo); - this.props.history.replace("/"); + if (this.query.redirect) { + this.props.history.replace(decodeURIComponent(this.query.redirect)); + } else { + this.props.history.replace("/"); + } }); }; diff --git a/front/src/redux/action/LoginInfoAction.js b/front/src/redux/action/LoginInfoAction.js index 3682e2b..8462515 100644 --- a/front/src/redux/action/LoginInfoAction.js +++ b/front/src/redux/action/LoginInfoAction.js @@ -1,3 +1,7 @@ +// 定义登录信息在store中的名字 +export const DATA_NAME = "loginInfo"; + +//定义修改loginInfo type export const CHANGE_LOGIN_INFO = "changeLoginStatus"; export const changeLoginInfo = (token, userInfo) => { diff --git a/front/src/redux/reducer/index.js b/front/src/redux/reducer/index.js index 09f9f55..cd94578 100644 --- a/front/src/redux/reducer/index.js +++ b/front/src/redux/reducer/index.js @@ -1,8 +1,11 @@ import { combineReducers } from "redux"; +import { DATA_NAME } from "../action/loginInfoAction"; import loginInfo from "./loginInfo"; -const reducer = combineReducers({ - loginInfo -}); +const data = {}; +data[DATA_NAME] = loginInfo; + + +const reducer = combineReducers(data); export default reducer; diff --git a/front/src/redux/reducer/loginInfo.js b/front/src/redux/reducer/loginInfo.js index e8f35bc..f54eec1 100644 --- a/front/src/redux/reducer/loginInfo.js +++ b/front/src/redux/reducer/loginInfo.js @@ -1,4 +1,4 @@ -import * as loginAction from "../action/LoginInfoAction"; +import * as loginAction from "../action/loginInfoAction"; function getInitData() { let token, userInfo; diff --git a/front/src/util/httpUtil.js b/front/src/util/httpUtil.js index b3840e0..95d399a 100644 --- a/front/src/util/httpUtil.js +++ b/front/src/util/httpUtil.js @@ -23,12 +23,19 @@ instance.interceptors.response.use( } else { message = "出问题啦:" + error.response.status; description = JSON.stringify(error.response.data); + //401跳转到登录页面 } notification.open({ message, description, duration: 2 }); + setTimeout(() => { + if (error.response && error.response.status === 401) { + let redirect = encodeURIComponent(window.location.pathname + window.location.search); + window.location.replace("/public/login?redirect=" + redirect); + } + }, 1000); return Promise.reject(error); } ); diff --git a/front/yarn.lock b/front/yarn.lock index 51df87a..1690370 100644 --- a/front/yarn.lock +++ b/front/yarn.lock @@ -8252,6 +8252,15 @@ qs@~6.5.2: resolved "https://registry.yarnpkg.com/qs/-/qs-6.5.2.tgz#cb3ae806e8740444584ef154ce8ee98d403f3e36" integrity sha512-N5ZAX4/LxJmF+7wN74pUD6qAh9/wnvdQcjq9TZjevvXzSUo7bfmw91saqMjzGS2xq91/odN2dW/WOl7qQHNDGA== +query-string@^6.8.1: + version "6.8.1" + resolved "https://registry.yarnpkg.com/query-string/-/query-string-6.8.1.tgz#62c54a7ef37d01b538c8fd56f95740c81d438a26" + integrity sha512-g6y0Lbq10a5pPQpjlFuojfMfV1Pd2Jw9h75ypiYPPia3Gcq2rgkKiIwbkS6JxH7c5f5u/B/sB+d13PU+g1eu4Q== + dependencies: + decode-uri-component "^0.2.0" + split-on-first "^1.0.0" + strict-uri-encode "^2.0.0" + querystring-es3@^0.2.0: version "0.2.1" resolved "https://registry.yarnpkg.com/querystring-es3/-/querystring-es3-0.2.1.tgz#9ec61f79049875707d69414596fd907a4d711e73" @@ -9781,6 +9790,11 @@ spdy@^4.0.0: select-hose "^2.0.0" spdy-transport "^3.0.0" +split-on-first@^1.0.0: + version "1.1.0" + resolved "https://registry.yarnpkg.com/split-on-first/-/split-on-first-1.1.0.tgz#f610afeee3b12bce1d0c30425e76398b78249a5f" + integrity sha512-43ZssAJaMusuKWL8sKUBQXHWOpq8d6CfN/u1p4gUzfJkM05C8rxTmYrkIPTXapZpORA6LkkzcUulJ8FqA7Uudw== + split-string@^3.0.1, split-string@^3.0.2: version "3.1.0" resolved "https://registry.yarnpkg.com/split-string/-/split-string-3.1.0.tgz#7cb09dda3a86585705c64b39a6466038682e8fe2" @@ -9875,6 +9889,11 @@ stream-shift@^1.0.0: resolved "https://registry.yarnpkg.com/stream-shift/-/stream-shift-1.0.0.tgz#d5c752825e5367e786f78e18e445ea223a155952" integrity sha1-1cdSgl5TZ+eG944Y5EXqIjoVWVI= +strict-uri-encode@^2.0.0: + version "2.0.0" + resolved "https://registry.yarnpkg.com/strict-uri-encode/-/strict-uri-encode-2.0.0.tgz#b9c7330c7042862f6b142dc274bbcc5866ce3546" + integrity sha1-ucczDHBChi9rFC3CdLvMWGbONUY= + string-convert@^0.2.0: version "0.2.1" resolved "https://registry.yarnpkg.com/string-convert/-/string-convert-0.2.1.tgz#6982cc3049fbb4cd85f8b24568b9d9bf39eeff97"