ChargingPage.js 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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 && !this.state.isStoping) {
  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.showErrorDialog('A5', $t('charging.errUnable2Charge'));
  119. break;
  120. case 'SuspendedEV': //已连接上但未充电
  121. state.isAuthentic = true;
  122. //this.refreshChargeData();
  123. break;
  124. case 'Reserved': //预定中
  125. this.showErrorDialog('A5', $t('charging.errUnable2Reserved'));
  126. break;
  127. case 'Finishing': //已完成
  128. if (res.data.chargingPk) {
  129. Dialog.showProgressDialog();
  130. setTimeout(() => {
  131. Dialog.dismissLoading();
  132. this.setState({
  133. isStart: false,
  134. isPending: false,
  135. isCharging: false
  136. });
  137. startPage(PageList.summary, {
  138. chargingPk: res.data.chargingPk,
  139. id: this.state.stationInfo.id,
  140. name: this.state.stationInfo.name,
  141. address: this.state.stationInfo.address
  142. });
  143. }, 2000);
  144. } else {
  145. goBack();
  146. }
  147. break;
  148. default:
  149. this.showErrorDialog('A4', $t('charging.errNotChargeE0'));
  150. break;
  151. }
  152. this.setState(state)
  153. }
  154. }).catch(err => {
  155. Dialog.showResultDialog("An error occurred:\n" + err, "Retry", () => {
  156. this.getConnectorInfo();
  157. })
  158. //toastShort(err)
  159. })
  160. }
  161. refreshChargeData(time=2000) {
  162. if (this.isPageShow) {
  163. //console.log("[刷新获取充电信息]", time);
  164. setTimeout(() => {
  165. this.getConnectorInfo();
  166. }, time);
  167. }
  168. }
  169. onPaymentMethodChanged(payment) {
  170. this.setState({
  171. currentPayment: payment
  172. })
  173. }
  174. onAuthenticate() {
  175. this.waitAuthentic = true;
  176. this.setState({
  177. isAuthentic: true
  178. }, () => {
  179. this.refreshChargeData()
  180. })
  181. }
  182. onStartCharge() {
  183. this.setState({
  184. isCharging: true
  185. });
  186. this.waitStartCharging = true;
  187. apiCharge.startCharge(this.state.stationInfo).then(res => {
  188. console.log("[开始充电-onStartCharge]", res);
  189. setTimeout(() => {
  190. this.canAutoRefresh = true;
  191. this.refreshChargeData(500);
  192. //Dialog.dismissLoading();
  193. if (res.msg) {
  194. //Dialog.showResultDialog(res.msg)
  195. toastShort(res.msg)
  196. }
  197. //this.autoCheckIsCharge();
  198. }, 3000);
  199. }).catch(({err, code, data}) => {
  200. //toastShort(err);
  201. console.log("[开始充电错误]", err, code);
  202. //Dialog.dismissLoading();
  203. if (code == 5200) {
  204. this.showErrorDialog('none', "(" + data.transactionPk + ') ' + err);
  205. } else {
  206. this.showErrorDialog('A4', err);
  207. }
  208. });
  209. }
  210. onStopCharge() {
  211. Dialog.showDialog({
  212. title: $t('charging.titleStopCharging'),
  213. message: $t('charging.confirmStopCharging'),
  214. ok: $t('nav.confirm'),
  215. callback: ok => {
  216. if (ok == Dialog.BUTTON_OK) {
  217. this.stopCharge();
  218. }
  219. }
  220. });
  221. }
  222. stopCharge() {
  223. this.setState({
  224. isStoping: true
  225. })
  226. //Dialog.showProgressDialog();
  227. apiCharge.stopCharge().then(res => {
  228. if (res.data.chargingPk) {
  229. setTimeout(() => {
  230. //Dialog.dismissLoading();
  231. if (res.msg) {
  232. toastShort(res.msg)
  233. }
  234. this.setState({
  235. isCharging: false,
  236. isAuthentic: false
  237. });
  238. //this.init();
  239. startPage(PageList.summary, {
  240. chargingPk: res.data.chargingPk,
  241. id: this.state.stationInfo.id,
  242. name: this.state.stationInfo.name,
  243. address: this.state.stationInfo.address
  244. });
  245. }, 3000);
  246. } else {
  247. if (res.msg) {
  248. toastShort(res.msg)
  249. } else {
  250. toastShort($t('charging.errDetected'));
  251. }
  252. this.refreshChargeData(500);
  253. }
  254. }).catch((err) => {
  255. //Dialog.dismissLoading();
  256. toastShort(err);
  257. this.setState({
  258. isStart: false,
  259. isPending: false,
  260. isCharging: false
  261. });
  262. //模拟进入结算页
  263. /*startPage(PageList.summary, {
  264. chargingPk: 1,
  265. id: this.state.stationInfo.id,
  266. name: this.state.stationInfo.name,
  267. address: this.state.stationInfo.address
  268. });*/
  269. });
  270. }
  271. showErrorDialog(code, msg) {
  272. this.setState({
  273. errorCode: code,
  274. showErrorDialog: true,
  275. errorMessage: ''+msg
  276. });
  277. }
  278. closeError() {
  279. this.setState({
  280. showErrorDialog: false,
  281. showStationDialog: false
  282. });
  283. }
  284. render() {
  285. return (
  286. <View style={ui.flex1}>
  287. { this.state.isStoping
  288. ? <StepStop
  289. currentPayment={this.state.currentPayment}
  290. curerntPerUse={this.state.curerntPerUse}
  291. />
  292. : ( this.state.isCharging
  293. ? <StepCharging
  294. connectorInfo={this.state.connectorInfo}
  295. currentPayment={this.state.currentPayment}
  296. curerntPerUse={this.state.curerntPerUse}
  297. onStopCharge={() => this.onStopCharge()}
  298. />
  299. : ( this.state.isAuthentic
  300. ? <StepAuth
  301. status={this.state.connectorInfo?.status}
  302. currentPayment={this.state.currentPayment}
  303. curerntPerUse={this.state.curerntPerUse}
  304. onStartCharge={() => this.onStartCharge()}
  305. />
  306. : <StepStart
  307. connectorInfo={this.state.connectorInfo}
  308. currentPayment={this.state.currentPayment}
  309. curerntPerUse={this.state.curerntPerUse}
  310. onAuthenticate={() => this.onAuthenticate()}
  311. onPaymentMethodChanged={(type) => this.onPaymentMethodChanged(type)}
  312. />
  313. )
  314. )
  315. }
  316. <ErrorDialog
  317. visible={this.state.showErrorDialog}
  318. code={this.state.errorCode}
  319. message={this.state.errorMessage}
  320. onClose={() => {
  321. this.closeError();
  322. }}/>
  323. </View>
  324. );
  325. }
  326. }