QRScan.js 4.6 KB

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