orderinfo.js 4.36 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';
zhanghaozhe committed
5
import { http } 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;
        }
81
        let same = 0;
wangshuo committed
82 83 84
        if(FormBag.props.userInfo) {
            const userInfo = FormBag.props.userInfo;
            if(userInfo.real_name === real_name) {
85
                same += 1;
wangshuo committed
86 87
            }
            if(userInfo.cellphone === cellphone) {
88
                same += 1;
wangshuo committed
89 90
            }
            if(userInfo.qq === qq) {
91
                same += 1;
wangshuo committed
92 93
            }
        }
94 95 96 97
        let pathName = '/order';
        if(FormBag.props.userInfo.id) {
            pathName = pathName + '?id=' + FormBag.props.userInfo.id;
        }
98
        // console.log(values);
99 100 101 102 103 104 105
        if(same === 3) {
            FormBag.props.history.replace(
                pathName,
                {
                    ...values,
                }
            );
wangshuo committed
106 107
            return;
        }
108
        http.post(`${API['base-api']}/m/order/saveUserInfo`, {real_name: values.real_name, cellphone: (`${values.cellphone}`).replace(/\s+/g, ''), qq: values.qq}).then(res=>{
109
            // console.log(res);
110
            if(res.data.errno !== 200) {
wangshuo committed
111 112 113 114
                Toast.info(res.data.msg, undefined, undefined, false);
                return;
            }
            Toast.info('保存成功!', undefined, undefined, false);
115 116
            FormBag.props.history.replace(
                pathName,
117 118 119
                {
                    ...values,
                }
120
            );
wangshuo committed
121
        });
wangshuo committed
122 123 124 125 126 127
    },
})(InnerForm);

class Orderinfo extends Component {
    constructor(props) {
        super(props);
wangshuo committed
128
    }
wangshuo committed
129

wangshuo committed
130 131
    render() {
        return (
wangshuo committed
132
            <div>
wangshuo committed
133
                <HeaderBar title='报名信息' arrow={true}></HeaderBar>
wangshuo committed
134
                <MyForm history={this.props.history} userInfo={this.props.location.state}/>
wangshuo committed
135
            </div>
wangshuo committed
136 137
        )
    }
baiguangyao committed
138

wangshuo committed
139
};
baiguangyao committed
140

wangshuo committed
141
export default Orderinfo;