ChargingPage.js 11 KB

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