import React, { PureComponent } from 'react'; import pathToRegexp from 'path-to-regexp'; import { Card, Form, Input, Button, Radio, Switch } from 'antd'; import { connect } from 'dva'; import { routerRedux } from 'dva/router'; import PageHeaderLayout from '../../layouts/PageHeaderLayout'; import FooterToolbar from '../../components/FooterToolbar'; import { Hotax } from '../../utils/config'; import { statusToBool, boolToStatus } from '../../utils/utils'; import styles from './MerchantCreate.less'; const fieldLabels = { type: '商户类型', name: '商户名称', code: '商户编号', simple: '商户简称', contactName: '商户联系人', mobile: '联系电话', depositBank: '开户银行', bankAccount: '银行账户', licenseId: '营业执照', taxNumber: '纳税人识别号', receiptType: '发票类型', }; const domains = [{ title: '平台方', value: Hotax.DOMAIN_LJ, }, { title: '渠道商', value: Hotax.DOMAIN_PJ, }, { title: '供应商', value: Hotax.DOMAIN_CP, }]; const receipts = [{ title: '普通发票', value: 'COMMON', }, { title: '增值税发票', value: 'SPECIAL', }]; const formItemLayout = { labelCol: { xs: { span: 24 }, sm: { span: 6 }, }, wrapperCol: { xs: { span: 24 }, sm: { span: 14 }, md: { span: 12 }, }, }; @Form.create() @connect(({ loading, merchant }) => ({ merchant, submitting: loading.models.merchant, })) export default class MerchantCreatePage extends PureComponent { componentWillMount() { // 如果是添加商户操作,进入此页面时把state清一下 const { location } = this.props; const match = pathToRegexp('/merchant/create').exec(location.pathname); if (match) { this.cleanPageState(); } } componentDidMount() { const matchId = this.isEdit(); if (matchId) { this.props.dispatch({ type: 'merchant/fetchMerchantItem', payload: { id: matchId }, }); } } isEdit = () => { // 根据路径判断操作类型并决定是否需加载该条目信息 const { location } = this.props; const match = pathToRegexp('/merchant/edit/:id').exec(location.pathname); if (match) { return match[1]; } return false; }; cleanPageState = () => { this.props.dispatch({ type: 'merchant/cleanItemState', payload: {}, }); }; handlePageBack = () => { this.props.dispatch(routerRedux.push({ pathname: '/merchant/list', state: this.props.location.state, })); }; handlePageSubmit = (e) => { e.preventDefault(); this.props.form.validateFieldsAndScroll((err, values) => { if (!err) { const { status, ...rest } = values; const matchId = this.isEdit(); rest.status = boolToStatus(status); if (matchId) { rest.id = matchId; this.props.dispatch({ type: 'merchant/updateMerchantItem', payload: rest, states: this.props.location.state, }); } else { this.props.dispatch({ type: 'merchant/createMerchantItem', payload: rest, states: this.props.location.state, }); } } }); }; render() { const { submitting, merchant, form } = this.props; const { getFieldDecorator } = form; const { currentItem } = merchant; // 是否可编辑 const isEditable = !!this.isEdit(); return (
{getFieldDecorator('domain', { rules: [{ required: true, message: '请选择商户类型!' }], initialValue: currentItem.domain || Hotax.DOMAIN_PJ, })( { domains.map(item => ( {item.title} ) ) } )} {getFieldDecorator('code', { rules: [ { required: true, message: '请填写商户编号!', }, { pattern: /^[0-9]{4,6}$/g, message: '请输入4-6位数字编号!', }, ], initialValue: currentItem.code, })( )} {getFieldDecorator('name', { rules: [{ required: true, message: '请填写商户名称!' }], initialValue: currentItem.name, })( )} {getFieldDecorator('simple', { rules: [{ required: true, message: '请填写商户简称!' }], initialValue: currentItem.simple, })( )} {getFieldDecorator('contactName', { rules: [{ required: true, message: '请填写商户联系人!' }], initialValue: currentItem.contactName, })( )} {getFieldDecorator('mobile', { rules: [ { required: true, message: '请填写联系电话!', }, { pattern: /^[1][34578][0-9]{9}$/g, message: '请输入11位有效手机号!', }], initialValue: currentItem.mobile, })( )} {getFieldDecorator('depositBank', { rules: [{ required: true, message: '开户银行不能为空!', }], initialValue: currentItem.depositBank, })( )} {getFieldDecorator('bankAccount', { rules: [{ required: true, message: '银行账号不能为空!', }, { pattern: /^[0-9]{1,22}$/g, message: '请输入有效的银行账户!', }], initialValue: currentItem.bankAccount, })( )} {getFieldDecorator('licenseId', { initialValue: currentItem.licenseId, })( )} {getFieldDecorator('taxNumber', { initialValue: currentItem.taxNumber, })( )} {getFieldDecorator('receiptType', { initialValue: currentItem.receiptType || 'COMMON', })( { receipts.map(item => ( {item.title} ) ) } )} {getFieldDecorator('status', { valuePropName: 'checked', initialValue: status ? statusToBool(currentItem.status) : true, })( )}
); } }