ChargingPage.js 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  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.refreshChargeData(0);
  47. })
  48. }
  49. }
  50. testInit() {
  51. this.setState({
  52. isCharging: true,
  53. connectorInfo: {
  54. status: "Initiating"
  55. }
  56. }, () => {
  57. this.refreshChargeData(2000);
  58. })
  59. }
  60. init() {
  61. this.setState({
  62. isStoping: false,
  63. isCharging: false,
  64. isAuthentic: false
  65. });
  66. this.waitAuthentic = false;
  67. this.waitStartCharging = false;
  68. }
  69. getConnectorInfo() {
  70. //this.init();
  71. apiCharge.getConnectorDetail(this.state.stationInfo).then(res => {
  72. if (res.data.status) {
  73. const state = {
  74. isStoping: false,
  75. isCharging: false,
  76. isAuthentic: false,
  77. connectorInfo: {}
  78. }
  79. state.connectorInfo = res.data;
  80. console.log("状态", res.data.status);
  81. switch (res.data.status) {
  82. case 'Available': //可用的
  83. if (this.waitAuthentic) {
  84. state.isAuthentic = true;
  85. this.refreshChargeData(3000);
  86. } else {
  87. state.isAuthentic = false;
  88. }
  89. break;
  90. case 'Preparing': //已插入
  91. this.waitAuthentic = false;
  92. if (this.waitStartCharging) {
  93. state.isCharging = true;
  94. this.refreshChargeData(3000);
  95. } else {
  96. state.isAuthentic = true;
  97. //this.checkIsCharge();
  98. }
  99. break;
  100. case 'Charging': //正在充电
  101. state.isCharging = true;
  102. this.waitStartCharging = false;
  103. this.refreshChargeData(10000);
  104. break;
  105. case 'Initiating': //充电确认中
  106. state.isCharging = true;
  107. this.refreshChargeData();
  108. break;
  109. case 'SuspendedEVSE':
  110. this.setState({
  111. errorCode: 'A5',
  112. showErrorDialog: true,
  113. errorMessage: $t('charging.errUnable2Charge')
  114. });
  115. break;
  116. case 'SuspendedEV': //已连接上但未充电
  117. state.isAuthentic = true;
  118. //this.refreshChargeData();
  119. break;
  120. case 'Reserved': //预定中
  121. this.setState({
  122. errorCode: 'A5',
  123. showErrorDialog: true,
  124. errorMessage: $t('charging.errUnable2Reserved')
  125. });
  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.setState({
  150. errorCode: 'A4',
  151. showErrorDialog: true,
  152. errorMessage: $t('charging.errNotChargeE0')
  153. });
  154. break;
  155. }
  156. this.setState(state);
  157. }
  158. }).catch(err => {
  159. Dialog.showResultDialog("An error occurred:\n" + err, "Retry", () => {
  160. this.getConnectorInfo();
  161. });
  162. //toastShort(err)
  163. })
  164. }
  165. refreshChargeData(time=2000) {
  166. //console.log("[刷新获取充电信息]", time);
  167. setTimeout(() => {
  168. this.getConnectorInfo();
  169. }, time);
  170. }
  171. onPaymentMethodChanged(payment) {
  172. this.setState({
  173. currentPayment: payment
  174. })
  175. }
  176. onAuthenticate() {
  177. this.waitAuthentic = true;
  178. this.setState({
  179. isAuthentic: true
  180. }, () => {
  181. this.refreshChargeData()
  182. })
  183. }
  184. onStartCharge() {
  185. this.setState({
  186. isCharging: true
  187. });
  188. this.waitStartCharging = true;
  189. apiCharge.startCharge(this.state.stationInfo).then(res => {
  190. console.log("[开始充电-onStartCharge]", res);
  191. setTimeout(() => {
  192. this.canAutoRefresh = true;
  193. this.refreshChargeData(500);
  194. //Dialog.dismissLoading();
  195. if (res.msg) {
  196. //Dialog.showResultDialog(res.msg)
  197. toastShort(res.msg)
  198. }
  199. //this.autoCheckIsCharge();
  200. }, 3000);
  201. }).catch(({err, code, data}) => {
  202. //toastShort(err);
  203. console.log("[开始充电错误]", err, code);
  204. //Dialog.dismissLoading();
  205. if (code == 5200) {
  206. this.setState({
  207. errorCode: 'none',
  208. showErrorDialog: true,
  209. errorMessage: "(" + data.transactionPk + ') ' + err
  210. });
  211. } else {
  212. this.setState({
  213. errorCode: 'A4',
  214. showErrorDialog: true,
  215. errorMessage: ''+err
  216. });
  217. }
  218. });
  219. }
  220. onStopCharge() {
  221. Dialog.showDialog({
  222. title: $t('charging.titleStopCharging'),
  223. message: $t('charging.confirmStopCharging'),
  224. callback: ok => {
  225. if (ok == Dialog.BUTTON_OK) {
  226. this.stopCharge();
  227. }
  228. }
  229. });
  230. }
  231. stopCharge() {
  232. this.setState({
  233. isStoping: true
  234. })
  235. //Dialog.showProgressDialog();
  236. apiCharge.stopCharge().then(res => {
  237. if (res.data.chargingPk) {
  238. setTimeout(() => {
  239. //Dialog.dismissLoading();
  240. if (res.msg) {
  241. toastShort(res.msg)
  242. }
  243. //this.init();
  244. startPage(PageList.summary, {
  245. chargingPk: res.data.chargingPk,
  246. id: this.state.stationInfo.id,
  247. name: this.state.stationInfo.name,
  248. address: this.state.stationInfo.address
  249. });
  250. }, 3000);
  251. } else {
  252. if (res.msg) {
  253. toastShort(res.msg)
  254. } else {
  255. toastShort($t('charging.errDetected'));
  256. }
  257. this.refreshChargeData(500);
  258. }
  259. }).catch((err) => {
  260. //Dialog.dismissLoading();
  261. toastShort(err);
  262. this.setState({
  263. isStart: false,
  264. isPending: false,
  265. isCharging: false
  266. });
  267. //模拟进入结算页
  268. /*startPage(PageList.summary, {
  269. chargingPk: 1,
  270. id: this.state.stationInfo.id,
  271. name: this.state.stationInfo.name,
  272. address: this.state.stationInfo.address
  273. });*/
  274. });
  275. }
  276. closeError() {
  277. this.setState({
  278. showErrorDialog: false,
  279. showStationDialog: false
  280. });
  281. }
  282. render() {
  283. return (
  284. <View style={ui.flex1}>
  285. { this.state.isStoping
  286. ? <StepStop
  287. currentPayment={this.state.currentPayment}
  288. curerntPerUse={this.state.curerntPerUse}
  289. />
  290. : ( this.state.isCharging
  291. ? <StepCharging
  292. connectorInfo={this.state.connectorInfo}
  293. currentPayment={this.state.currentPayment}
  294. curerntPerUse={this.state.curerntPerUse}
  295. onStopCharge={() => this.onStopCharge()}
  296. />
  297. : ( this.state.isAuthentic
  298. ? <StepAuth
  299. status={this.state.connectorInfo?.status}
  300. currentPayment={this.state.currentPayment}
  301. curerntPerUse={this.state.curerntPerUse}
  302. onStartCharge={() => this.onStartCharge()}
  303. />
  304. : <StepStart
  305. connectorInfo={this.state.connectorInfo}
  306. currentPayment={this.state.currentPayment}
  307. curerntPerUse={this.state.curerntPerUse}
  308. onAuthenticate={() => this.onAuthenticate()}
  309. onPaymentMethodChanged={(type) => this.onPaymentMethodChanged(type)}
  310. />
  311. )
  312. )
  313. }
  314. <ErrorDialog
  315. visible={this.state.showErrorDialog}
  316. code={this.state.errorCode}
  317. message={this.state.errorMessage}
  318. onClose={() => {
  319. this.closeError();
  320. }}
  321. />
  322. </View>
  323. );
  324. }
  325. }