ChargingPage.js 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  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.waitStartCharging = false;
  35. }
  36. componentDidMount() {
  37. this.init();
  38. console.log("参数", this.props.route.params);
  39. if (this.props.route.params.connectorId && this.props.route.params.chargeBoxId) {
  40. this.setState({
  41. stationInfo: this.props.route.params
  42. }, () => {
  43. //测试进入
  44. //this.testInit();
  45. //正常进入
  46. this.getConnectorInfo();
  47. })
  48. }
  49. }
  50. testInit() {
  51. this.setState({
  52. isCharging: true,
  53. connectorInfo: {
  54. status: "Initiating"
  55. }
  56. }, () => {
  57. setTimeout(() => {
  58. this.getConnectorInfo();
  59. }, 2000);
  60. })
  61. }
  62. init() {
  63. this.setState({
  64. isStoping: false,
  65. isCharging: false,
  66. isAuthentic: false
  67. });
  68. this.waitAuthentic = false;
  69. this.waitStartCharging = false;
  70. }
  71. getConnectorInfo() {
  72. //this.init();
  73. apiCharge.getConnectorDetail(this.state.stationInfo).then(res => {
  74. if (res.data.status) {
  75. const state = {
  76. isStoping: false,
  77. isCharging: false,
  78. isAuthentic: false,
  79. connectorInfo: {}
  80. }
  81. state.connectorInfo = res.data;
  82. console.log("状态", res.data.status);
  83. switch (res.data.status) {
  84. case 'Available': //可用的
  85. if (this.waitAuthentic) {
  86. state.isAuthentic = true;
  87. this.refreshChargeData(3000);
  88. } else {
  89. state.isAuthentic = false;
  90. }
  91. break;
  92. case 'Preparing': //已插入
  93. this.waitAuthentic = false;
  94. if (this.waitStartCharging) {
  95. state.isCharging = true;
  96. this.refreshChargeData(3000);
  97. } else {
  98. state.isAuthentic = true;
  99. //this.checkIsCharge();
  100. }
  101. break;
  102. case 'Charging': //正在充电
  103. state.isCharging = true;
  104. this.waitStartCharging = false;
  105. this.refreshChargeData(10000);
  106. break;
  107. case 'Initiating': //充电确认中
  108. state.isCharging = true;
  109. this.refreshChargeData();
  110. break;
  111. case 'SuspendedEVSE':
  112. this.setState({
  113. errorCode: 'A5',
  114. showErrorDialog: true,
  115. errorMessage: $t('charging.errUnable2Charge')
  116. });
  117. break;
  118. case 'SuspendedEV': //已连接上但未充电
  119. state.isAuthentic = true;
  120. //this.refreshChargeData();
  121. break;
  122. case 'Reserved': //预定中
  123. this.setState({
  124. errorCode: 'A5',
  125. showErrorDialog: true,
  126. errorMessage: $t('charging.errUnable2Reserved')
  127. });
  128. break;
  129. case 'Finishing': //已完成
  130. if (res.data.chargingPk) {
  131. Dialog.showProgressDialog();
  132. setTimeout(() => {
  133. Dialog.dismissLoading();
  134. this.setState({
  135. isStart: false,
  136. isPending: false,
  137. isCharging: false
  138. });
  139. startPage(PageList.summary, {
  140. chargingPk: res.data.chargingPk,
  141. id: this.state.stationInfo.id,
  142. name: this.state.stationInfo.name,
  143. address: this.state.stationInfo.address
  144. });
  145. }, 2000);
  146. } else {
  147. goBack();
  148. }
  149. break;
  150. default:
  151. this.setState({
  152. errorCode: 'A4',
  153. showErrorDialog: true,
  154. errorMessage: $t('charging.errNotChargeE0')
  155. });
  156. break;
  157. }
  158. this.setState(state)
  159. }
  160. }).catch(err => {
  161. Dialog.showResultDialog("An error occurred:\n" + err, "Retry", () => {
  162. this.getConnectorInfo();
  163. })
  164. //toastShort(err)
  165. })
  166. }
  167. refreshChargeData(time=2000) {
  168. //console.log("[刷新获取充电信息]", time);
  169. setTimeout(() => {
  170. this.getConnectorInfo();
  171. }, time);
  172. }
  173. onPaymentMethodChanged(payment) {
  174. this.setState({
  175. currentPayment: payment
  176. })
  177. }
  178. onAuthenticate() {
  179. this.waitAuthentic = true;
  180. this.setState({
  181. isAuthentic: true
  182. }, () => {
  183. this.refreshChargeData()
  184. })
  185. }
  186. onStartCharge() {
  187. this.setState({
  188. isCharging: true
  189. });
  190. this.waitStartCharging = true;
  191. apiCharge.startCharge(this.state.stationInfo).then(res => {
  192. console.log("[开始充电-onStartCharge]", res);
  193. setTimeout(() => {
  194. this.canAutoRefresh = true;
  195. this.refreshChargeData(500);
  196. //Dialog.dismissLoading();
  197. if (res.msg) {
  198. //Dialog.showResultDialog(res.msg)
  199. toastShort(res.msg)
  200. }
  201. //this.autoCheckIsCharge();
  202. }, 3000);
  203. }).catch(({err, code, data}) => {
  204. //toastShort(err);
  205. console.log("[开始充电错误]", err, code);
  206. //Dialog.dismissLoading();
  207. if (code == 5200) {
  208. this.setState({
  209. errorCode: 'none',
  210. showErrorDialog: true,
  211. errorMessage: "(" + data.transactionPk + ') ' + err
  212. });
  213. } else {
  214. this.setState({
  215. errorCode: 'A4',
  216. showErrorDialog: true,
  217. errorMessage: ''+err
  218. });
  219. }
  220. });
  221. }
  222. onStopCharge() {
  223. Dialog.showDialog({
  224. title: $t('charging.titleStopCharging'),
  225. message: $t('charging.confirmStopCharging'),
  226. callback: ok => {
  227. if (ok == Dialog.BUTTON_OK) {
  228. this.stopCharge();
  229. }
  230. }
  231. });
  232. }
  233. stopCharge() {
  234. this.setState({
  235. isStoping: true
  236. })
  237. //Dialog.showProgressDialog();
  238. apiCharge.stopCharge().then(res => {
  239. if (res.data.chargingPk) {
  240. setTimeout(() => {
  241. //Dialog.dismissLoading();
  242. if (res.msg) {
  243. toastShort(res.msg)
  244. }
  245. //this.init();
  246. startPage(PageList.summary, {
  247. chargingPk: res.data.chargingPk,
  248. id: this.state.stationInfo.id,
  249. name: this.state.stationInfo.name,
  250. address: this.state.stationInfo.address
  251. });
  252. }, 3000);
  253. } else {
  254. if (res.msg) {
  255. toastShort(res.msg)
  256. } else {
  257. toastShort($t('charging.errDetected'));
  258. }
  259. this.refreshChargeData(500);
  260. }
  261. }).catch((err) => {
  262. //Dialog.dismissLoading();
  263. toastShort(err);
  264. this.setState({
  265. isStart: false,
  266. isPending: false,
  267. isCharging: false
  268. });
  269. //模拟进入结算页
  270. /*startPage(PageList.summary, {
  271. chargingPk: 1,
  272. id: this.state.stationInfo.id,
  273. name: this.state.stationInfo.name,
  274. address: this.state.stationInfo.address
  275. });*/
  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. />
  324. </View>
  325. );
  326. }
  327. }