ChargingPage.js 16 KB

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