changeUserInfo.js
5.84 KB
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
80
81
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
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
/**
* 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)
);
}