ChargingPage.js 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  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. })
  161. }
  162. refreshChargeData(time=2000) {
  163. //console.log("[刷新获取充电信息]", time);
  164. setTimeout(() => {
  165. this.getConnectorInfo();
  166. }, time);
  167. }
  168. onPaymentMethodChanged(payment) {
  169. this.setState({
  170. currentPayment: payment
  171. })
  172. }
  173. onAuthenticate() {
  174. this.waitAuthentic = true;
  175. this.setState({
  176. isAuthentic: true
  177. }, () => {
  178. this.refreshChargeData()
  179. })
  180. }
  181. onStartCharge() {
  182. this.setState({
  183. isCharging: true
  184. });
  185. this.waitStartCharging = true;
  186. apiCharge.startCharge(this.state.stationInfo).then(res => {
  187. console.log("[开始充电-onStartCharge]", res);
  188. setTimeout(() => {
  189. this.canAutoRefresh = true;
  190. this.refreshChargeData(500);
  191. //Dialog.dismissLoading();
  192. if (res.msg) {
  193. //Dialog.showResultDialog(res.msg)
  194. toastShort(res.msg)
  195. }
  196. //this.autoCheckIsCharge();
  197. }, 3000);
  198. }).catch(({err, code, data}) => {
  199. //toastShort(err);
  200. console.log("[开始充电错误]", err, code);
  201. //Dialog.dismissLoading();
  202. if (code == 5200) {
  203. this.setState({
  204. errorCode: 'none',
  205. showErrorDialog: true,
  206. errorMessage: "(" + data.transactionPk + ') ' + err
  207. });
  208. } else {
  209. this.setState({
  210. errorCode: 'A4',
  211. showErrorDialog: true,
  212. errorMessage: ''+err
  213. });
  214. }
  215. });
  216. }
  217. onStopCharge() {
  218. Dialog.showDialog({
  219. title: $t('charging.titleStopCharging'),
  220. message: $t('charging.confirmStopCharging'),
  221. callback: ok => {
  222. if (ok == Dialog.BUTTON_OK) {
  223. this.stopCharge();
  224. }
  225. }
  226. });
  227. }
  228. stopCharge() {
  229. this.setState({
  230. isStoping: true
  231. })
  232. //Dialog.showProgressDialog();
  233. apiCharge.stopCharge().then(res => {
  234. if (res.data.chargingPk) {
  235. setTimeout(() => {
  236. //Dialog.dismissLoading();
  237. if (res.msg) {
  238. toastShort(res.msg)
  239. }
  240. //this.init();
  241. startPage(PageList.summary, {
  242. chargingPk: res.data.chargingPk,
  243. id: this.state.stationInfo.id,
  244. name: this.state.stationInfo.name,
  245. address: this.state.stationInfo.address
  246. });
  247. }, 3000);
  248. } else {
  249. if (res.msg) {
  250. toastShort(res.msg)
  251. } else {
  252. toastShort($t('charging.errDetected'));
  253. }
  254. this.refreshChargeData(500);
  255. }
  256. }).catch((err) => {
  257. //Dialog.dismissLoading();
  258. toastShort(err);
  259. this.setState({
  260. isStart: false,
  261. isPending: false,
  262. isCharging: false
  263. });
  264. //模拟进入结算页
  265. /*startPage(PageList.summary, {
  266. chargingPk: 1,
  267. id: this.state.stationInfo.id,
  268. name: this.state.stationInfo.name,
  269. address: this.state.stationInfo.address
  270. });*/
  271. });
  272. }
  273. closeError() {
  274. this.setState({
  275. showErrorDialog: false,
  276. showStationDialog: false
  277. });
  278. }
  279. render() {
  280. return (
  281. <View style={ui.flex1}>
  282. { this.state.isStoping
  283. ? <StepStop
  284. currentPayment={this.state.currentPayment}
  285. curerntPerUse={this.state.curerntPerUse}
  286. />
  287. : ( this.state.isCharging
  288. ? <StepCharging
  289. connectorInfo={this.state.connectorInfo}
  290. currentPayment={this.state.currentPayment}
  291. curerntPerUse={this.state.curerntPerUse}
  292. onStopCharge={() => this.onStopCharge()}
  293. />
  294. : ( this.state.isAuthentic
  295. ? <StepAuth
  296. status={this.state.connectorInfo?.status}
  297. currentPayment={this.state.currentPayment}
  298. curerntPerUse={this.state.curerntPerUse}
  299. onStartCharge={() => this.onStartCharge()}
  300. />
  301. : <StepStart
  302. connectorInfo={this.state.connectorInfo}
  303. currentPayment={this.state.currentPayment}
  304. curerntPerUse={this.state.curerntPerUse}
  305. onAuthenticate={() => this.onAuthenticate()}
  306. onPaymentMethodChanged={(type) => this.onPaymentMethodChanged(type)}
  307. />
  308. )
  309. )
  310. }
  311. <ErrorDialog
  312. visible={this.state.showErrorDialog}
  313. code={this.state.errorCode}
  314. message={this.state.errorMessage}
  315. onClose={() => {
  316. this.closeError();
  317. }}
  318. />
  319. </View>
  320. );
  321. }
  322. }