一、用户页面仓库 src/pages/user/store/index.js
import reducer from './reducer';
import * as actionCreators from './actionCreators';
import * as constants from './actionTypes';
export {reducer, actionCreators, constants}
二、用户页面仓库 src/pages/user/store/actionTypes.js
export const GET_USERS = 'users/GET_USERS'
三、用户页面仓库 src/pages/user/store/actionCreators.js
// 常量
import * as constants from './actionTypes'
// UI组件
import { message } from 'antd';
// 接口
import {
getUsersApi,
deleteUsersApi,
postUsersApi, //用户添加接口
putUsersApi,
} from '../../../api/index'
// 用户信息列表
export const getUsersData = params => dispatch => {
getUsersApi(params).then(res => {
console.log(res)
if(res.meta.status === 200){
dispatch({
type: constants.GET_USERS,
data: res.data
})
}
})
}
// 用户信息删除
export const deleteUsersData = (id, callback) => dispatch => {
deleteUsersApi(id).then( res => {
if(res.meta.status === 200){
message.success('删除成功')
callback()
}else{
message.error(res.meta.msg);
}
})
};
// 用户信息添加
export const postUsersData = (params, callback) => dispatch => {
postUsersApi(params).then(res => {
console.log(res)
if(res.meta.status === 201){
message.success('添加成功')
callback()
}else{
message.error(res.meta.msg);
}
})
}
// 用户信息编辑
export const putUsersData = (id, params, callback) => dispatch => {
console.log(putUsersApi)
putUsersApi(id, params).then(res => {
console.log(res)
if(res.meta.status === 200)
{
message.success('修改成功')
callback()
}else
{
message.error(res.meta.msg);
}
})
}
四、用户页面仓库 src/pages/user/store/reducer.js
import {fromJS} from 'immutable'
import * as constants from './actionTypes'
const defaultState = fromJS({
// 表单列表数据
data: []
})
export default (state = defaultState, action) => {
switch(action.type)
{
case constants.GET_USERS:
return state.set ('data', fromJS(action.data))
default:
return state
}
}