QRScan.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /**
  2. * 扫描二维码
  3. * @邠心vbe on 2021/03/24
  4. */
  5. import React, { Component } from 'react'
  6. import { StyleSheet, View } from 'react-native'
  7. import QRCodeScanner from 'react-native-qrcode-scanner';
  8. import { RNCamera } from 'react-native-camera';
  9. import apiCharge from '../../api/apiCharge';
  10. import { PageList } from '../Router';
  11. import Dialog from '../../components/Dialog';
  12. export const QRResult = {
  13. haveResult: () => {
  14. return global.QrCodeResult && global.QrCodeResult.connectorPk;
  15. },
  16. setResult: (info) => {
  17. global.QrCodeResult = info;
  18. },
  19. getResult: () => {
  20. return global.QrCodeResult;
  21. },
  22. clearResult: () => {
  23. global.QrCodeResult = {};
  24. },
  25. applyInputStation: (text, sitePk, back) => {
  26. if (text.indexOf('-') > 0) {
  27. const arr = text.split('-');
  28. if (arr.length >= 2) {
  29. let bid = '', cid = '';
  30. for (let i = 0; i < arr.length; i++) {
  31. if (i == (arr.length-1)) {
  32. cid = arr[i];
  33. } else {
  34. if (i > 0) {
  35. bid += '-';
  36. }
  37. bid += arr[i];
  38. }
  39. }
  40. const qr = {
  41. sitePk: sitePk,
  42. chargeBoxId: bid,
  43. connectorId: cid
  44. }
  45. console.log('====================================');
  46. console.log(qr);
  47. console.log('====================================');
  48. Dialog.showProgressDialog();
  49. apiCharge.checkQRStatus(qr).then(res => {
  50. Dialog.dismissLoading();
  51. if (res.data && res.data.chargeBoxId) {
  52. QRResult.setResult(res.data);
  53. back(true)
  54. }
  55. }).catch(err => {
  56. Dialog.dismissLoading();
  57. back(false, '')
  58. Dialog.showDialog({
  59. title: 'Error',
  60. message: err,
  61. showCancel: false
  62. });
  63. })
  64. } else {
  65. back(false, 'Station ID is incorrect')
  66. }
  67. } else {
  68. back(false, 'Station ID is incorrect')
  69. }
  70. }
  71. }
  72. export default class QRScan extends Component {
  73. constructor(props) {
  74. super(props);
  75. this.state={
  76. isResult: true,
  77. params: this.props.route.params
  78. }
  79. }
  80. componentDidMount() {
  81. //console.log(this.state.params);
  82. this.props.navigation.addListener('focus', () => {
  83. setTimeout(() => {
  84. this.setState({
  85. isResult: false
  86. });
  87. }, 200);
  88. });
  89. this.props.navigation.addListener('beforeRemove', (e) => {
  90. if (!this.state.isResult) {
  91. e.preventDefault();
  92. this.setState({
  93. isResult: true
  94. }, () => {
  95. setTimeout(() => {
  96. goBack();
  97. }, 300);
  98. });
  99. }
  100. });
  101. }
  102. scanResult = (msg) => {
  103. this.setState({
  104. isResult: true
  105. });
  106. console.log("result2", msg);
  107. if (msg.data.indexOf('::') > 0) {
  108. const arr = msg.data.split('::');
  109. if (arr.length == 2) {
  110. const qr = {
  111. chargeBoxId: arr[0],
  112. connectorId: arr[1]
  113. }
  114. if (this.state.params.id) {
  115. qr.sitePk = this.state.params.id
  116. }
  117. this.getChargeDetail(qr);
  118. return;
  119. }
  120. }
  121. Dialog.showDialog({
  122. title: 'Error',
  123. message: 'It\'s not a legal QR code',
  124. showCancel: false,
  125. callback: (e) => {
  126. this.setState({
  127. isResult: false
  128. });
  129. }});
  130. }
  131. getChargeDetail(qr) {
  132. apiCharge.checkQRStatus(qr).then(res => {
  133. if (res.data && res.data.chargeBoxId) {
  134. QRResult.setResult(res.data);
  135. if (res.data.sitePk) {
  136. if (this.state.params.actionDetail) {
  137. startPage(PageList.chargeDetailPage, {stationInfo: {id: res.data.sitePk}, action: 'qr', from: PageList.home});
  138. } else {
  139. goBack();
  140. }
  141. //startPage(PageList.chargeDetail, {stationInfo: {id: res.data.sitePk}, action: 'qr'});
  142. }
  143. }
  144. }).catch(err => {
  145. Dialog.showDialog({
  146. title: 'Error',
  147. message: err,
  148. showCancel: false,
  149. callback: (e) => {
  150. this.setState({
  151. isResult: false
  152. });
  153. }
  154. });
  155. })
  156. }
  157. render() {
  158. return (
  159. <View style={styles.container}>
  160. { !this.state.isResult
  161. ? <QRCodeScanner
  162. fadeIn={true}
  163. onRead={this.scanResult}
  164. reactivate={false}
  165. reactivateTimeout={1000}
  166. cameraStyle={{ width: $width, height: $vh(100)}}
  167. containerStyle={{ width: $width, height: $vh(100)}}
  168. flashMode={RNCamera.Constants.FlashMode.off}
  169. checkAndroid6Permissions={true} />
  170. : <View style={ui.flex1}></View>
  171. /*<Image
  172. style={Styles.logo}
  173. source={require('../../images/app-logo.png')}/>*/
  174. }
  175. </View>
  176. );
  177. }
  178. }
  179. const styles = StyleSheet.create({
  180. container: {
  181. alignItems: 'center',
  182. justifyContent: 'center',
  183. backgroundColor: '#000',
  184. ...StyleSheet.absoluteFillObject
  185. }
  186. })