index.js 4.37 KB
Newer Older
FE committed
1 2 3 4 5 6 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 38 39 40 41 42 43 44 45 46 47 48 49 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
import React, { Component } from 'react';
import { isEmpty } from 'lodash';
import { http } from '@/utils';
import { Formik, Form, Field } from 'formik';
import { Toast } from "antd-mobile";
import './index.scss';

class AddressPopup extends Component {
  constructor(props) {
    super(props)
    this.state = {
      isLoading: false,
      addressInfo: {
        name: '',
        phone: '',
        address: '',
      },
    }
  }

  componentDidMount() {
    this.fetchUserAddress();
  }

  // 获取收货信息
  fetchUserAddress = () => {
    const { addressInfo } = this.state;
    http.get(`${API.home}/sys/user_address_info`).then(res => {
      const {code, data, msg} = res.data;
      if (code === 200) {
        this.setState({
          addressInfo: Object.assign({}, addressInfo, {
            name: data.name,
            phone: data.phone,
            address: data.address,
          }),
          isLoading: true,
        });
      }
    });
  }

  handleToSubmit = (params = {}) => {
    const { handleToHide } = this.props;
    http.post(`${API.home}/sys/collect_info`, params).then(res => {
      const {code, msg} = res.data;
      if (code === 200) {
        handleToHide();
      } else {
        Toast.info(msg, 2, null, false);
      }
    });
  }

  render() {
    const { isLoading, addressInfo } = this.state;
    return (
      <>
        {
          isLoading &&
          <Formik
            initialValues={{ 
              ...addressInfo
            }}
            validate={({name, phone, address}) => {
              const errors = {};

              if (!name) {
                errors.name = '请输入收件人';
              }
              if(!/^1[3-9]\d{9}$/.test(phone)) {
                errors.phone = '请填写正确格式的手机号';
              }
              if (!address) {
                errors.address = '请输入收货地址';
              }

              return errors;
            }}
FE committed
80 81
            validateOnBlur={false}
            validateOnChange={false}
FE committed
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 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
            onSubmit={(values) => {
              this.handleToSubmit(values);
            }}
            render={({errors}) => (
              <Form className="address-form">
                <p className="address-form__desc">请及时填写收货信息,获得实物奖品后将第一时间为您邮寄</p>
                <Field 
                  name="name"
                  render={({ field }) => (
                    <div className="address-form__item">
                      <input
                        {...field}
                        className="address-form__ipt"
                        type="text"
                        placeholder="收件人"
                      />
                      {
                        errors.name &&
                        <p className="address-form__tip">{errors.name}</p>
                      }
                    </div>
                  )}
                />
                <Field 
                  name="phone"
                  render={({ field }) => (
                    <div className="address-form__item">
                      <input
                        {...field}
                        className="address-form__ipt"
                        type="text"
                        placeholder="联系方式"
                      />
                      {
                        errors.phone &&
                        <p className="address-form__tip">{errors.phone}</p>
                      }
                    </div>
                  )}
                />
                <Field 
                  name="address"
                  render={({ field }) => (
                    <div className="address-form__item">
                      <input
                        {...field}
                        className="address-form__ipt"
                        type="text"
                        placeholder="收货地址"
                      />
                      {
                        errors.address &&
                        <p className="address-form__tip">{errors.address}</p>
                      }
                    </div>
                  )}
                />
                <button 
                  className="address-form__submit" 
FE committed
141
                  data-status="do"
FE committed
142 143 144 145 146 147 148 149 150 151 152 153
                  type="submit"
                >提交</button>
              </Form>
            )}
          />
        }
      </>
    );
  }
}

export default AddressPopup;