MerchantCreate.js 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. import React, { PureComponent } from 'react';
  2. import pathToRegexp from 'path-to-regexp';
  3. import { Card, Form, Input, Button, Radio, Switch } from 'antd';
  4. import { connect } from 'dva';
  5. import { routerRedux } from 'dva/router';
  6. import PageHeaderLayout from '../../layouts/PageHeaderLayout';
  7. import FooterToolbar from '../../components/FooterToolbar';
  8. import {
  9. DOMAIN_CP,
  10. DOMAIN_LJ,
  11. DOMAIN_PJ,
  12. } from '../../utils/config';
  13. import { statusToBool, boolToStatus } from '../../utils/utils';
  14. import styles from './MerchantCreate.less';
  15. const fieldLabels = {
  16. type: '商户类型',
  17. name: '商户名称',
  18. code: '商户编号',
  19. simple: '商户简称',
  20. contactName: '商户联系人',
  21. mobile: '联系电话',
  22. depositBank: '开户银行',
  23. bankAccount: '银行账户',
  24. licenseId: '营业执照',
  25. taxNumber: '纳税人识别号',
  26. receiptType: '发票类型',
  27. };
  28. const domains = [{
  29. title: '平台方',
  30. value: DOMAIN_LJ,
  31. }, {
  32. title: '渠道商',
  33. value: DOMAIN_PJ,
  34. }, {
  35. title: '供应商',
  36. value: DOMAIN_CP,
  37. }];
  38. const receipts = [{
  39. title: '普通发票',
  40. value: 'COMMON',
  41. }, {
  42. title: '增值税发票',
  43. value: 'SPECIAL',
  44. }];
  45. const formItemLayout = {
  46. labelCol: {
  47. xs: { span: 24 },
  48. sm: { span: 6 },
  49. },
  50. wrapperCol: {
  51. xs: { span: 24 },
  52. sm: { span: 14 },
  53. md: { span: 12 },
  54. },
  55. };
  56. @Form.create()
  57. @connect(({ loading, merchant }) => ({
  58. merchant,
  59. submitting: loading.models.merchant,
  60. }))
  61. export default class MerchantCreatePage extends PureComponent {
  62. componentWillMount() {
  63. // 如果是添加商户操作,进入此页面时把state清一下
  64. const { location } = this.props;
  65. const match = pathToRegexp('/merchant/create').exec(location.pathname);
  66. if (match) {
  67. this.cleanPageState();
  68. }
  69. }
  70. componentDidMount() {
  71. const matchId = this.isEdit();
  72. if (matchId) {
  73. this.props.dispatch({
  74. type: 'merchant/fetchMerchantItem',
  75. payload: { id: matchId },
  76. });
  77. }
  78. }
  79. isEdit = () => {
  80. // 根据路径判断操作类型并决定是否需加载该条目信息<create/edit>
  81. const { location } = this.props;
  82. const match = pathToRegexp('/merchant/edit/:id').exec(location.pathname);
  83. if (match) {
  84. return match[1];
  85. }
  86. return false;
  87. }
  88. cleanPageState = () => {
  89. this.props.dispatch({
  90. type: 'merchant/cleanItemState',
  91. payload: {},
  92. });
  93. }
  94. handlePageBack = () => {
  95. this.props.dispatch(routerRedux.push({
  96. pathname: '/merchant/list',
  97. state: this.props.location.state,
  98. }));
  99. }
  100. handlePageSubmit = (e) => {
  101. e.preventDefault();
  102. this.props.form.validateFieldsAndScroll((err, values) => {
  103. if (!err) {
  104. const { status, ...rest } = values;
  105. const matchId = this.isEdit();
  106. rest.status = boolToStatus(status);
  107. if (matchId) {
  108. rest.id = matchId;
  109. this.props.dispatch({
  110. type: 'merchant/updateMerchantItem',
  111. payload: rest,
  112. states: this.props.location.state,
  113. });
  114. } else {
  115. this.props.dispatch({
  116. type: 'merchant/createMerchantItem',
  117. payload: rest,
  118. states: this.props.location.state,
  119. });
  120. }
  121. }
  122. });
  123. }
  124. render() {
  125. const { submitting, merchant, form } = this.props;
  126. const { getFieldDecorator } = form;
  127. const { currentItem } = merchant;
  128. // 是否可编辑
  129. const isEditable = this.isEdit() ? true : false;
  130. return (
  131. <PageHeaderLayout>
  132. <Card style={{ marginBottom: 70 }}>
  133. <Form>
  134. <Form.Item label={fieldLabels.type} {...formItemLayout}>
  135. {getFieldDecorator('domain', {
  136. rules: [{ required: true, message: '请选择商户类型!' }],
  137. initialValue: currentItem.domain || DOMAIN_PJ,
  138. })(
  139. <Radio.Group disabled={isEditable} className={styles.radio}>
  140. {
  141. domains.map(item =>
  142. (
  143. <Radio.Button
  144. key={item.value}
  145. value={item.value}
  146. >{item.title}
  147. </Radio.Button>
  148. )
  149. )
  150. }
  151. </Radio.Group>
  152. )}
  153. </Form.Item>
  154. <Form.Item hasFeedback label={fieldLabels.code} {...formItemLayout}>
  155. {getFieldDecorator('code', {
  156. rules: [
  157. {
  158. required: true, message: '请填写商户编号!',
  159. }, {
  160. pattern: /^[0-9]{4,6}$/g, message: '请输入4-6位数字编号!',
  161. },
  162. ],
  163. initialValue: currentItem.code,
  164. })(
  165. <Input disabled={isEditable} placeholder="请输入" />
  166. )}
  167. </Form.Item>
  168. <Form.Item hasFeedback label={fieldLabels.name} {...formItemLayout}>
  169. {getFieldDecorator('name', {
  170. rules: [{ required: true, message: '请填写商户名称!' }],
  171. initialValue: currentItem.name,
  172. })(
  173. <Input placeholder="请输入" />
  174. )}
  175. </Form.Item>
  176. <Form.Item hasFeedback label={fieldLabels.simple} {...formItemLayout}>
  177. {getFieldDecorator('simple', {
  178. rules: [{ required: true, message: '请填写商户简称!' }],
  179. initialValue: currentItem.simple,
  180. })(
  181. <Input placeholder="请输入" />
  182. )}
  183. </Form.Item>
  184. <Form.Item hasFeedback label={fieldLabels.contactName} {...formItemLayout}>
  185. {getFieldDecorator('contactName', {
  186. rules: [{ required: true, message: '请填写商户联系人!' }],
  187. initialValue: currentItem.contactName,
  188. })(
  189. <Input placeholder="请输入" />
  190. )}
  191. </Form.Item>
  192. <Form.Item hasFeedback label={fieldLabels.mobile} {...formItemLayout}>
  193. {getFieldDecorator('mobile', {
  194. rules: [
  195. {
  196. required: true, message: '请填写联系电话!',
  197. }, {
  198. pattern: /^[1][3,4,5,7,8][0-9]{9}$/g, message: '请输入11位有效手机号!',
  199. }],
  200. initialValue: currentItem.mobile,
  201. })(
  202. <Input placeholder="请输入" />
  203. )}
  204. </Form.Item>
  205. <Form.Item hasFeedback label={fieldLabels.depositBank} {...formItemLayout}>
  206. {getFieldDecorator('depositBank', {
  207. rules: [{
  208. required: true, message: '开户银行不能为空!',
  209. }],
  210. initialValue: currentItem.depositBank,
  211. })(
  212. <Input placeholder="请输入" />
  213. )}
  214. </Form.Item>
  215. <Form.Item hasFeedback label={fieldLabels.bankAccount} {...formItemLayout}>
  216. {getFieldDecorator('bankAccount', {
  217. rules: [{
  218. required: true, message: '银行账号不能为空!',
  219. }, {
  220. pattern: /^[0-9]{1,22}$/g, message: '请输入有效的银行账户!',
  221. }],
  222. initialValue: currentItem.bankAccount,
  223. })(
  224. <Input placeholder="请输入" />
  225. )}
  226. </Form.Item>
  227. <Form.Item label={fieldLabels.licenseId} {...formItemLayout}>
  228. {getFieldDecorator('licenseId', {
  229. initialValue: currentItem.licenseId,
  230. })(
  231. <Input placeholder="请输入" />
  232. )}
  233. </Form.Item>
  234. <Form.Item label={fieldLabels.taxNumber} {...formItemLayout}>
  235. {getFieldDecorator('taxNumber', {
  236. initialValue: currentItem.taxNumber,
  237. })(
  238. <Input placeholder="请输入" />
  239. )}
  240. </Form.Item>
  241. <Form.Item label={fieldLabels.receiptType} {...formItemLayout}>
  242. {getFieldDecorator('receiptType', {
  243. initialValue: currentItem.receiptType || 'COMMON',
  244. })(
  245. <Radio.Group className={styles.radio}>
  246. {
  247. receipts.map(item =>
  248. (
  249. <Radio.Button
  250. key={item.value}
  251. value={item.value}
  252. >{item.title}
  253. </Radio.Button>
  254. )
  255. )
  256. }
  257. </Radio.Group>
  258. )}
  259. </Form.Item>
  260. <Form.Item label="状态" {...formItemLayout}>
  261. {getFieldDecorator('status', {
  262. valuePropName: 'checked',
  263. initialValue: status ? statusToBool(currentItem.status) : true,
  264. })(
  265. <Switch
  266. checkedChildren="启用"
  267. unCheckedChildren="不启用"
  268. />
  269. )}
  270. </Form.Item>
  271. </Form>
  272. </Card>
  273. <FooterToolbar style={{ width: '100%' }}>
  274. <Button onClick={this.handlePageBack} style={{ marginRight: 10 }}>
  275. 取消
  276. </Button>
  277. <Button type="primary" onClick={this.handlePageSubmit} loading={submitting}>
  278. 提交
  279. </Button>
  280. </FooterToolbar>
  281. </PageHeaderLayout>
  282. );
  283. }
  284. }