EditProfile.js 14 KB

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