QRScan.js 7.4 KB

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