ChargingPage.js 15 KB

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