Wallets.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. /**
  2. * 多钱包页面
  3. * @邠心vbe on 2025/01/17
  4. */
  5. import React, { Component } from 'react';
  6. import { FlatList, StyleSheet } from 'react-native';
  7. import { View, Text } from 'react-native';
  8. import PagerView from 'react-native-pager-view';
  9. import apiCharge from '../../api/apiCharge';
  10. import apiWallets from '../../api/apiWallets';
  11. import { ElevationObject } from '../../components/Button';
  12. import VbeSkeleton from '../../components/VbeSkeleton';
  13. import ViewHistory from './ViewHistory';
  14. import ViewWallet from './ViewWallet';
  15. export default class Wallets extends Component {
  16. constructor(props) {
  17. super(props);
  18. this.state = {
  19. loading: true,
  20. wallets: [],
  21. active: 0,
  22. history: {},
  23. historyList: [],
  24. loadingList: false
  25. };
  26. }
  27. componentDidMount() {
  28. setTimeout(() => {
  29. this.getWalletsInfo();
  30. }, 500);
  31. this.props.navigation.addListener('focus', () => {
  32. if (!this.state.loading) {
  33. this.refresh();
  34. }
  35. });
  36. }
  37. refresh() {
  38. this.setState({
  39. history: {},
  40. historyList: [],
  41. loadingList: true
  42. }, () => {
  43. this.getWalletsInfo(true);
  44. })
  45. }
  46. getWalletsInfo(refresh) {
  47. apiWallets.getWalletsInfo().then(res => {
  48. if (res.data) {
  49. this.setState({
  50. wallets: res.data
  51. })
  52. if (refresh) {
  53. let wallet = this.state.wallets[this.state.active];
  54. if (wallet && wallet.walletTypeCode) {
  55. setTimeout(() => {
  56. this.getTransactionInfo(wallet.walletTypeCode);
  57. }, 300);
  58. }
  59. }
  60. }
  61. }).catch(err => {
  62. toastShort(err)
  63. }).finally(() => {
  64. this.setState({
  65. loading: false
  66. })
  67. })
  68. }
  69. getTransactionInfo(code) {
  70. if (this.state.history[code]) {
  71. this.setState({
  72. loadingList: false,
  73. historyList: this.state.history[code]
  74. })
  75. return;
  76. }
  77. apiWallets.getTransactionList({
  78. walletTypeCode: code
  79. }).then(res => {
  80. if (res.data) {
  81. this.state.history[code] = res.data;
  82. this.setState({
  83. historyList: res.data
  84. })
  85. } else {
  86. this.setState({
  87. historyList: []
  88. })
  89. }
  90. }).catch(err => {
  91. this.setState({
  92. historyList: []
  93. })
  94. }).finally(() => {
  95. this.setState({
  96. loadingList: false
  97. })
  98. })
  99. }
  100. changeCard(e) {
  101. //console.log("changeCard", e.nativeEvent.position);
  102. this.setState({
  103. active: e.nativeEvent.position
  104. }, () => {
  105. let wallet = this.state.wallets[this.state.active];
  106. if (wallet && wallet.walletTypeCode) {
  107. this.setState({
  108. loadingList: true
  109. });
  110. setTimeout(() => {
  111. this.getTransactionInfo(wallet.walletTypeCode);
  112. }, 300);
  113. }
  114. })
  115. }
  116. setDefault(index) {
  117. const code = this.state.wallets[index]?.walletTypeCode;
  118. if (code) {
  119. Dialog.showProgressDialog();
  120. apiCharge.setDefaultPaymentType({
  121. defaultPaymentMethod: code
  122. }).then(res => {
  123. toastShort("success");
  124. this.getWalletsInfo();
  125. }).catch(err => {
  126. toastShort(err)
  127. }).finally(() => {
  128. Dialog.dismissLoading();
  129. })
  130. }
  131. }
  132. divideView = (props) => {
  133. return (<View style={styles.divideView}></View>)
  134. }
  135. render() {
  136. if (this.state.loading) {
  137. return (
  138. <VbeSkeleton
  139. style={[ui.flex1, $padding(16)]}
  140. layout={[
  141. {width: "100%", height: 210, borderRadius: 16},
  142. {width: "100%", height: $vh(100) - 330, borderRadius: 16, marginTop: 24},
  143. ]}
  144. animationDirection={"horizontalRight"}
  145. />
  146. )
  147. }
  148. return (
  149. <View style={styles.container}>
  150. <PagerView
  151. style={styles.pagerView}
  152. initialPage={this.state.active}
  153. onPageSelected={e => this.changeCard(e)}>
  154. { this.state.wallets.map((item, index) => (
  155. <ViewWallet
  156. key={index}
  157. wallet={item}
  158. setDefault={() => this.setDefault(index)}/>
  159. ))}
  160. </PagerView>
  161. <View style={styles.indicatorView}>
  162. { this.state.wallets.map((item, index) => (
  163. <MaterialCommunityIcons
  164. key={index}
  165. style={$margin(2)}
  166. name={this.state.active == index ? "checkbox-blank-circle" : "checkbox-blank-circle-outline"}
  167. size={10}/>
  168. ))}
  169. </View>
  170. <View style={styles.listView}>
  171. { this.state.loadingList
  172. ? <VbeSkeleton
  173. style={[ui.flex1, $padding(16)]}
  174. layout={[
  175. {width: "100%", height: 14, borderRadius: 16},
  176. {width: "100%", height: 14, borderRadius: 16, marginTop: 12},
  177. {width: "60%", height: 14, borderRadius: 16, marginTop: 12},
  178. {width: "100%", height: 14, borderRadius: 16, marginTop: 24},
  179. {width: "100%", height: 14, borderRadius: 16, marginTop: 12},
  180. {width: "60%", height: 14, borderRadius: 16, marginTop: 12}
  181. ]}/>
  182. : <FlatList
  183. data={this.state.historyList}
  184. renderItem={ViewHistory}
  185. ItemSeparatorComponent={this.divideView}
  186. keyExtractor={item => item.refPk}
  187. ListEmptyComponent={<Text style={styles.noData}>No Data</Text>}/>
  188. }
  189. </View>
  190. </View>
  191. );
  192. }
  193. }
  194. const styles = StyleSheet.create({
  195. container: {
  196. flex: 1,
  197. backgroundColor: pageBackground
  198. },
  199. pagerView: {
  200. height: 210
  201. },
  202. indicatorView: {
  203. padding: 8,
  204. alignItems: 'center',
  205. flexDirection: 'row',
  206. justifyContent: 'center',
  207. },
  208. listView: {
  209. flex: 1,
  210. overflow: 'hidden',
  211. ...$margin(8, 16, 16),
  212. ...ElevationObject(2),
  213. borderRadius: 16,
  214. backgroundColor: colorLight
  215. },
  216. noData: {
  217. color: textPlacehoder,
  218. fontSize: 14,
  219. padding: 20,
  220. textAlign: 'center'
  221. },
  222. divideView: {
  223. height: 1,
  224. opacity: 0.5,
  225. marginLeft: 16,
  226. marginRight: 16,
  227. backgroundColor: '#e8e8e8'
  228. }
  229. })