TabCharge.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610
  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. /**
  128. * 激活自动进入新充电页面的开关
  129. */
  130. activeNewUIPage() {
  131. this.autoIntoCharging = true;
  132. }
  133. toChargingPage() {
  134. this.autoIntoCharging = false;
  135. startPage(PageList.chargingPage, {
  136. id: this.state.stationInfo.id,
  137. name: this.state.stationInfo.name,
  138. address: this.state.stationInfo.address,
  139. chargeBoxId: this.state.connectorInfo.chargeBoxId,
  140. connectorId: this.state.connectorInfo.connectorId
  141. });
  142. }
  143. enterStatioinId() {
  144. if (QRResult.haveResult()) {
  145. if (PagerUtil.ENABLE_NEW_UI) {
  146. this.onRefresh();
  147. } else {
  148. const info = QRResult.getResult()
  149. console.log('EnterResult', info);
  150. this.setState({
  151. isAuthentic: true,
  152. connectorInfo: info
  153. //soc: info.chargeType == 'AC' ? 0 : 'In Charging'
  154. });
  155. this.checkChargeStatus();
  156. }
  157. }
  158. }
  159. onPaymentMethodChanged(payment) {
  160. this.setState({
  161. currentPayment: payment
  162. })
  163. }
  164. onMethodChange() {
  165. this.changeMethod = true;
  166. startPage(PageList.paymentMethod, {info: this.state.connectorInfo, type: this.state.currentPayment});
  167. }
  168. onMethodChanged() {
  169. if (this.changeMethod) {
  170. this.changeMethod = false;
  171. if (global.paymentOption?.title) {
  172. this.setState({
  173. //currentPerUser: global.paymentOption.amount,
  174. currentPayment: global.paymentOption.value,
  175. currentPaytype: global.paymentOption.title
  176. }, () => {
  177. global.paymentOption= {}
  178. })
  179. }
  180. }
  181. }
  182. //打开输入弹窗
  183. onEnterStation() {
  184. this.setState({
  185. showStationDialog: true
  186. })
  187. }
  188. //自动刷新
  189. autoCheckIsCharge() {
  190. if (this.canAutoRefresh) {
  191. this.checkIsCharge();
  192. }
  193. }
  194. //自动刷新状态
  195. autoCheckChargeStatus() {
  196. setTimeout(() => {
  197. if (this.canAutoRefresh) {
  198. this.checkChargeStatus();
  199. }
  200. }, 10000);
  201. }
  202. //扫码后获取连接器数据
  203. getConnectorInfo() {
  204. console.log("[TabCharge] getConnectorInfo", this.state.stationInfo.id);
  205. apiCharge.checkIsCharging({sitePk: this.state.stationInfo.id}).then(res => {
  206. this.setState({
  207. connectorInfo: res.data
  208. }, () => {
  209. this.checkChargeStatus();
  210. });
  211. }).catch((err) => {
  212. });
  213. }
  214. //获取充电进度数据(百分比)
  215. checkIsCharge(showError) {
  216. const params = {
  217. sitePk: this.state.stationInfo.id
  218. }
  219. apiCharge.checkIsCharging(params).then(res => {
  220. this.setState({
  221. isStart: true,
  222. isCharging: true,
  223. isAuthentic: true,
  224. connectorInfo: res.data,
  225. lastUpdated: utils.getNowHHmm()
  226. });
  227. if (this.canAutoRefresh) {
  228. setTimeout(() => {
  229. this.autoCheckIsCharge();
  230. }, 30000);
  231. }
  232. PagerUtil.onCharge(this.props);
  233. }).catch(({err, code}) => {
  234. //TODO 模拟测试
  235. //TODO 模拟测试-End
  236. this.setState({
  237. isStart: false,
  238. isCharging: false
  239. });
  240. setTimeout(() => {
  241. this.autoCheckIsCharge();
  242. this.canAutoRefresh = false;
  243. }, 30000);
  244. if (showError) {
  245. this.setState({
  246. errorCode: 'A4',
  247. showErrorDialog: true,
  248. errorMessage: 'Your vehicle doesn’t seem to be charging. Please check your vehicle.'
  249. });
  250. }
  251. });
  252. }
  253. //获取充电进度数据-end
  254. //获取充电桩对应接口的状态
  255. checkChargeStatus(haveResult=false) {
  256. //TODO 模拟测试
  257. /*this.setState({
  258. isStart: true,
  259. isCharging: true,
  260. isAuthentic: true
  261. });
  262. return;*/
  263. //TODO 模拟测试-End
  264. const params = {
  265. connectorId: this.state.connectorInfo.connectorId,
  266. chargeBoxId: this.state.connectorInfo.chargeBoxId,
  267. }
  268. if (!PagerUtil.ENABLE_NEW_UI) {
  269. if (!params.chargeBoxId || !params.connectorId) {
  270. this.checkIsCharge();
  271. return;
  272. }
  273. }
  274. apiCharge.getCurrentStatus(params).then(res => {
  275. if (res.data.status) {
  276. if (PagerUtil.ENABLE_NEW_UI && (this.intoChargingStatus.indexOf(res.data.status) >= 0 || haveResult)) {
  277. //进入新流程
  278. this.setState({
  279. canIntoCharging: true
  280. }, () => {
  281. if (this.autoIntoCharging) {
  282. this.toChargingPage();
  283. } else {
  284. PagerUtil.onCharge(this.props);
  285. }
  286. })
  287. } else {
  288. if (haveResult) {
  289. PagerUtil.onCharge(this.props);
  290. }
  291. switch (res.data.status) {
  292. case 'Available': //可用的
  293. this.state.connectorInfo.isCheckThrough = false;
  294. this.setState({
  295. isStart: false,
  296. isPending: false,
  297. isCharging: false,
  298. connectorInfo: this.state.connectorInfo
  299. });
  300. break;
  301. case 'Preparing': //已插入
  302. this.state.connectorInfo.isCheckThrough = true;
  303. this.setState({
  304. isStart: false,
  305. isPending: false,
  306. isCharging: false,
  307. available: true,
  308. connectorInfo: this.state.connectorInfo
  309. });
  310. //this.checkIsCharge();
  311. break;
  312. case 'Charging': //正在充电
  313. this.canAutoRefresh = true;
  314. this.state.connectorInfo.isCheckThrough = true;
  315. this.setState({
  316. isPending: false,
  317. connectorInfo: this.state.connectorInfo
  318. });
  319. this.checkIsCharge();
  320. break;
  321. case 'Initiating': //充电确认中
  322. this.canAutoRefresh = true;
  323. this.state.connectorInfo.isCheckThrough = true;
  324. this.setState({
  325. isStart: true,
  326. isPending: true,
  327. isCharging: true,
  328. isAuthentic: true,
  329. connectorInfo: this.state.connectorInfo
  330. });
  331. this.autoCheckChargeStatus();
  332. break;
  333. case 'SuspendedEVSE':
  334. this.setState({
  335. errorCode: 'A5',
  336. showErrorDialog: true,
  337. errorMessage: $t('charging.errUnable2Charge')
  338. });
  339. break;
  340. case 'SuspendedEV': //已连接上但未充电
  341. this.checkIsCharge(true);
  342. break;
  343. case 'Reserved': //预定中
  344. this.setState({
  345. errorCode: 'A5',
  346. showErrorDialog: true,
  347. errorMessage: $t('charging.errUnable2Reserved')
  348. });
  349. break;
  350. case 'Finishing': //已完成
  351. if (res.data.chargingPk) {
  352. Dialog.showProgressDialog();
  353. setTimeout(() => {
  354. Dialog.dismissLoading();
  355. this.setState({
  356. isStart: false,
  357. isPending: false,
  358. isCharging: false
  359. });
  360. startPage(PageList.summary, {
  361. chargingPk: res.data.chargingPk,
  362. id: this.state.stationInfo.id,
  363. name: this.state.stationInfo.name,
  364. address: this.state.stationInfo.address
  365. });
  366. }, 2000);
  367. } else {
  368. this.setState({
  369. isStart: true,
  370. isPending: false,
  371. isCharging: false
  372. });
  373. }
  374. break;
  375. default:
  376. this.setState({
  377. isStart: false,
  378. isPending: false,
  379. isCharging: false
  380. });
  381. this.setState({
  382. errorCode: 'A4',
  383. showErrorDialog: true,
  384. errorMessage: $t('charging.errNotChargeE0')
  385. });
  386. break;
  387. }
  388. }
  389. } else {
  390. this.setState({
  391. errorCode: 'A4',
  392. showErrorDialog: true,
  393. errorMessage: $t('charging.errNotChargeE0')
  394. });
  395. }
  396. }).catch((err) => {
  397. toastShort(err)
  398. this.setState({
  399. errorCode: 'A9',
  400. showErrorDialog: true,
  401. errorMessage: $t('charging.errAuthenticationError')
  402. });
  403. })
  404. }
  405. //获取充电桩对应接口的状态-end
  406. //开始充电-start
  407. startCharge() {
  408. if (this.state.connectorInfo.isCheckThrough) {
  409. Dialog.showProgressDialog();
  410. const params = {
  411. chargeBoxId: this.state.connectorInfo.chargeBoxId,
  412. connectorId: this.state.connectorInfo.connectorId
  413. }
  414. apiCharge.startCharge(params).then(res => {
  415. console.log(res);
  416. setTimeout(() => {
  417. this.setState({
  418. isStart: true,
  419. isPending: true,
  420. isCharging: true
  421. });
  422. this.canAutoRefresh = true;
  423. this.autoCheckChargeStatus();
  424. Dialog.dismissLoading();
  425. if (res.msg) {
  426. Dialog.showResultDialog(res.msg)
  427. }
  428. //this.autoCheckIsCharge();
  429. }, 3000);
  430. }).catch(({err, code, data}) => {
  431. //toastShort(err);
  432. console.log("开始充电错误", err, code);
  433. Dialog.dismissLoading();
  434. if (code == 5200) {
  435. this.setState({
  436. errorCode: 'none',
  437. showErrorDialog: true,
  438. errorMessage: "(" + data.transactionPk + ') ' + err
  439. });
  440. } else {
  441. this.setState({
  442. errorCode: 'A4',
  443. showErrorDialog: true,
  444. errorMessage: ''+err
  445. });
  446. }
  447. });
  448. } else {
  449. this.setState({
  450. errorCode: 'A1',
  451. showErrorDialog: true,
  452. errorMessage: $t('charging.errNotConnected')
  453. });
  454. }
  455. }
  456. //开始充电-end
  457. //停止充电-start
  458. onStopCharge() {
  459. if (this.state.isCharging) {
  460. Dialog.showDialog({
  461. title: $t('charging.titleStopCharging'),
  462. message: $t('charging.confirmStopCharging'),
  463. callback: ok => {
  464. if (ok == Dialog.BUTTON_OK) {
  465. this.stopCharge();
  466. }
  467. }
  468. });
  469. } else {
  470. this.stopCharge();
  471. }
  472. }
  473. stopCharge() {
  474. this.canAutoRefresh = false;
  475. Dialog.showProgressDialog();
  476. apiCharge.stopCharge().then(res => {
  477. if (res.msg) {
  478. toastShort(res.msg)
  479. }
  480. if (res.data.chargingPk) {
  481. setTimeout(() => {
  482. Dialog.dismissLoading();
  483. this.setState({
  484. isStart: false,
  485. isPending: false,
  486. isCharging: false
  487. });
  488. startPage(PageList.summary, {
  489. chargingPk: res.data.chargingPk,
  490. id: this.state.stationInfo.id,
  491. name: this.state.stationInfo.name,
  492. address: this.state.stationInfo.address
  493. });
  494. }, 3000);
  495. } else {
  496. Dialog.dismissLoading();
  497. toastShort($t('charging.errDetected'));
  498. }
  499. }).catch((err) => {
  500. Dialog.dismissLoading();
  501. toastShort(err);
  502. this.setState({
  503. isStart: false,
  504. isPending: false,
  505. isCharging: false
  506. });
  507. //模拟进入结算页
  508. /*startPage(PageList.summary, {
  509. chargingPk: 1,
  510. id: this.state.stationInfo.id,
  511. name: this.state.stationInfo.name,
  512. address: this.state.stationInfo.address
  513. });*/
  514. });
  515. }
  516. //停止充电-end
  517. closeError() {
  518. this.setState({
  519. showErrorDialog: false,
  520. showStationDialog: false
  521. });
  522. }
  523. render() {
  524. return (
  525. <ScrollView
  526. style={styles.container}
  527. keyboardShouldPersistTaps={isIOS ? 'never' : 'handled'}
  528. contentContainerStyle={$padding(0, 16)}
  529. refreshControl={
  530. <RefreshControl
  531. {...MyRefreshProps()}
  532. refreshing={this.state.refreshing}
  533. onRefresh={() => this.onPullRefresh()}
  534. />
  535. }>
  536. { this.state.isAuthentic //是否扫码认证
  537. ? <StepChargeView
  538. isStart={this.state.isStart} //是否开始充电
  539. isPending={this.state.isPending}
  540. isCharging={this.state.isCharging}
  541. lastUpdated={this.state.lastUpdated}
  542. connectorInfo={this.state.connectorInfo}
  543. currentPayment={this.state.currentPayment}
  544. onStartCharge={() => this.startCharge()}
  545. onStopCharge={() => this.onStopCharge()}
  546. onPaymentMethodChanged={(type) => this.onPaymentMethodChanged(type)}/>
  547. : <StepStartView
  548. isPrivate={this.state.isPrivate}
  549. canIntoCharging={this.state.canIntoCharging}
  550. stationInfo={this.state.stationInfo}
  551. onEnterStation={() => this.onEnterStation()}
  552. onIntoCharging={() => this.toChargingPage()}
  553. />
  554. }
  555. <ErrorDialog
  556. visible={this.state.showErrorDialog}
  557. code={this.state.errorCode}
  558. message={this.state.errorMessage}
  559. onClose={() => {
  560. this.closeError();
  561. }}
  562. />
  563. <EnterStationDialog
  564. visible={this.state.showStationDialog}
  565. stationId={this.state.stationInfo.id}
  566. onConfirm={() => this.enterStatioinId()}
  567. onClose={() => this.closeError()}
  568. />
  569. </ScrollView>
  570. );
  571. }
  572. }
  573. const styles = StyleSheet.create({
  574. container: {
  575. flex: 1
  576. },
  577. title: {
  578. color: '#000',
  579. fontSize: 14,
  580. fontWeight: 'bold',
  581. paddingTop: 16,
  582. paddingBottom: 16
  583. }
  584. })