QRScan.js 6.1 KB

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