index.js 4.9 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
import React, { Component } from 'react';
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 = {}) => {
FE committed
43 44 45 46 47
    const { successBindAddress } = this.props;
    http.post(`${API.home}/sys/update_address`, {
      act_type: 'treasure',
      ...params
    }).then(res => {
FE committed
48 49
      const {code, msg} = res.data;
      if (code === 200) {
FE committed
50
        successBindAddress();
FE committed
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
      } else {
        Toast.info(msg, 2, null, false);
      }
    });
  }

  render() {
    const { isLoading, addressInfo } = this.state;
    const {tip, prize, skip = 'default'} = this.props;
    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;
            }}
            validateOnBlur={false}
            validateOnChange={false}
            onSubmit={(values) => {
              this.handleToSubmit(values);
            }}
            render={({errors}) => (
              <Form className="address-form" data-skip={skip}>
FE committed
90
                <h2 className="address-form__title">收货信息</h2>
FE committed
91 92
                {
                  prize ? (
FE committed
93 94 95 96
                    <p className='address__prize'>
                      您抽中了
                      <span style={{'color': '#FF4000'}}>{prize}</span>
                    </p>
FE committed
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 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
                  ) : (null)
                }
                {
                  tip ? (<div className="address-form__subtitle">{tip}</div>) : (<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" 
                  data-status="do"
                  type="submit"
                >提交</button>
              </Form>
            )}
          />
        }
      </>
    );
  }
}

export default AddressPopup;