diff --git a/src/components/Layout/Header.js b/src/components/Layout/Header.js index f778050..07a6587 100644 --- a/src/components/Layout/Header.js +++ b/src/components/Layout/Header.js @@ -35,7 +35,7 @@ function Header ({ user, logout, switchSider, siderFold, isNavbar, menuPopoverVi - {user.name} < /span>} + {user.username} < /span>} > 注销 diff --git a/src/mock/users.js b/src/mock/users.js index 17c5c07..235aa84 100644 --- a/src/mock/users.js +++ b/src/mock/users.js @@ -66,11 +66,11 @@ module.exports = { if (user && user[0].password === password) { const now = new Date() now.setDate(now.getDate() + 1) - res.cookie('token', JSON.stringify({ id: 2, deadline: now.getTime() }), { + res.cookie('token', JSON.stringify({ id: user[0].id, deadline: now.getTime() }), { maxAge: 900000, httpOnly: true, }) - res.json({ success: true, message: '登录成功' }) + res.json({ success: true, message: 'Ok' }) } else { res.status(400).send({ message: 'Bad Request' }) } diff --git a/src/models/app.js b/src/models/app.js index 86a4840..64b1c38 100644 --- a/src/models/app.js +++ b/src/models/app.js @@ -5,10 +5,7 @@ import { parse } from 'qs' export default { namespace: 'app', state: { - user: { - name: '吴彦祖', - userPermissions: [], - }, + user: {}, loginButtonLoading: false, menuPopoverVisible: false, siderFold: localStorage.getItem('antdAdminSiderFold') === 'true', @@ -55,12 +52,19 @@ export default { if (data.success && data.user) { yield put({ type: 'queryUserSuccess', - payload: { - user: data.user, - }, + payload: data.user, }) + if (location.pathname === '/login') { + yield put(routerRedux.push('/dashboard')) + } } else { - yield put(routerRedux.push('/login')) + if (location.pathname !== '/login') { + if (location.pathname === '/dashboard') { + window.location = `${location.origin}/login?from=/dashboard` + } else { + yield put(routerRedux.push(`/login?from=${location.pathname}`)) + } + } } }, *logout ({ @@ -68,10 +72,7 @@ export default { }, { call, put }) { const data = yield call(logout, parse(payload)) if (data.success) { - yield put({ - type: 'logoutSuccess', - }) - yield put(routerRedux.push('/login')) + yield put({ type: 'queryUser' }) } else { throw (data) } @@ -108,10 +109,10 @@ export default { }, }, reducers: { - queryUserSuccess (state, action) { + queryUserSuccess (state, { payload: user }) { return { ...state, - ...action.payload, + user, } }, showLoginButtonLoading (state) { diff --git a/src/models/dashboard.js b/src/models/dashboard.js index 0541b25..18edc51 100644 --- a/src/models/dashboard.js +++ b/src/models/dashboard.js @@ -188,9 +188,14 @@ export default { }, }, subscriptions: { - setup ({ dispatch }) { - dispatch({ type: 'queryWeather' }) + setup ({ dispatch, history }) { + history.listen(location => { + if (location.pathname === '/dashboard') { + console.log('dashboard') + } + }) dispatch({ type: 'query' }) + dispatch({ type: 'queryWeather' }) }, }, effects: { diff --git a/src/models/login.js b/src/models/login.js index b4fa25b..bf208c5 100644 --- a/src/models/login.js +++ b/src/models/login.js @@ -1,11 +1,13 @@ import { login } from '../services/login' import { routerRedux } from 'dva/router' +import { queryURL } from '../utils' export default { namespace: 'login', state: { loginLoading: false, }, + effects: { *login ({ payload, @@ -14,7 +16,13 @@ export default { const data = yield call(login, payload) yield put({ type: 'hideLoginLoading' }) if (data.success) { - yield put(routerRedux.push('/dashboard')) + const from = queryURL('from') + yield put({ type: 'app/queryUser' }) + if (from) { + yield put(routerRedux.push(from)) + } else { + yield put(routerRedux.push('/dashboard')) + } } else { throw data } diff --git a/src/router.js b/src/router.js index 334c5ee..c427d0c 100644 --- a/src/router.js +++ b/src/router.js @@ -1,5 +1,5 @@ import React, { PropTypes } from 'react' -import { Router, routerRedux } from 'dva/router' +import { Router } from 'dva/router' import App from './routes/app' const cached = {} @@ -12,6 +12,7 @@ const registerModel = (app, model) => { const Routers = function ({ history, app }) { const handleChildRoute = ({ location }) => { + console.log(location) // const state = app._store.getState() // if (!state.app.login && location.pathname !== '/login') { // app._store.dispatch(routerRedux.push('/login')) diff --git a/src/utils/index.js b/src/utils/index.js index 7e96b25..b0c168f 100644 --- a/src/utils/index.js +++ b/src/utils/index.js @@ -40,10 +40,24 @@ Date.prototype.format = function (format) { return format } + +/** + * @param {String} + * @return {String} + */ + +const queryURL = (name) => { + let reg = new RegExp(`(^|&)${name}=([^&]*)(&|$)`, 'i') + let r = window.location.search.substr(1).match(reg) + if (r != null) return decodeURI(r[2]) + return null +} + module.exports = { config, menu, request, color, classnames, + queryURL, }