EditProfile.js
20.2 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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
//
// ClassName.js
//
// Created by Cheney Mars on 2018/9/26.
// Copyright © 2018年 saygoodlolita. All rights reserved.
//
import React, {Component} from "react";
import {
DeviceEventEmitter,
FlatList,
Image,
InteractionManager,
NativeModules, Platform,
Text,
TouchableOpacity,
View
} from "react-native";
import CommentMore from "../detail/CommentMore";
import {zoomW} from "../../utils/getSize";
import {NoDoublePress, xnToast} from "../../utils/utils";
import AppService from "../../service/AppService";
import CommentInput from "../detail/CommentInput"
import ActionSheet from 'react-native-actionsheet-api';
import Picker from 'react-native-picker';
import moment from "moment";
const defaultIcon = require("../../img/defaultIcon.png");
const goImg = require("../../img/goDetail.png");
export default class EditProfile extends Component {
constructor(props) {
super(props);
this.state = {// userInfo
data:[{title: '头像', detail: ' '}, {title: '用户名', detail: ' '},{title: '介绍', detail: ' '},{title: '性别', detail: ' '},{title: '生日', detail: ' '},{title: '地址', detail: ' '}],
curPage:0,
yearList: [],
monthList: [],
dayList: [],
startYear: new Date().getFullYear(),
startMonth: new Date().getMonth() + 1,
startDay: new Date().getDate(),
isShowCommentInput:false, // 是否显示CommentInput输入框组件
minLength:0,//输入框最少输入多少字
}
}
// 更新数据
updateUserData(dic,curpage) {
AppService.updateUserInfo(dic)
.then(data => {
if (data.message) {
xnToast(data.message);
return;
}
if (data.errors.length > 0) {
xnToast(data.errors[0].message);
return;
}
xnToast("修改成功");
if(this.refs.CommentInput != undefined){
InteractionManager.runAfterInteractions(() => {
this.refs.CommentInput.reset();
});
}
this.getUserInfo();
DeviceEventEmitter.emit('refreshPageInfo');
DeviceEventEmitter.emit('refreshHeader');
})
.catch(error => {
this.setState({
loading: false
});
xnToast(error);
});
}
//写评论
inputClick (minLength,value){
if (Platform.OS == 'ios'){
this.setState({
isShowCommentInput:true
});
}
InteractionManager.runAfterInteractions(() => {
this.refs.CommentInput.setMinLength(minLength);
this.refs.CommentInput.setDefaultValue(value);
this.refs.CommentInput.setCustomPublish();
this.refs.CommentInput.showInputLayout();
});
};
showMoreModal(index) {
// 点击头像
Picker.hide();
if (index == 0) {
ActionSheet.showActionSheetWithOptions({
options: [ '从相册中选择', '拍照', '取消'],
cancelButtonIndex: 2,
//destructiveButtonIndex: 0,
tintColor: global.homeColor,
},
(buttonIndex) => {
if (buttonIndex == 0) {
this.toAlbum();
}
if (buttonIndex == 1) {
this.openCamera();
}
}
);
}
// 点击用户名
if (index == 1) {
this.setState({
curPage: index,
minLength:1,
},function () {
this.inputClick(1,this.state.data[1].detail);
// if(this.refs.CommentInput != undefined){
// InteractionManager.runAfterInteractions(() => {
// this.refs.CommentInput.setMinLength(1);
// });
// }
});
}
// 点击介绍
if (index == 2) {
this.setState({
curPage: index,
minLength:0,
},function () {
this.inputClick(0,this.state.data[2].detail);
// if(this.refs.CommentInput != undefined){
// InteractionManager.runAfterInteractions(() => {
// this.refs.CommentInput.setMinLength(0);
// });
// }
});
}
// 点击性别
if (index == 3) {
ActionSheet.showActionSheetWithOptions({
options: [ '男', '女', '取消'],
cancelButtonIndex: 2,
//destructiveButtonIndex: 0,
tintColor: global.homeColor,
},
(buttonIndex) => {
if (buttonIndex == 0) {
let dic = {gender:"男", id:this.props.navigation.state.params.userInfo.id};
this.updateUserData(dic);
}
if (buttonIndex == 1) {
let dic = {gender:"女", id:this.props.navigation.state.params.userInfo.id};
this.updateUserData(dic);
}
}
);
}
// 点击生日
if (index == 4) {
this.switchStartDate();
}
// 点击地址
if (index == 5) {
NativeModules.system.openMapView("","").then((data)=>{
let location = JSON.parse(data);
const addressNameD = `${"中国"}${location.province}${location.urban}${location.name}`;
let dic = {addressName:addressNameD, id:this.props.navigation.state.params.userInfo.id};
this.updateUserData(dic);
})
}
}
// 选择开始日期
switchStartDate() {
Picker.init({pickerBg: [255, 255, 255, 1],
pickerData: [this.state.yearList, this.state.monthList, this.state.dayList],
selectedValue: [this.state.startYear, this.state.startMonth, this.state.startDay],
pickerConfirmBtnText: '确认',
pickerCancelBtnText: '取消',
pickerTitleText: '选择日期',
onPickerConfirm: (data) => {
let year = data[0];
let month = data[1];
let day = data[2];
let intMonth = parseInt(month);
let intDay = parseInt(day);
if (month < 10) {
month = '0' + month;
}
if (day < 10) {
day = '0' + day;
}
const startTime = `${year}${month}${day}`;
if (Number(startTime)>moment(Number(new Date())).format('YYYYMMDD')) {
xnToast('生日不能选今天以后的日期');
return;
}
let dic = {birthday:startTime, id:this.props.navigation.state.params.userInfo.id};
this.updateUserData(dic);
},
onPickerSelect: (data) => {
},
});
Picker.show();
}
componentWillMount() {
this.getUserInfo();
const currentYear = new Date().getFullYear();
const year = [];
const month = [];
const day = [];
// 按照客户的要求,不再限制1970年之前,改为和微信小程序的日期原则一样从公元1年开始
for (let y = 1; y <= currentYear; y++) {
year.push(y);
if (y === currentYear) {
this.setState({
yearList: year,
});
}
}
for (let m = 0; m < 12; m++) {
month.push(m + 1);
if (m === 11) {
this.setState({
monthList: month,
});
}
}
for (let d = 0; d < 31; d++) {
day.push(d + 1);
if (d === 30) {
this.setState({
dayList: day,
});
}
}
}
// 获取个人信息
getUserInfo = () => {
if (global.isConnected) {
// alert("查询")
AppService.getEditInfo({
id: this.props.navigation.state.params.userInfo.id
}).then((data) => {
if (data.message) {
xnToast(data.message);
return;
}
if (data.errors == null || data.errors.length > 0) {
xnToast(data.errors[0].message);
} else {
let userInfo = data.userExtend;;
let dataList = [];
let iconData = {
title:"头像",
detail:userInfo.headFileUrl,
};
dataList.push(iconData);
let nameData = {
title:"用户名",
detail:userInfo.name
};
dataList.push(nameData);
let descriptionData = {
title:"介绍",
detail:userInfo.description
};
dataList.push(descriptionData);
let genderData = {
title:"性别",
detail:userInfo.gender
};
dataList.push(genderData);
let birthdayData = {
title:"生日",
detail: String(userInfo.birthday).length == 8 ? String(userInfo.birthday).substring(0,4)+'-'+String(userInfo.birthday).substring(4,6)+'-'+String(userInfo.birthday).substring(6,8) : ''
};
dataList.push(birthdayData);
let addressData = {
title:"地址",
detail:userInfo.addressName
};
dataList.push(addressData);
this.setState({
data: dataList
});
}
})
} else {
xnToast('请检查网络');
}
};
// 点击发布
toComment=(data)=> {
let curpage = this.state.curPage;
// 更新用户名stri
if (curpage == 1) {
let dic = {name:data.content, id:this.props.navigation.state.params.userInfo.id};
if(dic.name &&dic.name.length>10){
xnToast("用户名不能超过10个字符");
InteractionManager.runAfterInteractions(() => {
if(this.refs.CommentInput != undefined){
this.refs.CommentInput.focus();
}
});
return;
}
this.updateUserData(dic,curpage);
}
// 更新介绍
if (curpage == 2) {
let dic = {description:data.content, id:this.props.navigation.state.params.userInfo.id};
if(dic.description && curpage == 2 &&dic.description.length>30){
xnToast("介绍不能超过30个字符");
InteractionManager.runAfterInteractions(() => {
if (this.refs.CommentInput !=undefined){
this.refs.CommentInput.focus();
}
});
return;
}
this.updateUserData(dic,curpage);
}
}
componentDidMount() {
let _this = this;
// 关闭CommentInput输入框组件的通知
this.closeCommentInputIos = DeviceEventEmitter.addListener('closeCommentInputIos',function(){
console.log("closeCommentInputIos");
_this.setState({
isShowCommentInput:false,
})
});
}
componentWillUnmount() {
Picker.hide();
if (this.closeCommentInputIos != null) {
this.closeCommentInputIos.remove();
}
}
static navigationOptions = ({ navigation, screenProps }) => ({
headerTitle: "编辑资料",
headerLeft: (
<TouchableOpacity activeOpacity={1} style={{flexDirection:'row',height:'100%',width:'100%'}}
onPress={() => NoDoublePress.onPress(() => {
DeviceEventEmitter.emit("closeCommentInputIos");})}>
<View style={{ width: 375 / 2 / zoomW, height: '100%', flexDirection: 'row' }}>
<TouchableOpacity activeOpacity={1} style={{ height: '100%', justifyContent: 'center', paddingLeft: 15 / zoomW, paddingRight: 15 / zoomW }}
onPress={() => NoDoublePress.onPress(() => {
DeviceEventEmitter.emit("closeCommentInputIos");
navigation.goBack();})}>
<Image style={{ width: 18 / zoomW, height: 18 }} source={require('../../img/loadBack.png')} resizeMode="contain"/>
</TouchableOpacity>
</View>
</TouchableOpacity>
),
headerRight: (
<TouchableOpacity activeOpacity={1} style={{flexDirection:'row',height:'100%',width:'100%'}}
onPress={() => NoDoublePress.onPress(() => {
DeviceEventEmitter.emit("closeCommentInputIos");})}>
<View style={{ flexDirection: "row", flex: 1, justifyContent: "center" }}>
<View
style={{
flexDirection: "column",
justifyContent: "center",
paddingRight: 15,
paddingLeft: 10
}}
>
<Image
style={{ width: 20 / zoomW, height: 20 / zoomW }}
resizeMode="contain"
/>
</View>
</View>
</TouchableOpacity>
)
});
//从相册选择
toAlbum(){
let that = this;
setTimeout(() =>{
try {
//带裁剪的
NativeModules.system.choosePicWithCrop(true).then((data)=>{
that.handleCameraData(data);
});
}catch (ex){
//不带裁剪功能的
NativeModules.system.choosePic(1).then((data)=> {
that.handleCameraData(data);
});
}
},500);
}
//照片选择结果处理
handleCameraData(data){
if(data ==''){
//没有选择图片
return ;
}
let path = data;
let nameList =[];
for (let i=0;i<data.length;i++) {
let path = data[i].filePath;
nameList.push(global.userId+path.substring(path.lastIndexOf('/') + 1))
// let fileName = path.substring(path.lastIndexOf('/') + 1);
}
this.setState({
loading:true
});
AppService.getAccess({nameList: nameList}).then((res)=>{
if (res.message) {
xnToast(res.message);
this.setState({
loading: false
});
return;
}
if (res.errors.length > 0) {
xnToast(res.errors[0].message);
} else {
NativeModules.system.batchUpload(res.accessKeyId,res.accessKeySecret,res.securityToken,res.infoList,path).then((data)=>{
// 拿到相册图片
let dic = {headFileUrl:data[0], id:this.props.navigation.state.params.userInfo.id};
this.updateUserData(dic); })
}
}).catch((error) => {
this.setState({
loading: false
}, () => {
xnToast(error.message);
});
})
}
openCamera(){
try{
//带裁剪功能
NativeModules.system.takePhotoWithCropAndPassportId(global.passport.id).then((data) => {
let res = JSON.parse(data);
let dic = {headFileUrl:res.url, id:this.props.navigation.state.params.userInfo.id};
this.updateUserData(dic);
});
}catch (ex){
//不带裁剪功能
NativeModules.system.takePhotoWithpassportId(global.passport.id).then((data) => {
let res = JSON.parse(data);
let dic = {headFileUrl:res.url, id:this.props.navigation.state.params.userInfo.id};
this.updateUserData(dic);
});
}
}
render() {
return (
<View style={{flex:1,backgroundColor:"#F6F6F6",display: "flex",alignItems: "center",justifyContent:"flex-end"}}>
<CommentMore
ref='CommentMore'
detailInfo = 'ss'
collectClick = {()=>{}}
deleteClick = {()=>{this.props.navigation.goBack();}}
forum={global.forum}
/>
<FlatList style={{backgroundColor:"#F6F6F6", marginTop:10,width:"100%"}}
data={this.state.data}
renderItem={({item,index}) =>
<TouchableOpacity
onPress = {()=>{this.showMoreModal(index)}}
>
<View>
<View style= {{backgroundColor:"white", flexDirection:"row",alignItems:"center",justifyContent:"space-between"}}>
<Text style={{fontSize:16, marginLeft:15}}>{item.title}</Text>
<View style= {{flexDirection:"row", marginRight:15,alignItems:"center"}}>
{
index == 0 ? <Image style= {{borderRadius:17,height:34,width:34,marginTop:12,marginBottom:12,marginRight:10}} source={{ uri: item.detail+'?x-oss-process=image/resize,w_300'}}/>
: <Text style= {{marginTop:12,marginBottom:12,marginRight:10, fontSize:14, color:"#999999",width:214,textAlign:'right'}}>{item.detail}</Text>
}
<Image source={goImg}/>
</View>
</View>
<View style={{height:0.5,backgroundColor:"#EEEEEE",marginLeft:16, marginRight:14}}>
</View>
{index==2 ? <View><Text></Text></View> : null}
</View>
</TouchableOpacity>
}
/>
{Platform.OS == 'ios' && this.state.isShowCommentInput && <CommentInput ref = "CommentInput"
callback = { (data)=>{this.toComment(data)}}
nav = {this.props.navigation}
isEdit={true}
hasNavHeight = {true}
/>}
{Platform.OS == 'android' &&<CommentInput ref = "CommentInput"
callback = { (data)=>{this.toComment(data)}}
nav = {this.props.navigation}
isEdit={true}
hasNavHeight = {true}
/>}
</View>
);
}
}