PersonInfoScreen.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. import React, {Component} from 'react';
  2. import Toast from '@remobile/react-native-toast';
  3. import CommonTitleBar from '../views/CommonTitleBar';
  4. import ListItemDivider from '../views/ListItemDivider';
  5. import ImagePicker from 'react-native-image-crop-picker';
  6. import StorageUtil from '../utils/StorageUtil';
  7. import CountEmitter from '../event/CountEmitter';
  8. import LoadingView from '../views/LoadingView';
  9. import ReplyPopWin from '../views/ReplyPopWin';
  10. import Global from '../utils/Global';
  11. import Utils from '../utils/Utils';
  12. import {Dimensions, Image, StyleSheet, Text, TouchableHighlight, View} from 'react-native';
  13. const {width} = Dimensions.get('window');
  14. export default class PersonInfoScreen extends Component {
  15. constructor(props) {
  16. super(props);
  17. this.state = {
  18. showProgress: false,
  19. };
  20. StorageUtil.get('username', (error, object) => {
  21. if (!error && object != null) {
  22. let username = object.username;
  23. this.setState({username: username});
  24. }
  25. });
  26. let userInfo = this.props.navigation.state.params.userInfo;
  27. this.userInfo = userInfo;
  28. }
  29. componentWillMount() {
  30. this.setState({nick: this.userInfo.nick, avatar: this.userInfo.avatar});
  31. }
  32. render() {
  33. return (
  34. <View style={styles.container}>
  35. <CommonTitleBar nav={this.props.navigation} title={"个人信息"}/>
  36. {
  37. this.state.showProgress ? (
  38. <LoadingView cancel={() => this.setState({showProgress: false})}/>
  39. ) : (null)
  40. }
  41. <View style={styles.list}>
  42. <TouchableHighlight underlayColor={Global.touchableHighlightColor} onPress={() => {
  43. this.modifyAvatar()
  44. }}>
  45. <View style={styles.listItem}>
  46. <Text style={styles.listItemLeftText}>头像</Text>
  47. <View style={styles.rightContainer}>
  48. <Image style={[styles.listItemRight, styles.avatarImg]}
  49. source={Utils.isEmpty(this.state.avatar) ? require('../../images/avatar.png') : {uri: this.state.avatar}}/>
  50. </View>
  51. </View>
  52. </TouchableHighlight>
  53. <ListItemDivider/>
  54. <TouchableHighlight underlayColor={Global.touchableHighlightColor} onPress={() => {
  55. this.modifyUserNick()
  56. }}>
  57. <View style={styles.listItem} activeOpacity={0.6}>
  58. <Text style={styles.listItemLeftText}>昵称</Text>
  59. <View style={styles.rightContainer}>
  60. <Text>{this.state.nick || ''}</Text>
  61. </View>
  62. <Image source={require('../../images/ic_right_arrow.png')} style={styles.rightArrow}/>
  63. </View>
  64. </TouchableHighlight>
  65. <ListItemDivider/>
  66. <View style={styles.listItem}>
  67. <Text style={styles.listItemLeftText}>微信号</Text>
  68. <View style={styles.rightContainer}>
  69. <Text>{this.state.username || ''}</Text>
  70. </View>
  71. </View>
  72. <ListItemDivider/>
  73. <View style={styles.listItem}>
  74. <Text style={styles.listItemLeftText}>二维码名片</Text>
  75. <View style={styles.rightContainer}>
  76. <Image style={[styles.listItemRight, styles.qrcodeImg]} source={require('../../images/ic_qr_code.png')}/>
  77. </View>
  78. </View>
  79. <ListItemDivider/>
  80. <View style={styles.listItem}>
  81. <Text style={styles.listItemLeftText}>更多</Text>
  82. </View>
  83. <View style={{height: 20, width: width}}/>
  84. <TouchableHighlight underlayColor={Global.touchableHighlightColor} onPress={() => {
  85. }}>
  86. <View style={styles.listItem}>
  87. <Text style={styles.listItemLeftText}>我的地址</Text>
  88. </View>
  89. </TouchableHighlight>
  90. </View>
  91. <View style={{backgroundColor: 'transparent', position: 'absolute', left: 0, top: 0, width: width}}>
  92. <ReplyPopWin ref="replyPopWin"/>
  93. </View>
  94. </View>
  95. );
  96. }
  97. modifyUserNick() {
  98. this.refs.replyPopWin.showModalWhenUpdateInfo(this.state.username, (contactId, nickName) => {
  99. // 请求服务器,修改昵称
  100. this.setState({showProgress: true});
  101. let url = "http://app.yubo725.top/updateNick";
  102. let formData = new FormData();
  103. formData.append('contactId', contactId);
  104. formData.append('nick', nickName);
  105. fetch(url, {method: 'POST', body: formData}).then((res) => res.json())
  106. .then((json) => {
  107. this.setState({showProgress: false});
  108. if (json != null && json.code == 1) {
  109. this.setState({nick: nickName});
  110. this.userInfo.nick = nickName;
  111. StorageUtil.set('userInfo-' + this.state.username, {'info': this.userInfo}, () => {
  112. CountEmitter.emit('updateUserInfo');
  113. });
  114. Toast.showShortCenter('修改成功');
  115. } else {
  116. Toast.showShortCenter('修改失败');
  117. }
  118. }).catch((e) => {
  119. this.setState({showProgress: false});
  120. Toast.showShortCenter('修改失败' + e.toString());
  121. })
  122. });
  123. }
  124. modifyAvatar() {
  125. // 修改头像
  126. ImagePicker.openPicker({
  127. width: 300,
  128. height: 300,
  129. cropping: true
  130. }).then(image => {
  131. // image: Object {size: 34451, mime: "image/jpeg", height: 300, width: 300, path: "file:///data/user/0/com.testreactnavigation/cache/…p-picker/01b5d49d-3805-45d3-bdd7-4f49939706d0.jpg"}
  132. this.setState({showProgress: true});
  133. let path = image.path;
  134. let filename = path.substring(path.lastIndexOf('/') + 1, path.length);
  135. let username = this.state.username;
  136. if (Utils.isEmpty(username)) {
  137. Toast.showShortCenter('用户未登录');
  138. } else {
  139. let formData = new FormData();
  140. formData.append('username', username);
  141. let file = {uri: image.path, type: 'multipart/form-data', name: filename};
  142. formData.append('file', file);
  143. let url = 'http://app.yubo725.top/updateAvatar';
  144. fetch(url, {method: 'POST', body: formData})
  145. .then((res) => res.json())
  146. .then((json) => {
  147. this.setState({showProgress: false});
  148. if (!Utils.isEmpty(json)) {
  149. if (json.code == 1) {
  150. Toast.showShortCenter('修改头像成功');
  151. this.setState({avatar: json.msg});
  152. this.userInfo.avatar = json.msg;
  153. StorageUtil.set('userInfo-' + this.state.username, {'info': this.userInfo}, () => {
  154. // 发送消息通知其他界面更新头像
  155. CountEmitter.emit('updateAvatar');
  156. });
  157. } else {
  158. console.warn(JSON.stringify(json))
  159. Toast.showShortCenter('' + json.msg);
  160. // console.warn('json.msg: ' + json.msg)
  161. }
  162. }
  163. }).catch((e) => {
  164. this.setState({showProgress: false});
  165. Toast.showShortCenter('' + e.toString());
  166. console.warn('exception: ' + e.toString())
  167. })
  168. }
  169. });
  170. }
  171. }
  172. const styles = StyleSheet.create({
  173. container: {
  174. flex: 1,
  175. flexDirection: 'column',
  176. },
  177. list: {
  178. flex: 1,
  179. flexDirection: 'column',
  180. marginTop: 20,
  181. },
  182. listItem: {
  183. flexDirection: 'row',
  184. padding: 15,
  185. alignItems: 'center',
  186. backgroundColor: '#FFFFFF'
  187. },
  188. listItemLeftText: {
  189. alignItems: 'flex-start',
  190. color: '#000000',
  191. fontSize: 16,
  192. },
  193. rightContainer: {
  194. flex: 1,
  195. flexDirection: 'row',
  196. justifyContent: 'flex-end',
  197. },
  198. listItemRight: {
  199. alignItems: 'flex-end'
  200. },
  201. avatarImg: {
  202. width: 60,
  203. height: 60,
  204. },
  205. qrcodeImg: {
  206. width: 25,
  207. height: 25,
  208. },
  209. rightArrow: {
  210. width: 8,
  211. height: 14,
  212. marginLeft: 10
  213. }
  214. })