userlogin

This commit is contained in:
zuiidea
2017-04-14 11:45:00 +08:00
parent 688d411dc1
commit 4cf25852da
7 changed files with 50 additions and 21 deletions
+1 -1
View File
@@ -35,7 +35,7 @@ function Header ({ user, logout, switchSider, siderFold, isNavbar, menuPopoverVi
<SubMenu style={{
float: 'right',
}} title={< span > <Icon type="user" />
{user.name} < /span>}
{user.username} < /span>}
>
<Menu.Item key="logout">
<a>注销</a>
+2 -2
View File
@@ -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' })
}
+15 -14
View File
@@ -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) {
+7 -2
View File
@@ -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: {
+9 -1
View File
@@ -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
}
+2 -1
View File
@@ -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'))
+14
View File
@@ -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,
}