EditProfile.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  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. this.updateProfile(this.state.userInfo);
  218. } else {
  219. toastShort($t('common.uploadFailed'));
  220. }
  221. }).catch(err => {
  222. toastShort(err);
  223. });
  224. }
  225. changeCountry(value, index) {
  226. if (this.state.userInfo.countryCode !== value) {
  227. var info = Object.assign({}, userInfo);
  228. info.countryCode = value;
  229. this.updateProfile(info)
  230. }
  231. }
  232. render() {
  233. return (
  234. <View style={styles.container}>
  235. <Pressable
  236. style={styles.profileView}
  237. android_ripple={ripple}
  238. onPress={() => this.pickAvatar()}>
  239. <TextView style={styles.label}>{$t('profile.avatar')}</TextView>
  240. <Text style={ui.flex1}></Text>
  241. { userInfo.photoUrl
  242. ? <Image
  243. style={styles.avatar}
  244. source={{uri: utils.getImageUrl(userInfo.photoUrl)}}/>
  245. : <Image
  246. style={styles.avatar}
  247. source={require('../../images/user/ic-avatar-default.png')}/>
  248. }
  249. <ArrowRight/>
  250. </Pressable>
  251. <Divide/>
  252. <Pressable
  253. style={styles.profileView}
  254. android_ripple={ripple}
  255. onPress={() => {
  256. this.setState({
  257. editDialog: {
  258. key: 'nickName',
  259. title: $t('profile.updateNickname'),
  260. value: this.state.userInfo.nickName,
  261. visible: true
  262. }
  263. });
  264. }}>
  265. <TextView style={styles.label}>{$t('profile.nickname')}</TextView>
  266. <TextView style={styles.infoText}>{this.state.userInfo.nickName}</TextView>
  267. <ArrowRight/>
  268. </Pressable>
  269. <Divide/>
  270. <View style={styles.profileView}>
  271. <TextView style={styles.label}>{$t('sign.labelCountry')}</TextView>
  272. <TextView style={styles.infoText}></TextView>
  273. <ArrowRight/>
  274. <Dropdown
  275. style={styles.selectView}
  276. title={$t('sign.labelCountry')}
  277. list={this.state.countryList}
  278. value={this.state.userInfo.countryCode}
  279. nameKey='countryName'
  280. valueKey='countryCode'
  281. showIcon={false}
  282. textStyle={styles.rightText}
  283. onChange={(value, index)=> this.changeCountry(value, index)}
  284. customerItemView={
  285. (item, index, onClick) =>
  286. <CountryDropCode
  287. key={index}
  288. country={item}
  289. value={this.state.userInfo.countryCode}
  290. onClick={onClick}/>
  291. }/>
  292. </View>
  293. <Divide/>
  294. <Pressable
  295. style={styles.profileView}
  296. android_ripple={ripple}
  297. onPress={() => {
  298. this.setState({
  299. editDialog: {
  300. key: 'phone',
  301. visible: true,
  302. showCalling: true,
  303. title: $t('profile.updatePhoneNumber'),
  304. value: this.state.userInfo.phone,
  305. areaNo: this.state.userInfo.callingCode
  306. }
  307. });
  308. }}>
  309. <TextView style={styles.label}>{$t('profile.phoneNumber')}</TextView>
  310. <TextView style={styles.infoText}>{(utils.isNotEmpty(this.state.userInfo.callingCode) && "+" + this.state.userInfo.callingCode)} {this.state.userInfo.phone}</TextView>
  311. <ArrowRight/>
  312. </Pressable>
  313. <Divide/>
  314. <Pressable
  315. style={[styles.profileView, {
  316. paddingRight: 48
  317. }]}>
  318. <TextView style={styles.label}>{$t('profile.email')}</TextView>
  319. <TextView style={styles.infoText}>{userInfo.email}</TextView>
  320. {/*<ArrowRight/>*/}
  321. </Pressable>
  322. <Divide/>
  323. {/* <Pressable
  324. style={styles.profileView}
  325. android_ripple={ripple}
  326. onPress={() => {
  327. /*this.setState({
  328. editDialog: {
  329. key: 'addressLine',
  330. title: 'Update Address',
  331. value: userInfo.addressLine,
  332. visible: true
  333. }
  334. });*
  335. this.editAddress = true;
  336. startPage(PageList.editAddress);
  337. }}>
  338. <TextView style={styles.label}>{$t('profile.address')}</TextView>
  339. <TextView style={styles.infoText}>{userInfo.addressLine}</TextView>
  340. <ArrowRight/>
  341. </Pressable> */}
  342. <Pressable
  343. style={[styles.profileView, {
  344. paddingRight: 48
  345. }]}>
  346. <TextView style={styles.label}>{$t('profile.userType')}</TextView>
  347. <TextView style={styles.infoText}>{userInfo.userType}</TextView>
  348. {/*<ArrowRight/>*/}
  349. </Pressable>
  350. <Divide/>
  351. {/* <Pressable
  352. style={[styles.profileView, {
  353. paddingRight: 48
  354. }]}>
  355. <TextView style={styles.label}>{$t('profile.registerDate')}</TextView>
  356. <TextView style={styles.infoText}>{"-"}</TextView>
  357. </Pressable> */}
  358. <EditDialog
  359. title={this.state.editDialog.title}
  360. visible={this.state.editDialog.visible}
  361. value={this.state.editDialog.value}
  362. areaNo={this.state.editDialog.areaNo}
  363. countryList={this.state.countryList}
  364. showCalling={this.state.editDialog.showCalling}
  365. keyType={this.state.editDialog.key == 'phone' ? 'phone-pad' : 'default'}
  366. onChangedText={(text, areaNo) => {
  367. if (text) {
  368. var info = Object.assign({}, userInfo);
  369. if (this.state.editDialog.key == 'phone') {
  370. if (!/^[0-9\+]\d{6,15}$/.test(text)) {
  371. toastShort($t('profile.errPhoneNumberFormat'));
  372. return;
  373. }
  374. info.callingCode = areaNo;
  375. }
  376. info[this.state.editDialog.key] = text;
  377. /*this.setState({
  378. userInfo: userInfo
  379. });
  380. this.canUpdate = true;*/
  381. this.updateProfile(info);
  382. }
  383. this.hideDialog();
  384. }}/>
  385. </View>
  386. );
  387. }
  388. }
  389. const styles = StyleSheet.create({
  390. container: {
  391. flex: 1,
  392. backgroundColor: pageBackground
  393. },
  394. profileView: {
  395. padding: 16,
  396. alignItems: 'center',
  397. flexDirection: 'row'
  398. },
  399. label: {
  400. color: '#666',
  401. fontSize: 14
  402. },
  403. infoText: {
  404. flex: 1,
  405. color: textPrimary,
  406. fontSize: 14,
  407. paddingLeft: 16,
  408. textAlign: 'right'
  409. },
  410. avatar: {
  411. width: 62,
  412. height: 62,
  413. borderRadius: 80,
  414. },
  415. dialog: {
  416. width: DialogMaxWidth,
  417. paddingTop: 16,
  418. paddingLeft: 20,
  419. paddingRight: 20,
  420. paddingBottom: 16,
  421. marginLeft: 'auto',
  422. marginRight: 'auto',
  423. backgroundColor: colorLight,
  424. borderRadius: isIOS ? 20 : 3
  425. },
  426. editNickname: {
  427. color: '#000',
  428. fontSize: 18,
  429. textAlign: 'center'
  430. },
  431. nickInput: {
  432. flex: 1,
  433. color: textPrimary,
  434. minHeight: 43,
  435. fontSize: 14,
  436. paddingTop: 8,
  437. paddingLeft: 16,
  438. paddingRight: 16,
  439. paddingBottom: 8,
  440. borderRadius: 4,
  441. marginTop: 24,
  442. backgroundColor: '#F6F6F6'
  443. },
  444. modalButtons: {
  445. marginTop: 32,
  446. paddingBottom: 12,
  447. alignItems: 'center',
  448. flexDirection: 'row'
  449. },
  450. btnCancel: {
  451. flex: 1,
  452. borderWidth: 1,
  453. borderColor: colorCancel,
  454. backgroundColor: pageBackground
  455. },
  456. btnOk: {
  457. flex: 1,
  458. marginLeft: 16,
  459. borderWidth: 1,
  460. borderColor: colorAccent,
  461. },
  462. closeIcon: {
  463. top: 12,
  464. right: 12,
  465. position: "absolute"
  466. },
  467. countryView: {
  468. width: 85,
  469. marginTop: 24,
  470. marginRight: 8,
  471. minHeight: 43,
  472. borderRadius: 4,
  473. alignItems: 'center',
  474. flexDirection: 'row',
  475. ...$padding(0, 5, 0, 12),
  476. backgroundColor: '#F6F6F6'
  477. },
  478. selectView: {
  479. top: 0,
  480. left: 0,
  481. bottom: 0,
  482. width: $vw(100),
  483. alignItems: 'center',
  484. flexDirection: 'row',
  485. position: 'absolute',
  486. },
  487. selectText: {
  488. color: textPrimary,
  489. fontSize: 15
  490. },
  491. rightText: {
  492. right: 48,
  493. color: textPrimary,
  494. fontSize: 14,
  495. position: 'absolute'
  496. },
  497. });