PayNow.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /**
  2. * PayNow支付页面
  3. * @邠心vbe on 2021/05/12
  4. */
  5. import React, { Component } from 'react';
  6. import { View, Text, StyleSheet, Image, ScrollView } from 'react-native';
  7. import CameraRoll from "@react-native-community/cameraroll";
  8. import ViewShot from "react-native-view-shot";
  9. import Button from '../../components/Button';
  10. import Dialog from '../../components/Dialog';
  11. import { Balance } from '../wallet/Payment';
  12. import {check, request, openSettings, PERMISSIONS, RESULTS} from 'react-native-permissions';
  13. export default class PayNow extends Component {
  14. constructor(props) {
  15. super(props);
  16. this.state = {
  17. amount: 0,
  18. base64: ''
  19. };
  20. this.denied = true;
  21. this.viewShot = React.createRef();
  22. }
  23. componentDidMount() {
  24. if (this.props.route.params.amount) {
  25. this.setState({
  26. amount: this.props.route.params.amount,
  27. base64: 'data:image/png;base64,' + this.props.route.params.base64
  28. });
  29. }
  30. }
  31. savePhoto() {
  32. Dialog.showProgressDialog();
  33. this.viewShot.current.capture().then(uri => {
  34. Dialog.dismissLoading();
  35. CameraRoll.save(uri, { type: 'photo' });
  36. console.log("Save Photo with ", uri);
  37. toastShort('Save to gallery successfully');
  38. }).catch(err => {
  39. toastShort(err)
  40. Dialog.dismissLoading();
  41. });
  42. }
  43. getPermission() {
  44. request(
  45. isIOS
  46. ? PERMISSIONS.IOS.PHOTO_LIBRARY_ADD_ONLY
  47. : PERMISSIONS.ANDROID.WRITE_EXTERNAL_STORAGE)
  48. .then(res => {
  49. console.log('getPermission', res)
  50. this.checkPermission();
  51. }).catch(err => {
  52. console.info('getPermission', err)
  53. });
  54. }
  55. checkPermission() {
  56. check(
  57. isIOS
  58. ? PERMISSIONS.IOS.PHOTO_LIBRARY_ADD_ONLY
  59. : PERMISSIONS.ANDROID.WRITE_EXTERNAL_STORAGE)
  60. .then(res => {
  61. switch (res) {
  62. case RESULTS.UNAVAILABLE:
  63. console.log('此功能不可用(在此设备上/在此上下文中)');
  64. if (isIOS) {
  65. this.savePhoto();
  66. } else {
  67. toastShort('Save to gallery failed');
  68. }
  69. break;
  70. case RESULTS.DENIED:
  71. console.log('权限未被请求/被拒绝,但可以请求');
  72. if (this.denied) {
  73. this.denied = false;
  74. this.getPermission();
  75. } else {
  76. this.denied = true;
  77. toastShort('Save to gallery failed');
  78. }
  79. break;
  80. case RESULTS.LIMITED:
  81. console.log('权限是有限的:有些操作是可能的');
  82. this.savePhoto();
  83. break;
  84. case RESULTS.GRANTED:
  85. console.log('许可被授予');
  86. this.savePhoto();
  87. break;
  88. case RESULTS.BLOCKED:
  89. console.log('权限被拒绝,不再可请求');
  90. Dialog.showDialog({
  91. title: 'Error',
  92. message: 'Can not save images, Please grant storage or gallery permissions.',
  93. ok: 'SETTING',
  94. callback: btn => {
  95. if (btn == Dialog.BUTTON_OK) {
  96. console.log('ok');
  97. openSettings().catch(() => console.warn('cannot open settings'));
  98. }
  99. }
  100. });
  101. //toastShort('Save to gallery failed');
  102. break;
  103. }
  104. }).catch(err => {
  105. })
  106. }
  107. render() {
  108. return (
  109. <ScrollView
  110. style={styles.container}>
  111. <View style={styles.headerView}>
  112. <Balance/>
  113. </View>
  114. <ViewShot
  115. ref={this.viewShot}
  116. style={styles.contentView}
  117. options={{
  118. format: "jpg",
  119. quality: 1
  120. }}>
  121. { this.state.base64
  122. ? <Image
  123. style={styles.qrCode}
  124. source={{uri: this.state.base64}}/>
  125. : <View style={styles.qrCode}></View>
  126. }
  127. </ViewShot>
  128. <Text style={styles.title}>Top Up Amount: {currency + ' ' + this.state.amount}</Text>
  129. <Text style={styles.tips}>Please save the QR Code, and use your bank app to scan the QR Code.</Text>
  130. <Text style={styles.tips}>Once the payment is completed, the top up amount will be credited into your credit wallet automatically.</Text>
  131. <View style={styles.button2}>
  132. <Button
  133. text='Save QR Code to Gallery'
  134. elevation={1.5}
  135. onClick={() => {
  136. this.checkPermission();
  137. }}/>
  138. </View>
  139. <View style={styles.button}>
  140. <Button
  141. text='Payment Completed'
  142. elevation={1.5}
  143. onClick={() => {
  144. goBack();
  145. }}/>
  146. </View>
  147. </ScrollView>
  148. );
  149. }
  150. }
  151. const styles = StyleSheet.create({
  152. container: {
  153. flex: 1,
  154. backgroundColor: '#F5F5F5'
  155. },
  156. headerView: {
  157. paddingBottom: 76,
  158. //backgroundColor: colorAccent
  159. },
  160. contentView: {
  161. marginTop: -88
  162. },
  163. qrCode: {
  164. margin: 16,
  165. width: $vw(100) - 32,
  166. height: $vw(100) - 32,
  167. borderRadius: 4,
  168. backgroundColor: '#efefef'
  169. },
  170. title: {
  171. color: '#333',
  172. fontSize: 18,
  173. paddingBottom: 16,
  174. textAlign: 'center'
  175. },
  176. tips: {
  177. color: '#333',
  178. padding: 16,
  179. },
  180. button: {
  181. margin: 16,
  182. paddingBottom: 16
  183. },
  184. button2: {
  185. marginLeft: 16,
  186. marginRight: 16
  187. }
  188. })