charge.ts 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569
  1. import Http from "../utils/http";
  2. import {host, isDebug} from "../utils/constant";
  3. const cHttp = new Http(host);
  4. export function startCharge(connectorId: string, query?: string) {
  5. return cHttp.get<{
  6. ConnectorID: string;
  7. FailReason: number;
  8. StartChargeSeq: string;
  9. StartChargeSeqStat: number;
  10. SuccStat: number;
  11. }>(`/charge/startCharge/${connectorId}?${query}`, {
  12. statusCodeHandle: false,
  13. });
  14. }
  15. export async function cancelAppointmentCharge() {
  16. return cHttp.get("/charge/cancelBooking");
  17. }
  18. export async function startAppointmentCharge(connectorId: string) {
  19. return cHttp.get(`/charge/immediatelyCharge/${connectorId}`);
  20. }
  21. export async function searchQRCode(imgUrl: string) {
  22. return cHttp.get(`/charge/qrCode?imgUrl=${imgUrl}`);
  23. }
  24. export async function changeAppointmentTime(
  25. startChargeSeq: string,
  26. startTime: string
  27. ) {
  28. return cHttp.get(
  29. `/charge/modifyBookingTime?startChargeSeq=${startChargeSeq}&startTime=${startTime}`
  30. );
  31. }
  32. export function fetchStationPriceDesc(ConnectorID: string, StationID?: string) {
  33. return cHttp.get(`/charge/businessPolicy/${ConnectorID}`).then((res) => {
  34. const nowHour = new Date().getHours();
  35. let maxPrice = 0;
  36. let minPrice = 9;
  37. let minPriceTime = "00:00";
  38. let currentPrice = 0;
  39. let currentTime = "00:00~24:00";
  40. res.useTime = "";
  41. if (res && res.policyInfos && res.policyInfos.length) {
  42. res.policyInfos.forEach((item: any, index: number) => {
  43. const hour = item.startTime.substring(0, 2);
  44. const min = item.startTime.substring(3, 5);
  45. if (index === 0) {
  46. res.useTime = `${hour}:${min}~24:00`;
  47. }
  48. let tempPrice = Number(
  49. Number(item.elecPrice + item.servicePrice).toFixed(2)
  50. );
  51. item.totalPrice = tempPrice;
  52. if (tempPrice > maxPrice) {
  53. maxPrice = tempPrice;
  54. }
  55. if (index >= res.policyInfos.length - 1) {
  56. // 最后一个
  57. item.startTimeFormat = `${hour}:${min}~24:00`;
  58. if (Number(hour) <= nowHour) {
  59. currentPrice = tempPrice;
  60. currentTime = `${hour}:${min}~24:00`;
  61. }
  62. if (tempPrice < minPrice) {
  63. minPrice = tempPrice;
  64. minPriceTime = `${hour}:${min}~24:00`;
  65. }
  66. } else {
  67. const nhour = res.policyInfos[index + 1].startTime.substring(0, 2);
  68. const nmin = res.policyInfos[index + 1].startTime.substring(3, 5);
  69. item.startTimeFormat = `${hour}:${min}~${nhour}:${nmin}`;
  70. if (nowHour >= Number(hour) && nowHour < Number(nhour)) {
  71. currentPrice = tempPrice;
  72. currentTime = `${hour}:${min}~${nhour}:${nmin}`;
  73. }
  74. if (tempPrice < minPrice) {
  75. minPrice = tempPrice;
  76. minPriceTime = `${hour}:${min}~${nhour}:${nmin}`;
  77. }
  78. }
  79. });
  80. }
  81. res.maxPrice = maxPrice;
  82. res.minPrice = minPrice;
  83. res.minPriceTime = minPriceTime;
  84. res.currentPrice = currentPrice;
  85. res.currentTime = currentTime;
  86. if (StationID) {
  87. res.StationID = StationID;
  88. }
  89. return res;
  90. });
  91. }
  92. export function cancelCharge(connectorId: string) {
  93. return cHttp.get(`/charge/stopCharge/${connectorId}`, {
  94. statusCodeHandle: false,
  95. });
  96. }
  97. export function fetchChargeStatus(
  98. checkAppointment?: boolean,
  99. checkCharge?: boolean
  100. ) {
  101. return cHttp
  102. .get("/charge/chargeStatus", {
  103. statusCodeHandle: false,
  104. })
  105. .then((res) => {
  106. if (res) {
  107. res.isAppointment = [0].includes(res.chargeStatus);
  108. res.isStarted = [1, 2, 3].includes(res.chargeStatus);
  109. }
  110. // 充电状态:0:预约中 1:启动中 2:充电中 3:停止中 4:已结束 5:未知
  111. if (checkAppointment && res && res.isAppointment) {
  112. uni.hideLoading();
  113. uni.showModal({
  114. title: "温馨提示",
  115. content: "当前已有预约中的订单,请取消再扫码充电",
  116. showCancel: false,
  117. confirmText: "查看预约",
  118. confirmColor: "#2d9e95",
  119. success(modal) {
  120. if (modal.confirm) {
  121. uni.redirectTo({
  122. url: `/pages-charge/appointment/appointment?connectorId=${res.connectorId}`,
  123. });
  124. }
  125. },
  126. });
  127. }
  128. if (checkCharge && res && res.isStarted) {
  129. uni.hideLoading();
  130. uni.showModal({
  131. title: "温馨提示",
  132. content: "当前已有进行中的订单,请结束订单再扫码充电",
  133. showCancel: false,
  134. confirmText: "查看详情",
  135. confirmColor: "#2d9e95",
  136. success(modal) {
  137. if (modal.confirm) {
  138. uni.redirectTo({
  139. url: `/pages-charge/ordering/ordering?connectorId=${res.connectorId}&start=1`,
  140. });
  141. }
  142. },
  143. });
  144. }
  145. return res;
  146. });
  147. }
  148. export function fetchStations(
  149. page: number,
  150. pageSize: number,
  151. latitude?: number,
  152. longitude?: number,
  153. baseLatitude?: number,
  154. baseLongitude?: number,
  155. options?: {
  156. distance?: number;
  157. status?: number;
  158. }
  159. ): Promise<any[]> {
  160. return fetchAllStations(latitude, longitude)
  161. .then((res) => {
  162. // console.log(latitude, longitude);
  163. // let list = JSON.parse(JSON.stringify(res));
  164. const data: any[] = JSON.parse(JSON.stringify(res)); // []
  165. const start = (page - 1) * pageSize;
  166. const end = start + pageSize;
  167. // 暂时废弃距离范围
  168. // if (latitude && longitude) {
  169. // list.forEach((item: any) => {
  170. // item.stationLatDistance = _getDistance(
  171. // latitude,
  172. // longitude,
  173. // item.location.stationLat,
  174. // item.location.stationLng
  175. // );
  176. // });
  177. // list.sort((item1: any, item2: any) => {
  178. // return item1.stationLatDistance - item2.stationLatDistance;
  179. // });
  180. // }
  181. // 暂时废弃距离筛选
  182. // if (options) {
  183. // if (options.distance && !isDebug) {
  184. // list = list.filter(
  185. // (item: any) => item.stationLatDistance <= (options.distance || 20)
  186. // );
  187. // }
  188. // }
  189. // list.forEach((item: any, index: number) => {
  190. // if (index >= start && index < end) {
  191. // data.push(item);
  192. // }
  193. // });
  194. // 暂时显示全部电站
  195. if (latitude && longitude) {
  196. data.forEach((item: any) => {
  197. item.stationLatDistance = _getDistance(
  198. latitude,
  199. longitude,
  200. item.location.stationLat,
  201. item.location.stationLng
  202. );
  203. });
  204. data.sort((item1: any, item2: any) => {
  205. return item1.stationLatDistance - item2.stationLatDistance;
  206. });
  207. }
  208. if (baseLatitude && baseLongitude) {
  209. data.forEach((item: any) => {
  210. item.stationLatDistance = _getDistance(
  211. baseLatitude,
  212. baseLongitude,
  213. item.location.stationLat,
  214. item.location.stationLng
  215. );
  216. });
  217. }
  218. return _fetchStationStatus(data);
  219. })
  220. .then((list) => {
  221. // 暂时废弃状态筛选
  222. // if (options && options.status) {
  223. // let res = false;
  224. // list = list.filter((item: any) => {
  225. // res = false;
  226. // item.equipmentInfos.forEach((eqInfo: any) => {
  227. // eqInfo.connectorInfos.forEach((coInfo: any) => {
  228. // if (
  229. // !res &&
  230. // options.status === 1 &&
  231. // coInfo.connectorStatusInfo &&
  232. // coInfo.connectorStatusInfo.status === 1
  233. // ) {
  234. // res = true;
  235. // }
  236. // if (
  237. // !res &&
  238. // options.status === 2 &&
  239. // coInfo.connectorStatusInfo &&
  240. // coInfo.connectorStatusInfo.status !== 1
  241. // ) {
  242. // res = true;
  243. // }
  244. // });
  245. // });
  246. // return res;
  247. // });
  248. // }
  249. return list;
  250. });
  251. }
  252. export function fetchStation(id: number) {
  253. return fetchAllStations()
  254. .then((res) => {
  255. const findIndex = res.findIndex((item) => Number(item.StationID) === id);
  256. if (findIndex < 0) {
  257. throw {
  258. errMsg: "not found",
  259. };
  260. }
  261. return _fetchStationStatus([res[findIndex]]);
  262. })
  263. .then((list) => {
  264. return list[0];
  265. });
  266. }
  267. export function fetchStationDetail(stationId: string) {
  268. return cHttp
  269. .get(`/charge/stationInfo/${stationId}`)
  270. .then((res) => {
  271. console.log("xxxxx", res)
  272. return res;
  273. });
  274. }
  275. /**
  276. * 根据shortId或connectorId或equipmentId 查询站点信息(因前期为查询全部站点后前端过滤,特此进行性能优化)
  277. * @param shortId
  278. * @param connectorId
  279. * @param equipmentId
  280. */
  281. export function fetchStationDetailByShortIdOrConnectorIdOrEquipmentId(shortId?: string, connectorId?: string, equipmentId?: string) {
  282. if (shortId) {
  283. return fetchStationDetailByShortId(shortId);
  284. } else if (connectorId) {
  285. return fetchStationDetailByConnectorId(connectorId)
  286. } else if (equipmentId) {
  287. return fetchStationDetailByByEquipmentId(equipmentId)
  288. }
  289. }
  290. export function fetchStationDetailByShortId(shortId: string) {
  291. return cHttp
  292. .get(`/charge/stationInfoByShortId/${shortId}`)
  293. .then((res) => {
  294. return res;
  295. });
  296. }
  297. export function fetchStationDetailByConnectorId(connectorId: string) {
  298. return cHttp
  299. .get(`/charge/stationInfoByConnectorId/${connectorId}`)
  300. .then((res) => {
  301. return res;
  302. });
  303. }
  304. /**
  305. * equipmentId 可能不唯一,慎重使用
  306. * @param equipmentId
  307. */
  308. export function fetchStationDetailByByEquipmentId(equipmentId: string) {
  309. return cHttp
  310. .get(`/charge/stationInfoByEquipmentId/${equipmentId}`)
  311. .then((res) => {
  312. return res;
  313. });
  314. }
  315. export function fetchStationByIds(ids: number[]) {
  316. return fetchAllStations().then((res) => {
  317. const list = res.filter((item) => ids.includes(Number(item.StationID)));
  318. return _fetchStationStatus(list);
  319. });
  320. }
  321. export function fetchStationByConnectorIdOrShortId(sn: string) {
  322. let equipment = -1;
  323. return fetchAllStations()
  324. .then((res) => {
  325. let station: any = undefined;
  326. console.log("fetchStationByConnectorIdOrShortId", res)
  327. res.forEach((item) => {
  328. if (!station && item.equipmentInfos && item.equipmentInfos) {
  329. item.equipmentInfos.forEach(
  330. (equipmentInfo: any, equipmentIndex: number) => {
  331. if (sn.length <= 16) {
  332. // 此处传入的是 shortId
  333. if (equipmentInfo.shortId === sn) {
  334. station = item;
  335. equipment = equipmentIndex;
  336. }
  337. } else {
  338. if (
  339. equipmentInfo.connectorInfos &&
  340. equipmentInfo.connectorInfos
  341. ) {
  342. // connectorId
  343. equipmentInfo.connectorInfos.forEach((connectorInfo: any) => {
  344. if (connectorInfo.connectorId === sn) {
  345. station = item;
  346. equipment = equipmentIndex;
  347. }
  348. });
  349. }
  350. }
  351. }
  352. );
  353. }
  354. });
  355. if (station) {
  356. return _fetchStationStatus([station]);
  357. } else {
  358. return Promise.reject({});
  359. }
  360. })
  361. .then((res) => {
  362. if (res && res.length) {
  363. return {
  364. station: res[0],
  365. equipment: res[0].equipmentInfos[equipment],
  366. };
  367. } else {
  368. return Promise.reject({});
  369. }
  370. });
  371. }
  372. export function searchStation(keyword: string) {
  373. return fetchAllStations().then((res) => {
  374. // const reg = new RegExp(keyword.trim(), "ig");
  375. // console.log(res)
  376. const stationIdList = res.filter(
  377. (item) => item.stationName.includes(keyword) || item.address.includes(keyword)
  378. ).map(k => k.stationId+"");
  379. // console.log("serach>>>>>",list)
  380. if (stationIdList) {
  381. return _fetchStations(1, 100, 0, 0, stationIdList)
  382. } else {
  383. return Promise.reject([])
  384. }
  385. });
  386. }
  387. export function fetchAllStations(latitude?: number, longitude?: number): Promise<any[]> {
  388. if (getApp<any>().globalData.stations.length > 0) {
  389. return Promise.resolve(getApp<any>().globalData.stations);
  390. }
  391. const page = 1;
  392. const page_size = 99;
  393. return new Promise((resolve, reject) => {
  394. _fetchAllStations(page, page_size, [], latitude, longitude)
  395. .then((list) => {
  396. // if (!isProduction) {
  397. // list.push({
  398. // ...list[0],
  399. // StationName: '这是模拟数据,不要使用充电',
  400. // Address: '这是模拟数据,测试一下附近的点',
  401. // StationLat: 22.540545,
  402. // StationLng: 113.942695,
  403. // StationID: '0000'
  404. // })
  405. // }
  406. console.log("所有电站数据", list);
  407. getApp<any>().globalData.stations = list;
  408. resolve(list);
  409. })
  410. .catch(reject);
  411. });
  412. }
  413. function _fetchAllStations(
  414. page: number,
  415. pageSize: number,
  416. list: any[],
  417. latitude?: number,
  418. longitude?: number
  419. ): Promise<any[]> {
  420. return _fetchStations(page, pageSize, latitude, longitude).then((res) => {
  421. list = list.concat(res);
  422. if (res.length >= pageSize) {
  423. return _fetchAllStations(page + 1, pageSize, list, latitude, longitude);
  424. } else {
  425. // eslint-disable-next-line promise/no-return-wrap
  426. return Promise.resolve(list);
  427. }
  428. });
  429. }
  430. export function _fetchStations(page: number, pageSize: number,
  431. latitude?: number, longitude?: number, stationIdList?: Array<string>) {
  432. return cHttp
  433. .get(`/charge/listStation?pageNum=${page}&pageSize=${pageSize}&latitude=${latitude||22.696779}&longitude=${longitude||114.044805}&stationIdList=${stationIdList||[]}`)
  434. .then((res) => {
  435. // console.log("xxxxx", res)
  436. const data = res.list || [];
  437. data.forEach((item: any) => {
  438. item.StationID = item.stationId;
  439. item.fastEquipmentInfos = [];
  440. item.slowEquipmentInfos = [];
  441. item.totalFee = Number(
  442. Number(
  443. Number(item.electricityFee) +
  444. Number(item.parkFee) +
  445. Number(item.serviceFee)
  446. ).toFixed(2)
  447. );
  448. });
  449. return data.filter((item: any) => {
  450. return !["1657"].includes(item.StationID);
  451. });
  452. });
  453. }
  454. export function _getDistance(lat1: number, lng1: number, lat2: number, lng2: number) {
  455. var radLat1 = (lat1 * Math.PI) / 180.0;
  456. var radLat2 = (lat2 * Math.PI) / 180.0;
  457. var a = radLat1 - radLat2;
  458. var b = (lng1 * Math.PI) / 180.0 - (lng2 * Math.PI) / 180.0;
  459. var s =
  460. 2 *
  461. Math.asin(
  462. Math.sqrt(
  463. Math.pow(Math.sin(a / 2), 2) +
  464. Math.cos(radLat1) * Math.cos(radLat2) * Math.pow(Math.sin(b / 2), 2)
  465. )
  466. );
  467. s = s * 6378.137; // EARTH_RADIUS;
  468. s = Math.round(s * 10000) / 10000;
  469. return s;
  470. }
  471. function _fetchStationStatus(list: any[]) {
  472. if (list.length <= 0) {
  473. return Promise.resolve([]);
  474. }
  475. let _list: any[] = [];
  476. return cHttp
  477. .get(
  478. `/charge/stationStatus?stationIds=${list
  479. .filter((item) => item.StationID !== "0000")
  480. .map((item) => item.StationID)
  481. .join(",")}`
  482. )
  483. .then((res) => {
  484. const StationStatusInfos = res || [];
  485. const ConnectorIDs: string[] = [];
  486. const StationIDs: string[] = [];
  487. console.log("_fetchStationStatus", list)
  488. list.forEach((item: any) => {
  489. item.fastEquipmentInfos = [];
  490. item.slowEquipmentInfos = [];
  491. const StationStatusInfo = StationStatusInfos.find(
  492. (status: any) => Number(status.stationId) === Number(item.StationID)
  493. );
  494. let ConnectorID = "";
  495. item.equipmentInfos?.forEach((eqInfo: any) => {
  496. eqInfo.connectorInfos.forEach((coInfo: any) => {
  497. if (StationStatusInfo) {
  498. const connectorStatusInfo =
  499. StationStatusInfo.connectorStatusInfos.find(
  500. (costatus: any) => costatus.connectorId === coInfo.connectorId
  501. );
  502. if (connectorStatusInfo) {
  503. // debug
  504. // connectorStatusInfo.status = 255
  505. coInfo.connectorStatusInfo = connectorStatusInfo;
  506. }
  507. }
  508. if (!ConnectorID) {
  509. ConnectorID = coInfo.connectorId;
  510. }
  511. });
  512. if ([1].includes(Number(eqInfo.equipmentType))) {
  513. item.fastEquipmentInfos.push(eqInfo);
  514. } else {
  515. item.slowEquipmentInfos.push(eqInfo);
  516. }
  517. });
  518. if (ConnectorID) {
  519. ConnectorIDs.push(ConnectorID);
  520. StationIDs.push(item.StationID);
  521. }
  522. });
  523. _list = list;
  524. return Promise.all(
  525. ConnectorIDs.map((cid, cindex) => {
  526. return fetchStationPriceDesc(cid, StationIDs[cindex]);
  527. })
  528. );
  529. })
  530. .then((res) => {
  531. if (res && res.length) {
  532. _list.forEach((item) => {
  533. const i = res.findIndex((r) => r.StationID === item.StationID);
  534. if (i >= 0) {
  535. item.totalFee = res[i].currentPrice;
  536. }
  537. });
  538. }
  539. return _list;
  540. });
  541. }