PaymentWeb.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /**
  2. * 网页支付页面
  3. * @邠心vbe on 2022/01/10
  4. */
  5. import React, { Component } from 'react';
  6. import { View, AppState, Linking, StyleSheet, Image } from 'react-native';
  7. import * as Progress from 'react-native-progress';
  8. import WebView from 'react-native-webview';
  9. import TextView from '../../components/TextView';
  10. import { PaymentDefault } from './PaymentConfig';
  11. export default class PaymentWeb extends Component {
  12. constructor(props) {
  13. super(props);
  14. this.state = {
  15. isCallback: false,
  16. progress: 0,
  17. paymentInfo: {
  18. url: '',
  19. amount: 0,
  20. type: ''
  21. }
  22. };
  23. this.canBack = false;
  24. this.stateListener;
  25. }
  26. componentDidMount() {
  27. const param = this.props.route.params
  28. console.log('PaymentWeb', param);
  29. if (param.url) {
  30. this.setState({
  31. isCallback: true,
  32. paymentInfo: param
  33. }, () => this.init());
  34. }
  35. this.stateListener = AppState.addEventListener("change", state => {
  36. if (state == 'active' && this.state.isCallback) {
  37. //console.log("AppState", state);
  38. this.completePayment();
  39. }
  40. });
  41. /*this.props.navigation.addListener('beforeRemove', e => {
  42. if (!this.canBack) {
  43. if (this.state.isRepay) {
  44. this.onReset();
  45. e.preventDefault();
  46. } else if (this.state.isReturn) {
  47. this.canBack = true;
  48. goBack();
  49. setTimeout(() => {
  50. goBack();
  51. }, 100);
  52. } else {
  53. this.onClose();
  54. e.preventDefault();
  55. }
  56. }
  57. })*/
  58. }
  59. componentWillUnmount() {
  60. if (this.stateListener) {
  61. this.stateListener.remove();
  62. }
  63. }
  64. init() {
  65. if (!PaymentDefault.enableInnerWebView) {
  66. setTimeout(() => {
  67. Linking.openURL(this.state.paymentInfo.url)
  68. }, 500);
  69. }
  70. }
  71. completePayment() {
  72. this.setState({
  73. isCallback: false
  74. }, () => {
  75. setTimeout(() => {
  76. goBack();
  77. }, 500);
  78. });
  79. }
  80. onPostMessage(data) {
  81. console.log('WebviewMessage', data)
  82. this.completePayment();
  83. }
  84. render() {
  85. if (PaymentDefault.enableInnerWebView) {
  86. return (
  87. <View style={styles.container}>
  88. <Progress.Bar
  89. style={styles.progress}
  90. width={$vw(100)}
  91. height={2}
  92. color={colorPrimaryDark}
  93. borderWidth={0}
  94. borderRadius={0}
  95. progress={this.state.progress}/>
  96. <WebView
  97. style={ui.flex1}
  98. source={{ uri: this.state.paymentInfo.url}}
  99. mixedContentMode={'always'}
  100. applicationNameForUserAgent={'vbe_lumi/1.0'}
  101. onLoadProgress={({nativeEvent}) => this.setState({ progress: nativeEvent.progress }) }
  102. onLoadEnd={(e) => {
  103. //console.log('webview-end', e)
  104. this.setState({ progress: 0 })
  105. }}
  106. onMessage={e => this.onPostMessage(e.nativeEvent.data)}/>
  107. </View>
  108. );
  109. } else {
  110. return (
  111. <View style={styles.container}>
  112. { this.state.isCallback &&
  113. <View style={styles.center}>
  114. <View style={styles.loadingIcon}>
  115. { isIOS
  116. ? <Image
  117. style={styles.loadingIconImage}
  118. source={require('../../images/icon/loading.gif')}/>
  119. : <Progress.CircleSnail
  120. size={56}
  121. duration={667}
  122. thickness={4}
  123. color={[colorAccent]}
  124. direction={'clockwise'}
  125. spinDuration={2000}/>
  126. }
  127. </View>
  128. <EndView/>
  129. <TextView style={styles.loadingText}>{$t("nav.loading")}</TextView>
  130. </View>
  131. }
  132. </View>
  133. )
  134. }
  135. }
  136. }
  137. const styles = StyleSheet.create({
  138. container: {
  139. flex: 1,
  140. backgroundColor: pageBackground
  141. },
  142. progress: {
  143. top: 0,
  144. left: 0,
  145. zIndex: 10,
  146. position: 'absolute'
  147. },
  148. center: {
  149. flex: 1,
  150. paddingBottom: 56,
  151. alignItems: 'center',
  152. justifyContent: 'center'
  153. },
  154. loadingText: {
  155. fontSize: 12,
  156. color: textSecondary
  157. },
  158. loadingIcon: {
  159. width: 56,
  160. height: 56,
  161. overflow: 'hidden',
  162. borderRadius: 56
  163. },
  164. loadingIconImage: {
  165. width: 56,
  166. height: 56,
  167. transform: [{scale: 1.5}]
  168. }
  169. })