index.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. import React, { Component } from 'react';
  2. import { Card, Form, List, Input, Switch, Tooltip, Button, message } from 'antd';
  3. import { routerRedux } from 'dva/router';
  4. import { connect } from 'dva';
  5. import queryString from 'query-string';
  6. import PageHeaderLayout from '../../../layouts/PageHeaderLayout';
  7. import CampusSelectModal from './campus';
  8. import { Codes, pageSize } from '../../../utils/config';
  9. @Form.create()
  10. @connect(state => ({
  11. terminalDetail: state.terminalDetail,
  12. campus: state.campus,
  13. }))
  14. export default class TerminalProfile extends Component {
  15. handleCampusSelectClick = () => {
  16. this.props.dispatch({ type: 'terminalDetail/showModal' });
  17. this.props.dispatch({
  18. type: 'campus/query',
  19. payload: { pageNo: 1, pageSize },
  20. });
  21. }
  22. handleCampusModalOk = (data) => {
  23. this.props.dispatch({
  24. type: 'terminalDetail/saveCampus',
  25. payload: data,
  26. });
  27. }
  28. handleCampusModalCancel = () => {
  29. this.props.dispatch({ type: 'terminalDetail/hideModal' });
  30. }
  31. handleCampusModalSearch = (data) => {
  32. const newData = { ...data };
  33. if (newData.keyword) {
  34. newData[newData.field] = newData.keyword;
  35. }
  36. delete newData.field;
  37. delete newData.keyword;
  38. this.props.dispatch({
  39. type: 'campus/query',
  40. payload: { ...newData, pageNo: 1, pageSize },
  41. });
  42. }
  43. handleCampusModalTableChange = (pagination, filterArgs, filters) => {
  44. const newFilters = { ...filters };
  45. if (newFilters.keyword) {
  46. newFilters[newFilters.field] = newFilters.keyword;
  47. }
  48. delete newFilters.field;
  49. delete newFilters.keyword;
  50. const getValue = obj => Object.keys(obj).map(key => obj[key]).join(',');
  51. const tableFilters = Object.keys(filterArgs).reduce((obj, key) => {
  52. const newObj = { ...obj };
  53. newObj[key] = getValue(filterArgs[key]);
  54. return newObj;
  55. }, {});
  56. const data = { ...newFilters, ...tableFilters, pageNo: pagination.current, pageSize: pagination.pageSize };
  57. Object.keys(data).map(key => (data[key] ? null : delete data[key]));
  58. this.props.dispatch({ type: 'campus/query', payload: data });
  59. }
  60. handlePageCancel = () => {
  61. const { dispatch, terminalDetail } = this.props;
  62. const { filters } = terminalDetail;
  63. dispatch(routerRedux.push({
  64. pathname: '/terminal/user',
  65. search: queryString.stringify(filters),
  66. }));
  67. }
  68. handlePageSubmit = (e) => {
  69. e.preventDefault();
  70. const { dispatch, form, terminalDetail } = this.props;
  71. const { validateFields, getFieldsValue } = form;
  72. const { currentItem, operType, filters } = terminalDetail;
  73. const { campusId, id, status } = currentItem;
  74. validateFields((errors) => {
  75. if (errors) return;
  76. if (!campusId) {
  77. message.error('请选择校区!');
  78. return;
  79. }
  80. const data = { ...getFieldsValue(), campusId };
  81. if (operType === 'create') {
  82. data.status = Codes.CODE_NORMAL;
  83. } else if (operType === 'update') {
  84. data.status = status;
  85. data.id = id;
  86. }
  87. dispatch({
  88. type: `terminalDetail/${operType}`,
  89. payload: data,
  90. callback: () => {
  91. dispatch(routerRedux.push({
  92. pathname: '/terminal/user',
  93. search: queryString.stringify(filters),
  94. }));
  95. },
  96. });
  97. });
  98. }
  99. render() {
  100. const { form, terminalDetail, campus } = this.props;
  101. const { currentItem, modalShow, operType } = terminalDetail;
  102. const { name, password, status, campusId, campusName, merchantName } = currentItem;
  103. const { getFieldDecorator } = form;
  104. const formItemLayout = {
  105. labelCol: {
  106. xs: { span: 24 },
  107. sm: { span: 7 },
  108. md: { span: 8 },
  109. },
  110. wrapperCol: {
  111. xs: { span: 24 },
  112. sm: { span: 12 },
  113. md: { span: 10 },
  114. },
  115. };
  116. const submitFormLayout = {
  117. wrapperCol: {
  118. xs: { span: 24, offset: 0 },
  119. sm: { span: 10, offset: 8 },
  120. },
  121. };
  122. return (
  123. <PageHeaderLayout>
  124. <Card title="终端用户">
  125. <Form layout="horizontal" onSubmit={this.handlePageSubmit}>
  126. <Form.Item hasFeedback label="所属校区" {...formItemLayout}>
  127. <Tooltip placement="top" title="点击选择校区">
  128. <Button disabled={operType === 'update'} style={{ marginRight: 20 }} type="primary" size="small" icon="select" onClick={this.handleCampusSelectClick}>选择</Button>
  129. </Tooltip>
  130. {operType === 'update' ?
  131. (
  132. <List
  133. size="small"
  134. bordered
  135. dataSource={[
  136. `所属校区: ${campusName}`,
  137. `所属渠道: ${merchantName}`,
  138. ]}
  139. renderItem={item => <List.Item>{item}</List.Item>}
  140. />
  141. ) : (
  142. campusId ? <List size="small" bordered dataSource={[`${campusName}`]} renderItem={item => <List.Item>{item}</List.Item>} /> : null)
  143. }
  144. </Form.Item>
  145. <Form.Item label="终端名称:" hasFeedback {...formItemLayout}>
  146. {getFieldDecorator('name', {
  147. initialValue: name,
  148. })(<Input placeholder="请输入(例: 教室三/教室四...)" />)}
  149. </Form.Item>
  150. {operType === 'create' ?
  151. (
  152. <Form.Item label="终端密码:" hasFeedback {...formItemLayout}>
  153. {getFieldDecorator('password', {
  154. rules: [{ required: true, type: 'string', message: '密码为必填项!' }],
  155. initialValue: password,
  156. })(<Input placeholder="请输入" />)}
  157. </Form.Item>
  158. ) : (
  159. <Form.Item label="终端密码:" hasFeedback {...formItemLayout}>
  160. {getFieldDecorator('password', {
  161. initialValue: password,
  162. })(<Input placeholder="修改密码时填写" />)}
  163. </Form.Item>
  164. )
  165. }
  166. <Form.Item label="账号状态:" {...formItemLayout}>
  167. {getFieldDecorator('status', {
  168. valuePropsName: 'checked',
  169. })(
  170. <Switch
  171. defaultChecked={status === Codes.CODE_NORMAL}
  172. checkedChildren="使用中"
  173. unCheckedChildren="禁用中"
  174. />
  175. )}
  176. </Form.Item>
  177. <Form.Item {...submitFormLayout} style={{ marginTop: 32 }}>
  178. <Button onClick={this.handlePageCancel}>取消</Button>
  179. <Button type="primary" style={{ marginLeft: 35 }} htmlType="submit">提交</Button>
  180. </Form.Item>
  181. </Form>
  182. {/* 校区模态选择框 */}
  183. <CampusSelectModal
  184. rowKeyName="id"
  185. modalVisible={modalShow}
  186. width={600}
  187. onOk={this.handleCampusModalOk}
  188. onCancel={this.handleCampusModalCancel}
  189. onSearch={this.handleCampusModalSearch}
  190. fsTableDataSource={campus.list || []}
  191. fsTableLoading={campus.listLoading}
  192. fsTablePagination={campus.pagination}
  193. fsTableOnChange={this.handleCampusModalTableChange}
  194. />
  195. </Card>
  196. </PageHeaderLayout>
  197. );
  198. }
  199. }