charge.ts 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571
  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. let {latitude, longitude} = getApp<any>().globalData.fetchLocation
  382. return _fetchStations(1, 100, latitude, longitude, stationIdList)
  383. } else {
  384. return Promise.reject([])
  385. }
  386. });
  387. }
  388. export function fetchAllStations(latitude?: number, longitude?: number): Promise<any[]> {
  389. if (getApp<any>().globalData.stations.length > 0) {
  390. return Promise.resolve(getApp<any>().globalData.stations);
  391. }
  392. const page = 1;
  393. const page_size = 99;
  394. return new Promise((resolve, reject) => {
  395. _fetchAllStations(page, page_size, [], latitude, longitude)
  396. .then((list) => {
  397. // if (!isProduction) {
  398. // list.push({
  399. // ...list[0],
  400. // StationName: '这是模拟数据,不要使用充电',
  401. // Address: '这是模拟数据,测试一下附近的点',
  402. // StationLat: 22.540545,
  403. // StationLng: 113.942695,
  404. // StationID: '0000'
  405. // })
  406. // }
  407. console.log("所有电站数据", list);
  408. getApp<any>().globalData.stations = list;
  409. resolve(list);
  410. })
  411. .catch(reject);
  412. });
  413. }
  414. function _fetchAllStations(
  415. page: number,
  416. pageSize: number,
  417. list: any[],
  418. latitude?: number,
  419. longitude?: number
  420. ): Promise<any[]> {
  421. return _fetchStations(page, pageSize, latitude, longitude).then((res) => {
  422. list = list.concat(res);
  423. if (res.length >= pageSize) {
  424. return _fetchAllStations(page + 1, pageSize, list, latitude, longitude);
  425. } else {
  426. // eslint-disable-next-line promise/no-return-wrap
  427. return Promise.resolve(list);
  428. }
  429. });
  430. }
  431. export function _fetchStations(page: number, pageSize: number,
  432. latitude?: number, longitude?: number, stationIdList?: Array<string>) {
  433. return cHttp
  434. .get(`/charge/listStation?pageNum=${page}&pageSize=${pageSize}&latitude=${latitude||22.696779}&longitude=${longitude||114.044805}&stationIdList=${stationIdList||[]}`)
  435. .then((res) => {
  436. // console.log("xxxxx", res)
  437. const data = res.list || [];
  438. data.forEach((item: any) => {
  439. item.StationID = item.stationId;
  440. item.fastEquipmentInfos = [];
  441. item.slowEquipmentInfos = [];
  442. item.totalFee = Number(
  443. Number(
  444. Number(item.electricityFee) +
  445. Number(item.parkFee) +
  446. Number(item.serviceFee)
  447. ).toFixed(2)
  448. );
  449. });
  450. return data.filter((item: any) => {
  451. return !["1657"].includes(item.StationID);
  452. });
  453. });
  454. }
  455. export function _getDistance(lat1: number, lng1: number, lat2: number, lng2: number) {
  456. var radLat1 = (lat1 * Math.PI) / 180.0;
  457. var radLat2 = (lat2 * Math.PI) / 180.0;
  458. var a = radLat1 - radLat2;
  459. var b = (lng1 * Math.PI) / 180.0 - (lng2 * Math.PI) / 180.0;
  460. var s =
  461. 2 *
  462. Math.asin(
  463. Math.sqrt(
  464. Math.pow(Math.sin(a / 2), 2) +
  465. Math.cos(radLat1) * Math.cos(radLat2) * Math.pow(Math.sin(b / 2), 2)
  466. )
  467. );
  468. s = s * 6378.137; // EARTH_RADIUS;
  469. s = Math.round(s * 10000) / 10000;
  470. return s;
  471. }
  472. function _fetchStationStatus(list: any[]) {
  473. if (list.length <= 0) {
  474. return Promise.resolve([]);
  475. }
  476. let _list: any[] = [];
  477. return cHttp
  478. .get(
  479. `/charge/stationStatus?stationIds=${list
  480. .filter((item) => item.StationID !== "0000")
  481. .map((item) => item.StationID)
  482. .join(",")}`
  483. )
  484. .then((res) => {
  485. const StationStatusInfos = res || [];
  486. const ConnectorIDs: string[] = [];
  487. const StationIDs: string[] = [];
  488. console.log("_fetchStationStatus", list)
  489. list.forEach((item: any) => {
  490. item.fastEquipmentInfos = [];
  491. item.slowEquipmentInfos = [];
  492. const StationStatusInfo = StationStatusInfos.find(
  493. (status: any) => Number(status.stationId) === Number(item.StationID)
  494. );
  495. let ConnectorID = "";
  496. item.equipmentInfos?.forEach((eqInfo: any) => {
  497. eqInfo.connectorInfos.forEach((coInfo: any) => {
  498. if (StationStatusInfo) {
  499. const connectorStatusInfo =
  500. StationStatusInfo.connectorStatusInfos.find(
  501. (costatus: any) => costatus.connectorId === coInfo.connectorId
  502. );
  503. if (connectorStatusInfo) {
  504. coInfo.connectorStatusInfo = connectorStatusInfo;
  505. }
  506. }
  507. if (!ConnectorID) {
  508. ConnectorID = coInfo.connectorId;
  509. }
  510. });
  511. if ([1].includes(Number(eqInfo.equipmentType))) {
  512. item.fastEquipmentInfos.push(eqInfo);
  513. } else {
  514. item.slowEquipmentInfos.push(eqInfo);
  515. }
  516. });
  517. if (ConnectorID) {
  518. ConnectorIDs.push(ConnectorID);
  519. StationIDs.push(item.StationID);
  520. }
  521. });
  522. _list = list;
  523. return Promise.all(
  524. ConnectorIDs.map((cid, cindex) => {
  525. return fetchStationPriceDesc(cid, StationIDs[cindex]);
  526. })
  527. );
  528. })
  529. .then((res) => {
  530. if (res && res.length) {
  531. _list.forEach((item) => {
  532. const i = res.findIndex((r) => r.StationID === item.StationID);
  533. if (i >= 0) {
  534. item.totalFee = res[i].currentPrice;
  535. }
  536. });
  537. }
  538. return _list;
  539. })
  540. .catch(() => {
  541. return list;
  542. });
  543. }