ChargingPage.js 12 KB

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