QRScan.js 6.8 KB

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