Skip to content

Commit fe8d1ef

Browse files
author
zhongzhenlong
committed
2345678
1 parent 1c65be0 commit fe8d1ef

File tree

10 files changed

+303
-75
lines changed

10 files changed

+303
-75
lines changed

src/main.js

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,10 @@
11
import React from 'react'
22
import { render } from 'react-dom'
33
import { Provider } from 'react-redux'
4-
import { browserHistory } from 'react-router'
5-
import { syncHistoryWithStore } from 'react-router-redux'
64
import 'babel-polyfill'
7-
import store from './redux'
5+
import redux from './redux'
86
import Routers from './router'
97
import appActions from './redux/actions/app'
10-
/*
11-
这个history 修改了history.listen,使其传入所有location首先更新提供的store。
12-
这样确保了sote是最新状态,无论它是来自导航事件或者时光机的action(例如replay),增强历史记录的监听器将保持同步。
13-
*/
14-
const history = syncHistoryWithStore(browserHistory, store)
158

169

1710
const resize = ({ dispatch }) => {
@@ -20,6 +13,8 @@ const resize = ({ dispatch }) => {
2013
dispatch(appActions.toggleSider({ payload: { siderFold, siderVisible } }))
2114
}
2215

16+
const { store, history } = redux
17+
2318
document.body.clientWidth < 769 && resize(store)
2419

2520
let tid
@@ -28,13 +23,13 @@ window.onresize = () => {
2823
tid = setTimeout(resize(store), 300)
2924
}
3025

31-
render(
32-
<Provider store={store}>
33-
{/*
34-
常用的 history 有三种形式, 但是你也可以使用 React Router 实现自定义的 history。
35-
browserHistory : 使用 React Router 的应用推荐的 history
36-
hashHistory
37-
createMemoryHistory
38-
*/}
39-
<Routers history={history} />
40-
</Provider>, document.getElementById('root'))
26+
27+
render((<Provider store={store}>
28+
{/*
29+
常用的 history 有三种形式, 但是你也可以使用 React Router 实现自定义的 history。
30+
browserHistory : 使用 React Router 的应用推荐的 history
31+
hashHistory
32+
createMemoryHistory
33+
*/}
34+
<Routers history={history} />
35+
</Provider>), document.querySelector('#root'))

src/menu.js

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -5,64 +5,64 @@ export const arrayMenu = [
55
{ id: 1, icon: 'laptop', name: '仪表盘', router: '/home' },
66
{ id: 101, name: '系统管理', icon: 'line-chart', router: '/sys' },
77
{ id: 102, pid: 101, name: '用户管理', icon: 'mail', router: '/sys/user' },
8-
{ id: 103, pid: 101, name: '活跃用户', icon: 'usergroup-add', router: '/bi/active' },
9-
{ id: 104, pid: 101, name: '广告转化', icon: 'area-chart' },
10-
{ id: 1041, pid: 104, name: '数据模型配置', icon: 'api', router: '/bi/adTemplate' },
11-
{ id: 1042, pid: 104, name: '广告点击', icon: 'bar-chart', router: '/bi/adClick' },
12-
{ id: 1043, pid: 104, name: '广告转化', icon: 'dot-chart', router: '/bi/adTake' },
13-
{ id: 106, pid: 101, name: 'PV/UV', icon: 'heart-o', router: '/bi/pv' },
14-
{ id: 201, name: '直播数据', icon: 'video-camera' },
15-
{ id: 202, pid: 201, name: '直播数据', icon: 'bar-chart', router: '/live/realtime' },
16-
{ id: 203, pid: 201, name: '用户数据', icon: 'solution', router: '/live/eachUser' },
17-
{ id: 204, pid: 201, name: '观看时长统计', icon: 'video-camera', router: '/live/avgPeriod' },
18-
{ id: 205, pid: 201, name: '事件统计', icon: 'bulb', router: '/live/events' },
19-
{ id: 301, name: '缓存管理', icon: 'database' },
20-
{ id: 302, pid: 301, name: 'REDIS', icon: 'hdd', router: '/redis/index' },
8+
{ id: 103, pid: 101, name: '测试菜单', icon: 'usergroup-add', router: '/test/test' },
9+
{ id: 104, pid: 101, name: '测试菜单', icon: 'area-chart' },
10+
{ id: 1041, pid: 104, name: '测试菜单', icon: 'api', router: '/test/test' },
11+
{ id: 1042, pid: 104, name: '测试菜单', icon: 'bar-chart', router: '/test/test' },
12+
{ id: 1043, pid: 104, name: '测试菜单', icon: 'dot-chart', router: '/test/test' },
13+
{ id: 106, pid: 101, name: '测试菜单', icon: 'heart-o', router: '/test/test' },
14+
{ id: 201, name: '测试菜单', icon: 'video-camera' },
15+
{ id: 202, pid: 201, name: '测试菜单', icon: 'bar-chart', router: '/test/test' },
16+
{ id: 203, pid: 201, name: '测试菜单', icon: 'solution', router: '/test/test' },
17+
{ id: 204, pid: 201, name: '测试菜单', icon: 'video-camera', router: '/test/test' },
18+
{ id: 205, pid: 201, name: '测试菜单', icon: 'bulb', router: '/test/test' },
19+
{ id: 301, name: '测试菜单', icon: 'database' },
20+
{ id: 302, pid: 301, name: '测试菜单', icon: 'hdd', router: '/test/index' },
2121
]
2222

2323
export const treeMenu = [{ id: 1, icon: 'laptop', name: '仪表盘', router: '/dashboard' }, {
2424
id: 101,
25-
name: '行为数据',
25+
name: '测试菜单',
2626
icon: 'line-chart',
27-
children: [{ id: 102, pid: 101, name: '消息推送', icon: 'mail', router: '/bi/msgPush' }, {
27+
children: [{ id: 102, pid: 101, name: '消息推送', icon: 'mail', router: '/test/test' }, {
2828
id: 103,
2929
pid: 101,
30-
name: '活跃用户',
30+
name: '测试菜单',
3131
icon: 'usergroup-add',
3232
router: '/bi/active',
3333
}, {
3434
id: 104,
3535
pid: 101,
36-
name: '广告转化',
36+
name: '测试菜单',
3737
icon: 'area-chart',
38-
children: [{ id: 1041, pid: 104, name: '数据模型配置', icon: 'api', router: '/bi/adTemplate' }, {
38+
children: [{ id: 1041, pid: 104, name: '测试菜单', icon: 'api', router: '/test/test' }, {
3939
id: 1042,
4040
pid: 104,
41-
name: '广告点击',
41+
name: '测试菜单',
4242
icon: 'bar-chart',
4343
router: '/bi/adClick',
44-
}, { id: 1043, pid: 104, name: '广告转化', icon: 'dot-chart', router: '/bi/adTake' }],
45-
}, { id: 106, pid: 101, name: 'PV/UV', icon: 'heart-o', router: '/bi/pv' }],
44+
}, { id: 1043, pid: 104, name: '测试菜单', icon: 'dot-chart', router: '/test/test' }],
45+
}, { id: 106, pid: 101, name: '测试菜单', icon: 'heart-o', router: '/test/test' }],
4646
}, {
4747
id: 201,
48-
name: '直播数据',
48+
name: '测试菜单',
4949
icon: 'video-camera',
50-
children: [{ id: 202, pid: 201, name: '直播数据', icon: 'bar-chart', router: '/live/realtime' }, {
50+
children: [{ id: 202, pid: 201, name: '测试菜单', icon: 'bar-chart', router: '/test/test' }, {
5151
id: 203,
5252
pid: 201,
53-
name: '用户数据',
53+
name: '测试菜单',
5454
icon: 'solution',
5555
router: '/live/eachUser',
56-
}, { id: 204, pid: 201, name: '观看时长统计', icon: 'video-camera', router: '/live/avgPeriod' }, {
56+
}, { id: 204, pid: 201, name: '测试菜单', icon: 'video-camera', router: '/test/test' }, {
5757
id: 205,
5858
pid: 201,
59-
name: '事件统计',
59+
name: '测试菜单',
6060
icon: 'bulb',
6161
router: '/live/events',
6262
}],
6363
}, {
6464
id: 301,
65-
name: '缓存管理',
65+
name: '测试菜单',
6666
icon: 'database',
67-
children: [{ id: 302, pid: 301, name: 'REDIS', icon: 'hdd', router: '/redis/index' }],
67+
children: [{ id: 302, pid: 301, name: '测试菜单', icon: 'hdd', router: '/test/index' }],
6868
}]

src/mock/user.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,16 @@ let usersListData = Mock.mock({
1919
],
2020
})
2121

22+
const shuffle = function (input) {
23+
for (let i = input.length - 1; i >= 0; i--) {
24+
const randomIndex = Math.floor(Math.random() * (i + 1))
25+
const itemAtIndex = input[randomIndex]
26+
input[randomIndex] = input[i]
27+
input[i] = itemAtIndex
28+
}
29+
return input
30+
}
31+
2232

2333
let database = usersListData.data
2434

@@ -83,7 +93,7 @@ module.exports = {
8393
pageSize = pageSize || 10
8494
page = page || 1
8595

86-
let newData = database
96+
let newData = shuffle(database)
8797

8898
res.status(200).json({
8999
data: newData.slice((page - 1) * pageSize, page * pageSize),

src/redux/domain/index.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import sysUser from './sysUser'
2+
3+
export default {
4+
sysUser,
5+
}

src/redux/domain/sysUser.js

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import 'es6-promise'
2+
import fetch from 'isomorphic-fetch'
3+
import { sysUserType } from '../actions/actionTypes'
4+
5+
export default {
6+
state: {
7+
list: [],
8+
pagination: {
9+
showSizeChanger: true,
10+
showQuickJumper: true,
11+
showTotal: total => `共${total}条`,
12+
current: 1,
13+
total: 0,
14+
},
15+
initLoaded: false,
16+
loading: false,
17+
},
18+
subscriptions: {
19+
setup ({ dispatch, history }, { queryList }) {
20+
return history.listen((location) => {
21+
if (location.pathname === '/sys/user') {
22+
dispatch(queryList({}))
23+
}
24+
})
25+
},
26+
},
27+
actions: {
28+
queryList: ({ payload }) => (dispatch) => {
29+
let url = `http://127.0.0.1:3000/users?da=${JSON.stringify(payload)}`
30+
dispatch({
31+
type: sysUserType.LOAD,
32+
})
33+
return fetch(url, { mode: 'cors', 'Content-Type': 'application/json' })
34+
.then((response) => {
35+
if (response.ok) {
36+
response.json().then(json => dispatch({
37+
type: sysUserType.LOAD_FINISHED,
38+
payload: { list: json.data },
39+
}))
40+
} else {
41+
console.log('status', response.status)
42+
}
43+
})
44+
.catch(error => console.log(error))
45+
},
46+
},
47+
reducers: {
48+
[sysUserType.LOAD_FINISHED]: (state, { payload }) => {
49+
return { ...state, ...payload, initLoaded: true }
50+
},
51+
},
52+
}

src/redux/index.js

Lines changed: 46 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
/* eslint-disable linebreak-style */
2-
import { createStore, applyMiddleware, combineReducers } from 'redux'
2+
import { applyMiddleware, combineReducers, createStore } from 'redux'
33
import thunkMiddleware from 'redux-thunk'
44
import { browserHistory } from 'react-router'
55
import { createLogger } from 'redux-logger'
6-
import { routerMiddleware, routerReducer } from 'react-router-redux'
6+
import { routerMiddleware, routerReducer, syncHistoryWithStore } from 'react-router-redux'
77
import initState from './models'
8-
import reducers from './reducers'
8+
import reducersHolder from './reducers'
9+
import domains from './domain'
10+
import { createReducer } from '../utils/index'
11+
import { message } from 'antd'
912

1013

1114
/*
@@ -15,37 +18,62 @@ import reducers from './reducers'
1518
browserHistory.push('/some/path')
1619
如果我想通过Redux操作发出导航事件怎么办?
1720
push(location), replace(location), go(number), goBack(), goForward(),必须为这些action creator安装routerMiddleware
18-
<pre>
19-
import { routerMiddleware, push } from 'react-router-redux'
20-
// Dispatch from anywhere like normal.
21-
store.dispatch(push('/foo'))
22-
</pre>
23-
*/
21+
<pre>
22+
import { routerMiddleware, push } from 'react-router-redux'
23+
// Dispatch from anywhere like normal.
24+
store.dispatch(push('/foo'))
25+
</pre>
26+
*/
2427
const routerReduxMiddleware = routerMiddleware(browserHistory)
2528

2629
const loggerMiddleware = createLogger({
2730
level: 'info',
2831
collapsed: true,
2932
})
3033

31-
3234
/**
3335
* 添加redux中间件
3436
* 将所有中间件组成一个数组,依次执行,中间件的次序有讲究。
3537
*
3638
*/
3739
const createStoreWithMiddleware = applyMiddleware(
38-
// Action 是由store.dispatch方法发送的。而store.dispatch方法正常情况下,参数只能是对象,不能是函数。
39-
// 使用redux-thunk中间件,改造store.dispatch,使得后者可以接受函数作为参数。
40-
thunkMiddleware,
41-
// 日志中间件
42-
loggerMiddleware,
43-
routerReduxMiddleware,
40+
// Action 是由store.dispatch方法发送的。而store.dispatch方法正常情况下,参数只能是对象,不能是函数。
41+
// 使用redux-thunk中间件,改造store.dispatch,使得后者可以接受函数作为参数。
42+
thunkMiddleware,
43+
// 日志中间件
44+
loggerMiddleware,
45+
routerReduxMiddleware,
4446
)(createStore)
4547

48+
const subscriptionHolder = []
49+
for (const key in domains) {
50+
if (Object.prototype.hasOwnProperty.call(domains, key)) {
51+
const { state, reducers, actions, subscriptions } = domains[key]
52+
reducersHolder[key] = createReducer(state || {}, reducers)
53+
initState[key] = state || {}
54+
subscriptionHolder.push({ subscriptions, actions })
55+
}
56+
}
57+
4658
const rootReducer = combineReducers({
47-
...reducers,
59+
...reducersHolder,
4860
routing: routerReducer, // 如果你使用 combineReducers,它应该被嵌套在 routing这个key下
4961
})
5062

51-
export default createStoreWithMiddleware(rootReducer, initState)
63+
const store = createStoreWithMiddleware(rootReducer, initState)
64+
/*
65+
这个history 修改了history.listen,使其传入所有location首先更新提供的store。
66+
这样确保了sote是最新状态,无论它是来自导航事件或者时光机的action(例如replay),增强历史记录的监听器将保持同步。
67+
*/
68+
const history = syncHistoryWithStore(browserHistory, store)
69+
history.listen((location) => { alert(location.pathname) })
70+
const unListeners = []
71+
subscriptionHolder.forEach(({ subscriptions, actions }) => {
72+
for (const method in subscriptions) {
73+
if ({}.hasOwnProperty.call(subscriptions, method)) {
74+
unListeners.push(subscriptions[method]({ ...store, history }, actions))
75+
}
76+
}
77+
})
78+
79+
export default { store, history }

src/redux/models/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import appInitState from './app'
2-
import sysUser from './sysUser'
2+
// import sysUser from './sysUser'
33
// import topicInitState from './modules/topic/topicInitState'
44
// import userInitState from './modules/user/userInitState'
55

66
export default {
77
app: appInitState,
8-
sysUser,
8+
// sysUser,
99
// topic: new topicInitState,
1010
// user: new userInitState
1111
}

src/redux/reducers/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@ import sysUser from './sysUser'
33

44
export default {
55
app,
6-
sysUser,
6+
// sysUser,
77
}

src/views/sys/users.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import React, { PropTypes } from 'react'
22
import { connect } from 'react-redux'
33
import { Button, Col, message, Form, Row, Input, Table } from 'antd'
4-
import * as userActions from '../../redux/actions/sys/user'
4+
import { actions as userActions } from '../../redux/domain/sysUser'
55

66
const SysUserView = ({
77
sysUser,
@@ -12,11 +12,11 @@ const SysUserView = ({
1212
},
1313
...others
1414
}) => {
15-
const { filterProps, pagination, loading, list: dataSource, initLoaded } = sysUser
15+
const { filterProps, pagination, loading, list: dataSource } = sysUser
1616

1717
const { queryList } = others
1818

19-
!initLoaded && queryList({ payload: {} })
19+
// !initLoaded && queryList({ payload: {} })
2020

2121
const handleSubmit = () => {
2222
let fields = getFieldsValue()
@@ -41,7 +41,7 @@ const SysUserView = ({
4141
dataIndex: 'avatar',
4242
key: 'avatar',
4343
width: 64,
44-
render: (text) => <img alt={'avatar'} width={24} src={text} />,
44+
render: text => <img alt={'avatar'} width={24} src={text} />,
4545
}, {
4646
title: 'Name',
4747
dataIndex: 'name',
@@ -58,9 +58,9 @@ const SysUserView = ({
5858
title: 'Gender',
5959
dataIndex: 'isMale',
6060
key: 'isMale',
61-
render: (text) => <span>{text
61+
render: text => (<span>{text
6262
? 'Male'
63-
: 'Female'}</span>,
63+
: 'Female'}</span>),
6464
}, {
6565
title: 'Phone',
6666
dataIndex: 'phone',
@@ -77,7 +77,7 @@ const SysUserView = ({
7777
title: 'CreateTime',
7878
dataIndex: 'createTime',
7979
key: 'createTime',
80-
},]
80+
}]
8181

8282
const listProps = {
8383
pagination: { ...pagination, showSizeChanger: true, showQuickJumper: true },

0 commit comments

Comments
 (0)