/** * Created by zack on 2018/4/20. */ import { View, Text, StyleSheet, Image, TouchableOpacity, FlatList, Modal, Animated, TextInput, Platform, Keyboard, Picker, DatePickerAndroid, DatePickerIOS, } from 'react-native' import React, {Component} from 'react' import {NavigationBarHeight, TabBarHeight, ScreenDimensions} from '../../utils/DimensionsTools' import HttpTools from '../../network/HttpTools' import {auth, getUserProfile, userProfile, userEmailCode, userSmsCode, userEmail, userMobile} from '../../network/API' import MineHeaderImageItem from './View/MineHeaderImageItem' import MineTextItem from './View/MineTextItem' import {GlobalUserStorageTool, UserStorageKey} from '../../utils/GlobalUserStorageTool' import ChinaRegionWheelPicker from 'rn-wheel-picker-china-region' import ToastMsg from '../../utils/ToastMsg' export default class MineViewController extends Component { constructor(props) { super(props) this.state = { avatar: props.UserInfo.avatar, nickName: props.UserInfo.nickname, gender: props.UserInfo.gender, birthday: props.UserInfo.birthday, introduction: '', address: '', email: '', phone: '', oldPhone: '', phoneCode: '', verificationCode: '', password: '', newPassword: '', isNickNameModalVisible: false, isIntroductionModalVisible: false, isUpdatePhoneModalVisible: false, isUpdateEmailModalVisible: false, isUpdatePasswordModalVisible: false, isDatePickerModalVisible: false, isGenderModalVisible: false, isPickerVisible: false } this.nickNameModalMarginTop = new Animated.Value((ScreenDimensions.height - 137)) this.introductionModalMarginTop = new Animated.Value((ScreenDimensions.height - 175)) this.updatePhoneModalMarginTop = new Animated.Value((ScreenDimensions.height - 166)) this.updateEmailModalMarginTop = new Animated.Value((ScreenDimensions.height - 166)) this.updatePasswordModalMarginTop = new Animated.Value((ScreenDimensions.height - 228)) this.oldGender = '0' this.oldBirthday = '' } componentDidMount() { GlobalUserStorageTool.load(UserStorageKey.UserInfo, (ret) => { if (ret) { global.token = ret.token this.getUserProfile() } }) // GlobalUserStorageTool.load(UserStorageKey.AccountAndPassword, (ret) => { // if (ret) { // this.setState({password: ret.password}) // } // }) if (Platform.OS === 'ios') {//keyboardDidShow this.keyboardWillShowSub = Keyboard.addListener('keyboardWillShow', this.keyboardWillShow.bind(this)); this.keyboardWillHideSub = Keyboard.addListener('keyboardWillHide', this.keyboardWillHide.bind(this)); }else { this.keyboardWillShowSub = Keyboard.addListener('keyboardDidShow', this.keyboardWillShow.bind(this)); this.keyboardWillHideSub = Keyboard.addListener('keyboardDidHide', this.keyboardWillHide.bind(this)); } } componentWillUnMount() { if (this.keyboardWillShowSub && this.keyboardWillHideSub) { this.keyboardWillShowSub.remove(); this.keyboardWillHideSub.remove(); } } getUserProfile() { const param = { } HttpTools.get(getUserProfile, param, (response) => { if (response) { this.setState({ avatar: response.avatar, birthday: response.birthday, //email: response.email, gender: response.gender, introduction: response.introduction && response.introduction.length > 0 ? response.introduction : this.state.introduction, nickName: response.nickname, oldPhone: response.phone, phoneCode: response.phone_code, address: response.reside }) this.oldGender = response.gender } }, (error) => { console.log(error) }) } getVerificationCode(account) { this.setState({verificationCode: ''}) let param let url = '' if (account.indexOf('@') !== -1) { url = userEmailCode param = JSON.stringify({ email: account, purpose: "change_email" }) }else { url = userSmsCode param = JSON.stringify({ phone: account, phone_code: this.state.phoneCode, purpose: "change_phone" }) } HttpTools.post(url, param, (response) => { ToastMsg.show('验证码已发送到你的' + (url === userSmsCode ? '手机' : '邮箱')) }, (error) => { ToastMsg.show('获取验证码失败') }) } updatePhone(newPhone, verificationCode) { const param = JSON.stringify({ code: verificationCode, phone: newPhone, phone_code: this.state.phoneCode }) HttpTools.put(userMobile, param, (response) => { Keyboard.dismiss() ToastMsg.show('更改手机号成功') }, (error) => { ToastMsg.show('更改手机号失败') }) } updateEmail(newEmail, verificationCode) { const param = JSON.stringify({ code: verificationCode, email: newEmail }) HttpTools.put(userEmail, param, (response) => { Keyboard.dismiss() ToastMsg.show('更改邮箱成功') }, (error) => { ToastMsg.show('更改邮箱失败') }) } updateProfile(key, value) { let param = null if (key === 'nickname') { param = JSON.stringify({ nickname: value }) }else if (key === 'gender') { param = JSON.stringify({ gender: value, }) }else if (key === 'birthday') { param = JSON.stringify({ birthday: value, }) }else if (key === 'introduction') { param = JSON.stringify({ introduction: value, }) }else if (key === 'reside') { param = JSON.stringify({ reside: value, }) } HttpTools.put(userProfile, param, (response)=> { console.log(response) }, (error) => { console.log(error) }) } showAndroidDatePicker = async () => { try { const {action, year, month, day} = await DatePickerAndroid.open({ date: (new Date()) }); if (action !== DatePickerAndroid.dismissedAction) { let date = new Date(year, month, day); let year = date.getFullYear() let month = date.getMonth() + 1 let day = date.getDate() if (month <= 9) { month = '0' + month } if (day <= 9) { day = '0' + day } let birthday = year + '-' + month + '-' + day this.setState({birthday: birthday}) this.updateProfile('birthday', birthday) } } catch ({code, message}) { console.warn('Cannot open date picker', message); } }; didSelectedItem(key) { if (key === 0) { this.props.navigator.push({ screen: 'UserCenterViewController', title: '', backButtonTitle: '', navigatorStyle: { navBarHidden: true }, passProps: { } }) } else if (key === 1) { this.setState({isNickNameModalVisible: true}) }else if (key === 2) { this.setState({isGenderModalVisible: true}) } else if (key === 3) { if (Platform.OS === 'ios') { this.setState({isDatePickerModalVisible: true}) }else { let date = this.showAndroidDatePicker() } } else if (key === 4) { this.setState({isIntroductionModalVisible: true}) }else if (key === 5) { this.setState({isPickerVisible: true}) }else if (key === 7) { this.setState({isUpdatePasswordModalVisible: true}) } else if (key === 8) { this.setState({isUpdatePhoneModalVisible: true}) }else if (key === 9) { this.setState({isUpdateEmailModalVisible: true}) } } keyboardWillShow(event) { if (this.state.isNickNameModalVisible) { Animated.timing(this.nickNameModalMarginTop,{duration: event.duration,toValue: (ScreenDimensions.height - 137 - event.endCoordinates.height)}).start() } else if (this.state.isIntroductionModalVisible) { Animated.timing(this.introductionModalMarginTop,{duration: event.duration,toValue: (ScreenDimensions.height - 175 - event.endCoordinates.height)}).start() } else if (this.state.isUpdatePhoneModalVisible) { Animated.timing(this.updatePhoneModalMarginTop,{duration: event.duration,toValue: (ScreenDimensions.height - 166 - event.endCoordinates.height)}).start() } else if (this.state.isUpdateEmailModalVisible) { Animated.timing(this.updateEmailModalMarginTop,{duration: event.duration,toValue: (ScreenDimensions.height - 166 - event.endCoordinates.height)}).start() }else if (this.state.isUpdatePasswordModalVisible) { Animated.timing(this.updatePasswordModalMarginTop,{duration: event.duration,toValue: (ScreenDimensions.height - 228 - event.endCoordinates.height)}).start() } } keyboardWillHide(event) { if (this.state.isNickNameModalVisible) { Animated.timing(this.nickNameModalMarginTop,{duration: Platform.OS === 'ios' ? event.duration : 300,toValue: (ScreenDimensions.height - 137)}).start(() => { this.setState({isNickNameModalVisible: false}) }) }else if (this.state.isIntroductionModalVisible){ Animated.timing(this.nickNameModalMarginTop,{duration: Platform.OS === 'ios' ? event.duration : 300,toValue: (ScreenDimensions.height - 175)}).start(() => { this.setState({isIntroductionModalVisible: false}) }) } else if (this.state.isUpdatePhoneModalVisible) { Animated.timing(this.updatePhoneModalMarginTop,{duration: Platform.OS === 'ios' ? event.duration : 300,toValue: (ScreenDimensions.height - 166)}).start(() => { this.setState({isUpdatePhoneModalVisible: false}) }) } else if (this.state.isUpdateEmailModalVisible) { Animated.timing(this.updateEmailModalMarginTop,{duration: Platform.OS === 'ios' ? event.duration : 300,toValue: (ScreenDimensions.height - 166)}).start(() => { this.setState({isUpdateEmailModalVisible: false}) }) }else if (this.state.isUpdatePasswordModalVisible) { Animated.timing(this.updatePasswordModalMarginTop,{duration: Platform.OS === 'ios' ? event.duration : 300,toValue: (ScreenDimensions.height - 228)}).start(() => { this.setState({isUpdatePasswordModalVisible: false}) }) } } renderDatePicker() { if (Platform.OS === 'ios') { return( { let year = date.getFullYear() let month = date.getMonth() + 1 let day = date.getDate() if (month <= 9) { month = '0' + month } if (day <= 9) { day = '0' + day } let birthday = year + '-' + month + '-' + day this.setState({birthday: birthday}) }} mode={'date'} /> ) } } renderItem(item) { const key = item.key let desc = '' if (key === 0) { desc = this.state.avatar return( { this.didSelectedItem(0) }} avatar = {this.state.avatar} /> ) }else if (key === 1) { desc = this.state.nickName }else if (key === 2) { desc = this.state.gender }else if (key === 3) { desc = this.state.birthday }else if (key === 4) { desc = this.state.introduction }else if (key === 5) { desc = this.state.address }else if (key === 6) { return( ) }else{ desc = '******' } return( { this.didSelectedItem(key) }} /> ) } render() { return ( this.renderItem(item)} keyExtractor = {(item,index) =>{ return 'key' + item.key + index }} ListFooterComponent = {() => { return( ) }} /> {}} onShow={() => { }} > { this.setState({isNickNameModalVisible: false}) }} activeOpacity= {1} style={{width: ScreenDimensions.width, height: ScreenDimensions.height, backgroundColor: 'rgba(0,0,0,0.15)',}} > { Keyboard.dismiss() }}> {'取消'} {'修改昵称'} { this.setState({isNickNameModalVisible: false}) this.updateProfile('nickname', this.state.nickName) }}> {'确定'} { this.setState({nickName: text}) }} keyboardType={'numeric'} defaultValue={this.state.phone} style={styles.TextInput}/> {}} onShow={() => { }} > { Keyboard.dismiss() }}> {'取消'} {'个人简介'} { this.setState({isIntroductionModalVisible: false}) this.updateProfile('introduction', this.state.introduction) }}> {'确定'} { this.setState({introduction: text}) }} defaultValue={this.state.introduction} style={styles.introductionTextInput}/> {'30'} {}} onShow={() => { }} > { Keyboard.dismiss() }}> {'取消'} {'修改手机'} { this.updatePhone(this.state.phone, this.state.verificationCode) }}> {'确定'} { this.setState({phone: text}) }} style={styles.UpdateTextInput}/> { this.setState({verificationCode: text}) }} keyboardType={'phone-pad'} placeholder={'请输入验证码'} placeholderTextColor={'#b8bcbf'} style={styles.VerifyTextInput}/> { if (!this.state.phone && !this.state.phone.length) { ToastMsg.show('请输入您的新手机号') return } if (this.state.oldPhone === this.state.phone) { ToastMsg.show('不需要更新相同的手机号') return } this.getVerificationCode(this.state.phone) }} style={styles.VerifyButtonBgView}> {'获取验证码'} {}} onShow={() => { }} > { Keyboard.dismiss() }}> {'取消'} {'修改邮箱'} { this.updateEmail(this.state.email, this.state.verificationCode) }}> {'确定'} { this.setState({email: text}) }} style={styles.UpdateTextInput}/> { this.setState({verificationCode: text}) }} keyboardType={'phone-pad'} placeholder={'请输入验证码'} placeholderTextColor={'#b8bcbf'} style={styles.VerifyTextInput}/> { if (!this.state.email || !this.state.email.length) { //还要正则判断邮箱格式 ToastMsg.show('请输入您的邮箱') return } this.getVerificationCode(this.state.email) }} style={styles.VerifyButtonBgView}> {'获取验证码'} {}} onShow={() => { }} > { Keyboard.dismiss() }}> {'取消'} {'修改密码'} { this.setState({isUpdatePasswordModalVisible: false}) }}> {'确定'} { this.setState({password: text}) }} style={styles.UpdatePasswordTextInput}/> { this.setState({verificationCode: text}) }} keyboardType={'phone-pad'} placeholder={'请输入验证码'} placeholderTextColor={'#b8bcbf'} style={styles.VerifyTextInput}/> { this.getVerificationCode() }} style={styles.VerifyButtonBgView}> {'获取验证码'} { this.setState({newPassword: text}) }} style={styles.UpdatePasswordTextInput}/> {}} onShow={() => { }} > { this.setState({isDatePickerModalVisible: false, birthday: this.oldBirthday}) }}> {'取消'} {'修改生日'} { this.setState({isDatePickerModalVisible: false}) this.oldBirthday = this.state.birthday this.updateProfile('birthday', this.state.birthday) }}> {'确定'} {this.renderDatePicker()} {}} onShow={() => { }} > { this.setState({isGenderModalVisible: false}) this.setState({gender: this.oldGender}) }}> {'取消'} {'修改性别'} { this.setState({isGenderModalVisible: false}) this.oldGender = this.state.gender this.updateProfile('gender', parseInt(this.state.gender)) }}> {'确定'} this.setState({gender: itemValue})}> { const region = param.province + '-' + param.city + '-' + param.area this.setState({address: region}) this.updateProfile('reside', region) }} onCancel={() => { this.setState({isPickerVisible: false}) }} // 点击取消_onPressCancel androidPickerHeight={100} // 安卓手机下可以设置picker区域的高度 /> ); } } const styles = StyleSheet.create({ View: { width: ScreenDimensions.width, height: ScreenDimensions.height, justifyContent: 'center', alignItems: 'center', backgroundColor: 'white' }, ListView: { marginTop: NavigationBarHeight.height, width: ScreenDimensions.width, height: ScreenDimensions.height - NavigationBarHeight.height, backgroundColor: 'white' }, NickNameModalBgView: { width: ScreenDimensions.width, height: 137, backgroundColor: 'white' }, ModalHeaderView: { width: ScreenDimensions.width, height: 44, flexDirection: 'row', alignItems: 'center', justifyContent: 'space-between' }, CancelText: { fontSize: 15, marginLeft: 15, color: '#e66a6a' }, ConfirmText: { fontSize: 15, marginRight: 15, color: '#e66a6a' }, TitleText: { fontSize: 16, color: '#333333' }, TextInput: { width: ScreenDimensions.width - 60, height: 44, fontSize: 14, color: '#b8bcbf', textAlign: 'left', marginLeft: 30, marginTop: 10 }, introductionBgModalView: { width: ScreenDimensions.width, height: 175, backgroundColor: 'white', }, introductionTextInput: { width: ScreenDimensions.width - 60, height: 130, fontSize: 14, color: '#b8bcbf', textAlign: 'left', marginLeft: 30, paddingTop: 15, paddingBottom: 15, textAlignVertical: 'top' }, NumberText: { fontSize: 14, color: '#b8bcbf', position: 'absolute', bottom: 5, right: 5, }, UpdatePhoneModalBgView: { width: ScreenDimensions.width, height: 175, backgroundColor: 'white', }, UpdateTextInput: { width: ScreenDimensions.width - 60, height: 55, fontSize: 14, color: '#b8bcbf', textAlign: 'left', marginLeft: 30, }, UpdatePasswordModalBgView: { width: ScreenDimensions.width, height: 228, backgroundColor: 'white', }, UpdatePasswordTextInput: { width: ScreenDimensions.width - 60, height: 60, fontSize: 14, color: '#b8bcbf', textAlign: 'left', marginLeft: 30, }, VerifyTextInputBgView: { width: ScreenDimensions.width - 60, height: 50, marginLeft: 30, }, VerifyTextInputContainerView: { width: ScreenDimensions.width - 60, height: 60, flexDirection: 'row', alignItems: 'center', justifyContent: 'space-between' }, VerifyTextInput: { width: ScreenDimensions.width - 60 - 100, textAlign: 'left', fontSize: 14, color: '#4A4A4A', height:60, }, VerifyButtonBgView: { width: 100, flexDirection: 'row', alignItems: 'center', justifyContent: 'space-between', }, VerifyButtonText: { fontSize: 14, color: '#5a9cd9', }, VerifyLineView: { width: 1, height: 21, backgroundColor: 'rgba(80, 80, 80, 0.36)' }, GenderModalBgView: { width: ScreenDimensions.width, height: 208, backgroundColor: 'white' }, GenderItemBgView: { width: ScreenDimensions.width, height: 50, backgroundColor: 'white', justifyContent: 'center', alignItems: 'center' }, })