ChargingPage.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  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. apiCharge.getConnectorDetail(this.state.stationInfo).then(res => {
  99. if (res.data.status && !this.state.isStoping) {
  100. const state = {
  101. isStoping: false,
  102. isCharging: false,
  103. isAuthentic: false,
  104. connectorInfo: {}
  105. }
  106. state.connectorInfo = res.data;
  107. if (res.data.currentPaymentType && res.data.currentPaymentType == PAYTYPE.PAY_PER_USE) {
  108. state.currentPayment = PAYTYPE.PAY_PER_USE
  109. }
  110. console.log("状态", res.data.status);
  111. switch (res.data.status) {
  112. case 'Available': //可用的
  113. if (this.waitAuthentic) {
  114. state.isAuthentic = true;
  115. this.refreshChargeData(3000);
  116. } else {
  117. state.isAuthentic = false;
  118. }
  119. break;
  120. case 'Preparing': //已插入
  121. this.waitAuthentic = false;
  122. if (this.waitStartCharging) {
  123. state.isCharging = true;
  124. this.refreshChargeData(3000);
  125. } else {
  126. state.isAuthentic = true;
  127. //this.checkIsCharge();
  128. }
  129. break;
  130. case 'Charging': //正在充电
  131. state.isCharging = true;
  132. this.waitStartCharging = false;
  133. this.refreshChargeData(10000);
  134. break;
  135. case 'Initiating': //充电确认中
  136. state.isCharging = true;
  137. this.refreshChargeData();
  138. break;
  139. case 'SuspendedEVSE':
  140. this.showErrorDialog('A5', $t('charging.errUnable2Charge'));
  141. break;
  142. case 'SuspendedEV': //已连接上但未充电
  143. state.isAuthentic = true;
  144. //this.refreshChargeData();
  145. break;
  146. case 'Reserved': //预定中
  147. this.showErrorDialog('A5', $t('charging.errUnable2Reserved'));
  148. break;
  149. case 'Finishing': //已完成
  150. if (res.data.chargingPk) {
  151. Dialog.showProgressDialog();
  152. setTimeout(() => {
  153. Dialog.dismissLoading();
  154. this.setState({
  155. isStart: false,
  156. isPending: false,
  157. isCharging: false
  158. });
  159. startPage(PageList.summary, {
  160. chargingPk: res.data.chargingPk,
  161. id: this.state.stationInfo.id,
  162. name: this.state.stationInfo.name,
  163. address: this.state.stationInfo.address
  164. });
  165. }, 2000);
  166. } else {
  167. goBack();
  168. }
  169. break;
  170. default:
  171. this.showErrorDialog('A4', $t('charging.errNotChargeE0'));
  172. break;
  173. }
  174. this.setState(state)
  175. }
  176. }).catch(err => {
  177. Dialog.showResultDialog("An error occurred:\n" + err, "Retry", () => {
  178. this.getConnectorInfo();
  179. })
  180. //toastShort(err)
  181. })
  182. }
  183. refreshChargeData(time=2000) {
  184. if (this.isPageShow) {
  185. //console.log("[刷新获取充电信息]", time);
  186. setTimeout(() => {
  187. this.getConnectorInfo();
  188. }, time);
  189. }
  190. }
  191. onPaymentMethodChanged(payment) {
  192. this.setState({
  193. currentPayment: payment
  194. })
  195. }
  196. onAuthenticate() {
  197. this.waitAuthentic = true;
  198. this.setState({
  199. isAuthentic: true
  200. }, () => {
  201. this.refreshChargeData()
  202. })
  203. }
  204. onStartCharge() {
  205. this.setState({
  206. isCharging: true
  207. });
  208. this.waitStartCharging = true;
  209. if (this.state.currentPayment == PAYTYPE.PAY_PER_USE) {
  210. this.onStartChargePerUse();
  211. return;
  212. }
  213. apiCharge.startCharge(this.state.stationInfo).then(res => {
  214. console.log("[开始充电-onStartCharge]", res);
  215. setTimeout(() => {
  216. this.canAutoRefresh = true;
  217. this.refreshChargeData(500);
  218. //Dialog.dismissLoading();
  219. if (res.msg) {
  220. //Dialog.showResultDialog(res.msg)
  221. toastShort(res.msg)
  222. }
  223. //this.autoCheckIsCharge();
  224. }, 3000);
  225. }).catch(({err, code, data}) => {
  226. //toastShort(err);
  227. console.log("[开始充电错误]", err, code);
  228. //Dialog.dismissLoading();
  229. if (code == 5200) {
  230. this.showErrorDialog('none', "(" + data.transactionPk + ') ' + err);
  231. } else {
  232. this.showErrorDialog('A4', err);
  233. }
  234. this.setState({
  235. isCharging: false
  236. });
  237. });
  238. }
  239. onStartChargePerUse() {
  240. const params = {
  241. paymentOption: this.state.currentPayment,
  242. ...this.state.stationInfo
  243. }
  244. apiWallet.doPaymentV2(params).then(res => {
  245. if (res.data.webPaymentUrl) {
  246. this.setState({
  247. currentPerUse: "Pending"
  248. })
  249. startPage(PageList.paymentWeb, { amount: params.payAmount, url: res.data.webPaymentUrl, type: 'PayPerUse' });
  250. } else {
  251. toastShort('Error 0')
  252. }
  253. }).catch(({err}) => {
  254. this.showErrorDialog('A9', err);
  255. this.setState({
  256. isCharging: false
  257. });
  258. });
  259. }
  260. onStopCharge() {
  261. Dialog.showDialog({
  262. title: $t('charging.titleStopCharging'),
  263. message: $t('charging.confirmStopCharging'),
  264. ok: $t('nav.confirm'),
  265. callback: ok => {
  266. if (ok == Dialog.BUTTON_OK) {
  267. this.stopCharge();
  268. }
  269. }
  270. });
  271. }
  272. stopCharge() {
  273. this.setState({
  274. isStoping: true
  275. })
  276. //Dialog.showProgressDialog();
  277. apiCharge.stopCharge().then(res => {
  278. if (res.data.chargingPk) {
  279. setTimeout(() => {
  280. //Dialog.dismissLoading();
  281. if (res.msg) {
  282. toastShort(res.msg)
  283. }
  284. this.setState({
  285. isCharging: false,
  286. isAuthentic: false
  287. });
  288. //this.init();
  289. startPage(PageList.summary, {
  290. chargingPk: res.data.chargingPk,
  291. id: this.state.stationInfo.id,
  292. name: this.state.stationInfo.name,
  293. address: this.state.stationInfo.address
  294. });
  295. }, 3000);
  296. } else {
  297. if (res.msg) {
  298. toastShort(res.msg)
  299. } else {
  300. toastShort($t('charging.errDetected'));
  301. }
  302. this.refreshChargeData(500);
  303. }
  304. }).catch((err) => {
  305. //Dialog.dismissLoading();
  306. toastShort(err);
  307. this.setState({
  308. isStart: false,
  309. isPending: false,
  310. isCharging: false
  311. });
  312. //模拟进入结算页
  313. /*startPage(PageList.summary, {
  314. chargingPk: 1,
  315. id: this.state.stationInfo.id,
  316. name: this.state.stationInfo.name,
  317. address: this.state.stationInfo.address
  318. });*/
  319. });
  320. }
  321. showErrorDialog(code, msg) {
  322. this.setState({
  323. errorCode: code,
  324. showErrorDialog: true,
  325. errorMessage: ''+msg
  326. });
  327. }
  328. closeError() {
  329. this.setState({
  330. showErrorDialog: false,
  331. showStationDialog: false
  332. });
  333. }
  334. render() {
  335. return (
  336. <View style={ui.flex1}>
  337. { this.state.isStoping
  338. ? <StepStop
  339. currentPayment={this.state.currentPayment}
  340. />
  341. : ( this.state.isCharging
  342. ? <StepCharging
  343. connectorInfo={this.state.connectorInfo}
  344. currentPayment={this.state.currentPayment}
  345. onStopCharge={() => this.onStopCharge()}
  346. />
  347. : ( this.state.isAuthentic
  348. ? <StepAuth
  349. status={this.state.connectorInfo?.status}
  350. currentPayment={this.state.currentPayment}
  351. onStartCharge={() => this.onStartCharge()}
  352. onPaymentMethodChanged={(type) => this.onPaymentMethodChanged(type)}
  353. />
  354. : <StepStart
  355. connectorInfo={this.state.connectorInfo}
  356. currentPayment={this.state.currentPayment}
  357. onAuthenticate={() => this.onAuthenticate()}
  358. onPaymentMethodChanged={(type) => this.onPaymentMethodChanged(type)}
  359. />
  360. )
  361. )
  362. }
  363. <ErrorDialog
  364. visible={this.state.showErrorDialog}
  365. code={this.state.errorCode}
  366. message={this.state.errorMessage}
  367. onClose={() => {
  368. this.closeError();
  369. }}/>
  370. </View>
  371. );
  372. }
  373. }