Wallet.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. /**
  2. * 钱包页面
  3. * @邠心vbe on 2021/05/07
  4. */
  5. import React, { Component } from 'react';
  6. import { View, Text, StyleSheet, ScrollView, RefreshControl, Image, Pressable } from 'react-native';
  7. import { Balance } from './Payment';
  8. import { PageList } from '../Router';
  9. import History from './History';
  10. import Overview from './Overview';
  11. import { MyRefreshProps } from '../../components/ThemesConfig';
  12. import { ElevationObject } from '../../components/Button';
  13. import TextView from '../../components/TextView';
  14. export default class Wallet extends Component {
  15. constructor(props) {
  16. super(props);
  17. this.state = {
  18. balance: 0,
  19. tabIndex: 0,
  20. refreshing: false,
  21. tabWidth: 0,
  22. tabHeight: 0,
  23. balanceText: '',
  24. pageShown: false
  25. };
  26. }
  27. componentDidMount() {
  28. this.props.navigation.addListener('focus', () => {
  29. this.onRefresh();
  30. getUserInfo(info => {
  31. this.setState({
  32. balance: info.credit,
  33. balanceText: info.creditStr,
  34. pageShown: true
  35. })
  36. }, true)
  37. });
  38. this.props.navigation.addListener('beforeRemove', (e) => {
  39. if (this.state.pageShown) {
  40. e.preventDefault();
  41. this.setState({
  42. pageShown: false
  43. }, () => {
  44. setTimeout(() => {
  45. goBack();
  46. }, 100);
  47. });
  48. }
  49. });
  50. }
  51. changeTab(index) {
  52. if (this.state.tabIndex !== index) {
  53. this.setState({
  54. tabIndex: index
  55. });
  56. }
  57. }
  58. onRefresh() {
  59. this.setState({
  60. refreshing: true
  61. });
  62. }
  63. stopRefresh() {
  64. this.setState({
  65. refreshing: false
  66. });
  67. }
  68. tabLayout({nativeEvent}) {
  69. //console.log('tab layout\n\n\n\n\n', nativeEvent.layout);
  70. if (nativeEvent.layout.width) {
  71. this.setState({
  72. tabWidth: nativeEvent.layout.width,
  73. tabHeight: 0.171955 * nativeEvent.layout.width
  74. })
  75. }
  76. }
  77. render() {
  78. return (
  79. <ScrollView
  80. style={styles.container}
  81. refreshControl={
  82. <RefreshControl
  83. {...MyRefreshProps()}
  84. refreshing={this.state.refreshing}
  85. onRefresh={() => this.onRefresh()}
  86. />
  87. }>
  88. {/* <View style={styles.balanceView}>
  89. <Payment
  90. balance={this.state.balance}
  91. isPayPerUse={false}
  92. isWallet={true}
  93. payType={"Credit Wallet"}
  94. topup={() => toTopupPage()}/>
  95. </View> */}
  96. <Balance
  97. balance={this.state.balance}
  98. balanceText={this.state.balanceText}
  99. action={$t('wallet.purchaseCredits')/*"Top Up"*/}
  100. page={PageList.topupNew}
  101. />
  102. <View style={styles.contentView}>
  103. <View
  104. style={styles.tabView}
  105. onLayout={props => this.tabLayout(props)}>
  106. { this.state.tabIndex == 0
  107. ? <Image
  108. style={[styles.tabBackgroundLeft, {width: this.state.tabWidth + 1, height: this.state.tabHeight}]}
  109. resizeMode="cover"
  110. source={require('../../images/wallet/tab-left.png')}
  111. />
  112. : <Image
  113. style={[styles.tabBackgroundRight, {width: this.state.tabWidth + 1, height: this.state.tabHeight}]}
  114. resizeMode="cover"
  115. source={require('../../images/wallet/tab-right.png')}
  116. />
  117. }
  118. <Pressable
  119. style={styles.tabItem}
  120. onPress={() => this.changeTab(0)}>
  121. <TextView
  122. style={[
  123. styles.tabText,
  124. this.state.tabIndex == 0 && styles.active
  125. ]}
  126. numberOfLines={1}>
  127. {$t('wallet.tabOverview')}
  128. </TextView>
  129. </Pressable>
  130. <View style={{width: 16}}></View>
  131. <Pressable
  132. style={styles.tabItem}
  133. onPress={() => this.changeTab(1)}>
  134. <TextView
  135. style={[
  136. styles.tabText,
  137. this.state.tabIndex == 1 && styles.active
  138. ]}
  139. numberOfLines={1}>
  140. {$t('wallet.tabHistory')}
  141. </TextView>
  142. </Pressable>
  143. </View>
  144. <Overview
  145. atAglance={false}
  146. skeleton={!this.state.pageShown}
  147. refresh={this.state.refreshing}
  148. refreshed={() => this.stopRefresh()}
  149. shown={this.state.tabIndex == 0}/>
  150. <History
  151. refresh={this.state.refreshing}
  152. refreshed={() => this.stopRefresh()}
  153. shown={this.state.tabIndex == 1}/>
  154. </View>
  155. </ScrollView>
  156. );
  157. }
  158. }
  159. const styles = StyleSheet.create({
  160. container: {
  161. flex: 1,
  162. backgroundColor: '#f5f5f5'
  163. },
  164. balanceView: {
  165. paddingTop: 12,
  166. paddingLeft: 16,
  167. paddingRight: 16,
  168. paddingBottom: 12,
  169. backgroundColor: colorLight
  170. },
  171. tabView: {
  172. height: 60,
  173. padding: 16,
  174. overflow: 'hidden',
  175. alignItems: 'center',
  176. flexDirection: 'row',
  177. justifyContent: 'center'
  178. },
  179. tabItem: {
  180. flex: 1,
  181. marginTop: -14,
  182. alignItems: 'center',
  183. justifyContent: 'center'
  184. },
  185. tabText: {
  186. color: textSecondary,
  187. fontSize: 16,
  188. paddingTop: 5,
  189. paddingLeft: 36,
  190. paddingRight: 36,
  191. paddingBottom: 5,
  192. },
  193. active: {
  194. color: textPrimary,
  195. fontWeight: 'bold'
  196. },
  197. contentView: {
  198. ...$margin(8, 16, 16),
  199. overflow: 'hidden',
  200. borderRadius: 6,
  201. ...ElevationObject(5),
  202. backgroundColor: colorLight
  203. },
  204. tabBackgroundLeft: {
  205. top: -1,
  206. left: -1,
  207. right: 0,
  208. paddingRight: 1,
  209. width: $vw(90),
  210. height: $vw(14),
  211. position: 'absolute'
  212. },
  213. tabBackgroundRight: {
  214. top: -1,
  215. left: 0,
  216. right: -1,
  217. paddingRight: 1,
  218. width: $vw(90),
  219. height: $vw(14),
  220. position: 'absolute'
  221. }
  222. })