QRScan.js 6.9 KB

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