orderinfo.js 3.54 KB
Newer Older
zhanghaozhe committed
1 2 3 4 5
import React, { Component } from "react"
import { InputItem, Button, WingBlank, Toast } from "antd-mobile"
import { withFormik } from "formik"
import { HeaderBar } from "../../common"
import { http } from "src/utils"
baiguangyao committed
6

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

zhanghaozhe committed
39 40 41 42 43 44 45 46 47
    <div style={{ marginTop: "21px" }}>
      <WingBlank>
        <Button type="primary" onClick={handleSubmit}>
          确认
        </Button>
      </WingBlank>
    </div>
  </form>
)
wangshuo committed
48 49

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

class Orderinfo extends Component {
zhanghaozhe committed
123 124 125 126 127 128 129 130 131 132 133 134
  render() {
    return (
      <div>
        <HeaderBar title="报名信息" arrow={true}></HeaderBar>
        <MyForm
          history={this.props.history}
          userInfo={this.props.location.state}
        />
      </div>
    )
  }
}
baiguangyao committed
135

zhanghaozhe committed
136
export default Orderinfo