QRScan.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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. if (this.state.params.actionDetail) {
  124. startPage(PageList.chargeDetailPage, {stationInfo: {id: res.data.sitePk}, action: 'qr', from: PageList.home});
  125. } else {
  126. goBack();
  127. }
  128. //startPage(PageList.chargeDetail, {stationInfo: {id: res.data.sitePk}, action: 'qr'});
  129. }
  130. }
  131. }).catch(err => {
  132. Dialog.showDialog({
  133. title: 'Error',
  134. message: err,
  135. showCancel: false,
  136. callback: (e) => {
  137. this.setState({
  138. isResult: false
  139. });
  140. }});
  141. })
  142. }
  143. render() {
  144. return (
  145. <View style={styles.container}>
  146. { !this.state.isResult
  147. ? <QRCodeScanner
  148. fadeIn={false}
  149. onRead={this.scanResult}
  150. reactivate={false}
  151. reactivateTimeout={1000}
  152. cameraStyle={{ width: $width, height: $vh(100)}}
  153. containerStyle={{ width: $width, height: $vh(100)}}
  154. flashMode={RNCamera.Constants.FlashMode.off}
  155. checkAndroid6Permissions={true} />
  156. : <Image
  157. style={Styles.logo}
  158. source={require('../../images/app-logo.png')}/>
  159. }
  160. </View>
  161. );
  162. }
  163. }
  164. const styles = StyleSheet.create({
  165. container: {
  166. alignItems: 'center',
  167. justifyContent: 'center',
  168. backgroundColor: '#242B32',
  169. ...StyleSheet.absoluteFillObject
  170. }
  171. })