QRScan.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. /**
  2. * 扫描二维码
  3. * @邠心vbe on 2021/03/24
  4. */
  5. import React, { Component } from 'react'
  6. import { Pressable, 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. import app from '../../../app.json';
  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. let sb = arr[i]?.trim();
  33. if (i == (arr.length-1)) {
  34. cid = sb;
  35. } else {
  36. if (i > 0) {
  37. bid += '-';
  38. }
  39. bid += sb;
  40. }
  41. }
  42. const qr = {
  43. sitePk: sitePk,
  44. chargeBoxId: bid,
  45. connectorId: cid
  46. }
  47. console.log('====================================');
  48. console.log(qr);
  49. console.log('====================================');
  50. Dialog.showProgressDialog();
  51. apiCharge.checkQRStatus(qr).then(res => {
  52. Dialog.dismissLoading();
  53. if (res.data && res.data.chargeBoxId) {
  54. QRResult.setResult(res.data);
  55. back(true)
  56. }
  57. }).catch(({err, code}) => {
  58. Dialog.dismissLoading();
  59. back(false, '')
  60. Dialog.showDialog({
  61. title: 'Error',
  62. message: err,
  63. showCancel: false,
  64. callback: btn => {
  65. if (code == 5194 && btn == Dialog.BUTTON_OK) {
  66. startPage(PageList.myVehicles)
  67. }
  68. }
  69. });
  70. })
  71. } else {
  72. back(false, 'Station ID is incorrect')
  73. }
  74. } else {
  75. back(false, 'Station ID is incorrect')
  76. }
  77. }
  78. }
  79. export default class QRScan extends Component {
  80. constructor(props) {
  81. super(props);
  82. this.state={
  83. isResult: true,
  84. params: this.props.route.params,
  85. flashLight: false
  86. }
  87. }
  88. componentDidMount() {
  89. //console.log(this.state.params);
  90. this.props.navigation.addListener('focus', () => {
  91. setTimeout(() => {
  92. this.setState({
  93. isResult: false
  94. });
  95. }, 200);
  96. });
  97. this.props.navigation.addListener('beforeRemove', (e) => {
  98. if (!this.state.isResult) {
  99. e.preventDefault();
  100. this.setState({
  101. isResult: true
  102. }, () => {
  103. setTimeout(() => {
  104. goBack();
  105. }, 300);
  106. });
  107. }
  108. });
  109. }
  110. scanResult = (msg) => {
  111. this.setState({
  112. isResult: true
  113. });
  114. console.log("result2", msg);
  115. if (msg.data.indexOf('::') > 0) {
  116. const arr = msg.data.split('::');
  117. if (arr.length == 2) {
  118. const qr = {
  119. chargeBoxId: arr[0],
  120. connectorId: arr[1]
  121. }
  122. if (this.state.params.id) {
  123. qr.sitePk = this.state.params.id
  124. }
  125. this.getChargeDetail(qr);
  126. return;
  127. }
  128. } else {
  129. const qr = {
  130. qrContent: msg.data
  131. }
  132. if (this.state.params.id) {
  133. qr.sitePk = this.state.params.id
  134. }
  135. this.getChargeDetail(qr);
  136. return;
  137. }
  138. Dialog.showDialog({
  139. title: 'Error',
  140. message: 'It\'s not a legal QR code',
  141. showCancel: false,
  142. callback: (e) => {
  143. this.setState({
  144. isResult: false
  145. });
  146. }});
  147. }
  148. getChargeDetail(qr) {
  149. console.log('===============SCAN QR===============');
  150. console.log(qr);
  151. console.log('===============SCAN QR===============');
  152. apiCharge.checkQRStatus(qr).then(res => {
  153. if (res.data && res.data.chargeBoxId) {
  154. QRResult.setResult(res.data);
  155. if (res.data.sitePk) {
  156. if (this.state.params.actionDetail) {
  157. startPage(PageList.chargeDetailPage, {stationInfo: {id: res.data.sitePk}, action: 'qr', from: PageList.home});
  158. } else {
  159. goBack();
  160. }
  161. //startPage(PageList.chargeDetail, {stationInfo: {id: res.data.sitePk}, action: 'qr'});
  162. }
  163. }
  164. }).catch(({err, code}) => {
  165. Dialog.showDialog({
  166. title: 'Error',
  167. message: err,
  168. showCancel: false,
  169. callback: (btn) => {
  170. this.setState({
  171. isResult: false
  172. });
  173. if (code == 5194 && btn == Dialog.BUTTON_OK && app.vehicle.enable) {
  174. startPage(app.vehicle.newVersionPage ? PageList.vehiclesListV2 : PageList.myVehicles)
  175. }
  176. }
  177. });
  178. })
  179. }
  180. switchFlash() {
  181. this.setState({
  182. flashLight: !this.state.flashLight
  183. })
  184. }
  185. render() {
  186. return (
  187. <View style={styles.container}>
  188. { !this.state.isResult
  189. ? <QRCodeScanner
  190. fadeIn={true}
  191. onRead={this.scanResult}
  192. reactivate={false}
  193. reactivateTimeout={1000}
  194. cameraStyle={{ width: $width, height: $vh(100)}}
  195. containerStyle={{ width: $width, height: $vh(100)}}
  196. flashMode={this.state.flashLight ? RNCamera.Constants.FlashMode.torch : RNCamera.Constants.FlashMode.off}
  197. checkAndroid6Permissions={true} />
  198. : <View style={ui.flex1}></View>
  199. /*<Image
  200. style={Styles.logo}
  201. source={require('../../images/app-logo.png')}/> */
  202. }
  203. { !this.state.isResult &&
  204. <Pressable
  205. style={styles.flashLight}
  206. onPress={() => this.switchFlash()}>
  207. <MaterialIcons
  208. name={this.state.flashLight ? "flashlight-on" : "flashlight-off"}
  209. size={36}
  210. color="#fff"/>
  211. </Pressable>
  212. }
  213. </View>
  214. );
  215. }
  216. }
  217. const styles = StyleSheet.create({
  218. container: {
  219. alignItems: 'center',
  220. justifyContent: 'center',
  221. backgroundColor: '#000',
  222. ...StyleSheet.absoluteFillObject
  223. },
  224. flashLight: {
  225. bottom: 90,
  226. zIndex: 2,
  227. opacity: 0.7,
  228. padding: 8,
  229. position: 'absolute'
  230. }
  231. })