PayPerUse.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. /**
  2. * PayPerUse PayNow支付页面
  3. * @邠心vbe on 2022/01/10
  4. */
  5. import React, { Component } from 'react';
  6. import { View, Text, StyleSheet, Image, ScrollView } from 'react-native';
  7. import {CameraRoll} from "@react-native-camera-roll/camera-roll";
  8. import ViewShot from "react-native-view-shot";
  9. import Button from '../../components/Button';
  10. import Dialog from '../../components/Dialog';
  11. import {check, request, openSettings, PERMISSIONS, RESULTS} from 'react-native-permissions';
  12. export default class PayPerUse extends Component {
  13. constructor(props) {
  14. super(props);
  15. this.state = {
  16. amount: 0,
  17. base64: ''
  18. };
  19. this.denied = true;
  20. this.viewShot = React.createRef();
  21. }
  22. componentDidMount() {
  23. if (this.props.route.params.amount) {
  24. this.setState({
  25. amount: this.props.route.params.amount,
  26. base64: 'data:image/png;base64,' + this.props.route.params.base64
  27. });
  28. }
  29. }
  30. savePhoto() {
  31. Dialog.showProgressDialog();
  32. this.viewShot.current.capture().then(uri => {
  33. Dialog.dismissLoading();
  34. CameraRoll.save(uri, { type: 'photo' });
  35. console.log("Save Photo with ", uri);
  36. toastShort($t('payment.successSave2gallery'));
  37. }).catch(errs => {
  38. toastShort(errs)
  39. Dialog.dismissLoading();
  40. });
  41. }
  42. getPermission() {
  43. request(
  44. isIOS
  45. ? PERMISSIONS.IOS.PHOTO_LIBRARY_ADD_ONLY
  46. : PERMISSIONS.ANDROID.WRITE_EXTERNAL_STORAGE)
  47. .then(res => {
  48. console.log('getPermission', res)
  49. this.checkPermission();
  50. }).catch(errs => {
  51. console.info('getPermission', errs)
  52. });
  53. }
  54. checkPermission() {
  55. check(
  56. isIOS
  57. ? PERMISSIONS.IOS.PHOTO_LIBRARY_ADD_ONLY
  58. : PERMISSIONS.ANDROID.WRITE_EXTERNAL_STORAGE)
  59. .then(res => {
  60. switch (res) {
  61. case RESULTS.UNAVAILABLE:
  62. console.log('此功能不可用(在此设备上/在此上下文中)');
  63. if (isIOS) {
  64. this.savePhoto();
  65. } else {
  66. toastShort($t('payment.errSave2gallery'));
  67. }
  68. break;
  69. case RESULTS.DENIED:
  70. console.log('权限未被请求/被拒绝,但可以请求');
  71. if (this.denied) {
  72. this.denied = false;
  73. this.getPermission();
  74. } else {
  75. this.denied = true;
  76. toastShort($t('payment.errSave2gallery'));
  77. }
  78. break;
  79. case RESULTS.LIMITED:
  80. console.log('权限是有限的:有些操作是可能的');
  81. this.savePhoto();
  82. break;
  83. case RESULTS.GRANTED:
  84. console.log('许可被授予');
  85. this.savePhoto();
  86. break;
  87. case RESULTS.BLOCKED:
  88. console.log('权限被拒绝,不再可请求');
  89. Dialog.showDialog({
  90. title: $t('common.error'),
  91. message: $t('payment.errSave2galleryPermission'),
  92. ok: $t('payment.btnSetting'),
  93. callback: btn => {
  94. if (btn == Dialog.BUTTON_OK) {
  95. console.log('ok');
  96. openSettings().catch(() => console.warn('cannot open settings'));
  97. }
  98. }
  99. });
  100. //toastShort('Save to gallery failed');
  101. break;
  102. }
  103. }).catch(err1 => {
  104. })
  105. }
  106. render() {
  107. return (
  108. <ScrollView
  109. style={styles.container}>
  110. <View style={styles.headerView}>
  111. <Text></Text>
  112. </View>
  113. <ViewShot
  114. ref={this.viewShot}
  115. style={styles.contentView}
  116. options={{
  117. format: "jpg",
  118. quality: 1
  119. }}>
  120. { this.state.base64
  121. ? <Image
  122. style={styles.qrCode}
  123. source={{uri: this.state.base64}}/>
  124. : <View style={styles.qrCode}></View>
  125. }
  126. </ViewShot>
  127. <Text style={styles.title}>{$t('payment.labelAmount')} {currency + ' ' + this.state.amount}</Text>
  128. <Text style={styles.tips}>{$t('payment.tipsPayPerUseSave')}</Text>
  129. <Text style={styles.tips}>{$t('payment.tipsPayPerUseCharge')}</Text>
  130. <Text style={styles.tips}>{$t('payment.tipsPayPerUse')}</Text>
  131. <View style={styles.button}>
  132. <Button
  133. text={$t('payment.btnSave2gallery')}
  134. elevation={1.5}
  135. onClick={() => {
  136. this.checkPermission();
  137. }}/>
  138. </View>
  139. <View style={styles.button2}>
  140. <Button
  141. text={$t('payment.paymentCompleted')}
  142. elevation={1.5}
  143. onClick={() => {
  144. goBack();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: '#F0F0F0'
  169. },
  170. title: {
  171. color: textPrimary,
  172. fontSize: 18,
  173. paddingBottom: 16,
  174. textAlign: 'center'
  175. },
  176. tips: {
  177. color: textPrimary,
  178. ...$padding(0, 16, 16),
  179. },
  180. button: {
  181. margin: 16,
  182. //paddingBottom: 16
  183. },
  184. button2: {
  185. marginLeft: 16,
  186. marginRight: 16,
  187. }
  188. })