QRScan.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. this.props.navigation.addListener('focus', () => {
  89. this.setState({
  90. isResult: false
  91. });
  92. });
  93. }
  94. scanResult = (msg) => {
  95. this.setState({
  96. isResult: true
  97. });
  98. console.log("result2", msg);
  99. if (msg.data.indexOf('::') > 0) {
  100. const arr = msg.data.split('::');
  101. if (arr.length == 2) {
  102. const qr = {
  103. chargeBoxId: arr[0],
  104. connectorId: arr[1]
  105. }
  106. if (this.state.params.id) {
  107. qr.sitePk = this.state.params.id
  108. }
  109. this.getChargeDetail(qr);
  110. return;
  111. }
  112. }
  113. Dialog.showDialog({
  114. title: 'Error',
  115. message: 'It\'s not a legal QR code',
  116. showCancel: false,
  117. callback: (e) => {
  118. this.setState({
  119. isResult: false
  120. });
  121. }});
  122. }
  123. getChargeDetail(qr) {
  124. apiCharge.checkQRStatus(qr).then(res => {
  125. if (res.data && res.data.chargeBoxId) {
  126. QRResult.setResult(res.data);
  127. if (res.data.sitePk) {
  128. goBack();
  129. startPage(PageList.chargeDetail, {stationInfo: {id: res.data.sitePk}, action: 'qr'});
  130. }
  131. }
  132. }).catch(err => {
  133. Dialog.showDialog({
  134. title: 'Error',
  135. message: err,
  136. showCancel: false,
  137. callback: (e) => {
  138. this.setState({
  139. isResult: false
  140. });
  141. }});
  142. })
  143. }
  144. render() {
  145. return (
  146. <View style={styles.container}>
  147. { !this.state.isResult
  148. ? <QRCodeScanner
  149. fadeIn={false}
  150. onRead={this.scanResult}
  151. reactivate={false}
  152. reactivateTimeout={1000}
  153. cameraStyle={{ width: $width, height: $vht(100)}}
  154. containerStyle={{ width: $width, height: $vht(100)}}
  155. flashMode={RNCamera.Constants.FlashMode.off}
  156. checkAndroid6Permissions={true} />
  157. : <Image
  158. style={Styles.logo}
  159. source={require('../../images/tool-logo.png')}/>
  160. }
  161. </View>
  162. );
  163. }
  164. }
  165. const styles = StyleSheet.create({
  166. container: {
  167. alignItems: 'center',
  168. justifyContent: 'center',
  169. backgroundColor: '#242B32',
  170. ...StyleSheet.absoluteFillObject
  171. }
  172. })