QRScan.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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. let sb = arr[i]?.trim();
  32. if (i == (arr.length-1)) {
  33. cid = sb;
  34. } else {
  35. if (i > 0) {
  36. bid += '-';
  37. }
  38. bid += sb;
  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, code}) => {
  57. Dialog.dismissLoading();
  58. back(false, '')
  59. Dialog.showDialog({
  60. title: 'Error',
  61. message: err,
  62. showCancel: false,
  63. callback: btn => {
  64. if (code == 5194 && btn == Dialog.BUTTON_OK) {
  65. startPage(PageList.myVehicles)
  66. }
  67. }
  68. });
  69. })
  70. } else {
  71. back(false, 'Station ID is incorrect')
  72. }
  73. } else {
  74. back(false, 'Station ID is incorrect')
  75. }
  76. }
  77. }
  78. export default class QRScan extends Component {
  79. constructor(props) {
  80. super(props);
  81. this.state={
  82. isResult: true,
  83. params: this.props.route.params
  84. }
  85. }
  86. componentDidMount() {
  87. //console.log(this.state.params);
  88. this.props.navigation.addListener('focus', () => {
  89. setTimeout(() => {
  90. this.setState({
  91. isResult: false
  92. });
  93. }, 200);
  94. });
  95. this.props.navigation.addListener('beforeRemove', (e) => {
  96. if (!this.state.isResult) {
  97. e.preventDefault();
  98. this.setState({
  99. isResult: true
  100. }, () => {
  101. setTimeout(() => {
  102. goBack();
  103. }, 300);
  104. });
  105. }
  106. });
  107. }
  108. scanResult = (msg) => {
  109. this.setState({
  110. isResult: true
  111. });
  112. console.log("result2", msg);
  113. if (msg.data.indexOf('::') > 0) {
  114. const arr = msg.data.split('::');
  115. if (arr.length == 2) {
  116. const qr = {
  117. chargeBoxId: arr[0],
  118. connectorId: arr[1]
  119. }
  120. if (this.state.params.id) {
  121. qr.sitePk = this.state.params.id
  122. }
  123. this.getChargeDetail(qr);
  124. return;
  125. }
  126. } else {
  127. const qr = {
  128. qrContent: msg.data
  129. }
  130. if (this.state.params.id) {
  131. qr.sitePk = this.state.params.id
  132. }
  133. this.getChargeDetail(qr);
  134. return;
  135. }
  136. Dialog.showDialog({
  137. title: 'Error',
  138. message: 'It\'s not a legal QR code',
  139. showCancel: false,
  140. callback: (e) => {
  141. this.setState({
  142. isResult: false
  143. });
  144. }});
  145. }
  146. getChargeDetail(qr) {
  147. console.log('===============SCAN QR===============');
  148. console.log(qr);
  149. console.log('===============SCAN QR===============');
  150. apiCharge.checkQRStatus(qr).then(res => {
  151. if (res.data && res.data.chargeBoxId) {
  152. QRResult.setResult(res.data);
  153. if (res.data.sitePk) {
  154. if (this.state.params.actionDetail) {
  155. startPage(PageList.chargeDetailPage, {stationInfo: {id: res.data.sitePk}, action: 'qr', from: PageList.home});
  156. } else {
  157. goBack();
  158. }
  159. //startPage(PageList.chargeDetail, {stationInfo: {id: res.data.sitePk}, action: 'qr'});
  160. }
  161. }
  162. }).catch(({err, code}) => {
  163. Dialog.showDialog({
  164. title: 'Error',
  165. message: err,
  166. showCancel: false,
  167. callback: (btn) => {
  168. this.setState({
  169. isResult: false
  170. });
  171. if (code == 5194 && btn == Dialog.BUTTON_OK) {
  172. startPage(PageList.myVehicles)
  173. }
  174. }
  175. });
  176. })
  177. }
  178. render() {
  179. return (
  180. <View style={styles.container}>
  181. { !this.state.isResult
  182. ? <QRCodeScanner
  183. fadeIn={true}
  184. onRead={this.scanResult}
  185. reactivate={false}
  186. reactivateTimeout={1000}
  187. cameraStyle={{ width: $width, height: $vh(100)}}
  188. containerStyle={{ width: $width, height: $vh(100)}}
  189. flashMode={RNCamera.Constants.FlashMode.off}
  190. checkAndroid6Permissions={true} />
  191. : <View style={ui.flex1}></View>
  192. /*<Image
  193. style={Styles.logo}
  194. source={require('../../images/app-logo.png')}/> */
  195. }
  196. </View>
  197. );
  198. }
  199. }
  200. const styles = StyleSheet.create({
  201. container: {
  202. alignItems: 'center',
  203. justifyContent: 'center',
  204. backgroundColor: '#000',
  205. ...StyleSheet.absoluteFillObject
  206. }
  207. })