orderinfo.js 4.1 KB
Newer Older
baiguangyao committed
1
import React, { Component } from 'react';
wangshuo committed
2
import { Flex, NavBar, List, InputItem, Button, WhiteSpace, WingBlank, Toast } from 'antd-mobile';
wangshuo committed
3
import { Formik, Field, Form, withFormik } from 'formik';
wangshuo committed
4
import {HeaderBar} from '../../common';
wangshuo committed
5
import { http, api } from "@/utils";
baiguangyao committed
6

wangshuo committed
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
const InnerForm = ({
    values,
    errors,
    touched,
    setFieldValue,
    handleBlur,
    handleSubmit,
    isSubmitting,
}) => (
        <form>
            <InputItem
                clear
                name="real_name"
                onChange={(value) => setFieldValue('real_name', value)}
                placeholder="请输入您的真实姓名"
wangshuo committed
22
                value={values.real_name}
wangshuo committed
23 24 25 26 27 28 29
            >姓名</InputItem>
            <InputItem
                clear
                type='phone'
                name='cellphone'
                placeholder="请输入您的手机号码"
                onChange={(value) => setFieldValue('cellphone', value)}
wangshuo committed
30
                value={values.cellphone}
wangshuo committed
31 32 33 34 35 36 37
            >电话</InputItem>
            <InputItem
                clear
                name='qq'
                placeholder="请输入您的QQ号码"
                maxLength='15'
                onChange={(value) => setFieldValue('qq', value)}
wangshuo committed
38
                value={values.qq}
wangshuo committed
39 40 41 42 43 44 45 46 47 48 49
            >QQ</InputItem>

            <div style={{ marginTop: '21px' }}>
                <WingBlank>
                    <Button type='primary' onClick={handleSubmit}>确认</Button>
                </WingBlank>
            </div>
        </form>
    );

const MyForm = withFormik({
wangshuo committed
50
    mapPropsToValues: props => ({ ...props.userInfo }),
wangshuo committed
51 52 53 54 55 56 57 58
    validate: (values, props) => {
        const errors = {};
        return errors;
    },
    handleSubmit: (
        values,
        FormBag
    ) => {
wangshuo committed
59 60 61 62 63
        const {real_name, cellphone, qq} = values;
        if (!real_name) {
            Toast.info('请输入姓名!', undefined, undefined, false);
            return;
        }else if ((`${real_name}`).replace(/\s+/g, '').length === 0) {
wangshuo committed
64 65 66
            Toast.info('请输入姓名!', undefined, undefined, false);
            return;
        }
wangshuo committed
67
        if (!cellphone) {
wangshuo committed
68 69
            Toast.info('请输入手机号!', undefined, undefined, false);
            return;
wangshuo committed
70
        } else if (!/1\d{10}/g.test((`${cellphone}`).replace(/\s+/g, ''))) {
wangshuo committed
71 72 73
            Toast.info('请输入正确的手机号!', undefined, undefined, false);
            return;
        }
wangshuo committed
74
        if (!qq) {
wangshuo committed
75 76
            Toast.info('请输入QQ号!', undefined, undefined, false);
            return;
wangshuo committed
77
        } else if (!/\d{5,}/g.test(qq)) {
wangshuo committed
78 79 80
            Toast.info('请输入正确QQ号!', undefined, undefined, false);
            return;
        }
wangshuo committed
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
        let same = false;
        if(FormBag.props.userInfo) {
            const userInfo = FormBag.props.userInfo;
            if(userInfo.real_name === real_name) {
                same = true;
            }
            if(userInfo.cellphone === cellphone) {
                same = true;
            }
            if(userInfo.qq === qq) {
                same = true;
            }
        }
        if(same) {
            FormBag.props.history.replace({
                pathname: '/order',
                state: values
            });
            return;
        }
        http.post(`${api.home}/m/order/saveUserInfo`, {real_name: values.real_name, cellphone: (`${values.cellphone}`).replace(/\s+/g, ''), qq: values.qq}).then(res=>{
wangshuo committed
102 103 104 105 106
            if(res.data.code !== 200) {
                Toast.info(res.data.msg, undefined, undefined, false);
                return;
            }
            Toast.info('保存成功!', undefined, undefined, false);
wangshuo committed
107
            FormBag.props.history.replace({
wangshuo committed
108 109 110
                pathname: '/order',
                state: values
            });
wangshuo committed
111
        });
wangshuo committed
112 113 114 115 116 117
    },
})(InnerForm);

class Orderinfo extends Component {
    constructor(props) {
        super(props);
wangshuo committed
118
    }
wangshuo committed
119

wangshuo committed
120 121
    render() {
        return (
wangshuo committed
122
            <div>
wangshuo committed
123
                <HeaderBar title='报名信息' arrow={true}></HeaderBar>
wangshuo committed
124
                <MyForm history={this.props.history} userInfo={this.props.location.state}/>
wangshuo committed
125
            </div>
wangshuo committed
126 127
        )
    }
baiguangyao committed
128

wangshuo committed
129
};
baiguangyao committed
130

wangshuo committed
131
export default Orderinfo;