前端:管理员模块重构初步完成

pull/1/head
YunaiV 2019-03-22 16:00:28 +08:00
parent b29844f0d0
commit ed26110a7f
3 changed files with 267 additions and 202 deletions

View File

@ -22,26 +22,37 @@ export default {
state: { state: {
// 分页列表相关 // 分页列表相关
list: [], list: [],
searchParams: SEARCH_PARAMS_DEFAULT, listLoading: false,
pagination: PaginationHelper.defaultPaginationConfig, pagination: PaginationHelper.defaultPaginationConfig,
searchParams: SEARCH_PARAMS_DEFAULT,
// 添加 or 修改表单相关 // 添加 or 修改表单相关
modalVisible: false, modalVisible: false,
modalType: undefined, // 'add' or 'update' 表单 modalType: undefined, // 'add' or 'update' 表单
formVals: {}, // 当前表单值 formVals: {}, // 当前表单值
modalLoading: false,
// 分配角色表单相关 // 分配角色表单相关
roleList: [], roleList: [],
roleCheckedKeys: [], roleModalVisible: false,
roleCheckedKeys: [], // 此处的 Key ,就是角色编号
roleAssignLoading: false, roleAssignLoading: false,
}, },
effects: { effects: {
// 查询列表 // 查询列表
* query({ payload }, { call, put }) { * query({ payload }, { call, put }) {
const response = yield call(queryAdmin, payload); // 显示加载中
yield put({ yield put({
type: 'querySuccess', type: 'changeListLoading',
payload: true,
});
// 请求
const response = yield call(queryAdmin, payload);
// 响应
yield put({
type: 'setAll',
payload: { payload: {
list: response.data.list, list: response.data.list,
pagination: PaginationHelper.formatPagination(response.data, payload), pagination: PaginationHelper.formatPagination(response.data, payload),
@ -50,10 +61,24 @@ export default {
} }
}, },
}); });
// 隐藏加载中
yield put({
type: 'changeListLoading',
payload: false,
});
}, },
* add({ payload }, { call, put }) { * add({ payload }, { call, put }) {
const { callback, body } = payload; const { callback, body } = payload;
// 显示加载中
yield put({
type: 'changeModalLoading',
payload: true,
});
// 请求
const response = yield call(addAdmin, body); const response = yield call(addAdmin, body);
// 响应
if (response.code === 0) { if (response.code === 0) {
if (callback) { if (callback) {
callback(response); callback(response);
@ -66,10 +91,24 @@ export default {
}, },
}); });
} }
// 隐藏加载中
yield put({
type: 'changeModalLoading',
payload: false,
});
}, },
* update({ payload }, { call, put }) { * update({ payload }, { call, put }) {
const { callback, body } = payload; const { callback, body } = payload;
// 显示加载中
yield put({
type: 'changeModalLoading',
payload: true,
});
// 请求
const response = yield call(updateAdmin, body); const response = yield call(updateAdmin, body);
// 响应
if (response.code === 0) { if (response.code === 0) {
if (callback) { if (callback) {
callback(response); callback(response);
@ -82,91 +121,132 @@ export default {
}, },
}); });
} }
},
*updateStatus({ payload }, { call, put }) { // 隐藏加载中
const { body, queryParams } = payload;
yield call(updateAdminStatus, body);
message.info('更新成功!');
yield put({ yield put({
type: 'query', type: 'changeModalLoading',
payload: { payload: false,
...queryParams,
},
});
},
*delete({ payload }, { call, put }) {
const { queryParams, body } = payload;
yield call(deleteAdmin, body);
message.info('删除成功!');
yield put({
type: 'query',
payload: {
...queryParams,
},
}); });
}, },
* updateStatus({ payload }, { call, put }) {
// 请求
const response = yield call(updateAdminStatus, payload);
// 响应
if (response.code === 0) {
message.info('更新状态成功!');
// 刷新列表
yield put({
type: 'query',
payload: {
...PaginationHelper.defaultPayload
},
});
}
},
* delete({ payload }, { call, put }) {
// 请求
const response = yield call(deleteAdmin, payload);
// 响应
if (response.code === 0) {
message.info('删除成功!');
// 刷新列表
yield put({
type: 'query',
payload: {
...PaginationHelper.defaultPayload
},
});
}
},
* queryRoleList({ payload }, { call, put }) { * queryRoleList({ payload }, { call, put }) {
// 显示加载中
yield put({ yield put({
type: 'changeRoleAssignLoading', type: 'changeRoleAssignLoading',
payload: true, payload: true,
}); });
// 请求
const response = yield call(queryAdminRoleList, payload); const response = yield call(queryAdminRoleList, payload);
// 响应
if (response.code === 0) {
const roleList = response.data; const roleList = response.data;
const roleTreeData = buildTreeNode(roleList, 'name', 'id'); const roleTreeData = buildTreeNode(roleList, 'name', 'id');
const roleCheckedKeys = findCheckedKeys(roleList); const roleCheckedKeys = findCheckedKeys(roleList);
yield put({ yield put({
type: 'querySuccess', type: 'setAll',
payload: { payload: {
roleList: roleTreeData, roleList: roleTreeData,
roleCheckedKeys, roleCheckedKeys,
}, },
}); });
}
// 隐藏加载中
yield put({ yield put({
type: 'changeRoleAssignLoading', type: 'changeRoleAssignLoading',
payload: false, payload: false,
}); });
}, },
*roleAssign({ payload }, { call }) {
const params = { * roleAssign({ payload }, { call, put }) {
id: payload.id, const { callback, body } = payload;
roleIds: arrayToStringParams(payload.roleIds), // 显示加载中
}; yield put({
const response = yield call(adminRoleAssign, params); type: 'changeRoleAssignLoading',
payload: true,
});
// 请求
const response = yield call(adminRoleAssign, {
id: body.id,
roleIds: arrayToStringParams(body.roleIds),
});
// 响应
if (response.code === 0) { if (response.code === 0) {
message.info('操作成功!'); if (callback) {
callback(response);
} }
}
// 隐藏加载中
yield put({
type: 'changeRoleAssignLoading',
payload: false,
});
}, },
}, },
reducers: { reducers: {
querySuccess(state, { payload }) {
return {
...state,
...payload,
};
},
changeRoleCheckedKeys(state, { payload }) { changeRoleCheckedKeys(state, { payload }) {
return { return {
...state, ...state,
roleCheckedKeys: payload, roleCheckedKeys: payload,
}; };
}, },
// 修改加载中的状态
changeRoleAssignLoading(state, { payload }) { changeRoleAssignLoading(state, { payload }) {
return { return {
...state, ...state,
roleAssignLoading: payload, roleAssignLoading: payload,
}; };
}, },
setAll(state, { payload }) { changeModalLoading(state, { payload }) {
console.log('setAll'); return {
console.log({
...state, ...state,
...payload, modalLoading: payload,
}); };
},
changeListLoading(state, { payload }) {
return {
...state,
listLoading: payload,
};
},
// 设置所有属性
setAll(state, { payload }) {
return { return {
...state, ...state,
...payload, ...payload,

View File

@ -15,7 +15,54 @@ const { TreeNode } = Tree;
const status = ['未知', '正常', '禁用']; const status = ['未知', '正常', '禁用'];
// 列表 // 列表
function List ({ dataSource, pagination, searchParams, dispatch, handleModalVisible }) { function List ({ dataSource, loading, pagination, searchParams, dispatch,
handleModalVisible, handleRoleAssignModalVisible}) {
function handleRoleAssign(record) {
// 显示 Modal
handleRoleAssignModalVisible(true, record);
// 查询角色列表
dispatch({
type: 'adminList/queryRoleList',
payload: {
id: record.id,
},
});
}
function handleStatus(record) {
Modal.confirm({
title: record.status === 1 ? '确认禁用' : '取消禁用',
content: `${record.username}`,
onOk() {
dispatch({
type: 'adminList/updateStatus',
payload: {
id: record.id,
status: record.status === 1 ? 2 : 1,
},
});
},
onCancel() {},
});
}
function handleDelete(record) {
Modal.confirm({
title: `确认删除?`,
content: `${record.username}`,
onOk() {
dispatch({
type: 'adminList/delete',
payload: {
id: record.id,
},
});
},
onCancel() {},
});
}
const columns = [ const columns = [
{ {
title: '用户名', title: '用户名',
@ -41,27 +88,32 @@ function List ({ dataSource, pagination, searchParams, dispatch, handleModalVisi
title: '操作', title: '操作',
width: 360, width: 360,
render: (text, record) => { render: (text, record) => {
const statusText = record.status === 1 ? '禁用' : '禁用'; const statusText = record.status === 1 ? '禁用' : '开启'; // TODO 芋艿,此处要改
return ( return (
<Fragment> <Fragment>
<a onClick={() => handleModalVisible(true, 'update', record)}>编辑</a> <a onClick={() => handleModalVisible(true, 'update', record)}>编辑</a>
<Divider type="vertical" /> <Divider type="vertical" />
<a onClick={() => this.handleRoleAssign(record)}>角色分配</a> <a onClick={() => handleRoleAssign(record)}>角色分配</a>
<Divider type="vertical" /> <Divider type="vertical" />
<a className={styles.tableDelete} onClick={() => this.handleStatus(record)}> <a className={styles.tableDelete} onClick={() => handleStatus(record)}>
{statusText} {statusText}
</a> </a>
{
record.status === 2 ?
<span>
<Divider type="vertical" /> <Divider type="vertical" />
<a className={styles.tableDelete} onClick={() => this.handleDelete(record)}> <a className={styles.tableDelete} onClick={() => handleDelete(record)}>
删除 删除
</a> </a>
</span> : ''
}
</Fragment> </Fragment>
); );
}, },
}, },
]; ];
function onPageChange(page) { function onPageChange(page) { // 翻页
dispatch({ dispatch({
type: 'adminList/query', type: 'adminList/query',
payload: { payload: {
@ -70,19 +122,14 @@ function List ({ dataSource, pagination, searchParams, dispatch, handleModalVisi
...searchParams ...searchParams
} }
}) })
}; }
return ( return (
<Table <Table
columns={columns} columns={columns}
dataSource={dataSource} dataSource={dataSource}
loading={loading}
rowKey="id" rowKey="id"
// pagination={{
// current: pageNo,
// pageSize: pageSize,
// total: count,
// onChange: this.onPageChange
// }}
pagination={pagination} pagination={pagination}
onChange={onPageChange} onChange={onPageChange}
/> />
@ -178,7 +225,7 @@ const AddOrUpdateForm = Form.create()(props => {
type: 'adminList/update', type: 'adminList/update',
payload: { payload: {
body: { body: {
...formVals, id: formVals.id,
...fields, ...fields,
}, },
callback: () => { callback: () => {
@ -232,22 +279,34 @@ const AddOrUpdateForm = Form.create()(props => {
); );
}); });
// 角色分配 // 角色分配 Modal
const RoleAssignModal = Form.create()(props => { const RoleAssignModal = Form.create()(props => {
const { const {
modalVisible, modalVisible,
form, form,
handleOk,
handleModalVisible, handleModalVisible,
treeData, treeData,
checkedKeys, checkedKeys,
loading, loading,
handleCheckBoxClick, formVals,
dispatch,
} = props; } = props;
const handleCheckBoxClick = checkedKeys => {
// 获得新选择
const newCheckedKeys = checkedKeys.map(item => {
return parseInt(item);
});
// 设置到 model 中
dispatch({
type: 'adminList/changeRoleCheckedKeys',
payload: newCheckedKeys,
});
};
const renderTreeNodes = data => { const renderTreeNodes = data => {
return data.map(item => { return data.map(item => {
if (item.children) { if (item.children) { // 递归拼接节点
return ( return (
<TreeNode title={item.title} key={item.key} dataRef={item}> <TreeNode title={item.title} key={item.key} dataRef={item}>
{renderTreeNodes(item.children)} {renderTreeNodes(item.children)}
@ -284,9 +343,23 @@ const RoleAssignModal = Form.create()(props => {
const okHandle = () => { const okHandle = () => {
form.validateFields((err, fieldsValue) => { form.validateFields((err, fieldsValue) => {
if (err) return; if (err) return;
// debugger;
dispatch({
type: 'adminList/roleAssign',
payload: {
body: {
id: formVals.id,
roleIds: checkedKeys,
},
callback: () => {
// 清空表单
form.resetFields(); form.resetFields();
handleOk({ // 提示
fields: fieldsValue, message.success('分配角色成功');
// 关闭弹窗
handleModalVisible(false);
},
},
}); });
}); });
}; };
@ -299,7 +372,9 @@ const RoleAssignModal = Form.create()(props => {
onOk={okHandle} onOk={okHandle}
onCancel={() => handleModalVisible()} onCancel={() => handleModalVisible()}
> >
<Spin spinning={loading}>{renderModalContent(treeData)}</Spin> <Spin spinning={loading}>
{renderModalContent(treeData)}
</Spin>
</Modal> </Modal>
); );
}); });
@ -308,14 +383,12 @@ const RoleAssignModal = Form.create()(props => {
// list: adminList.list, // list: adminList.list,
// pagination: adminList.pagination, // pagination: adminList.pagination,
...adminList, ...adminList,
data: adminList,
loading: loading.models.resourceList, loading: loading.models.resourceList,
})) }))
@Form.create() @Form.create()
class AdminList extends PureComponent { class AdminList extends PureComponent {
state = { state = {
// 分配角色弹窗 // 分配角色弹窗
modalRoleVisible: false, modalRoleVisible: false,
modalRoleRow: {}, modalRoleRow: {},
@ -331,130 +404,36 @@ class AdminList extends PureComponent {
}); });
} }
handleModalVisible = (modalVisible, modalType, formVals) => { handleModalVisible = (modalVisible, modalType, record) => {
// debugger;
const { dispatch } = this.props; const { dispatch } = this.props;
dispatch({ dispatch({
type: 'adminList/setAll', type: 'adminList/setAll',
payload: { payload: {
modalVisible, modalVisible,
modalType, modalType,
formVals: formVals || {} formVals: record || {}
}, },
}); });
}; };
handleStatus(row) { handleRoleAssignModalVisible = (roleModalVisible, record) => {
const { dispatch, data } = this.props;
const queryParams = {
pageNo: data.pageNo,
pageSize: data.pageSize,
};
const title = row.status === 1 ? '确认禁用' : '取消禁用';
const updateStatus = row.status === 1 ? 2 : 1;
Modal.confirm({
title: `${title}?`,
content: `${row.username}`,
onOk() {
dispatch({
type: 'adminList/updateStatus',
payload: {
body: {
id: row.id,
status: updateStatus,
},
queryParams,
},
});
},
onCancel() {},
});
}
handleDelete(row) {
const { dispatch, data } = this.props;
const queryParams = {
pageNo: data.pageNo,
pageSize: data.pageSize,
};
Modal.confirm({
title: `确认删除?`,
content: `${row.username}`,
onOk() {
dispatch({
type: 'adminList/delete',
payload: {
body: {
id: row.id,
},
queryParams,
},
});
},
onCancel() {},
});
}
handleRoleAssign = row => {
const { dispatch } = this.props; const { dispatch } = this.props;
this.setState({
modalRoleVisible: !!true,
modalRoleRow: row,
});
dispatch({ dispatch({
type: 'adminList/queryRoleList', type: 'adminList/setAll',
payload: { payload: {
id: row.id, roleModalVisible: roleModalVisible,
formVals: record || {}
}, },
}); });
}; };
handleRoleAssignCheckBoxClick = checkedKeys => {
const { dispatch } = this.props;
const newCheckedKeys = checkedKeys.map(item => {
return parseInt(item);
});
dispatch({
type: 'adminList/changeRoleCheckedKeys',
payload: newCheckedKeys,
});
};
handleRoleAssignOK = () => {
const { dispatch, data } = this.props;
const { modalRoleRow } = this.state;
dispatch({
type: 'adminList/roleAssign',
payload: {
id: modalRoleRow.id,
roleIds: data.roleCheckedKeys,
},
});
this.handleRoleAssignModalVisibleClose(false);
};
handleRoleAssignModalVisibleClose = fag => {
this.setState({
modalRoleVisible: !!fag,
});
};
render() { render() {
// let that = this; // let that = this;
const { dispatch, list, searchParams, pagination, modalVisible, data, modalType, formVals } = this.props; const { dispatch,
const { roleList, roleCheckedKeys, roleAssignLoading } = data; list, listLoading, searchParams, pagination,
// const { modalVisible, modalType, formVals,
// // modalVisible, confirmLoading,
// // modalType, roleList, roleModalVisible, roleAssignLoading, roleCheckedKeys } = this.props;
// formVals,
// modalRoleVisible,
// } = this.state;
const modalRoleVisible = false;
// 列表属性 // 列表属性
const listProps = { const listProps = {
@ -462,7 +441,10 @@ class AdminList extends PureComponent {
pagination, pagination,
searchParams, searchParams,
dispatch, dispatch,
loading: listLoading,
confirmLoading,
handleModalVisible: this.handleModalVisible, // Function handleModalVisible: this.handleModalVisible, // Function
handleRoleAssignModalVisible: this.handleRoleAssignModalVisible, // Function
}; };
// 搜索表单属性 // 搜索表单属性
@ -470,14 +452,24 @@ class AdminList extends PureComponent {
dispatch, dispatch,
}; };
// 添加 // 添加 or 更新表单属性
const addFormProps = { const addOrUpdateFormProps = {
modalVisible, modalVisible,
modalType: modalType, modalType,
formVals: formVals, formVals,
dispatch, dispatch,
handleModalVisible: this.handleModalVisible, // Function handleModalVisible: this.handleModalVisible, // Function
}; };
// 分配角色 Modal 属性
const roleAssignModal = {
loading: roleAssignLoading,
treeData: roleList,
formVals,
checkedKeys: roleCheckedKeys,
modalVisible: roleModalVisible,
dispatch,
handleModalVisible: this.handleRoleAssignModalVisible, // Function
};
return ( return (
<PageHeaderWrapper> <PageHeaderWrapper>
@ -497,19 +489,11 @@ class AdminList extends PureComponent {
</div> </div>
</div> </div>
<List {...listProps} /> <List {...listProps} />
</Card> </Card>
<AddOrUpdateForm {...addFormProps} />
<RoleAssignModal <AddOrUpdateForm {...addOrUpdateFormProps} />
loading={roleAssignLoading}
treeData={roleList} <RoleAssignModal {...roleAssignModal} />
checkedKeys={roleCheckedKeys}
handleOk={this.handleRoleAssignOK}
modalVisible={modalRoleVisible}
handleCheckBoxClick={this.handleRoleAssignCheckBoxClick}
handleModalVisible={() => this.handleRoleAssignModalVisibleClose(false)}
/>
</PageHeaderWrapper> </PageHeaderWrapper>
); );
} }

View File

@ -6,7 +6,8 @@ export function buildTreeNode(nodes, titleKey, nodeKey) {
if (item.children) { if (item.children) {
res.children = buildTreeNode(item.children, titleKey, nodeKey); res.children = buildTreeNode(item.children, titleKey, nodeKey);
} }
res.title = `${item.id}-${item[titleKey]}`; // res.title = `${item.id}-${item[titleKey]}`;
res.title = `${item[titleKey]}`;
res.key = item[nodeKey]; res.key = item[nodeKey];
return res; return res;
}); });