Topup.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. /**
  2. * 钱包充值页面
  3. * @邠心vbe on 2021/05/08
  4. */
  5. import React, { Component } from 'react';
  6. import { View, Text, ScrollView, StyleSheet, Switch } from 'react-native';
  7. import apiWallet from '../../api/apiWallet';
  8. import Button from '../../components/Button';
  9. import Dialog from '../../components/Dialog';
  10. import { PageList } from '../Router';
  11. import { Balance } from './Payment';
  12. import PaythodList from './PaythodList';
  13. export default class Topup extends Component {
  14. constructor(props) {
  15. super(props);
  16. this.state = {
  17. isAuto: false,
  18. topupList: [],
  19. selectIndex: 0,
  20. payType: {},
  21. balance: 0,
  22. };
  23. }
  24. componentDidMount() {
  25. this.props.navigation.addListener('focus', () => {
  26. getUserInfo(info => {
  27. this.setState({
  28. balance: info.credit
  29. })
  30. }, true);
  31. });
  32. this.getTopupList();
  33. }
  34. getTopupList() {
  35. Dialog.showProgressDialog();
  36. apiWallet.getTopUpAmountList().then(res => {
  37. Dialog.dismissLoading();
  38. if (res.data.length > 0) {
  39. this.setState({
  40. topupList: res.data
  41. });
  42. }
  43. }).catch(err => {
  44. Dialog.dismissLoading();
  45. })
  46. }
  47. topup2() {
  48. const params = {
  49. payAmount: this.state.topupList[this.state.selectIndex]?.key,
  50. }
  51. apiWallet.doPaymentV2(params).then(res => {
  52. Dialog.dismissLoading();
  53. if (res.data.webPaymentUrl) {
  54. startPage(PageList.paymentWeb, { amount: params.payAmount, url: res.data.webPaymentUrl, type: 'Topup' });
  55. } else {
  56. toastShort('Error 0')
  57. }
  58. }).catch(err => {
  59. Dialog.dismissLoading();
  60. toastShort(err);
  61. });
  62. }
  63. topup() {
  64. const params = {
  65. payAmount: this.state.topupList[this.state.selectIndex]?.key,
  66. fomoPayType: this.state.payType.fomoPayType
  67. }
  68. console.log('params',params);
  69. if (params.payAmount) {
  70. if (params.fomoPayType == 'PAYNOW' || params.fomoPayType == 'GRABPAY') {
  71. //PAYNOW支付
  72. Dialog.showProgressDialog();
  73. apiWallet.doPayment(params).then(res => {
  74. Dialog.dismissLoading();
  75. if (res.data.fomoId && res.data.qrCodeInBase64) {
  76. startPage(PageList.paynow, { amount: params.payAmount, base64: res.data.qrCodeInBase64 });
  77. } else if (res.data.url) {
  78. startPage(PageList.paymentWeb, { amount: params.payAmount, url: res.data.url, type: 'Topup' });
  79. } else {
  80. toastShort('Error 0')
  81. }
  82. }).catch(err => {
  83. Dialog.dismissLoading();
  84. toastShort(err);
  85. });
  86. } else {
  87. //信用卡支付
  88. startPage(PageList.formCard, { amount: params.payAmount, payType: params.fomoPayType });
  89. }
  90. } else {
  91. toastShort('Error 1')
  92. }
  93. }
  94. render() {
  95. return (
  96. <ScrollView style={styles.container}>
  97. <View style={styles.headerView}>
  98. <Balance balance={this.state.balance}/>
  99. </View>
  100. <View style={styles.contentView}>
  101. <View style={styles.topupView}>
  102. <WalletTitle>Top Up</WalletTitle>
  103. <Text style={styles.subTitle}>Choose Top Up Value: </Text>
  104. <View style={styles.topupItems}>
  105. { this.state.topupList.map((item, index) => {
  106. return (
  107. <Text
  108. key={index}
  109. style={[
  110. styles.topupItem,
  111. index > 0 && styles.right,
  112. index == this.state.selectIndex && styles.selected]}
  113. onPress={() => {
  114. this.setState({
  115. selectIndex: index
  116. })
  117. }}
  118. >{currency}{item.key}</Text>
  119. );
  120. })
  121. }
  122. </View>
  123. </View>
  124. <View style={styles.topupView}>
  125. <WalletTitle>Payment</WalletTitle>
  126. <Text style={styles.subTitle}>Pay with: </Text>
  127. <PaythodList
  128. onChange={type => {
  129. this.setState({
  130. payType: type
  131. });
  132. }}/>
  133. </View>
  134. {/* <View style={ui.flexc}>
  135. <Text
  136. style={styles.autoView}
  137. onPress={() => {
  138. this.setState({
  139. isAuto: !this.state.isAuto
  140. });
  141. }}>Automatically top up S$20 when wallet balance is below S$5</Text>
  142. <Switch
  143. value={this.state.isAuto}
  144. trackColor={isIOS ? { false: "#B2B2B2", true: colorAccent } : null}
  145. onValueChange={v => {
  146. this.setState({
  147. isAuto: v
  148. });
  149. }}/>
  150. </View> */}
  151. </View>
  152. <View style={styles.buttonView}>
  153. <Button
  154. text='TOP UP'
  155. elevation={1.5}
  156. onClick={() => {
  157. //startPage(PageList.paycard)
  158. this.topup();
  159. }}/>
  160. </View>
  161. </ScrollView>
  162. );
  163. }
  164. }
  165. export const WalletTitle = ({children}) => {
  166. return (
  167. <View style={styles.topupTitle}>
  168. <Text style={styles.titleLeft}></Text>
  169. <Text style={styles.titleText}>{children}</Text>
  170. </View>
  171. );
  172. }
  173. const styles = StyleSheet.create({
  174. container: {
  175. flex: 1,
  176. backgroundColor: '#F5F5F5'
  177. },
  178. headerView: {
  179. paddingBottom: 76,
  180. //backgroundColor: colorAccent
  181. },
  182. contentView: {
  183. padding: 16,
  184. marginTop: -88
  185. },
  186. topupView: {
  187. padding: 16,
  188. borderRadius: 10,
  189. marginBottom: 16,
  190. backgroundColor: colorLight
  191. },
  192. topupTitle: {
  193. alignItems: 'center',
  194. flexDirection: 'row'
  195. },
  196. titleLeft: {
  197. width: 4,
  198. height: 15,
  199. borderRadius: 16,
  200. backgroundColor: colorPrimary
  201. },
  202. titleText: {
  203. color: textPrimary,
  204. fontSize: 16,
  205. paddingLeft: 10
  206. },
  207. subTitle: {
  208. color: textPrimary,
  209. fontSize: 14,
  210. marginTop: 16,
  211. marginBottom: 16
  212. },
  213. topupItems: {
  214. paddingBottom: 16,
  215. alignItems: 'center',
  216. flexDirection: 'row'
  217. },
  218. topupItem: {
  219. flex: 1,
  220. height: 60,
  221. lineHeight: 60,
  222. color: '#666',
  223. fontSize: 24,
  224. borderWidth: 1,
  225. borderRadius: 4,
  226. borderColor: '#999',
  227. textAlign: 'center'
  228. },
  229. right: {
  230. marginLeft: 20
  231. },
  232. selected: {
  233. color: textLight,
  234. borderColor: colorDark,
  235. backgroundColor: colorPrimary
  236. },
  237. autoView: {
  238. flex: 1,
  239. color: textPrimary,
  240. fontSize: 14,
  241. paddingRight: 32
  242. },
  243. buttonView: {
  244. padding: 16,
  245. marginTop: 0,
  246. marginBottom: 16
  247. }
  248. })
  249. export const TopupStyle = styles;