ChargingPage.js 15 KB

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