EditProfile.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  1. /**
  2. * Edit Profile页面
  3. * @邠心vbe on 2021/05/07
  4. */
  5. import React, { Component, useEffect, useState } from 'react';
  6. import { View, Text, StyleSheet, Pressable, Image, TextInput } from 'react-native';
  7. import ImagePicker from 'react-native-image-crop-picker';
  8. import Modal from 'react-native-modal';
  9. import apiUpload from '../../api/apiUpload';
  10. import apiUser from '../../api/apiUser';
  11. import { ModalProps } from '../../components/BottomModal';
  12. import Button, { ViewHeight } from '../../components/Button';
  13. import { CountryDropCode, CountryDropNum, GetCountryList } from '../../components/CountryIcon';
  14. import Dropdown from '../../components/Dropdown';
  15. import TextView from '../../components/TextView';
  16. import { UploadThemes } from '../../components/ThemesConfig';
  17. import utils from '../../utils/utils';
  18. import { DialogMaxWidth } from '../charge/InfoDialog';
  19. import { PageList } from '../Router';
  20. //Image Picker参数
  21. const options = {
  22. cropping: true,
  23. multiple: false,
  24. minFiles: 1,
  25. maxFiles: 3,
  26. mediaType: 'photo',
  27. compressImageQuality: 0.8,
  28. cropperCircleOverlay: true,
  29. ...UploadThemes
  30. }
  31. //向右的箭头
  32. const ArrowRight = () => {
  33. return (
  34. <Entypo
  35. size={16}
  36. color='#999'
  37. name='chevron-thin-right'
  38. style={{
  39. marginLeft: 16
  40. }}
  41. />
  42. );
  43. }
  44. //分割线
  45. const Divide = () => {
  46. return (
  47. <View style={{
  48. paddingLeft: 16,
  49. paddingRight: 16
  50. }}>
  51. <Text style={{
  52. height: 1,
  53. backgroundColor: '#eee'
  54. }}></Text>
  55. </View>
  56. );
  57. }
  58. //编辑弹窗
  59. const EditDialog = ({visible, title, value, keyType, countryList, areaNo="", showCalling=false, onChangedText}) => {
  60. var changedText = value;
  61. var [callingCode, setCalling] = useState();
  62. useEffect(() => {
  63. if (visible && areaNo) {
  64. setCalling(areaNo)
  65. }
  66. }, [visible])
  67. return (
  68. <Modal
  69. isVisible={visible}
  70. {...ModalProps}
  71. onBackdropPress={() => onChangedText('')}
  72. onBackButtonPress={() => onChangedText('')}>
  73. <View style={styles.dialog}>
  74. <TextView style={styles.editNickname}>{title}</TextView>
  75. <View style={ui.flexc}>
  76. { showCalling &&
  77. <Dropdown
  78. prefixText="+"
  79. list={countryList}
  80. value={callingCode}
  81. nameKey='countryNum'
  82. valueKey='countryNum'
  83. autoSelect={false}
  84. style={styles.countryView}
  85. textStyle={styles.selectText}
  86. onChange={(value, index)=> {
  87. setCalling(value);
  88. }}
  89. customerItemView={
  90. (item, index, onClick) =>
  91. <CountryDropNum
  92. key={index}
  93. country={item}
  94. value={callingCode}
  95. onClick={onClick}/>
  96. }
  97. />
  98. }
  99. <TextInput
  100. style={styles.nickInput}
  101. defaultValue={value}
  102. keyboardType={keyType}
  103. onChangeText={text => {
  104. changedText = text;
  105. }}/>
  106. </View>
  107. <View style={styles.modalButtons}>
  108. <Button
  109. style={styles.btnCancel}
  110. viewStyle={ViewHeight(42)}
  111. textColor={textCancel}
  112. text={$t('common.cancel')}
  113. onClick={() => {
  114. onChangedText('');
  115. }}/>
  116. <Button
  117. style={styles.btnOk}
  118. viewStyle={ViewHeight(42)}
  119. text={$t('common.save')}
  120. onClick={() => {
  121. onChangedText(changedText, callingCode);
  122. }}/>
  123. </View>
  124. {/* <Ionicons
  125. name='close'
  126. size={28}
  127. color={'#999'}
  128. style={styles.closeIcon}
  129. onPress={() => {
  130. onChangedText('');
  131. }} /> */}
  132. </View>
  133. </Modal>
  134. );
  135. }
  136. //页面
  137. export default class EditProfile extends Component {
  138. constructor(props) {
  139. super(props);
  140. this.state = {
  141. editDialog: {
  142. key: '',
  143. value: '',
  144. title: '',
  145. visible: false
  146. },
  147. userInfo: {},
  148. countryList: []
  149. };
  150. this.editAddress = false;
  151. }
  152. componentDidMount() {
  153. /*this.props.navigation.addListener('beforeRemove', e => {
  154. if (this.canUpdate) {
  155. e.preventDefault();
  156. //console.log('----update---', userInfo);
  157. }
  158. });*/
  159. this.props.navigation.addListener('focus', e => {
  160. if (this.editAddress && userInfo.editAddress) {
  161. this.updateProfile(global.userInfo);
  162. }
  163. this.editAddress = false;
  164. });
  165. this.setState({
  166. userInfo: userInfo
  167. })
  168. GetCountryList(list => {
  169. this.setState({
  170. countryList: list
  171. })
  172. })
  173. }
  174. updateProfile(_userInfo) {
  175. apiUser.setProfile(_userInfo).then(res => {
  176. toastShort($t('profile.updateSuccess'));
  177. userInfo = _userInfo;
  178. this.setState({
  179. userInfo: _userInfo
  180. });
  181. //this.canUpdate = false;
  182. //goBack();
  183. }).catch(err => {
  184. //this.canUpdate = false;
  185. toastShort(err);
  186. });
  187. }
  188. hideDialog() {
  189. this.state.editDialog.visible = false;
  190. this.setState({
  191. editDialog: this.state.editDialog
  192. });
  193. }
  194. pickAvatar() {
  195. console.log("pickAvatar", $t('common.cropperTitle'));
  196. ImagePicker.openPicker({
  197. width: 200,
  198. height: 200,
  199. ...options,
  200. cropperToolbarTitle: $t('common.cropperTitle')
  201. }).then(image => {
  202. if (image.path) {
  203. this.uploadAvatar(image)
  204. }
  205. }).catch(errs => {
  206. console.info("pickAvatarError", errs);
  207. });
  208. }
  209. uploadAvatar(image) {
  210. apiUpload.uploadImage(image.path, image.mime, 'USER_PROFILE').then(res => {
  211. if (res.success && res.data.picturePath) {
  212. //this.canUpdate = true;
  213. this.state.userInfo.photoUrl = res.data.picturePath
  214. /*this.setState({
  215. userInfo: userInfo
  216. });*/
  217. utils.logEventTracking("file_upload: " + res.data.picturePath)
  218. this.updateProfile(this.state.userInfo);
  219. } else {
  220. toastShort($t('common.uploadFailed'));
  221. }
  222. }).catch(err => {
  223. toastShort(err);
  224. });
  225. }
  226. changeCountry(value, index) {
  227. if (this.state.userInfo.countryCode !== value) {
  228. var info = Object.assign({}, userInfo);
  229. info.countryCode = value;
  230. this.updateProfile(info)
  231. }
  232. }
  233. render() {
  234. return (
  235. <View style={styles.container}>
  236. <Pressable
  237. style={styles.profileView}
  238. android_ripple={ripple}
  239. onPress={() => this.pickAvatar()}>
  240. <TextView style={styles.label}>{$t('profile.avatar')}</TextView>
  241. <Text style={ui.flex1}></Text>
  242. { userInfo.photoUrl
  243. ? <Image
  244. style={styles.avatar}
  245. source={{uri: utils.getImageUrl(userInfo.photoUrl)}}/>
  246. : <Image
  247. style={styles.avatar}
  248. source={require('../../images/user/ic-avatar-default.png')}/>
  249. }
  250. <ArrowRight/>
  251. </Pressable>
  252. <Divide/>
  253. <Pressable
  254. style={styles.profileView}
  255. android_ripple={ripple}
  256. onPress={() => {
  257. this.setState({
  258. editDialog: {
  259. key: 'nickName',
  260. title: $t('profile.updateNickname'),
  261. value: this.state.userInfo.nickName,
  262. visible: true
  263. }
  264. });
  265. }}>
  266. <TextView style={styles.label}>{$t('profile.nickname')}</TextView>
  267. <TextView style={styles.infoText}>{this.state.userInfo.nickName}</TextView>
  268. <ArrowRight/>
  269. </Pressable>
  270. <Divide/>
  271. <View style={styles.profileView}>
  272. <TextView style={styles.label}>{$t('sign.labelCountry')}</TextView>
  273. <TextView style={styles.infoText}></TextView>
  274. <ArrowRight/>
  275. <Dropdown
  276. style={styles.selectView}
  277. title={$t('sign.labelCountry')}
  278. list={this.state.countryList}
  279. value={this.state.userInfo.countryCode}
  280. nameKey='countryName'
  281. valueKey='countryCode'
  282. showIcon={false}
  283. textStyle={styles.rightText}
  284. onChange={(value, index)=> this.changeCountry(value, index)}
  285. customerItemView={
  286. (item, index, onClick) =>
  287. <CountryDropCode
  288. key={index}
  289. country={item}
  290. value={this.state.userInfo.countryCode}
  291. onClick={onClick}/>
  292. }/>
  293. </View>
  294. <Divide/>
  295. <Pressable
  296. style={styles.profileView}
  297. android_ripple={ripple}
  298. onPress={() => {
  299. this.setState({
  300. editDialog: {
  301. key: 'phone',
  302. visible: true,
  303. showCalling: true,
  304. title: $t('profile.updatePhoneNumber'),
  305. value: this.state.userInfo.phone,
  306. areaNo: this.state.userInfo.callingCode
  307. }
  308. });
  309. }}>
  310. <TextView style={styles.label}>{$t('profile.phoneNumber')}</TextView>
  311. <TextView style={styles.infoText}>{(utils.isNotEmpty(this.state.userInfo.callingCode) && "+" + this.state.userInfo.callingCode)} {this.state.userInfo.phone}</TextView>
  312. <ArrowRight/>
  313. </Pressable>
  314. <Divide/>
  315. <Pressable
  316. style={[styles.profileView, {
  317. paddingRight: 48
  318. }]}>
  319. <TextView style={styles.label}>{$t('profile.email')}</TextView>
  320. <TextView style={styles.infoText}>{userInfo.email}</TextView>
  321. {/*<ArrowRight/>*/}
  322. </Pressable>
  323. <Divide/>
  324. {/* <Pressable
  325. style={styles.profileView}
  326. android_ripple={ripple}
  327. onPress={() => {
  328. /*this.setState({
  329. editDialog: {
  330. key: 'addressLine',
  331. title: 'Update Address',
  332. value: userInfo.addressLine,
  333. visible: true
  334. }
  335. });*
  336. this.editAddress = true;
  337. startPage(PageList.editAddress);
  338. }}>
  339. <TextView style={styles.label}>{$t('profile.address')}</TextView>
  340. <TextView style={styles.infoText}>{userInfo.addressLine}</TextView>
  341. <ArrowRight/>
  342. </Pressable> */}
  343. <Pressable
  344. style={[styles.profileView, {
  345. paddingRight: 48
  346. }]}>
  347. <TextView style={styles.label}>{$t('profile.userType')}</TextView>
  348. <TextView style={styles.infoText}>{userInfo.userType}</TextView>
  349. {/*<ArrowRight/>*/}
  350. </Pressable>
  351. <Divide/>
  352. {/* <Pressable
  353. style={[styles.profileView, {
  354. paddingRight: 48
  355. }]}>
  356. <TextView style={styles.label}>{$t('profile.registerDate')}</TextView>
  357. <TextView style={styles.infoText}>{"-"}</TextView>
  358. </Pressable> */}
  359. <EditDialog
  360. title={this.state.editDialog.title}
  361. visible={this.state.editDialog.visible}
  362. value={this.state.editDialog.value}
  363. areaNo={this.state.editDialog.areaNo}
  364. countryList={this.state.countryList}
  365. showCalling={this.state.editDialog.showCalling}
  366. keyType={this.state.editDialog.key == 'phone' ? 'phone-pad' : 'default'}
  367. onChangedText={(text, areaNo) => {
  368. if (text) {
  369. var info = Object.assign({}, userInfo);
  370. if (this.state.editDialog.key == 'phone') {
  371. if (!/^[0-9\+]\d{6,15}$/.test(text)) {
  372. toastShort($t('profile.errPhoneNumberFormat'));
  373. return;
  374. }
  375. info.callingCode = areaNo;
  376. }
  377. info[this.state.editDialog.key] = text;
  378. /*this.setState({
  379. userInfo: userInfo
  380. });
  381. this.canUpdate = true;*/
  382. this.updateProfile(info);
  383. }
  384. this.hideDialog();
  385. }}/>
  386. </View>
  387. );
  388. }
  389. }
  390. const styles = StyleSheet.create({
  391. container: {
  392. flex: 1,
  393. backgroundColor: pageBackground
  394. },
  395. profileView: {
  396. padding: 16,
  397. alignItems: 'center',
  398. flexDirection: 'row'
  399. },
  400. label: {
  401. color: '#666',
  402. fontSize: 14
  403. },
  404. infoText: {
  405. flex: 1,
  406. color: textPrimary,
  407. fontSize: 14,
  408. paddingLeft: 16,
  409. textAlign: 'right'
  410. },
  411. avatar: {
  412. width: 62,
  413. height: 62,
  414. borderRadius: 80,
  415. },
  416. dialog: {
  417. width: DialogMaxWidth,
  418. paddingTop: 16,
  419. paddingLeft: 20,
  420. paddingRight: 20,
  421. paddingBottom: 16,
  422. marginLeft: 'auto',
  423. marginRight: 'auto',
  424. backgroundColor: colorLight,
  425. borderRadius: isIOS ? 20 : 3
  426. },
  427. editNickname: {
  428. color: '#000',
  429. fontSize: 18,
  430. textAlign: 'center'
  431. },
  432. nickInput: {
  433. flex: 1,
  434. color: textPrimary,
  435. minHeight: 43,
  436. fontSize: 14,
  437. paddingTop: 8,
  438. paddingLeft: 16,
  439. paddingRight: 16,
  440. paddingBottom: 8,
  441. borderRadius: 4,
  442. marginTop: 24,
  443. backgroundColor: '#F6F6F6'
  444. },
  445. modalButtons: {
  446. marginTop: 32,
  447. paddingBottom: 12,
  448. alignItems: 'center',
  449. flexDirection: 'row'
  450. },
  451. btnCancel: {
  452. flex: 1,
  453. borderWidth: 1,
  454. borderColor: colorCancel,
  455. backgroundColor: pageBackground
  456. },
  457. btnOk: {
  458. flex: 1,
  459. marginLeft: 16,
  460. borderWidth: 1,
  461. borderColor: colorAccent,
  462. },
  463. closeIcon: {
  464. top: 12,
  465. right: 12,
  466. position: "absolute"
  467. },
  468. countryView: {
  469. width: 85,
  470. marginTop: 24,
  471. marginRight: 8,
  472. minHeight: 43,
  473. borderRadius: 4,
  474. alignItems: 'center',
  475. flexDirection: 'row',
  476. ...$padding(0, 5, 0, 12),
  477. backgroundColor: '#F6F6F6'
  478. },
  479. selectView: {
  480. top: 0,
  481. left: 0,
  482. bottom: 0,
  483. width: $vw(100),
  484. alignItems: 'center',
  485. flexDirection: 'row',
  486. position: 'absolute',
  487. },
  488. selectText: {
  489. color: textPrimary,
  490. fontSize: 15
  491. },
  492. rightText: {
  493. right: 48,
  494. color: textPrimary,
  495. fontSize: 14,
  496. position: 'absolute'
  497. },
  498. });