PaymentWeb.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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 = undefined;
  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. if (!PaymentDefault.enableInnerWebView) {
  36. this.stateListener = AppState.addEventListener("change", state => {
  37. if (state == 'active' && this.state.isCallback) {
  38. //console.log("AppState", state);
  39. this.completePayment();
  40. }
  41. });
  42. }
  43. /*this.props.navigation.addListener('beforeRemove', e => {
  44. if (!this.canBack) {
  45. if (this.state.isRepay) {
  46. this.onReset();
  47. e.preventDefault();
  48. } else if (this.state.isReturn) {
  49. this.canBack = true;
  50. goBack();
  51. setTimeout(() => {
  52. goBack();
  53. }, 100);
  54. } else {
  55. this.onClose();
  56. e.preventDefault();
  57. }
  58. }
  59. })*/
  60. }
  61. componentWillUnmount() {
  62. if (this.stateListener) {
  63. this.stateListener.remove();
  64. }
  65. }
  66. init() {
  67. if (!PaymentDefault.enableInnerWebView) {
  68. setTimeout(() => {
  69. Linking.openURL(this.state.paymentInfo.url)
  70. }, 500);
  71. }
  72. }
  73. completePayment() {
  74. this.setState({
  75. isCallback: false
  76. }, () => {
  77. setTimeout(() => {
  78. goBack();
  79. }, 500);
  80. });
  81. }
  82. onPostMessage(data) {
  83. console.log('WebviewMessage', data)
  84. this.completePayment();
  85. }
  86. render() {
  87. if (PaymentDefault.enableInnerWebView) {
  88. return (
  89. <View style={styles.container}>
  90. <Progress.Bar
  91. style={styles.progress}
  92. width={$vw(100)}
  93. height={2}
  94. color={colorPrimaryDark}
  95. borderWidth={0}
  96. borderRadius={0}
  97. progress={this.state.progress}/>
  98. <WebView
  99. style={ui.flex1}
  100. source={{ uri: this.state.paymentInfo.url}}
  101. mixedContentMode={'always'}
  102. applicationNameForUserAgent={'vbe_lumi/1.0'}
  103. onLoadProgress={({nativeEvent}) => this.setState({ progress: nativeEvent.progress }) }
  104. onLoadEnd={(e) => {
  105. //console.log('webview-end', e)
  106. this.setState({ progress: 0 })
  107. }}
  108. onMessage={e => this.onPostMessage(e.nativeEvent.data)}/>
  109. </View>
  110. );
  111. } else {
  112. return (
  113. <View style={styles.container}>
  114. { this.state.isCallback &&
  115. <View style={styles.center}>
  116. <View style={styles.loadingIcon}>
  117. { isIOS
  118. ? <Image
  119. style={styles.loadingIconImage}
  120. source={require('../../images/icon/loading.gif')}/>
  121. : <Progress.CircleSnail
  122. size={56}
  123. duration={667}
  124. thickness={4}
  125. color={[colorAccent]}
  126. direction={'clockwise'}
  127. spinDuration={2000}/>
  128. }
  129. </View>
  130. <EndView/>
  131. <TextView style={styles.loadingText}>{$t("nav.loading")}</TextView>
  132. </View>
  133. }
  134. </View>
  135. )
  136. }
  137. }
  138. }
  139. const styles = StyleSheet.create({
  140. container: {
  141. flex: 1,
  142. backgroundColor: pageBackground
  143. },
  144. progress: {
  145. top: 0,
  146. left: 0,
  147. zIndex: 10,
  148. position: 'absolute'
  149. },
  150. center: {
  151. flex: 1,
  152. paddingBottom: 56,
  153. alignItems: 'center',
  154. justifyContent: 'center'
  155. },
  156. loadingText: {
  157. fontSize: 12,
  158. color: textSecondary
  159. },
  160. loadingIcon: {
  161. width: 56,
  162. height: 56,
  163. overflow: 'hidden',
  164. borderRadius: 56
  165. },
  166. loadingIconImage: {
  167. width: 56,
  168. height: 56,
  169. transform: [{scale: 1.5}]
  170. }
  171. })