QRScan.js 4.3 KB

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