charge.ts 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  1. import Http from "../utils/http";
  2. import { host, isDebug } from "../utils/constant";
  3. const cHttp = new Http(host);
  4. export function startCharge(sn: 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/${sn}?${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(sn: string) {
  93. return cHttp.get(`/charge/stopCharge/${sn}`, {
  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: "#347DFF",
  119. success(modal) {
  120. if (modal.confirm) {
  121. uni.redirectTo({
  122. url: `/pages-charge/appointment/appointment?sn=${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: "#347DFF",
  136. success(modal) {
  137. if (modal.confirm) {
  138. uni.redirectTo({
  139. url: `/pages-charge/ordering/ordering?sn=${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()
  161. .then((res) => {
  162. // console.log(latitude, longitude);
  163. let list = JSON.parse(JSON.stringify(res));
  164. const data: any[] = [];
  165. const start = (page - 1) * pageSize;
  166. const end = start + pageSize;
  167. if (latitude && longitude) {
  168. list.forEach((item: any) => {
  169. item.stationLatDistance = _getDistance(
  170. latitude,
  171. longitude,
  172. item.location.stationLat,
  173. item.location.stationLng
  174. );
  175. });
  176. list.sort((item1: any, item2: any) => {
  177. return item1.stationLatDistance - item2.stationLatDistance;
  178. });
  179. }
  180. if (options) {
  181. if (options.distance && !isDebug) {
  182. list = list.filter(
  183. (item: any) => item.stationLatDistance <= (options.distance || 20)
  184. );
  185. }
  186. }
  187. list.forEach((item: any, index: number) => {
  188. if (index >= start && index < end) {
  189. data.push(item);
  190. }
  191. });
  192. if (baseLatitude && baseLongitude) {
  193. data.forEach((item: any) => {
  194. item.stationLatDistance = _getDistance(
  195. baseLatitude,
  196. baseLongitude,
  197. item.location.stationLat,
  198. item.location.stationLng
  199. );
  200. });
  201. }
  202. return _fetchStationStatus(data);
  203. })
  204. .then((list) => {
  205. if (options && options.status) {
  206. let res = false;
  207. list = list.filter((item: any) => {
  208. res = false;
  209. item.equipmentInfos.forEach((eqInfo: any) => {
  210. eqInfo.connectorInfos.forEach((coInfo: any) => {
  211. if (
  212. !res &&
  213. options.status === 1 &&
  214. coInfo.connectorStatusInfo &&
  215. coInfo.connectorStatusInfo.status === 1
  216. ) {
  217. res = true;
  218. }
  219. if (
  220. !res &&
  221. options.status === 2 &&
  222. coInfo.connectorStatusInfo &&
  223. coInfo.connectorStatusInfo.status !== 1
  224. ) {
  225. res = true;
  226. }
  227. });
  228. });
  229. return res;
  230. });
  231. }
  232. return list;
  233. });
  234. }
  235. export function fetchStation(id: number) {
  236. return fetchAllStations()
  237. .then((res) => {
  238. const findIndex = res.findIndex((item) => Number(item.StationID) === id);
  239. if (findIndex < 0) {
  240. throw {
  241. errMsg: "not found",
  242. };
  243. }
  244. return _fetchStationStatus([res[findIndex]]);
  245. })
  246. .then((list) => {
  247. return list[0];
  248. });
  249. }
  250. export function fetchStationByIds(ids: number[]) {
  251. return fetchAllStations().then((res) => {
  252. const list = res.filter((item) => ids.includes(Number(item.StationID)));
  253. return _fetchStationStatus(list);
  254. });
  255. }
  256. export function fetchStationByConnectorIdOrShortId(id: string) {
  257. let equipment = -1;
  258. return fetchAllStations()
  259. .then((res) => {
  260. let station: any = undefined;
  261. res.forEach((item) => {
  262. if (!station && item.equipmentInfos && item.equipmentInfos) {
  263. item.equipmentInfos.forEach(
  264. (equipmentInfo: any, equipmentIndex: number) => {
  265. if (id.length <= 16) {
  266. // 此处传入的是 shortId
  267. if (equipmentInfo.shortId === id) {
  268. station = item;
  269. equipment = equipmentIndex;
  270. }
  271. } else {
  272. if (
  273. equipmentInfo.connectorInfos &&
  274. equipmentInfo.connectorInfos
  275. ) {
  276. // connectorId
  277. equipmentInfo.connectorInfos.forEach((connectorInfo: any) => {
  278. if (connectorInfo.connectorId === id) {
  279. station = item;
  280. equipment = equipmentIndex;
  281. }
  282. });
  283. }
  284. }
  285. }
  286. );
  287. }
  288. });
  289. if (station) {
  290. return _fetchStationStatus([station]);
  291. } else {
  292. return Promise.reject({});
  293. }
  294. })
  295. .then((res) => {
  296. if (res && res.length) {
  297. return {
  298. station: res[0],
  299. equipment: res[0].equipmentInfos[equipment],
  300. };
  301. } else {
  302. return Promise.reject({});
  303. }
  304. });
  305. }
  306. export function searchStation(keyword: string) {
  307. return fetchAllStations().then((res) => {
  308. const reg = new RegExp(keyword, "ig");
  309. const list = res.filter(
  310. (item) => reg.test(item.stationName) || reg.test(item.address)
  311. );
  312. return _fetchStationStatus(list);
  313. });
  314. }
  315. export function fetchAllStations(): Promise<any[]> {
  316. if (getApp<any>().globalData.stations.length > 0) {
  317. return Promise.resolve(getApp<any>().globalData.stations);
  318. }
  319. const page = 1;
  320. const page_size = 99;
  321. return new Promise((resolve, reject) => {
  322. _fetchAllStations(page, page_size, [])
  323. .then((list) => {
  324. // if (!isProduction) {
  325. // list.push({
  326. // ...list[0],
  327. // StationName: '这是模拟数据,不要使用充电',
  328. // Address: '这是模拟数据,测试一下附近的点',
  329. // StationLat: 22.540545,
  330. // StationLng: 113.942695,
  331. // StationID: '0000'
  332. // })
  333. // }
  334. console.log("所有电站数据", list);
  335. getApp<any>().globalData.stations = list;
  336. resolve(list);
  337. })
  338. .catch(reject);
  339. });
  340. }
  341. function _fetchAllStations(
  342. page: number,
  343. pageSize: number,
  344. list: any[]
  345. ): Promise<any[]> {
  346. return _fetchStations(page, pageSize).then((res) => {
  347. list = list.concat(res);
  348. if (res.length >= pageSize) {
  349. return _fetchAllStations(page + 1, pageSize, list);
  350. } else {
  351. // eslint-disable-next-line promise/no-return-wrap
  352. return Promise.resolve(list);
  353. }
  354. });
  355. }
  356. function _fetchStations(page: number, pageSize: number) {
  357. return cHttp
  358. .get(`/charge/listStation?pageNum=${page}&pageSize=${pageSize}`)
  359. .then((res) => {
  360. const data = res || [];
  361. data.forEach((item: any) => {
  362. item.StationID = item.stationId;
  363. item.fastEquipmentInfos = [];
  364. item.slowEquipmentInfos = [];
  365. item.totalFee = Number(
  366. Number(
  367. Number(item.electricityFee) +
  368. Number(item.parkFee) +
  369. Number(item.serviceFee)
  370. ).toFixed(2)
  371. );
  372. });
  373. return data.filter((item: any) => {
  374. return !["1657"].includes(item.StationID);
  375. });
  376. });
  377. }
  378. function _getDistance(lat1: number, lng1: number, lat2: number, lng2: number) {
  379. var radLat1 = (lat1 * Math.PI) / 180.0;
  380. var radLat2 = (lat2 * Math.PI) / 180.0;
  381. var a = radLat1 - radLat2;
  382. var b = (lng1 * Math.PI) / 180.0 - (lng2 * Math.PI) / 180.0;
  383. var s =
  384. 2 *
  385. Math.asin(
  386. Math.sqrt(
  387. Math.pow(Math.sin(a / 2), 2) +
  388. Math.cos(radLat1) * Math.cos(radLat2) * Math.pow(Math.sin(b / 2), 2)
  389. )
  390. );
  391. s = s * 6378.137; // EARTH_RADIUS;
  392. s = Math.round(s * 10000) / 10000;
  393. return s;
  394. }
  395. function _fetchStationStatus(list: any[]) {
  396. if (list.length <= 0) {
  397. return Promise.resolve([]);
  398. }
  399. let _list: any[] = [];
  400. return cHttp
  401. .get(
  402. `/charge/stationStatus?stationIds=${list
  403. .filter((item) => item.StationID !== "0000")
  404. .map((item) => item.StationID)
  405. .join(",")}`
  406. )
  407. .then((res) => {
  408. const StationStatusInfos = res || [];
  409. const ConnectorIDs: string[] = [];
  410. const StationIDs: string[] = [];
  411. list.forEach((item: any) => {
  412. item.fastEquipmentInfos = [];
  413. item.slowEquipmentInfos = [];
  414. const StationStatusInfo = StationStatusInfos.find(
  415. (status: any) => Number(status.stationId) === Number(item.StationID)
  416. );
  417. let ConnectorID = "";
  418. item.equipmentInfos.forEach((eqInfo: any) => {
  419. eqInfo.connectorInfos.forEach((coInfo: any) => {
  420. if (StationStatusInfo) {
  421. const connectorStatusInfo =
  422. StationStatusInfo.connectorStatusInfos.find(
  423. (costatus: any) => costatus.connectorId === coInfo.connectorId
  424. );
  425. if (connectorStatusInfo) {
  426. coInfo.connectorStatusInfo = connectorStatusInfo;
  427. }
  428. }
  429. if (!ConnectorID) {
  430. ConnectorID = coInfo.connectorId;
  431. }
  432. });
  433. if ([1].includes(Number(eqInfo.equipmentType))) {
  434. item.fastEquipmentInfos.push(eqInfo);
  435. } else {
  436. item.slowEquipmentInfos.push(eqInfo);
  437. }
  438. });
  439. if (ConnectorID) {
  440. ConnectorIDs.push(ConnectorID);
  441. StationIDs.push(item.StationID);
  442. }
  443. });
  444. _list = list;
  445. return Promise.all(
  446. ConnectorIDs.map((cid, cindex) => {
  447. return fetchStationPriceDesc(cid, StationIDs[cindex]);
  448. })
  449. );
  450. })
  451. .then((res) => {
  452. if (res && res.length) {
  453. _list.forEach((item) => {
  454. const i = res.findIndex((r) => r.StationID === item.StationID);
  455. if (i >= 0) {
  456. item.totalFee = res[i].currentPrice;
  457. }
  458. });
  459. }
  460. return _list;
  461. });
  462. }