index.js 4.67 KB
Newer Older
zhanghaozhe committed
1 2 3 4 5
import React, { Component } from "react"
import { http } from "src/utils"
import { Formik, Form, Field } from "formik"
import { Toast } from "antd-mobile"
import "./index.scss"
xuzhenghua committed
6 7 8 9 10 11 12

class AddressPopup extends Component {
  constructor(props) {
    super(props)
    this.state = {
      isLoading: false,
      addressInfo: {
zhanghaozhe committed
13 14 15
        name: "",
        phone: "",
        address: "",
xuzhenghua committed
16 17 18 19 20
      },
    }
  }

  componentDidMount() {
zhanghaozhe committed
21
    this.fetchUserAddress()
xuzhenghua committed
22 23 24 25
  }

  // 获取收货信息
  fetchUserAddress = () => {
zhanghaozhe committed
26 27
    const { addressInfo } = this.state
    http.get(`${API.home}/sys/user_address_info`).then((res) => {
zhanghaozhe committed
28
      const { code, data } = res.data
xuzhenghua committed
29 30 31 32 33 34 35 36
      if (code === 200) {
        this.setState({
          addressInfo: Object.assign({}, addressInfo, {
            name: data.name,
            phone: data.phone,
            address: data.address,
          }),
          isLoading: true,
zhanghaozhe committed
37
        })
xuzhenghua committed
38
      }
zhanghaozhe committed
39
    })
xuzhenghua committed
40 41 42
  }

  handleToSubmit = (params = {}) => {
zhanghaozhe committed
43 44 45
    const { handleToHide } = this.props
    http.post(`${API.home}/sys/collect_info`, params).then((res) => {
      const { code, msg } = res.data
xuzhenghua committed
46
      if (code === 200) {
zhanghaozhe committed
47
        handleToHide()
xuzhenghua committed
48
      } else {
zhanghaozhe committed
49
        Toast.info(msg, 2, null, false)
xuzhenghua committed
50
      }
zhanghaozhe committed
51
    })
xuzhenghua committed
52 53 54
  }

  render() {
zhanghaozhe committed
55 56
    const { isLoading, addressInfo } = this.state
    const { tip, prize } = this.props
xuzhenghua committed
57 58
    return (
      <>
zhanghaozhe committed
59
        {isLoading && (
xuzhenghua committed
60
          <Formik
zhanghaozhe committed
61 62
            initialValues={{
              ...addressInfo,
xuzhenghua committed
63
            }}
zhanghaozhe committed
64 65
            validate={({ name, phone, address }) => {
              const errors = {}
xuzhenghua committed
66 67

              if (!name) {
zhanghaozhe committed
68
                errors.name = "请输入收件人"
xuzhenghua committed
69
              }
zhanghaozhe committed
70
              if (!/^1[3-9]\d{9}$/.test(phone)) {
zhanghaozhe committed
71
                errors.phone = "请填写正确格式的手机号"
xuzhenghua committed
72 73
              }
              if (!address) {
zhanghaozhe committed
74
                errors.address = "请输入收货地址"
xuzhenghua committed
75 76
              }

zhanghaozhe committed
77
              return errors
xuzhenghua committed
78 79 80 81
            }}
            validateOnBlur={false}
            validateOnChange={false}
            onSubmit={(values) => {
zhanghaozhe committed
82
              this.handleToSubmit(values)
xuzhenghua committed
83
            }}
zhanghaozhe committed
84
          >
zhanghaozhe committed
85
            {({ errors }) => (
xuzhenghua committed
86
              <Form className="address-form">
zhanghaozhe committed
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
                {prize ? (
                  <p className="address__prize">
                    您抽中了{prize}
                    <span style={{ color: "#FF4000" }}>实物奖品</span>
                  </p>
                ) : null}
                {tip ? (
                  <p className="address-form__desc">{tip}</p>
                ) : (
                  <p className="address-form__desc">
                    请及时填写收货信息,获得实物奖品后将第一时间为您邮寄
                  </p>
                )}
                <Field name="name">
                  {({ field }) => (
xuzhenghua committed
102 103 104 105 106 107 108
                    <div className="address-form__item">
                      <input
                        {...field}
                        className="address-form__ipt"
                        type="text"
                        placeholder="收件人"
                      />
zhanghaozhe committed
109
                      {errors.name && (
xuzhenghua committed
110
                        <p className="address-form__tip">{errors.name}</p>
zhanghaozhe committed
111
                      )}
xuzhenghua committed
112 113
                    </div>
                  )}
zhanghaozhe committed
114
                </Field>
zhanghaozhe committed
115 116
                <Field name="phone">
                  {({ field }) => (
xuzhenghua committed
117 118 119 120 121 122 123
                    <div className="address-form__item">
                      <input
                        {...field}
                        className="address-form__ipt"
                        type="text"
                        placeholder="联系方式"
                      />
zhanghaozhe committed
124
                      {errors.phone && (
xuzhenghua committed
125
                        <p className="address-form__tip">{errors.phone}</p>
zhanghaozhe committed
126
                      )}
xuzhenghua committed
127 128
                    </div>
                  )}
zhanghaozhe committed
129
                </Field>
zhanghaozhe committed
130 131
                <Field name="address">
                  {({ field }) => (
xuzhenghua committed
132 133 134 135 136 137 138
                    <div className="address-form__item">
                      <input
                        {...field}
                        className="address-form__ipt"
                        type="text"
                        placeholder="收货地址"
                      />
zhanghaozhe committed
139
                      {errors.address && (
xuzhenghua committed
140
                        <p className="address-form__tip">{errors.address}</p>
zhanghaozhe committed
141
                      )}
xuzhenghua committed
142 143
                    </div>
                  )}
zhanghaozhe committed
144 145 146
                </Field>
                <button
                  className="address-form__submit"
xuzhenghua committed
147 148
                  data-status="do"
                  type="submit"
zhanghaozhe committed
149 150
                >
                  提交
zhanghaozhe committed
151
                </button>
xuzhenghua committed
152 153
              </Form>
            )}
zhanghaozhe committed
154
          </Formik>
zhanghaozhe committed
155
        )}
xuzhenghua committed
156
      </>
zhanghaozhe committed
157
    )
xuzhenghua committed
158 159 160
  }
}

zhanghaozhe committed
161
export default AddressPopup