QRScan.js 5.0 KB

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