TabCharge.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599
  1. /**
  2. * 新版充电页面
  3. * @邠心vbe on 2023/02/06
  4. */
  5. import React, { Component } from 'react';
  6. import { StyleSheet, ScrollView, RefreshControl } from 'react-native';
  7. import apiCharge from '../../api/apiCharge';
  8. import Dialog from '../../components/Dialog';
  9. import { PageList } from '../Router';
  10. import { EnterStationDialog } from './Charging';
  11. import { QRResult } from '../charge/QRScan';
  12. import { ErrorDialog } from './InfoDialog';
  13. import PagerUtil from './PagerUtil';
  14. import StepStartView from '../charging/StepStartView';
  15. import StepChargeView from '../charging/StepChargeView';
  16. import utils from '../../utils/utils';
  17. import { PaymentDefault } from '../payment/PaymentConfig';
  18. import { MyRefreshProps } from '../../components/ThemesConfig';
  19. export default class TabCharge extends Component {
  20. constructor(props) {
  21. super(props);
  22. this.state = {
  23. available: false,
  24. isPrivate: false,
  25. refreshing: false,
  26. isStart: false,
  27. isPending: false,
  28. isCharging: false,
  29. isAuthentic: false,
  30. selectRate: '',
  31. connectorInfo: {},
  32. stationInfo: {},
  33. lastUpdated: '',
  34. errorCode: 'A9',
  35. errorMessage: '',
  36. showErrorDialog: false,
  37. showStationDialog: false,
  38. currentPerUser: undefined,
  39. canIntoCharging: false,
  40. //currentPayment: PAYTYPE.CREDIT_WALLET,
  41. //currentPaytype: "Credit Wallet",
  42. currentPayment: PaymentDefault.DEFAULT.payType,
  43. currentPaytype: PaymentDefault.DEFAULT.payName
  44. };
  45. this.changeMethod = false;
  46. this.canAutoRefresh = false;
  47. this.inputStationId = '';
  48. this.autoIntoCharging = true;
  49. this.intoChargingStatus = ["Preparing", "Charging", "Initiating", "SuspendedEVSE", "SuspendedEV"]
  50. }
  51. componentDidMount() {
  52. this.canAutoRefresh = true;
  53. PagerUtil.addOnRefresh(this);
  54. //this.onRefresh();
  55. }
  56. onRefresh() {
  57. console.log("Charge刷新", this.props.route.name);
  58. const info = PagerUtil.getStationInfo();
  59. this.setState({
  60. refreshing: false,
  61. stationInfo: info
  62. }, () => {
  63. this.init();
  64. console.log("站点信息", this.state.stationInfo);
  65. //this.checkIsCharge();
  66. });
  67. }
  68. onPullRefresh() {
  69. this.setState({
  70. refreshing: true
  71. })
  72. PagerUtil.setBackRefreshing();
  73. }
  74. init() {
  75. console.log("Charge刷新", "init");
  76. this.onMethodChanged();
  77. this.refreshAvailable();
  78. if (QRResult.haveResult()) {
  79. console.log("Charge刷新", "haveResult");
  80. const info = QRResult.getResult()
  81. console.log('QRResult', info);
  82. if (PagerUtil.ENABLE_NEW_UI) {
  83. //新充电流程
  84. this.setState({
  85. connectorInfo: info
  86. }, () => {
  87. this.checkChargeStatus(true);
  88. })
  89. } else {
  90. this.setState({
  91. isAuthentic: true,
  92. connectorInfo: info
  93. //soc: info.chargeType == 'AC' ? 0 : 'In Charging'
  94. }, () => {
  95. PagerUtil.onCharge(this.props);
  96. this.checkChargeStatus(true);
  97. });
  98. }
  99. //QRResult.clearResult();
  100. } else if (this.state.isStart) {
  101. console.log("Charge刷新", "isStart");
  102. this.checkIsCharge();
  103. } else {
  104. console.log("Charge刷新", "noStart");
  105. this.getConnectorInfo();
  106. //this.checkChargeStatus();
  107. }
  108. }
  109. componentWillUnmount() {
  110. this.canAutoRefresh = false;
  111. }
  112. //刷新可用充电接口
  113. refreshAvailable() {
  114. const info = this.state.stationInfo
  115. const all = info?.allConnector;
  116. if (info.siteType == 'Private') {
  117. this.setState({
  118. isPrivate: true
  119. })
  120. }
  121. if (all) {
  122. this.setState({
  123. available: !all.available > 0
  124. });
  125. }
  126. }
  127. toChargingPage() {
  128. this.autoIntoCharging = false;
  129. startPage(PageList.chargingPage, {
  130. id: this.state.stationInfo.id,
  131. name: this.state.stationInfo.name,
  132. address: this.state.stationInfo.address,
  133. chargeBoxId: this.state.connectorInfo.chargeBoxId,
  134. connectorId: this.state.connectorInfo.connectorId
  135. });
  136. }
  137. enterStatioinId() {
  138. if (QRResult.haveResult()) {
  139. const info = QRResult.getResult()
  140. console.log('EnterResult', info);
  141. this.setState({
  142. isAuthentic: true,
  143. connectorInfo: info
  144. //soc: info.chargeType == 'AC' ? 0 : 'In Charging'
  145. });
  146. this.checkChargeStatus();
  147. }
  148. }
  149. onPaymentMethodChanged(payment) {
  150. this.setState({
  151. currentPayment: payment
  152. })
  153. }
  154. onMethodChange() {
  155. this.changeMethod = true;
  156. startPage(PageList.paymentMethod, {info: this.state.connectorInfo, type: this.state.currentPayment});
  157. }
  158. onMethodChanged() {
  159. if (this.changeMethod) {
  160. this.changeMethod = false;
  161. if (global.paymentOption?.title) {
  162. this.setState({
  163. //currentPerUser: global.paymentOption.amount,
  164. currentPayment: global.paymentOption.value,
  165. currentPaytype: global.paymentOption.title
  166. }, () => {
  167. global.paymentOption= {}
  168. })
  169. }
  170. }
  171. }
  172. //打开输入弹窗
  173. onEnterStation() {
  174. this.setState({
  175. showStationDialog: true
  176. })
  177. }
  178. //自动刷新
  179. autoCheckIsCharge() {
  180. if (this.canAutoRefresh) {
  181. this.checkIsCharge();
  182. }
  183. }
  184. //自动刷新状态
  185. autoCheckChargeStatus() {
  186. setTimeout(() => {
  187. if (this.canAutoRefresh) {
  188. this.checkChargeStatus();
  189. }
  190. }, 10000);
  191. }
  192. //扫码后获取连接器数据
  193. getConnectorInfo() {
  194. console.log("[TabCharge] getConnectorInfo", this.state.stationInfo.id);
  195. apiCharge.checkIsCharging({sitePk: this.state.stationInfo.id}).then(res => {
  196. this.setState({
  197. connectorInfo: res.data
  198. }, () => {
  199. this.checkChargeStatus();
  200. });
  201. }).catch((err) => {
  202. });
  203. }
  204. //获取充电进度数据(百分比)
  205. checkIsCharge(showError) {
  206. const params = {
  207. sitePk: this.state.stationInfo.id
  208. }
  209. apiCharge.checkIsCharging(params).then(res => {
  210. this.setState({
  211. isStart: true,
  212. isCharging: true,
  213. isAuthentic: true,
  214. connectorInfo: res.data,
  215. lastUpdated: utils.getNowHHmm()
  216. });
  217. if (this.canAutoRefresh) {
  218. setTimeout(() => {
  219. this.autoCheckIsCharge();
  220. }, 30000);
  221. }
  222. PagerUtil.onCharge(this.props);
  223. }).catch(({err, code}) => {
  224. //TODO 模拟测试
  225. //TODO 模拟测试-End
  226. this.setState({
  227. isStart: false,
  228. isCharging: false
  229. });
  230. setTimeout(() => {
  231. this.autoCheckIsCharge();
  232. this.canAutoRefresh = false;
  233. }, 30000);
  234. if (showError) {
  235. this.setState({
  236. errorCode: 'A4',
  237. showErrorDialog: true,
  238. errorMessage: 'Your vehicle doesn’t seem to be charging. Please check your vehicle.'
  239. });
  240. }
  241. });
  242. }
  243. //获取充电进度数据-end
  244. //获取充电桩对应接口的状态
  245. checkChargeStatus(haveResult=false) {
  246. //TODO 模拟测试
  247. /*this.setState({
  248. isStart: true,
  249. isCharging: true,
  250. isAuthentic: true
  251. });
  252. return;*/
  253. //TODO 模拟测试-End
  254. const params = {
  255. connectorId: this.state.connectorInfo.connectorId,
  256. chargeBoxId: this.state.connectorInfo.chargeBoxId,
  257. }
  258. if (!PagerUtil.ENABLE_NEW_UI) {
  259. if (!params.chargeBoxId || !params.connectorId) {
  260. this.checkIsCharge();
  261. return;
  262. }
  263. }
  264. apiCharge.getCurrentStatus(params).then(res => {
  265. if (res.data.status) {
  266. if (PagerUtil.ENABLE_NEW_UI && (this.intoChargingStatus.indexOf(res.data.status) >= 0 || haveResult)) {
  267. //进入新流程
  268. this.setState({
  269. canIntoCharging: true
  270. }, () => {
  271. if (this.autoIntoCharging) {
  272. this.toChargingPage();
  273. } else {
  274. PagerUtil.onCharge(this.props);
  275. }
  276. })
  277. } else {
  278. if (haveResult) {
  279. PagerUtil.onCharge(this.props);
  280. }
  281. switch (res.data.status) {
  282. case 'Available': //可用的
  283. this.state.connectorInfo.isCheckThrough = false;
  284. this.setState({
  285. isStart: false,
  286. isPending: false,
  287. isCharging: false,
  288. connectorInfo: this.state.connectorInfo
  289. });
  290. break;
  291. case 'Preparing': //已插入
  292. this.state.connectorInfo.isCheckThrough = true;
  293. this.setState({
  294. isStart: false,
  295. isPending: false,
  296. isCharging: false,
  297. available: true,
  298. connectorInfo: this.state.connectorInfo
  299. });
  300. //this.checkIsCharge();
  301. break;
  302. case 'Charging': //正在充电
  303. this.canAutoRefresh = true;
  304. this.state.connectorInfo.isCheckThrough = true;
  305. this.setState({
  306. isPending: false,
  307. connectorInfo: this.state.connectorInfo
  308. });
  309. this.checkIsCharge();
  310. break;
  311. case 'Initiating': //充电确认中
  312. this.canAutoRefresh = true;
  313. this.state.connectorInfo.isCheckThrough = true;
  314. this.setState({
  315. isStart: true,
  316. isPending: true,
  317. isCharging: true,
  318. isAuthentic: true,
  319. connectorInfo: this.state.connectorInfo
  320. });
  321. this.autoCheckChargeStatus();
  322. break;
  323. case 'SuspendedEVSE':
  324. this.setState({
  325. errorCode: 'A5',
  326. showErrorDialog: true,
  327. errorMessage: $t('charging.errUnable2Charge')
  328. });
  329. break;
  330. case 'SuspendedEV': //已连接上但未充电
  331. this.checkIsCharge(true);
  332. break;
  333. case 'Reserved': //预定中
  334. this.setState({
  335. errorCode: 'A5',
  336. showErrorDialog: true,
  337. errorMessage: $t('charging.errUnable2Reserved')
  338. });
  339. break;
  340. case 'Finishing': //已完成
  341. if (res.data.chargingPk) {
  342. Dialog.showProgressDialog();
  343. setTimeout(() => {
  344. Dialog.dismissLoading();
  345. this.setState({
  346. isStart: false,
  347. isPending: false,
  348. isCharging: false
  349. });
  350. startPage(PageList.summary, {
  351. chargingPk: res.data.chargingPk,
  352. id: this.state.stationInfo.id,
  353. name: this.state.stationInfo.name,
  354. address: this.state.stationInfo.address
  355. });
  356. }, 2000);
  357. } else {
  358. this.setState({
  359. isStart: true,
  360. isPending: false,
  361. isCharging: false
  362. });
  363. }
  364. break;
  365. default:
  366. this.setState({
  367. isStart: false,
  368. isPending: false,
  369. isCharging: false
  370. });
  371. this.setState({
  372. errorCode: 'A4',
  373. showErrorDialog: true,
  374. errorMessage: $t('charging.errNotChargeE0')
  375. });
  376. break;
  377. }
  378. }
  379. } else {
  380. this.setState({
  381. errorCode: 'A4',
  382. showErrorDialog: true,
  383. errorMessage: $t('charging.errNotChargeE0')
  384. });
  385. }
  386. }).catch((err) => {
  387. toastShort(err)
  388. this.setState({
  389. errorCode: 'A9',
  390. showErrorDialog: true,
  391. errorMessage: $t('charging.errAuthenticationError')
  392. });
  393. })
  394. }
  395. //获取充电桩对应接口的状态-end
  396. //开始充电-start
  397. startCharge() {
  398. if (this.state.connectorInfo.isCheckThrough) {
  399. Dialog.showProgressDialog();
  400. const params = {
  401. chargeBoxId: this.state.connectorInfo.chargeBoxId,
  402. connectorId: this.state.connectorInfo.connectorId
  403. }
  404. apiCharge.startCharge(params).then(res => {
  405. console.log(res);
  406. setTimeout(() => {
  407. this.setState({
  408. isStart: true,
  409. isPending: true,
  410. isCharging: true
  411. });
  412. this.canAutoRefresh = true;
  413. this.autoCheckChargeStatus();
  414. Dialog.dismissLoading();
  415. if (res.msg) {
  416. Dialog.showResultDialog(res.msg)
  417. }
  418. //this.autoCheckIsCharge();
  419. }, 3000);
  420. }).catch(({err, code, data}) => {
  421. //toastShort(err);
  422. console.log("开始充电错误", err, code);
  423. Dialog.dismissLoading();
  424. if (code == 5200) {
  425. this.setState({
  426. errorCode: 'none',
  427. showErrorDialog: true,
  428. errorMessage: "(" + data.transactionPk + ') ' + err
  429. });
  430. } else {
  431. this.setState({
  432. errorCode: 'A4',
  433. showErrorDialog: true,
  434. errorMessage: ''+err
  435. });
  436. }
  437. });
  438. } else {
  439. this.setState({
  440. errorCode: 'A1',
  441. showErrorDialog: true,
  442. errorMessage: $t('charging.errNotConnected')
  443. });
  444. }
  445. }
  446. //开始充电-end
  447. //停止充电-start
  448. onStopCharge() {
  449. if (this.state.isCharging) {
  450. Dialog.showDialog({
  451. title: $t('charging.titleStopCharging'),
  452. message: $t('charging.confirmStopCharging'),
  453. callback: ok => {
  454. if (ok == Dialog.BUTTON_OK) {
  455. this.stopCharge();
  456. }
  457. }
  458. });
  459. } else {
  460. this.stopCharge();
  461. }
  462. }
  463. stopCharge() {
  464. this.canAutoRefresh = false;
  465. Dialog.showProgressDialog();
  466. apiCharge.stopCharge().then(res => {
  467. if (res.msg) {
  468. toastShort(res.msg)
  469. }
  470. if (res.data.chargingPk) {
  471. setTimeout(() => {
  472. Dialog.dismissLoading();
  473. this.setState({
  474. isStart: false,
  475. isPending: false,
  476. isCharging: false
  477. });
  478. startPage(PageList.summary, {
  479. chargingPk: res.data.chargingPk,
  480. id: this.state.stationInfo.id,
  481. name: this.state.stationInfo.name,
  482. address: this.state.stationInfo.address
  483. });
  484. }, 3000);
  485. } else {
  486. Dialog.dismissLoading();
  487. toastShort($t('charging.errDetected'));
  488. }
  489. }).catch((err) => {
  490. Dialog.dismissLoading();
  491. toastShort(err);
  492. this.setState({
  493. isStart: false,
  494. isPending: false,
  495. isCharging: false
  496. });
  497. //模拟进入结算页
  498. /*startPage(PageList.summary, {
  499. chargingPk: 1,
  500. id: this.state.stationInfo.id,
  501. name: this.state.stationInfo.name,
  502. address: this.state.stationInfo.address
  503. });*/
  504. });
  505. }
  506. //停止充电-end
  507. closeError() {
  508. this.setState({
  509. showErrorDialog: false,
  510. showStationDialog: false
  511. });
  512. }
  513. render() {
  514. return (
  515. <ScrollView
  516. style={styles.container}
  517. keyboardShouldPersistTaps={isIOS ? 'never' : 'handled'}
  518. contentContainerStyle={$padding(0, 16)}
  519. refreshControl={
  520. <RefreshControl
  521. {...MyRefreshProps()}
  522. refreshing={this.state.refreshing}
  523. onRefresh={() => this.onPullRefresh()}
  524. />
  525. }>
  526. { this.state.isAuthentic //是否扫码认证
  527. ? <StepChargeView
  528. isStart={this.state.isStart} //是否开始充电
  529. isPending={this.state.isPending}
  530. isCharging={this.state.isCharging}
  531. lastUpdated={this.state.lastUpdated}
  532. connectorInfo={this.state.connectorInfo}
  533. currentPayment={this.state.currentPayment}
  534. onStartCharge={() => this.startCharge()}
  535. onStopCharge={() => this.onStopCharge()}
  536. onPaymentMethodChanged={(type) => this.onPaymentMethodChanged(type)}/>
  537. : <StepStartView
  538. isPrivate={this.state.isPrivate}
  539. canIntoCharging={this.state.canIntoCharging}
  540. stationInfo={this.state.stationInfo}
  541. onEnterStation={() => this.onEnterStation()}
  542. onIntoCharging={() => this.toChargingPage()}
  543. />
  544. }
  545. <ErrorDialog
  546. visible={this.state.showErrorDialog}
  547. code={this.state.errorCode}
  548. message={this.state.errorMessage}
  549. onClose={() => {
  550. this.closeError();
  551. }}
  552. />
  553. <EnterStationDialog
  554. visible={this.state.showStationDialog}
  555. stationId={this.state.stationInfo.id}
  556. onConfirm={() => this.enterStatioinId()}
  557. onClose={() => this.closeError()}
  558. />
  559. </ScrollView>
  560. );
  561. }
  562. }
  563. const styles = StyleSheet.create({
  564. container: {
  565. flex: 1
  566. },
  567. title: {
  568. color: '#000',
  569. fontSize: 14,
  570. fontWeight: 'bold',
  571. paddingTop: 16,
  572. paddingBottom: 16
  573. }
  574. })