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.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. ok: $t('nav.confirm'),
  227. callback: ok => {
  228. if (ok == Dialog.BUTTON_OK) {
  229. this.stopCharge();
  230. }
  231. }
  232. });
  233. }
  234. stopCharge() {
  235. this.setState({
  236. isStoping: true
  237. })
  238. //Dialog.showProgressDialog();
  239. apiCharge.stopCharge().then(res => {
  240. if (res.data.chargingPk) {
  241. setTimeout(() => {
  242. //Dialog.dismissLoading();
  243. if (res.msg) {
  244. toastShort(res.msg)
  245. }
  246. //this.init();
  247. startPage(PageList.summary, {
  248. chargingPk: res.data.chargingPk,
  249. id: this.state.stationInfo.id,
  250. name: this.state.stationInfo.name,
  251. address: this.state.stationInfo.address
  252. });
  253. }, 3000);
  254. } else {
  255. if (res.msg) {
  256. toastShort(res.msg)
  257. } else {
  258. toastShort($t('charging.errDetected'));
  259. }
  260. this.refreshChargeData(500);
  261. }
  262. }).catch((err) => {
  263. //Dialog.dismissLoading();
  264. toastShort(err);
  265. this.setState({
  266. isStart: false,
  267. isPending: false,
  268. isCharging: false
  269. });
  270. //模拟进入结算页
  271. /*startPage(PageList.summary, {
  272. chargingPk: 1,
  273. id: this.state.stationInfo.id,
  274. name: this.state.stationInfo.name,
  275. address: this.state.stationInfo.address
  276. });*/
  277. });
  278. }
  279. closeError() {
  280. this.setState({
  281. showErrorDialog: false,
  282. showStationDialog: false
  283. });
  284. }
  285. render() {
  286. return (
  287. <View style={ui.flex1}>
  288. { this.state.isStoping
  289. ? <StepStop
  290. currentPayment={this.state.currentPayment}
  291. curerntPerUse={this.state.curerntPerUse}
  292. />
  293. : ( this.state.isCharging
  294. ? <StepCharging
  295. connectorInfo={this.state.connectorInfo}
  296. currentPayment={this.state.currentPayment}
  297. curerntPerUse={this.state.curerntPerUse}
  298. onStopCharge={() => this.onStopCharge()}
  299. />
  300. : ( this.state.isAuthentic
  301. ? <StepAuth
  302. status={this.state.connectorInfo?.status}
  303. currentPayment={this.state.currentPayment}
  304. curerntPerUse={this.state.curerntPerUse}
  305. onStartCharge={() => this.onStartCharge()}
  306. />
  307. : <StepStart
  308. connectorInfo={this.state.connectorInfo}
  309. currentPayment={this.state.currentPayment}
  310. curerntPerUse={this.state.curerntPerUse}
  311. onAuthenticate={() => this.onAuthenticate()}
  312. onPaymentMethodChanged={(type) => this.onPaymentMethodChanged(type)}
  313. />
  314. )
  315. )
  316. }
  317. <ErrorDialog
  318. visible={this.state.showErrorDialog}
  319. code={this.state.errorCode}
  320. message={this.state.errorMessage}
  321. onClose={() => {
  322. this.closeError();
  323. }}
  324. />
  325. </View>
  326. );
  327. }
  328. }