EditProfile.js 14 KB

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