ChargingPage.js 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  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 Dialog from '../../components/Dialog';
  9. import { ErrorDialog } from '../chargeV2/InfoDialog';
  10. import { PaymentDefault } from '../payment/PaymentConfig';
  11. import { PageList } from '../Router';
  12. import StepAuth from './StepAuth';
  13. import StepCharging from './StepCharging';
  14. import StepStart from './StepStart';
  15. import StepStop from './StepStop';
  16. export default class ChargingPage extends Component {
  17. constructor(props) {
  18. super(props);
  19. this.state = {
  20. isStoping: false,
  21. isCharging: false,
  22. isAuthentic: false,
  23. stationInfo: {},
  24. connectorInfo: {},
  25. errorCode: 'A9',
  26. errorMessage: '',
  27. lastUpdated: '',
  28. showErrorDialog: false,
  29. showStationDialog: false,
  30. curerntPerUse: undefined,
  31. currentPayment: PaymentDefault.DEFAULT.payType,
  32. currentPaytype: PaymentDefault.DEFAULT.payName
  33. };
  34. this.isPageShow = true;
  35. this.waitStartCharging = false;
  36. }
  37. componentDidMount() {
  38. this.init();
  39. this.isPageShow = true;
  40. console.log("参数", this.props.route.params);
  41. if (this.props.route.params.connectorId && this.props.route.params.chargeBoxId) {
  42. this.setState({
  43. stationInfo: this.props.route.params
  44. }, () => {
  45. //测试进入
  46. //this.testInit();
  47. //正常进入
  48. this.getConnectorInfo();
  49. })
  50. }
  51. }
  52. componentWillUnmount() {
  53. this.isPageShow = false;
  54. }
  55. testInit() {
  56. this.setState({
  57. isCharging: true,
  58. connectorInfo: {
  59. status: "Initiating"
  60. }
  61. }, () => {
  62. setTimeout(() => {
  63. this.getConnectorInfo();
  64. }, 2000);
  65. })
  66. }
  67. init() {
  68. this.setState({
  69. isStoping: false,
  70. isCharging: false,
  71. isAuthentic: false
  72. });
  73. this.waitAuthentic = false;
  74. this.waitStartCharging = false;
  75. }
  76. getConnectorInfo() {
  77. if (!this.isPageShow) return;
  78. //this.init();
  79. apiCharge.getConnectorDetail(this.state.stationInfo).then(res => {
  80. if (res.data.status) {
  81. const state = {
  82. isStoping: false,
  83. isCharging: false,
  84. isAuthentic: false,
  85. connectorInfo: {}
  86. }
  87. state.connectorInfo = res.data;
  88. console.log("状态", res.data.status);
  89. switch (res.data.status) {
  90. case 'Available': //可用的
  91. if (this.waitAuthentic) {
  92. state.isAuthentic = true;
  93. this.refreshChargeData(3000);
  94. } else {
  95. state.isAuthentic = false;
  96. }
  97. break;
  98. case 'Preparing': //已插入
  99. this.waitAuthentic = false;
  100. if (this.waitStartCharging) {
  101. state.isCharging = true;
  102. this.refreshChargeData(3000);
  103. } else {
  104. state.isAuthentic = true;
  105. //this.checkIsCharge();
  106. }
  107. break;
  108. case 'Charging': //正在充电
  109. state.isCharging = true;
  110. this.waitStartCharging = false;
  111. this.refreshChargeData(10000);
  112. break;
  113. case 'Initiating': //充电确认中
  114. state.isCharging = true;
  115. this.refreshChargeData();
  116. break;
  117. case 'SuspendedEVSE':
  118. this.setState({
  119. errorCode: 'A5',
  120. showErrorDialog: true,
  121. errorMessage: $t('charging.errUnable2Charge')
  122. });
  123. break;
  124. case 'SuspendedEV': //已连接上但未充电
  125. state.isAuthentic = true;
  126. //this.refreshChargeData();
  127. break;
  128. case 'Reserved': //预定中
  129. this.setState({
  130. errorCode: 'A5',
  131. showErrorDialog: true,
  132. errorMessage: $t('charging.errUnable2Reserved')
  133. });
  134. break;
  135. case 'Finishing': //已完成
  136. if (res.data.chargingPk) {
  137. Dialog.showProgressDialog();
  138. setTimeout(() => {
  139. Dialog.dismissLoading();
  140. this.setState({
  141. isStart: false,
  142. isPending: false,
  143. isCharging: false
  144. });
  145. startPage(PageList.summary, {
  146. chargingPk: res.data.chargingPk,
  147. id: this.state.stationInfo.id,
  148. name: this.state.stationInfo.name,
  149. address: this.state.stationInfo.address
  150. });
  151. }, 2000);
  152. } else {
  153. goBack();
  154. }
  155. break;
  156. default:
  157. this.setState({
  158. errorCode: 'A4',
  159. showErrorDialog: true,
  160. errorMessage: $t('charging.errNotChargeE0')
  161. });
  162. break;
  163. }
  164. this.setState(state)
  165. }
  166. }).catch(err => {
  167. Dialog.showResultDialog("An error occurred:\n" + err, "Retry", () => {
  168. this.getConnectorInfo();
  169. })
  170. //toastShort(err)
  171. })
  172. }
  173. refreshChargeData(time=2000) {
  174. if (this.isPageShow) {
  175. //console.log("[刷新获取充电信息]", time);
  176. setTimeout(() => {
  177. this.getConnectorInfo();
  178. }, time);
  179. }
  180. }
  181. onPaymentMethodChanged(payment) {
  182. this.setState({
  183. currentPayment: payment
  184. })
  185. }
  186. onAuthenticate() {
  187. this.waitAuthentic = true;
  188. this.setState({
  189. isAuthentic: true
  190. }, () => {
  191. this.refreshChargeData()
  192. })
  193. }
  194. onStartCharge() {
  195. this.setState({
  196. isCharging: true
  197. });
  198. this.waitStartCharging = true;
  199. apiCharge.startCharge(this.state.stationInfo).then(res => {
  200. console.log("[开始充电-onStartCharge]", res);
  201. setTimeout(() => {
  202. this.canAutoRefresh = true;
  203. this.refreshChargeData(500);
  204. //Dialog.dismissLoading();
  205. if (res.msg) {
  206. //Dialog.showResultDialog(res.msg)
  207. toastShort(res.msg)
  208. }
  209. //this.autoCheckIsCharge();
  210. }, 3000);
  211. }).catch(({err, code, data}) => {
  212. //toastShort(err);
  213. console.log("[开始充电错误]", err, code);
  214. //Dialog.dismissLoading();
  215. if (code == 5200) {
  216. this.setState({
  217. errorCode: 'none',
  218. showErrorDialog: true,
  219. errorMessage: "(" + data.transactionPk + ') ' + err
  220. });
  221. } else {
  222. this.setState({
  223. errorCode: 'A4',
  224. showErrorDialog: true,
  225. errorMessage: ''+err
  226. });
  227. }
  228. });
  229. }
  230. onStopCharge() {
  231. Dialog.showDialog({
  232. title: $t('charging.titleStopCharging'),
  233. message: $t('charging.confirmStopCharging'),
  234. ok: $t('nav.confirm'),
  235. callback: ok => {
  236. if (ok == Dialog.BUTTON_OK) {
  237. this.stopCharge();
  238. }
  239. }
  240. });
  241. }
  242. stopCharge() {
  243. this.setState({
  244. isStoping: true
  245. })
  246. //Dialog.showProgressDialog();
  247. apiCharge.stopCharge().then(res => {
  248. if (res.data.chargingPk) {
  249. setTimeout(() => {
  250. //Dialog.dismissLoading();
  251. if (res.msg) {
  252. toastShort(res.msg)
  253. }
  254. //this.init();
  255. startPage(PageList.summary, {
  256. chargingPk: res.data.chargingPk,
  257. id: this.state.stationInfo.id,
  258. name: this.state.stationInfo.name,
  259. address: this.state.stationInfo.address
  260. });
  261. }, 3000);
  262. } else {
  263. if (res.msg) {
  264. toastShort(res.msg)
  265. } else {
  266. toastShort($t('charging.errDetected'));
  267. }
  268. this.refreshChargeData(500);
  269. }
  270. }).catch((err) => {
  271. //Dialog.dismissLoading();
  272. toastShort(err);
  273. this.setState({
  274. isStart: false,
  275. isPending: false,
  276. isCharging: false
  277. });
  278. //模拟进入结算页
  279. /*startPage(PageList.summary, {
  280. chargingPk: 1,
  281. id: this.state.stationInfo.id,
  282. name: this.state.stationInfo.name,
  283. address: this.state.stationInfo.address
  284. });*/
  285. });
  286. }
  287. closeError() {
  288. this.setState({
  289. showErrorDialog: false,
  290. showStationDialog: false
  291. });
  292. }
  293. render() {
  294. return (
  295. <View style={ui.flex1}>
  296. { this.state.isStoping
  297. ? <StepStop
  298. currentPayment={this.state.currentPayment}
  299. curerntPerUse={this.state.curerntPerUse}
  300. />
  301. : ( this.state.isCharging
  302. ? <StepCharging
  303. connectorInfo={this.state.connectorInfo}
  304. currentPayment={this.state.currentPayment}
  305. curerntPerUse={this.state.curerntPerUse}
  306. onStopCharge={() => this.onStopCharge()}
  307. />
  308. : ( this.state.isAuthentic
  309. ? <StepAuth
  310. status={this.state.connectorInfo?.status}
  311. currentPayment={this.state.currentPayment}
  312. curerntPerUse={this.state.curerntPerUse}
  313. onStartCharge={() => this.onStartCharge()}
  314. />
  315. : <StepStart
  316. connectorInfo={this.state.connectorInfo}
  317. currentPayment={this.state.currentPayment}
  318. curerntPerUse={this.state.curerntPerUse}
  319. onAuthenticate={() => this.onAuthenticate()}
  320. onPaymentMethodChanged={(type) => this.onPaymentMethodChanged(type)}
  321. />
  322. )
  323. )
  324. }
  325. <ErrorDialog
  326. visible={this.state.showErrorDialog}
  327. code={this.state.errorCode}
  328. message={this.state.errorMessage}
  329. onClose={() => {
  330. this.closeError();
  331. }}
  332. />
  333. </View>
  334. );
  335. }
  336. }