ChargingPage.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  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("参数", 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. console.log("状态", res.data.status);
  136. switch (res.data.status) {
  137. case 'Available': //可用的
  138. if (this.waitAuthentic) {
  139. state.isAuthentic = true;
  140. this.refreshChargeData(3000);
  141. } else {
  142. state.isAuthentic = false;
  143. }
  144. break;
  145. case 'Preparing': //已插入
  146. this.waitAuthentic = false;
  147. if (this.waitStartCharging) {
  148. state.isCharging = true;
  149. if (res.data.payPerUsePaymentStatus) {
  150. //等待PayPerUse支付-初始化充电
  151. if (res.data.payPerUsePaymentStatus == "PENDING" || res.data.payPerUsePaymentStatus == "PAID") {
  152. this.refreshChargeData(3000);
  153. } else {
  154. this.showErrorDialog('A4', $t('charging.errPayperusePayment') + res.data.payPerUsePaymentStatus);
  155. state.isCharging = false;
  156. state.isAuthentic = true;
  157. }
  158. } else {
  159. //普通充电-初始化充电
  160. this.refreshChargeData(3000);
  161. }
  162. } else {
  163. state.isAuthentic = true;
  164. //this.checkIsCharge();
  165. }
  166. break;
  167. case 'Charging': //正在充电
  168. state.isCharging = true;
  169. this.waitStartCharging = false;
  170. this.refreshChargeData(10000);
  171. break;
  172. case 'Initiating': //充电确认中
  173. state.isCharging = true;
  174. if (res.data.payPerUsePaymentStatus) {
  175. //等待PayPerUse支付-初始化充电
  176. if (res.data.payPerUsePaymentStatus == "PENDING" || res.data.payPerUsePaymentStatus == "PAID") {
  177. this.refreshChargeData();
  178. } else {
  179. this.showErrorDialog('A4', $t('charging.errPayperusePayment') + res.data.payPerUsePaymentStatus);
  180. state.isCharging = false;
  181. state.isAuthentic = false;
  182. }
  183. } else {
  184. //普通充电-初始化充电
  185. this.refreshChargeData();
  186. }
  187. break;
  188. case 'SuspendedEVSE':
  189. this.showErrorDialog('A5', $t('charging.errUnable2Charge'));
  190. break;
  191. case 'SuspendedEV': //已连接上但未充电
  192. state.isAuthentic = true;
  193. //this.refreshChargeData();
  194. break;
  195. case 'Reserved': //预定中
  196. this.showErrorDialog('A5', $t('charging.errUnable2Reserved'));
  197. break;
  198. case 'Finishing': //已完成
  199. if (res.data.chargingPk) {
  200. Dialog.showProgressDialog();
  201. setTimeout(() => {
  202. Dialog.dismissLoading();
  203. this.setState({
  204. isStart: false,
  205. isPending: false,
  206. isCharging: false
  207. });
  208. startPage(PageList.summary, {
  209. chargingPk: res.data.chargingPk,
  210. id: this.state.stationInfo.id,
  211. name: this.state.stationInfo.name,
  212. address: this.state.stationInfo.address
  213. });
  214. }, 2000);
  215. } else {
  216. goBack();
  217. }
  218. break;
  219. default:
  220. this.showErrorDialog('A4', $t('charging.errNotChargeE0'));
  221. break;
  222. }
  223. this.setState(state)
  224. }
  225. }).catch(err => {
  226. Dialog.showResultDialog("An error occurred:\n" + err, "Retry", () => {
  227. this.getConnectorInfo();
  228. })
  229. //toastShort(err)
  230. })
  231. }
  232. refreshChargeData(time=2000) {
  233. if (this.isPageShow) {
  234. //console.log("[刷新获取充电信息]", time);
  235. setTimeout(() => {
  236. this.getConnectorInfo();
  237. }, time);
  238. }
  239. }
  240. onPaymentMethodChanged(payment) {
  241. this.setState({
  242. currentPayment: payment
  243. })
  244. }
  245. onAuthenticate() {
  246. this.waitAuthentic = true;
  247. this.setState({
  248. isAuthentic: true
  249. }, () => {
  250. this.refreshChargeData()
  251. })
  252. }
  253. onStartCharge() {
  254. this.setState({
  255. isCharging: true
  256. });
  257. this.waitStartCharging = true;
  258. if (app.charge.paymentMethod) { //V3版本开始充电
  259. this.onStartChargeV3();
  260. return;
  261. }
  262. if (this.state.currentPayment == PAYTYPE.PAY_PER_USE) { //V2版本PayPerUse
  263. this.onStartChargePerUse();
  264. return;
  265. }
  266. apiCharge.startCharge(this.state.stationInfo).then(res => {
  267. console.log("[开始充电-onStartCharge]", res);
  268. setTimeout(() => {
  269. this.canAutoRefresh = true;
  270. this.refreshChargeData(500);
  271. //Dialog.dismissLoading();
  272. if (res.msg) {
  273. //Dialog.showResultDialog(res.msg)
  274. toastShort(res.msg)
  275. }
  276. //this.autoCheckIsCharge();
  277. }, 3000);
  278. }).catch(({err, code, data}) => {
  279. //toastShort(err);
  280. console.log("[开始充电错误]", err, code);
  281. //Dialog.dismissLoading();
  282. if (code == 5200) {
  283. this.showErrorDialog('none', "(" + data.transactionPk + ') ' + err);
  284. } else {
  285. this.showErrorDialog('A4', err);
  286. }
  287. this.setState({
  288. isCharging: false
  289. });
  290. });
  291. }
  292. onStartChargePerUse() {
  293. const params = {
  294. paymentOption: this.state.currentPayment,
  295. ...this.state.stationInfo
  296. }
  297. apiWallet.doPaymentV2(params).then(res => {
  298. if (res.data.webPaymentUrl) {
  299. this.setState({
  300. currentPerUse: "Pending"
  301. })
  302. startPage(PageList.paymentWeb, { amount: params.payAmount, url: res.data.webPaymentUrl, type: 'PayPerUse' });
  303. } else {
  304. toastShort('Error 0')
  305. }
  306. }).catch(({err}) => {
  307. this.showErrorDialog('A9', err);
  308. this.setState({
  309. isCharging: false
  310. });
  311. });
  312. }
  313. onStartChargeV3() {
  314. const params = {
  315. sitePk: this.state.stationInfo.id,
  316. chargeBoxId: this.state.stationInfo.chargeBoxId,
  317. connectorId: this.state.stationInfo.connectorId
  318. }
  319. if (this.state.currentPayment?.code) {
  320. params.paymentMethod = this.state.currentPayment?.code
  321. }
  322. if (app.v3.vouchers && this.state.selectedVoucher?.userVoucherId) {
  323. params.userVoucherIds = [this.state.selectedVoucher.userVoucherId]
  324. }
  325. console.log("[开始充电V3-params]", params);
  326. apiCharge.startChargeV3(params).then(res => {
  327. console.log("[开始充电V3-response]", res);
  328. if (res.data.webPaymentUrl) {
  329. this.setState({
  330. currentPerUse: "Pending"
  331. })
  332. startPage(PageList.paymentWeb, { amount: params.sitePk, url: res.data.webPaymentUrl, type: 'PayPerUse' });
  333. } else {
  334. setTimeout(() => {
  335. this.canAutoRefresh = true;
  336. this.refreshChargeData(500);
  337. if (res.msg) {
  338. toastShort(res.msg)
  339. }
  340. }, 3000);
  341. }
  342. }).catch(({err, code, data}) => {
  343. //toastShort(err);
  344. console.log("[开始充电V3-错误]", err, code);
  345. //Dialog.dismissLoading();
  346. if (code == 5200) {
  347. this.showErrorDialog('none', "(" + data.transactionPk + ') ' + err);
  348. } else {
  349. this.showErrorDialog('A4', err);
  350. }
  351. this.setState({
  352. isCharging: false
  353. });
  354. });
  355. }
  356. onStopCharge() {
  357. Dialog.showDialog({
  358. title: $t('charging.titleStopCharging'),
  359. message: $t('charging.confirmStopCharging'),
  360. ok: $t('nav.confirm'),
  361. callback: ok => {
  362. if (ok == Dialog.BUTTON_OK) {
  363. this.stopCharge();
  364. }
  365. }
  366. });
  367. }
  368. stopCharge() {
  369. this.setState({
  370. isStoping: true
  371. })
  372. //Dialog.showProgressDialog();
  373. apiCharge.stopCharge().then(res => {
  374. if (res.data.chargingPk) {
  375. setTimeout(() => {
  376. //Dialog.dismissLoading();
  377. if (res.msg) {
  378. toastShort(res.msg)
  379. }
  380. this.setState({
  381. chargingPk: res.data.chargingPk,
  382. selectedVoucher: {}
  383. //isCharging: false,
  384. //isAuthentic: false
  385. });
  386. PagerUtil.setSelectedVoucher({});
  387. /*this.init();
  388. startPage(PageList.summary, {
  389. chargingPk: res.data.chargingPk,
  390. id: this.state.stationInfo.id,
  391. name: this.state.stationInfo.name,
  392. address: this.state.stationInfo.address
  393. });*/
  394. }, 3000);
  395. } else {
  396. if (res.msg) {
  397. toastShort(res.msg)
  398. } else {
  399. toastShort($t('charging.errDetected'));
  400. }
  401. this.refreshChargeData(500);
  402. }
  403. }).catch((err) => {
  404. //Dialog.dismissLoading();
  405. toastShort(err);
  406. this.setState({
  407. isStart: false,
  408. isPending: false,
  409. isCharging: false,
  410. isStoping: false
  411. });
  412. //模拟进入结算页
  413. /*startPage(PageList.summary, {
  414. chargingPk: 1,
  415. id: this.state.stationInfo.id,
  416. name: this.state.stationInfo.name,
  417. address: this.state.stationInfo.address
  418. });*/
  419. });
  420. }
  421. showErrorDialog(code, msg) {
  422. this.setState({
  423. errorCode: code,
  424. showErrorDialog: true,
  425. errorMessage: ''+msg
  426. });
  427. }
  428. closeError() {
  429. this.setState({
  430. showErrorDialog: false,
  431. showStationDialog: false
  432. });
  433. }
  434. render() {
  435. return (
  436. <View style={ui.flex1}>
  437. { this.state.isStoping
  438. ? <StepStop
  439. chargingPk={this.state.chargingPk}
  440. stationInfo={this.state.stationInfo}
  441. />
  442. : ( this.state.isCharging
  443. ? <StepCharging
  444. connectorInfo={this.state.connectorInfo}
  445. currentPayment={this.state.currentPayment}
  446. onStopCharge={() => this.onStopCharge()}
  447. selectedVoucher={this.state.selectedVoucher}
  448. />
  449. : ( this.state.isAuthentic
  450. ? <StepAuth
  451. status={this.state.connectorInfo?.status}
  452. connectorInfo={this.state.connectorInfo}
  453. chargeBoxId={this.state.stationInfo.chargeBoxId}
  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. onPaymentMethodChanged={(type) => this.onPaymentMethodChanged(type)}
  464. />
  465. )
  466. )
  467. }
  468. <ErrorDialog
  469. visible={this.state.showErrorDialog}
  470. code={this.state.errorCode}
  471. message={this.state.errorMessage}
  472. onClose={() => {
  473. this.closeError();
  474. }}/>
  475. </View>
  476. );
  477. }
  478. }