QRScan.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. /**
  2. * 扫描二维码
  3. * @邠心vbe on 2021/03/24
  4. */
  5. import React, { Component, useState } 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. import Button from '../../components/Button';
  14. import TextView from '../../components/TextView';
  15. import { EnterStationDialog } from '../chargeV2/Charging';
  16. import QRResult from './QRResult';
  17. // 函数组件:QR码扫描器
  18. const QRScanner = ({ onCodeScanned, isActive }) => {
  19. const [flashOn, switchFlash] = useState(false);
  20. return (
  21. isActive
  22. ? <>
  23. <QRCodeScanner
  24. fadeIn={true}
  25. onRead={onCodeScanned}
  26. reactivate={false}
  27. reactivateTimeout={1000}
  28. cameraStyle={{ width: $width, height: $vh(100)}}
  29. containerStyle={{ width: $width, height: $vh(100)}}
  30. flashMode={flashOn ? RNCamera.Constants.FlashMode.torch : RNCamera.Constants.FlashMode.off}
  31. checkAndroid6Permissions={true} />
  32. <Pressable
  33. style={styles.flashLight}
  34. onPress={() => switchFlash(!flashOn)}>
  35. <MaterialIcons
  36. name={flashOn ? "flashlight-on" : "flashlight-off"}
  37. size={36}
  38. color="#fff"/>
  39. </Pressable>
  40. </>
  41. : <View style={styles.tipsScreen}>
  42. {/* <TextView style={styles.tipsText}>
  43. Camera access has been denied. Please enable it in your device settings.
  44. </TextView> */}
  45. </View>
  46. );
  47. };
  48. export default class QRScan extends Component {
  49. constructor(props) {
  50. super(props);
  51. this.state={
  52. isResult: true,
  53. stationId: "",
  54. params: this.props.route.params,
  55. showInputStation: false
  56. }
  57. }
  58. componentDidMount() {
  59. this.props.navigation.addListener('focus', () => {
  60. setTimeout(() => {
  61. this.setState({
  62. isResult: false
  63. });
  64. }, 200);
  65. });
  66. this.props.navigation.addListener('beforeRemove', (e) => {
  67. if (!this.state.isResult) {
  68. e.preventDefault();
  69. this.setState({
  70. isResult: true
  71. }, () => {
  72. setTimeout(() => {
  73. goBack();
  74. }, 300);
  75. });
  76. }
  77. });
  78. }
  79. scanResult = (msg) => {
  80. this.setState({
  81. isResult: true
  82. });
  83. console.log("result2", msg);
  84. if (msg.data.indexOf('::') > 0) {
  85. const arr = msg.data.split('::');
  86. if (arr.length == 2) {
  87. const qr = {
  88. chargeBoxId: arr[0],
  89. connectorId: arr[1]
  90. }
  91. if (this.state.params.id) {
  92. qr.sitePk = this.state.params.id
  93. }
  94. this.getChargeDetail(qr);
  95. return;
  96. }
  97. } else {
  98. const qr = {
  99. qrContent: msg.data
  100. }
  101. if (this.state.params.id) {
  102. qr.sitePk = this.state.params.id
  103. }
  104. this.getChargeDetail(qr);
  105. return;
  106. }
  107. Dialog.showDialog({
  108. title: 'Error',
  109. message: 'It\'s not a legal QR code',
  110. showCancel: false,
  111. callback: (e) => {
  112. this.setState({
  113. isResult: false
  114. });
  115. }});
  116. }
  117. getChargeDetail(qr) {
  118. console.log('===============SCAN QR===============');
  119. console.log(qr);
  120. console.log('===============SCAN QR===============');
  121. apiCharge.checkQRStatus(qr).then(res => {
  122. if (res.data && res.data.chargeBoxId) {
  123. QRResult.setResult(res.data);
  124. if (res.data.sitePk) {
  125. if (this.state.params.actionDetail) {
  126. startPage(PageList.chargeDetailPage, {stationInfo: {id: res.data.sitePk}, action: 'qr', from: PageList.home});
  127. } else {
  128. goBack();
  129. }
  130. //startPage(PageList.chargeDetail, {stationInfo: {id: res.data.sitePk}, action: 'qr'});
  131. }
  132. }
  133. }).catch(({err, code}) => {
  134. Dialog.showDialog({
  135. title: 'Error',
  136. message: err,
  137. showCancel: false,
  138. callback: (btn) => {
  139. this.setState({
  140. isResult: false
  141. });
  142. if (code == 5194 && btn == Dialog.BUTTON_OK && app.vehicle.enable) {
  143. startPage(app.vehicle.newVersionPage ? PageList.vehiclesListV2 : PageList.myVehicles)
  144. }
  145. }
  146. });
  147. })
  148. }
  149. switchFlash() {
  150. this.setState({
  151. flashLight: !this.state.flashLight
  152. })
  153. }
  154. onEnterStation(visible) {
  155. this.setState({
  156. stationId: "",
  157. isResult: visible,
  158. showInputStation: visible
  159. })
  160. }
  161. enterStatioinId() {
  162. if (QRResult.haveResult()) {
  163. const result = QRResult.getResult()
  164. if (result.sitePk) {
  165. if (this.state.params.actionDetail) {
  166. startPage(PageList.chargeDetailPage, {stationInfo: {id: result.sitePk}, action: 'qr', from: PageList.home});
  167. } else {
  168. goBack();
  169. }
  170. //startPage(PageList.chargeDetail, {stationInfo: {id: res.data.sitePk}, action: 'qr'});
  171. }
  172. }
  173. }
  174. render() {
  175. return (
  176. <View style={styles.container}>
  177. <QRScanner
  178. onCodeScanned={this.scanResult}
  179. isActive={!this.state.isResult}
  180. />
  181. <Button
  182. style={styles.btnStationInput}
  183. text={$t('charging.enterStationId')}
  184. textColor={"rgba(255,255,255,.8)"}
  185. onClick={() => this.onEnterStation(true)}/>
  186. <EnterStationDialog
  187. visible={this.state.showInputStation}
  188. stationId={this.state.params?.id}
  189. onConfirm={() => this.enterStatioinId()}
  190. onClose={() => this.onEnterStation(false)}
  191. />
  192. </View>
  193. );
  194. }
  195. }
  196. const styles = StyleSheet.create({
  197. container: {
  198. alignItems: 'center',
  199. justifyContent: 'center',
  200. backgroundColor: '#000',
  201. ...StyleSheet.absoluteFillObject
  202. },
  203. tipsScreen: {
  204. flex: 1,
  205. padding: 48,
  206. alignItems: "center",
  207. justifyContent: "center",
  208. },
  209. tipsText: {
  210. color: textLight,
  211. fontSize: 14,
  212. textAlign: "center"
  213. },
  214. flashLight: {
  215. bottom: 140,
  216. zIndex: 2,
  217. opacity: 0.7,
  218. padding: 8,
  219. position: 'absolute'
  220. },
  221. btnStationInput: {
  222. left: 16,
  223. right: 16,
  224. bottom: 40,
  225. zIndex: 2,
  226. position: 'absolute',
  227. backgroundColor: "rgba(0,0,0,.4)"
  228. },
  229. })