WhiteListCreate.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. /* eslint-disable no-param-reassign,no-trailing-spaces */
  2. import React, { Component } from 'react';
  3. import pathToRegexp from 'path-to-regexp';
  4. import moment from 'moment';
  5. import { Card, Modal, Form, DatePicker, Button, List } from 'antd';
  6. import { connect } from 'dva';
  7. import { routerRedux } from 'dva/router';
  8. import Selector from '../../../components/AXTableSelector/Selector';
  9. import FooterToolbar from '../../../components/FooterToolbar';
  10. import { Hotax } from '../../../utils/config';
  11. const formItemLayout = {
  12. labelCol: {
  13. xs: { span: 24 },
  14. sm: { span: 9 },
  15. },
  16. wrapperCol: {
  17. xs: { span: 24 },
  18. sm: { span: 14 },
  19. md: { span: 10 },
  20. },
  21. };
  22. @Form.create()
  23. @connect(({ loading, terminal }) => ({
  24. terminal,
  25. tLoading: loading.models.terminal,
  26. }))
  27. export default class WhiteListCreatePage extends Component {
  28. state = {
  29. terminalSelectorDestroy: true,
  30. };
  31. componentDidMount() {
  32. const matchId = this.isEdit();
  33. if (matchId) {
  34. this.props.dispatch({
  35. type: 'terminal/fetchSpecialTerminalItem',
  36. payload: { userId: matchId },
  37. });
  38. } else {
  39. this.props.dispatch({
  40. type: 'terminal/fetchTerminalList',
  41. payload: {},
  42. });
  43. }
  44. }
  45. componentWillUnmount() {
  46. this.props.dispatch({
  47. type: 'terminal/cleanState',
  48. });
  49. }
  50. handleTerminalSelectorModalShow = () => {
  51. this.setState({
  52. terminalSelectorDestroy: false,
  53. });
  54. this.props.dispatch({
  55. type: 'terminal/fetchTerminalList',
  56. payload: {},
  57. });
  58. }
  59. handleTerminalSelectorFinish = (rows) => {
  60. this.setState({
  61. terminalSelectorDestroy: true,
  62. });
  63. if (!rows || !rows.length) {
  64. return;
  65. }
  66. const { id, code, merchantName } = rows[0];
  67. this.props.dispatch({
  68. type: 'terminal/fixCurrentItem',
  69. payload: {
  70. code,
  71. merchantName,
  72. userId: id,
  73. },
  74. });
  75. };
  76. handleTerminalSelectorCancel = () => {
  77. this.setState({
  78. terminalSelectorDestroy: true,
  79. });
  80. };
  81. handleTerminalSelectorChange = (params) => {
  82. this.props.dispatch({
  83. type: 'terminal/fetchTerminalList',
  84. payload: params,
  85. });
  86. }
  87. isEdit = () => {
  88. const { location } = this.props;
  89. const match = pathToRegexp('/terminal/whitelist/edit/:id').exec(location.pathname);
  90. if (match) {
  91. return match[1];
  92. }
  93. return false;
  94. }
  95. handlePageSubmit = () => {
  96. this.props.form.validateFieldsAndScroll((error, values) => {
  97. if (!error) {
  98. const { terminal } = this.props;
  99. const { currentItem } = terminal;
  100. const { id, code, userId, status } = currentItem;
  101. values = { id, code, userId, status, ...values };
  102. // 编辑模式
  103. if (this.isEdit()) {
  104. this.props.dispatch({
  105. type: 'terminal/updateSpecialTerminalItem',
  106. payload: values,
  107. states: this.props.location.state,
  108. });
  109. // 创建模式
  110. } else {
  111. delete values.id;
  112. values.status = Hotax.STATUS_NORMAL;
  113. this.props.dispatch({
  114. type: 'terminal/createSpecialTerminalItem',
  115. payload: values,
  116. states: this.props.location.state,
  117. });
  118. }
  119. }
  120. });
  121. }
  122. handlePageBack = () => {
  123. this.props.dispatch(routerRedux.push({
  124. pathname: '/terminal/whitelist',
  125. state: this.props.location.state,
  126. }));
  127. }
  128. render() {
  129. const { terminalSelectorDestroy } = this.state;
  130. const { form, tLoading, terminal } = this.props;
  131. const { getFieldDecorator } = form;
  132. const { currentItem } = terminal;
  133. const { code, merchantName, startTime, endTime } = currentItem;
  134. const getTerminalModal = () => {
  135. return (
  136. <Modal
  137. visible
  138. width={1100}
  139. footer={null}
  140. title="终端列表"
  141. maskClosable={false}
  142. onCancel={this.handleTerminalSelectorCancel}
  143. >
  144. <Selector
  145. multiple={false}
  146. loading={tLoading}
  147. selectorName="Terminal"
  148. list={terminal.list}
  149. pageNo={terminal.pageNo}
  150. pageSize={terminal.pageSize}
  151. totalSize={terminal.totalSize}
  152. onCancel={this.handleTerminalSelectorCancel}
  153. onChange={this.handleTerminalSelectorChange}
  154. onFinish={this.handleTerminalSelectorFinish}
  155. />
  156. </Modal>
  157. );
  158. };
  159. return (
  160. <div>
  161. <Card title={this.isEdit() ? '编辑白名单' : '创建白名单'} style={{ marginBottom: 70 }}>
  162. <Form>
  163. <Form.Item
  164. label={!this.isEdit() ? <a onClick={this.handleTerminalSelectorModalShow}>选择终端</a> : '终端账号'}
  165. {...formItemLayout}
  166. >
  167. <List
  168. bordered
  169. size="small"
  170. dataSource={[
  171. `终端编号: ${code || ''}`,
  172. `所属渠道: ${merchantName || ''}`,
  173. ]}
  174. renderItem={item => <List.Item>{item}</List.Item>}
  175. style={{ width: 280 }}
  176. />
  177. </Form.Item>
  178. <Form.Item label="起始日期" {...formItemLayout}>
  179. {getFieldDecorator('startTime', {
  180. rules: [{ required: true, message: '请选择起始日期' }],
  181. initialValue: startTime && moment(startTime),
  182. })(
  183. <DatePicker
  184. showTime
  185. placeholder="选择日期时间"
  186. format="YYYY-MM-DD HH:mm:ss"
  187. style={{ width: 280 }}
  188. />
  189. )}
  190. </Form.Item>
  191. <Form.Item label="截止日期" {...formItemLayout}>
  192. {getFieldDecorator('endTime', {
  193. initialValue: endTime && moment(endTime),
  194. })(
  195. <DatePicker
  196. showTime
  197. placeholder="选择日期时间"
  198. format="YYYY-MM-DD HH:mm:ss"
  199. style={{ width: 280 }}
  200. />
  201. )}
  202. </Form.Item>
  203. </Form>
  204. {!terminalSelectorDestroy && getTerminalModal()}
  205. </Card>
  206. <FooterToolbar style={{ width: '100%' }}>
  207. <Button onClick={this.handlePageBack} style={{ marginRight: 10 }}>
  208. 取消
  209. </Button>
  210. <Button type="primary" onClick={this.handlePageSubmit}>
  211. 提交
  212. </Button>
  213. </FooterToolbar>
  214. </div>
  215. );
  216. }
  217. }