QRScan.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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. console.log("scanResult", this.scaning);
  74. if (this.scaning) {
  75. return;
  76. }
  77. this.scaning = true;
  78. Vibration.vibrate(100);
  79. this.setState({
  80. isResult: true
  81. }, () => {
  82. const msg = codes[0]
  83. this.getQrMessage(msg?.value);
  84. });
  85. }
  86. getQrMessage(msg="") {
  87. utils.logEventTracking("scan_qr_result", msg)
  88. if (msg.indexOf('::') > 0) {
  89. const arr = msg.split('::');
  90. if (arr.length == 2) {
  91. const qr = {
  92. chargeBoxId: arr[0],
  93. connectorId: arr[1]
  94. }
  95. if (this.state.params.id) {
  96. qr.sitePk = this.state.params.id
  97. }
  98. this.getChargeDetail(qr);
  99. return;
  100. }
  101. } else if (msg.indexOf("http") == 0) {
  102. const qr = {
  103. qrContent: msg
  104. }
  105. if (this.state.params.id) {
  106. qr.sitePk = this.state.params.id
  107. }
  108. this.getChargeDetail(qr);
  109. return;
  110. }
  111. Dialog.showDialog({
  112. title: 'Error',
  113. message: 'It\'s not a legal QR code',
  114. showCancel: false,
  115. callback: (e) => {
  116. this.scaning = false;
  117. this.setState({
  118. isResult: false
  119. });
  120. },
  121. onBackPress: btn => {
  122. this.scaning = false;
  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: () => {
  160. Dialog.dismissDialog();
  161. this.setState({
  162. isResult: false
  163. });
  164. }
  165. })
  166. }).finally(() => {
  167. this.scaning = false;
  168. });
  169. }
  170. switchFlash() {
  171. this.setState({
  172. flashLight: !this.state.flashLight
  173. })
  174. }
  175. onEnterStation(visible) {
  176. this.setState({
  177. stationId: "",
  178. isResult: visible,
  179. showInputStation: visible
  180. })
  181. }
  182. enterStatioinId() {
  183. if (QRResult.haveResult()) {
  184. const result = QRResult.getResult()
  185. if (result.sitePk) {
  186. if (this.state.params.actionDetail) {
  187. startPage(PageList.chargeDetailPage, {stationInfo: {id: result.sitePk}, action: 'qr', from: PageList.home});
  188. } else {
  189. goBack();
  190. }
  191. //startPage(PageList.chargeDetail, {stationInfo: {id: res.data.sitePk}, action: 'qr'});
  192. }
  193. }
  194. }
  195. render() {
  196. return (
  197. <View style={styles.container}>
  198. <QRScanner
  199. onResult={this.scanResult}
  200. isActive={!this.state.isResult}
  201. />
  202. { showInputStationView &&
  203. <Button
  204. style={styles.btnStationInput}
  205. text={$t('charging.enterStationId')}
  206. textColor={"rgba(255,255,255,.8)"}
  207. onClick={() => this.onEnterStation(true)}/>
  208. }
  209. <EnterStationDialog
  210. visible={this.state.showInputStation}
  211. stationId={this.state.params?.id}
  212. onConfirm={() => this.enterStatioinId()}
  213. onClose={() => this.onEnterStation(false)}
  214. />
  215. </View>
  216. );
  217. }
  218. }
  219. const styles = StyleSheet.create({
  220. container: {
  221. alignItems: 'center',
  222. justifyContent: 'center',
  223. backgroundColor: '#000',
  224. ...StyleSheet.absoluteFillObject
  225. },
  226. btnStationInput: {
  227. left: 16,
  228. right: 16,
  229. bottom: 24,
  230. zIndex: 2,
  231. position: 'absolute',
  232. backgroundColor: "rgba(0,0,0,.4)"
  233. }
  234. })