TabCharge.js 16 KB

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