index.jsx 3.4 KB
Newer Older
徐立's avatar
徐立 committed
1 2 3
/**
 * 签到功能
 */
4 5
import React, { Component } from 'react';
import Location from './components';
徐立's avatar
徐立 committed
6 7 8
import { isEmpty } from 'lodash';
import { openToast } from './Notification';
import { successToast, failToast } from './Toast';
9

徐立's avatar
徐立 committed
10
export default class location extends Component {
钟是志's avatar
钟是志 committed
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
  constructor(props) {
    super(props);
    this.state = {
      locationMsg: {},
      btn: true,
      style: { display: 'none' },
    };
  }
  // 该方法在首次渲染之前调用(数据初始化)
  componentWillMount() {}
  //已经生成对应的dom结构
  componentDidMount = () => {};
  //在组件从 DOM 中移除的时候立刻被调用。
  componentWillUnmount = () => {};
  getLocationMsg = locationMsg => {
    //如果不为空的话显示提示
    this.setState({ loading: true }, () => {
      if (Object.keys(locationMsg).length !== 0) {
钟是志's avatar
钟是志 committed
29
        this.props.onChange(locationMsg);
钟是志's avatar
钟是志 committed
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 80 81 82 83 84 85 86 87
        this.setState({
          locationMsg: locationMsg,
          loading: false,
          style: { display: 'block' },
          btn: false,
        });
      } else {
        this.setState({
          loading: false,
        });
      }
    });
  };
  submitLocation = () => {
    const { showLocation } = this.props;
    if (!showLocation) return;
    if (isEmpty(this.state.locationMsg) && window.lat && window.lng) {
      let obj = {
        lat: window.lat,
        lng: window.lng,
      };
      this.props.onChange(obj);
      if (this.props.get === 'web') {
        openToast('success', '成功', '已添加定位地址');
      } else {
        successToast('已添加定位地址');
      }
    } else if (!isEmpty(this.state.locationMsg)) {
      this.props.onChange(this.state.locationMsg);
      if (this.props.get === 'web') {
        openToast('success', '成功', '已添加定位地址');
      } else {
        successToast('已添加定位地址');
      }
    } else {
      if (this.props.get === 'web') {
        openToast('error', '错误', '定位失败,请稍后重试');
      } else {
        successToast('定位失败,请稍后重试');
      }
    }
  };
  // 点击确定提交定位
  confirm = e => {
    console.log(e);
    if (this.state.btn) {
      if (this.props.get === 'web') {
        openToast('error', '错误', '正在定位,请稍后重试');
      } else {
        failToast('正在定位,请稍后重试');
      }
      return;
    }
    this.submitLocation();
  };
  render() {
    let { locationMsg, btn } = this.state;
    let { lat, lng, address, params, json, showLocation } = this.props;
88

钟是志's avatar
钟是志 committed
89 90 91 92 93 94 95 96 97 98
    return (
      <div style={{ display: 'flex' }}>
        <Location
          lat={lat}
          json={json}
          lng={lng}
          getLocationMsg={this.getLocationMsg.bind(this)}
          showMap={this.props.showMap}
          params={params}
        />
钟是志's avatar
钟是志 committed
99 100 101
        <div style={{ display: 'block'}}>
          {locationMsg?.address || ''}
         {/* <Popconfirm
钟是志's avatar
钟是志 committed
102 103 104 105 106 107 108 109 110 111 112 113 114 115
            title={btn ? '正在定位中' : locationMsg.address}
            onConfirm={this.confirm}
            okText="提交"
            cancelText="取消"
          >
            <Button
              disabled={btn}
              style={{ margin: 'auto', width: this.props.width }}
              loading={this.state.loading}
              className={styles.btn}
              type="primary"
            >
              {this.props.btnName}
            </Button>
钟是志's avatar
钟是志 committed
116
          </Popconfirm>*/}
钟是志's avatar
钟是志 committed
117 118 119 120
        </div>
      </div>
    );
  }
徐立's avatar
徐立 committed
121
}