ChargingPage.js 15 KB

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