QRScan.js 6.0 KB

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