QRScan.js 5.2 KB

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