QRScan.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. /**
  2. * 扫描二维码
  3. * @邠心vbe on 2021/03/24
  4. * 升级到 react-native-vision-camera
  5. */
  6. /* eslint-disable no-undef */
  7. import React, { Component } from 'react'
  8. import { StyleSheet, View, Vibration, AppState } from 'react-native'
  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 { EnterStationDialog } from '../chargeV2/Charging';
  15. import QRResult from './QRResult';
  16. import utils from '../../utils/utils';
  17. import QRScanner from './QRScanner';
  18. const showInputStationView = true;
  19. export default class QRScan extends Component {
  20. constructor(props) {
  21. super(props);
  22. this.state={
  23. isResult: true,
  24. stationId: "",
  25. params: this.props.route.params,
  26. showInputStation: false
  27. }
  28. this.scaning = false;
  29. this.stateListener;
  30. }
  31. componentDidMount() {
  32. this.props.navigation.addListener('focus', () => {
  33. this.scaning = false;
  34. setTimeout(() => {
  35. this.setState({
  36. isResult: false
  37. });
  38. }, 200);
  39. });
  40. this.props.navigation.addListener('beforeRemove', (e) => {
  41. if (!isIOS && !this.state.isResult) {
  42. e.preventDefault();
  43. this.setState({
  44. isResult: true
  45. }, () => {
  46. setTimeout(() => {
  47. goBack();
  48. }, 300);
  49. });
  50. }
  51. });
  52. this.stateListener = AppState.addEventListener("change", state => {
  53. console.log("state change", state);
  54. if (state == 'active') {
  55. if (this.state.isResult) {
  56. this.setState({
  57. isResult: false
  58. });
  59. }
  60. } else {
  61. this.setState({
  62. isResult: true
  63. });
  64. }
  65. });
  66. utils.logEventTracking("scan_qr_click")
  67. }
  68. componentWillUnmount() {
  69. if (this.stateListener) {
  70. this.stateListener.remove();
  71. }
  72. }
  73. scanResult = (codes) => {
  74. console.log("scanResult", this.scaning);
  75. if (this.scaning) {
  76. return;
  77. }
  78. this.scaning = true;
  79. Vibration.vibrate(100);
  80. this.setState({
  81. isResult: true
  82. }, () => {
  83. const msg = codes[0]
  84. this.getQrMessage(msg?.value);
  85. });
  86. }
  87. getQrMessage(msg="") {
  88. utils.logEventTracking("scan_qr_result", msg)
  89. if (msg.indexOf('::') > 0) {
  90. const arr = msg.split('::');
  91. if (arr.length == 2) {
  92. const qr = {
  93. chargeBoxId: arr[0],
  94. connectorId: arr[1]
  95. }
  96. if (this.state.params.id) {
  97. qr.sitePk = this.state.params.id
  98. }
  99. this.getChargeDetail(qr);
  100. return;
  101. }
  102. } else if (msg.indexOf("http") == 0) {
  103. const qr = {
  104. qrContent: msg
  105. }
  106. if (this.state.params.id) {
  107. qr.sitePk = this.state.params.id
  108. }
  109. this.getChargeDetail(qr);
  110. return;
  111. }
  112. Dialog.showDialog({
  113. title: 'Error',
  114. message: 'It\'s not a legal QR code',
  115. showCancel: false,
  116. callback: (e) => {
  117. this.scaning = false;
  118. this.setState({
  119. isResult: false
  120. });
  121. },
  122. onBackPress: btn => {
  123. this.scaning = false;
  124. Dialog.dismissDialog();
  125. this.setState({
  126. isResult: false
  127. });
  128. }
  129. });
  130. }
  131. getChargeDetail(qr) {
  132. console.log('===============SCAN QR===============');
  133. console.log(qr);
  134. console.log('===============SCAN QR===============');
  135. apiCharge.checkQRStatus(qr).then(res => {
  136. if (res.data && res.data.chargeBoxId) {
  137. QRResult.setResult(res.data);
  138. if (res.data.sitePk) {
  139. if (this.state.params.actionDetail) {
  140. startPage(PageList.chargeDetailPage, {stationInfo: {id: res.data.sitePk}, action: 'qr', from: PageList.home});
  141. } else {
  142. goBack();
  143. }
  144. //startPage(PageList.chargeDetail, {stationInfo: {id: res.data.sitePk}, action: 'qr'});
  145. }
  146. }
  147. }).catch(({err, code}) => {
  148. Dialog.showDialog({
  149. title: 'Error',
  150. message: err,
  151. showCancel: false,
  152. callback: (btn) => {
  153. this.setState({
  154. isResult: false
  155. });
  156. if (code == 5194 && btn == Dialog.BUTTON_OK && app.vehicle.enable) {
  157. startPage(app.vehicle.newVersionPage ? PageList.vehiclesListV2 : PageList.myVehicles)
  158. } else {
  159. this.scaning = false;
  160. }
  161. },
  162. onBackPress: () => {
  163. Dialog.dismissDialog();
  164. this.setState({
  165. isResult: false
  166. });
  167. this.scaning = false;
  168. }
  169. })
  170. });
  171. }
  172. switchFlash() {
  173. this.setState({
  174. flashLight: !this.state.flashLight
  175. })
  176. }
  177. onEnterStation(visible) {
  178. this.setState({
  179. stationId: "",
  180. isResult: visible,
  181. showInputStation: visible
  182. })
  183. }
  184. enterStatioinId() {
  185. if (QRResult.haveResult()) {
  186. const result = QRResult.getResult()
  187. if (result.sitePk) {
  188. if (this.state.params.actionDetail) {
  189. startPage(PageList.chargeDetailPage, {stationInfo: {id: result.sitePk}, action: 'qr', from: PageList.home});
  190. } else {
  191. goBack();
  192. }
  193. //startPage(PageList.chargeDetail, {stationInfo: {id: res.data.sitePk}, action: 'qr'});
  194. }
  195. }
  196. }
  197. render() {
  198. return (
  199. <View style={styles.container}>
  200. <QRScanner
  201. onResult={this.scanResult}
  202. isActive={!this.state.isResult}
  203. />
  204. { showInputStationView &&
  205. <Button
  206. style={styles.btnStationInput}
  207. text={$t('charging.enterStationId')}
  208. textColor={"rgba(255,255,255,.8)"}
  209. onClick={() => this.onEnterStation(true)}/>
  210. }
  211. <EnterStationDialog
  212. visible={this.state.showInputStation}
  213. stationId={this.state.params?.id}
  214. onConfirm={() => this.enterStatioinId()}
  215. onClose={() => this.onEnterStation(false)}
  216. />
  217. </View>
  218. );
  219. }
  220. }
  221. const styles = StyleSheet.create({
  222. container: {
  223. alignItems: 'center',
  224. justifyContent: 'center',
  225. backgroundColor: '#000',
  226. ...StyleSheet.absoluteFillObject
  227. },
  228. btnStationInput: {
  229. left: 16,
  230. right: 16,
  231. bottom: 24,
  232. zIndex: 2,
  233. position: 'absolute',
  234. backgroundColor: "rgba(0,0,0,.4)"
  235. }
  236. })