TerminalCreate.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. import React, { PureComponent } from 'react';
  2. import { message, Card, Modal, List, Form, Input, Button, Icon, Switch } from 'antd';
  3. import { connect } from 'dva';
  4. import { routerRedux } from 'dva/router';
  5. import Selector from '../../../components/AXTableSelector/Selector';
  6. import FooterToolbar from '../../../components/FooterToolbar';
  7. import { boolToStatus } from '../../../utils/utils';
  8. const Message = message;
  9. const fieldLabels = {
  10. campus: '所属校区',
  11. name: '终端名称',
  12. password: '终端密码',
  13. confirm: '确认密码',
  14. status: '初始状态',
  15. };
  16. const formItemLayout = {
  17. labelCol: {
  18. xs: { span: 24 },
  19. sm: { span: 7 },
  20. },
  21. wrapperCol: {
  22. xs: { span: 24 },
  23. sm: { span: 14 },
  24. md: { span: 10 },
  25. },
  26. };
  27. @Form.create()
  28. @connect(({ loading, terminal, campus }) => ({
  29. campus,
  30. terminal,
  31. cLoading: loading.models.campus,
  32. submitting: loading.models.terminal,
  33. }))
  34. export default class TerminalCreatePage extends PureComponent {
  35. state = {
  36. campusSelectorDestroy: true,
  37. };
  38. componentWillUnmount() {
  39. this.props.dispatch({
  40. type: 'terminal/cleanState',
  41. });
  42. }
  43. checkPassword = (rule, value, callback) => {
  44. if (value && value !== this.props.form.getFieldValue('password')) {
  45. callback('两次输入的密码不一致');
  46. } else {
  47. callback();
  48. }
  49. };
  50. handleCampusSelectorModalShow = () => {
  51. this.setState({
  52. campusSelectorDestroy: false,
  53. });
  54. this.props.dispatch({
  55. type: 'campus/fetchCampusList',
  56. payload: {},
  57. });
  58. };
  59. handleCampusSelectorFinish = (rows) => {
  60. this.setState({
  61. campusSelectorDestroy: true,
  62. });
  63. if (!rows || !rows.length) {
  64. return;
  65. }
  66. const { id, code, name, merchantName } = rows[0];
  67. this.props.dispatch({
  68. type: 'terminal/fixCurrentItem',
  69. payload: {
  70. merchantName,
  71. campusId: id,
  72. campusCode: code,
  73. campusName: name,
  74. },
  75. });
  76. };
  77. handleCampusSelectorCancel = () => {
  78. this.setState({
  79. campusSelectorDestroy: true,
  80. });
  81. };
  82. handleCampusSelectorChange = (params) => {
  83. this.props.dispatch({
  84. type: 'campus/fetchCampusList',
  85. payload: params,
  86. });
  87. };
  88. handlePageSubmit = () => {
  89. this.props.form.validateFieldsAndScroll((error, values) => {
  90. const { currentItem } = this.props.terminal;
  91. const { campusId } = currentItem;
  92. if (!campusId) {
  93. Message.error('请选择校区');
  94. return;
  95. }
  96. const { status, ...restProps } = values;
  97. restProps.status = boolToStatus(status);
  98. restProps.campusId = campusId;
  99. if (!error) {
  100. this.props.dispatch({
  101. type: 'terminal/createTerminalItem',
  102. payload: restProps,
  103. states: this.props.location.state,
  104. });
  105. }
  106. });
  107. };
  108. handlePageBack = () => {
  109. this.props.dispatch(routerRedux.push({
  110. pathname: '/terminal/user/list',
  111. state: this.props.location.state,
  112. }));
  113. };
  114. render() {
  115. const { campusSelectorDestroy } = this.state;
  116. const { form, cLoading, submitting, campus, terminal } = this.props;
  117. const { getFieldDecorator } = form;
  118. const { currentItem } = terminal;
  119. const { campusCode, campusName, merchantName } = currentItem;
  120. const getCampusModal = () => {
  121. return (
  122. <Modal
  123. visible
  124. width={1100}
  125. footer={null}
  126. title="校区列表"
  127. maskClosable={false}
  128. onCancel={this.handleCampusSelectorCancel}
  129. >
  130. <Selector
  131. multiple={false}
  132. loading={cLoading}
  133. selectorName="Campus"
  134. list={campus.list}
  135. pageNo={campus.pageNo}
  136. pageSize={campus.pageSize}
  137. totalSize={campus.totalSize}
  138. onCancel={this.handleCampusSelectorCancel}
  139. onChange={this.handleCampusSelectorChange}
  140. onFinish={this.handleCampusSelectorFinish}
  141. />
  142. </Modal>
  143. );
  144. };
  145. return (
  146. <div>
  147. <Card title="创建终端" style={{ marginBottom: 70 }}>
  148. <Form>
  149. <Form.Item
  150. {...formItemLayout}
  151. label={<a onClick={this.handleCampusSelectorModalShow}>选择校区</a>}
  152. >
  153. <List
  154. bordered
  155. size="small"
  156. dataSource={[
  157. `校区编号: ${campusCode || ''}`,
  158. `校区名称: ${campusName || ''}`,
  159. `所属厂商: ${merchantName || ''}`,
  160. ]}
  161. renderItem={item => <List.Item>{item}</List.Item>}
  162. />
  163. </Form.Item>
  164. <Form.Item label={fieldLabels.name} {...formItemLayout}>
  165. {getFieldDecorator('name')(
  166. <Input
  167. placeholder="选填(不填写会默认生成,格式为`教室xx`)"
  168. />
  169. )}
  170. </Form.Item>
  171. <Form.Item hasFeedback label={fieldLabels.password} {...formItemLayout}>
  172. {getFieldDecorator('password', {
  173. rules: [
  174. {
  175. required: true, message: '密码不能为空!',
  176. }, {
  177. pattern: /^[a-zA-Z0-9|-]+$/ig, message: '密码格式错误!',
  178. }],
  179. })(
  180. <Input
  181. type="password"
  182. placeholder="请输入"
  183. addonBefore={<Icon type="lock" />}
  184. />
  185. )}
  186. </Form.Item>
  187. <Form.Item hasFeedback label={fieldLabels.confirm} {...formItemLayout}>
  188. {getFieldDecorator('confirm', {
  189. rules: [
  190. {
  191. required: true, message: '请再次输入你的密码',
  192. }, {
  193. validator: this.checkPassword,
  194. }],
  195. })(
  196. <Input
  197. type="password"
  198. placeholder="请输入"
  199. addonBefore={<Icon type="lock" />}
  200. />
  201. )}
  202. </Form.Item>
  203. <Form.Item label={fieldLabels.status} {...formItemLayout}>
  204. {getFieldDecorator('status', {
  205. valuePropName: 'checked',
  206. initialValue: true,
  207. })(
  208. <Switch
  209. checkedChildren="启用"
  210. unCheckedChildren="禁用"
  211. />
  212. )}
  213. </Form.Item>
  214. </Form>
  215. {!campusSelectorDestroy && getCampusModal()}
  216. </Card>
  217. <FooterToolbar style={{ width: '100%' }}>
  218. <Button
  219. onClick={this.handlePageBack}
  220. style={{ marginRight: 10 }}
  221. >取消
  222. </Button>
  223. <Button
  224. type="primary"
  225. loading={submitting}
  226. onClick={this.handlePageSubmit}
  227. >提交
  228. </Button>
  229. </FooterToolbar>
  230. </div>
  231. );
  232. }
  233. }