QRScan.js 7.7 KB

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