QRScan.js 6.8 KB

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