ChargingPage.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541
  1. /**
  2. * 新充电流程:充电主页
  3. * @邠心vbe on 2023/06/20
  4. */
  5. import React, { Component } from 'react';
  6. import { View } from 'react-native';
  7. import app from '../../../app.json';
  8. import apiCharge from '../../api/apiCharge';
  9. import apiWallet from '../../api/apiWallet';
  10. import Dialog from '../../components/Dialog';
  11. import { ErrorDialog } from '../chargeV2/InfoDialog';
  12. import { PaymentDefault, PAYTYPE } from '../payment/PaymentConfig';
  13. import { PageList } from '../Router';
  14. import StepAuth from './StepAuth';
  15. import StepCharging from './StepCharging';
  16. import StepStart from './StepStart';
  17. import StepStop from './StepStop';
  18. import PagerUtil from '../chargeV2/PagerUtil';
  19. import DialogPayPerUse from './DialogPayPerUse';
  20. import utils from '../../utils/utils';
  21. export default class ChargingPageV4 extends Component {
  22. constructor(props) {
  23. super(props);
  24. this.state = {
  25. isStoping: false,
  26. isCharging: false,
  27. isAuthentic: false,
  28. chargingPk: "",
  29. stationInfo: {},
  30. connectorInfo: {},
  31. errorCode: 'A9',
  32. errorMessage: '',
  33. lastUpdated: '',
  34. showErrorDialog: false,
  35. showStationDialog: false,
  36. currentPerUse: "",
  37. currentPayment: PaymentDefault.DEFAULT.payType,
  38. currentPaytype: PaymentDefault.DEFAULT.payName,
  39. selectedVoucher: {},
  40. showDialogPayPerUse: false
  41. };
  42. this.isPageShow = true;
  43. this.waitStartCharging = false;
  44. }
  45. componentDidMount() {
  46. this.init();
  47. this.isPageShow = true;
  48. //console.log("参数", this.props.route.params);
  49. if (this.props.route.params.connectorId && this.props.route.params.chargeBoxId) {
  50. this.setState({
  51. stationInfo: this.props.route.params
  52. }, () => {
  53. //测试进入
  54. //this.testInit();
  55. //正常进入
  56. this.getConnectorInfo();
  57. })
  58. }
  59. this.props.navigation.addListener('focus', () => {
  60. //console.log("充电流程页面获取焦点" + this.isPageShow, this.state.currentPerUse);
  61. if (!this.isPageShow && this.state.currentPerUse == "Pending") {
  62. this.isPageShow = true;
  63. //console.log("继续充电流程");
  64. this.setState({
  65. currentPerUse: "Paid"
  66. })
  67. this.refreshChargeData();
  68. } else {
  69. this.isPageShow = true;
  70. //this.canShowLoginDialog();
  71. }
  72. if (PagerUtil.isSelectVoucher) {
  73. this.setState({
  74. selectedVoucher: PagerUtil.getSelectedVoucher()
  75. })
  76. }
  77. });
  78. this.props.navigation.addListener('blur', () => {
  79. this.isPageShow = false;
  80. //console.log("充电流程页面失去焦点");
  81. });
  82. }
  83. componentWillUnmount() {
  84. this.isPageShow = false;
  85. }
  86. testInit() {
  87. this.setState({
  88. isCharging: true,
  89. connectorInfo: {
  90. status: "Initiating"
  91. }
  92. }, () => {
  93. setTimeout(() => {
  94. this.getConnectorInfo();
  95. }, 2000);
  96. })
  97. }
  98. init() {
  99. this.setState({
  100. isStoping: false,
  101. isCharging: false,
  102. isAuthentic: false
  103. });
  104. this.waitAuthentic = false;
  105. this.waitStartCharging = false;
  106. }
  107. getConnectorInfo() {
  108. if (!this.isPageShow) return;
  109. //this.init();
  110. const params = {
  111. sitePk: this.state.stationInfo.id,
  112. chargeBoxId: this.state.stationInfo.chargeBoxId,
  113. connectorId: this.state.stationInfo.connectorId,
  114. paymentOption: this.state.currentPayment,
  115. }
  116. if (app.charge.paymentMethod && this.state.currentPayment?.code) {
  117. params.paymentMethod = this.state.currentPayment?.code
  118. }
  119. console.log("[ChargingPageV3]getConnectorInfo", params);
  120. apiCharge.getConnectorDetail(params).then(res => {
  121. if (res.data.status && !this.state.isStoping) {
  122. const state = {
  123. isStoping: false,
  124. isCharging: false,
  125. isAuthentic: false,
  126. connectorInfo: {}
  127. }
  128. state.connectorInfo = res.data;
  129. if (app.charge.paymentMethod && res.data.currentPaymentMethod) {
  130. //V3版获取当前支付方式
  131. state.currentPayment = {
  132. code: res.data.currentPaymentMethod
  133. }
  134. } else if (res.data.currentPaymentType && res.data.currentPaymentType == PAYTYPE.PAY_PER_USE) {
  135. //V2版获取当前支付方式
  136. state.currentPayment = PAYTYPE.PAY_PER_USE
  137. }
  138. if (res.data.vouchers && res.data.vouchers.length) {
  139. //获取已选择的优惠券
  140. state.selectedVoucher = res.data.vouchers[0];
  141. }
  142. console.log("状态", res.data.status);
  143. switch (res.data.status) {
  144. case 'Available': //可用的
  145. if (this.waitAuthentic) {
  146. state.isAuthentic = true;
  147. this.refreshChargeData(3000);
  148. } else {
  149. state.isAuthentic = false;
  150. }
  151. break;
  152. case 'Preparing': //已插入
  153. this.waitAuthentic = false;
  154. if (this.waitStartCharging) {
  155. state.isCharging = true;
  156. if (res.data.payPerUsePaymentStatus) {
  157. //等待PayPerUse支付-初始化充电
  158. if (res.data.payPerUsePaymentStatus == "PENDING" || res.data.payPerUsePaymentStatus == "PAID") {
  159. this.refreshChargeData(3000);
  160. } else {
  161. this.showErrorDialog('A4', $t('charging.errPayperusePayment') + res.data.payPerUsePaymentStatus);
  162. state.isCharging = false;
  163. state.isAuthentic = true;
  164. }
  165. } else {
  166. //普通充电-初始化充电
  167. this.refreshChargeData(3000);
  168. }
  169. } else {
  170. state.isAuthentic = true;
  171. //this.checkIsCharge();
  172. }
  173. break;
  174. case 'Charging': //正在充电
  175. state.isCharging = true;
  176. this.waitStartCharging = false;
  177. this.refreshChargeData(10000);
  178. break;
  179. case 'Initiating': //充电确认中
  180. state.isCharging = true;
  181. if (res.data.payPerUsePaymentStatus) {
  182. //等待PayPerUse支付-初始化充电
  183. if (res.data.payPerUsePaymentStatus == "PENDING" || res.data.payPerUsePaymentStatus == "PAID") {
  184. this.refreshChargeData();
  185. } else {
  186. this.showErrorDialog('A4', $t('charging.errPayperusePayment') + res.data.payPerUsePaymentStatus);
  187. state.isCharging = false;
  188. state.isAuthentic = false;
  189. }
  190. } else {
  191. //普通充电-初始化充电
  192. this.refreshChargeData();
  193. }
  194. break;
  195. case 'SuspendedEVSE':
  196. this.showErrorDialog('A5', $t('charging.errUnable2Charge'));
  197. break;
  198. case 'SuspendedEV': //已连接上但未充电
  199. state.isAuthentic = true;
  200. //this.refreshChargeData();
  201. break;
  202. case 'Reserved': //预定中
  203. this.showErrorDialog('A5', $t('charging.errUnable2Reserved'));
  204. break;
  205. case 'Finishing': //已完成
  206. if (res.data.chargingPk) {
  207. Dialog.showProgressDialog();
  208. setTimeout(() => {
  209. Dialog.dismissLoading();
  210. this.setState({
  211. isStart: false,
  212. isPending: false,
  213. isCharging: false
  214. });
  215. startPage(PageList.summary, {
  216. chargingPk: res.data.chargingPk,
  217. id: this.state.stationInfo.id,
  218. name: this.state.stationInfo.name,
  219. address: this.state.stationInfo.address
  220. });
  221. }, 2000);
  222. } else {
  223. goBack();
  224. }
  225. break;
  226. default:
  227. this.showErrorDialog('A4', $t('charging.errNotChargeE0'));
  228. break;
  229. }
  230. this.setState(state)
  231. }
  232. }).catch(err => {
  233. Dialog.showResultDialog("An error occurred:\n" + err, "Retry", () => {
  234. this.getConnectorInfo();
  235. })
  236. //toastShort(err)
  237. })
  238. }
  239. refreshChargeData(time=2000) {
  240. if (this.isPageShow) {
  241. //console.log("[刷新获取充电信息]", time);
  242. setTimeout(() => {
  243. this.getConnectorInfo();
  244. }, time);
  245. }
  246. }
  247. onPaymentMethodChanged(payment) {
  248. this.setState({
  249. currentPayment: payment
  250. })
  251. }
  252. onAuthenticate() {
  253. this.waitAuthentic = true;
  254. this.setState({
  255. isAuthentic: true
  256. }, () => {
  257. this.refreshChargeData()
  258. })
  259. }
  260. onStartCharge() {
  261. if (app.charge.paymentMethod) { //V3版本开始充电
  262. if (utils.isNotEmpty(this.state.stationInfo.payPerUseAmount) && this.state.currentPayment?.code.indexOf(PAYTYPE.PAY_PER_USE_CONTAIN) >= 0) {
  263. this.setState({
  264. showDialogPayPerUse: true
  265. });
  266. } else {
  267. this.onStartChargeV3();
  268. }
  269. return;
  270. }
  271. if (this.state.currentPayment == PAYTYPE.PAY_PER_USE) { //V2版本PayPerUse
  272. if (utils.isNotEmpty(this.state.stationInfo.payPerUseAmount)) {
  273. this.setState({
  274. showDialogPayPerUse: true
  275. });
  276. } else {
  277. this.onStartChargePerUse();
  278. }
  279. return;
  280. }
  281. this.setState({
  282. isCharging: true
  283. });
  284. this.waitStartCharging = true;
  285. apiCharge.startCharge(this.state.stationInfo).then(res => {
  286. console.log("[开始充电-onStartCharge]", res);
  287. setTimeout(() => {
  288. this.canAutoRefresh = true;
  289. this.refreshChargeData(500);
  290. //Dialog.dismissLoading();
  291. if (res.msg) {
  292. //Dialog.showResultDialog(res.msg)
  293. toastShort(res.msg)
  294. }
  295. //this.autoCheckIsCharge();
  296. }, 3000);
  297. }).catch(({err, code, data}) => {
  298. //toastShort(err);
  299. console.log("[开始充电错误]", err, code);
  300. //Dialog.dismissLoading();
  301. if (code == 5200) {
  302. this.showErrorDialog('none', "(" + data.transactionPk + ') ' + err);
  303. } else {
  304. this.showErrorDialog('A4', err);
  305. }
  306. this.setState({
  307. isCharging: false
  308. });
  309. });
  310. }
  311. backDialogPayPerUse(confirm) {
  312. this.setState({
  313. showDialogPayPerUse: false
  314. });
  315. if (confirm) {
  316. if (app.charge.paymentMethod) { //V3版本开始充电
  317. this.onStartChargeV3();
  318. return;
  319. }
  320. if (this.state.currentPayment == PAYTYPE.PAY_PER_USE) { //V2版本PayPerUse
  321. this.onStartChargePerUse();
  322. return;
  323. }
  324. }
  325. }
  326. onStartChargePerUse() {
  327. this.setState({
  328. isCharging: true
  329. });
  330. this.waitStartCharging = true;
  331. const params = {
  332. paymentOption: this.state.currentPayment,
  333. ...this.state.stationInfo
  334. }
  335. apiWallet.doPaymentV2(params).then(res => {
  336. if (res.data.webPaymentUrl) {
  337. this.setState({
  338. currentPerUse: "Pending"
  339. })
  340. startPage(PageList.paymentWeb, { amount: params.payAmount, url: res.data.webPaymentUrl, type: 'PayPerUse' });
  341. } else {
  342. toastShort('Error 0')
  343. }
  344. }).catch(({err}) => {
  345. this.showErrorDialog('A9', err);
  346. this.setState({
  347. isCharging: false
  348. });
  349. });
  350. }
  351. onStartChargeV3() {
  352. this.setState({
  353. isCharging: true
  354. });
  355. this.waitStartCharging = true;
  356. const params = {
  357. sitePk: this.state.stationInfo.id,
  358. chargeBoxId: this.state.stationInfo.chargeBoxId,
  359. connectorId: this.state.stationInfo.connectorId
  360. }
  361. if (this.state.currentPayment?.code) {
  362. params.paymentMethod = this.state.currentPayment?.code
  363. }
  364. if (app.v3.vouchers && this.state.selectedVoucher?.userVoucherId) {
  365. params.userVoucherIds = [this.state.selectedVoucher.userVoucherId]
  366. }
  367. console.log("[开始充电V3-params]", params);
  368. apiCharge.startChargeV3(params).then(res => {
  369. console.log("[开始充电V3-response]", res);
  370. if (res.data.webPaymentUrl) {
  371. this.setState({
  372. currentPerUse: "Pending"
  373. })
  374. startPage(PageList.paymentWeb, { amount: params.sitePk, url: res.data.webPaymentUrl, type: 'PayPerUse' });
  375. } else {
  376. setTimeout(() => {
  377. this.canAutoRefresh = true;
  378. this.refreshChargeData(500);
  379. if (res.msg) {
  380. toastShort(res.msg)
  381. }
  382. }, 3000);
  383. }
  384. }).catch(({err, code, data}) => {
  385. //toastShort(err);
  386. console.log("[开始充电V3-错误]", err, code);
  387. //Dialog.dismissLoading();
  388. if (code == 5200) {
  389. this.showErrorDialog('none', "(" + data.transactionPk + ') ' + err);
  390. } else {
  391. this.showErrorDialog('A4', err);
  392. }
  393. this.setState({
  394. isCharging: false
  395. });
  396. });
  397. }
  398. onStopCharge() {
  399. Dialog.showDialog({
  400. title: $t('charging.titleStopCharging'),
  401. message: $t('charging.confirmStopCharging'),
  402. ok: $t('nav.confirm'),
  403. callback: ok => {
  404. if (ok == Dialog.BUTTON_OK) {
  405. this.stopCharge();
  406. }
  407. }
  408. });
  409. }
  410. stopCharge() {
  411. this.setState({
  412. isStoping: true
  413. })
  414. //Dialog.showProgressDialog();
  415. apiCharge.stopCharge().then(res => {
  416. if (res.data.chargingPk) {
  417. setTimeout(() => {
  418. //Dialog.dismissLoading();
  419. if (res.msg) {
  420. toastShort(res.msg)
  421. }
  422. this.setState({
  423. chargingPk: res.data.chargingPk,
  424. selectedVoucher: {}
  425. //isCharging: false,
  426. //isAuthentic: false
  427. });
  428. PagerUtil.setSelectedVoucher({});
  429. /*this.init();
  430. startPage(PageList.summary, {
  431. chargingPk: res.data.chargingPk,
  432. id: this.state.stationInfo.id,
  433. name: this.state.stationInfo.name,
  434. address: this.state.stationInfo.address
  435. });*/
  436. }, 3000);
  437. } else {
  438. if (res.msg) {
  439. toastShort(res.msg)
  440. } else {
  441. toastShort($t('charging.errDetected'));
  442. }
  443. this.refreshChargeData(500);
  444. }
  445. }).catch((err) => {
  446. //Dialog.dismissLoading();
  447. toastShort(err);
  448. this.setState({
  449. isStart: false,
  450. isPending: false,
  451. isCharging: false,
  452. isStoping: false
  453. });
  454. //模拟进入结算页
  455. /*startPage(PageList.summary, {
  456. chargingPk: 1,
  457. id: this.state.stationInfo.id,
  458. name: this.state.stationInfo.name,
  459. address: this.state.stationInfo.address
  460. });*/
  461. });
  462. }
  463. showErrorDialog(code, msg) {
  464. this.setState({
  465. errorCode: code,
  466. showErrorDialog: true,
  467. errorMessage: ''+msg
  468. });
  469. }
  470. closeError() {
  471. this.setState({
  472. showErrorDialog: false,
  473. showStationDialog: false
  474. });
  475. }
  476. render() {
  477. return (
  478. <View style={ui.flex1}>
  479. { this.state.isStoping
  480. ? <StepStop
  481. chargingPk={this.state.chargingPk}
  482. stationInfo={this.state.stationInfo}
  483. />
  484. : ( this.state.isCharging
  485. ? <StepCharging
  486. connectorInfo={this.state.connectorInfo}
  487. currentPayment={this.state.currentPayment}
  488. onStopCharge={() => this.onStopCharge()}
  489. selectedVoucher={this.state.selectedVoucher}
  490. />
  491. : ( this.state.isAuthentic
  492. ? <StepAuth
  493. status={this.state.connectorInfo?.status}
  494. connectorInfo={this.state.connectorInfo}
  495. currentPayment={this.state.currentPayment}
  496. onStartCharge={() => this.onStartCharge()}
  497. selectedVoucher={this.state.selectedVoucher}
  498. onPaymentMethodChanged={(type) => this.onPaymentMethodChanged(type)}
  499. />
  500. : <StepStart
  501. connectorInfo={this.state.connectorInfo}
  502. currentPayment={this.state.currentPayment}
  503. onAuthenticate={() => this.onAuthenticate()}
  504. onPaymentMethodChanged={(type) => this.onPaymentMethodChanged(type)}
  505. />
  506. )
  507. )
  508. }
  509. <ErrorDialog
  510. visible={this.state.showErrorDialog}
  511. code={this.state.errorCode}
  512. message={this.state.errorMessage}
  513. onClose={() => {
  514. this.closeError();
  515. }}/>
  516. <DialogPayPerUse
  517. visible={this.state.showDialogPayPerUse}
  518. amount={this.state.stationInfo.payPerUseAmount}
  519. onClose={confirm => this.backDialogPayPerUse(confirm)}/>
  520. </View>
  521. );
  522. }
  523. }