ChargingPage.js 14 KB

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