changeUserInfo.js 5.84 KB
/**
 * Created by Cassie on 2018/03/21
 */
import React, { Component } from 'react';
import {
    StyleSheet,
    View,
    Text,
    TouchableOpacity,
    Image,
    TextInput,
    Animated,
    ActivityIndicator,
    NativeModules,
    NativeEventEmitter,
    DeviceEventEmitter,
    Alert,
    Modal,
    Platform,
    AsyncStorage,
    StatusBar,
    Dimensions,
    Button,
} from 'react-native';
import moment from 'moment';
import { zoomW, zoomH } from '../../utils/getSize';
import xnService from '../../service/AppService';

import { clock, xnToast, timeReduce, timePlus, timeEvery, getDay } from '../../utils/utils';

const StatusHeight = Platform.OS == 'ios' ? isIphoneX() ? 44 + 10 / zoomH : 20 + 10 / zoomH : StatusBar.currentHeight;

export default class changeUserInfo extends Component {
  static navigationOptions = ({ navigation }) => ({
    headerTitle: navigation.state.params.title,
    headerLeft: (
      <TouchableOpacity style={styles.backWrap} onPress={() => navigation.goBack()}>
        <Image source={require('../../img/back_gray.png')} resizeMode="contain" />
      </TouchableOpacity>
        ),
    headerRight: (
      <TouchableOpacity style={styles.renderRight} onPress={navigation.state.params ? navigation.state.params.save : () => console.log('onPress')}>
        <Text style={{ color: '#4B85E0', fontSize: 14 }}>保存</Text>
      </TouchableOpacity>
        ),
  });

  constructor(props) {
    super(props);
    this.state = {
      loading: false,
      text:String(this.props.navigation.state.params.defultValue)
    };
  }

  componentWillMount() {

  }

  componentDidMount() {
    this.props.navigation.setParams({
      save: this.save,
    });
  }

  save=() => {
    if (this.state.text.trim() == this.props.navigation.state.params.defultValue) {
      xnToast('您并未做出修改');
      return;
    }
    let params;
    if (this.props.navigation.state.params.type == 'nickName') {
        if(this.state.text.trim().length === 0) {
            xnToast('昵称不能为空');
            return;
        }
        if(this.state.text.trim().length > 22) {
            xnToast('昵称长度不能超过22位');
            return;
        }
      params = {
        id: global.user.id,
        nickName: this.state.text.trim(),
        age: Number(global.user.age),
      };
      global.user.nickName = this.state.text.trim();
    } else if (this.props.navigation.state.params.type == 'age') {
        if(this.state.text.trim().length === 0) {
            xnToast('年龄不能为空');
            return;
        }
        if (!(/^\d+$/.test(this.state.text.trim()))) {
            xnToast('年龄必须为非负整数');
            return;
        }
        if(this.state.text.trim().length > 3) {
            xnToast('年龄长度不能超过3位');
            return;
        }
      params = {
        id: global.user.id,
        age: Number(this.state.text.trim()),
      };
      global.user.age = Number(this.state.text.trim());
    } else if (this.props.navigation.state.params.type == 'employeeNumber') {
      params = {
        id: global.user.id,
        age: Number(this.state.text.trim()),
      };
      global.user.employeeNumber = Number(this.state.text.trim());
    }

    this.setState({
        loading: true,
    });
    xnService.undateUserInfo(params).then((data) => {
      this.setState({
        loading: false,
      });
      if (data.message) {
        xnToast(data.message);
        if (this.props.navigation.state.params.type == 'nickName') {
            global.user.nickName = this.props.navigation.state.params.defultValue;
        } else if (this.props.navigation.state.params.type == 'age') {
            global.user.age = this.props.navigation.state.params.defultValue;
        }
        return;
      }
      if (data.errors.length > 0) {
        xnToast(data.errors[0].message);
        if (this.props.navigation.state.params.type == 'nickName') {
          global.user.nickName = this.props.navigation.state.params.defultValue;
        } else if (this.props.navigation.state.params.type == 'age') {
          global.user.age = this.props.navigation.state.params.defultValue;
        }
      } else if (this.props.navigation.state.params.callback) {
        this.props.navigation.state.params.callback();
        this.props.navigation.goBack();
      }
    });
  }


  render() {
    return (
      <View style={styles.background}>
        <View style={styles.itemLine}>
            <TextInput
                style={{fontSize:14,color:'#404040',flex:1}}
                underlineColorAndroid="transparent"
                returnKeyType="done"
                value={this.state.text}
                defaultValue={this.state.text}
                onChangeText={(text) => { this.setState({ text: text }); }}
                keyboardType={this.props.navigation.state.params.type == 'age'?(Platform.OS === 'ios' ? 'decimal-pad' : 'phone-pad'):'default'}
            />
        </View>
        {this.state.loading && <View style={styles.loadingBg}>
          <ActivityIndicator size="large" />
        </View>}
      </View>
    );
  }
}

const styles = StyleSheet.create({
  backWrap: {
    justifyContent: 'center',
    paddingLeft: 18.5 / zoomW,
    paddingRight: 18.5 / zoomW,
    height: 44 / zoomH,
  },
  renderRight: {
    height: '100%',
    justifyContent: 'center',
    paddingLeft: 15 / zoomW,
    paddingRight: 15 / zoomW,
  },
  background: {
    flex: 1,
    backgroundColor: '#f6f6f6',
    display: 'flex',
    alignItems: 'center',
  },
  itemLine: {
    height: 44 / zoomH,
    width: '100%',
    backgroundColor: 'white',
    paddingLeft: 15 / zoomW,
    paddingRight: 15 / zoomW,
    marginTop: 12 / zoomH,
  },


});

// 是否 isIphoneX
export function isIphoneX() {
  const dimen = Dimensions.get('window');
  return (
        Platform.OS === 'ios' &&
        !Platform.isPad &&
        !Platform.isTVOS &&
        (dimen.height === 812 || dimen.width === 812)
  );
}