Home.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545
  1. /**
  2. * 首页通用页
  3. * @邠心vbe on 2021/08/13
  4. */
  5. import React, { Component } from 'react';
  6. import { View, StyleSheet, BackHandler, Linking, AppState } from 'react-native';
  7. import app from '../../../app.json'
  8. import { i18nUtil } from '../../i18n';
  9. import apiBase from '../../api/apiBase';
  10. import apiStation from '../../api/apiStation';
  11. import Dialog from '../../components/Dialog';
  12. import utils from '../../utils/utils';
  13. import { SettingUtil } from '../Settings';
  14. import MyStatusBar from '../../components/MyStatusBar';
  15. import LocationPermission from './maps/LocationPermission';
  16. import MapUI from './MapUI';
  17. import MapUILumi from './MapUILumi';
  18. import storage from '../../utils/storage';
  19. const KEY_STORE_LIST = "map_stop_list"
  20. export default class HomePage extends Component {
  21. constructor(props) {
  22. super(props);
  23. this.state = {
  24. region: {
  25. latitude: 1.3532623163977149,
  26. longitude: 103.87092316860532,
  27. latitudeDelta: 0.0922,
  28. longitudeDelta: 0.0421
  29. },
  30. mapReady: false,
  31. showStation: false,
  32. stationId: "",
  33. stationInfo: {},
  34. stopList: [],
  35. hasPermission: isIOS,
  36. permissionDenied: false
  37. };
  38. this.isHide = true;
  39. this.denied = true;
  40. this.backSeconds = 0;
  41. this.refreshTime = 0;
  42. this.settingInfo = {}
  43. this.viewIndex = -1;
  44. this.filter = {
  45. parkingFee: 'ALL',
  46. connectorType: ''
  47. };
  48. this.refresh = false;
  49. this.stateListener;
  50. this.forceUpdateDialog = undefined;
  51. this.locationListener = undefined;
  52. }
  53. componentDidMount() {
  54. const navigation = this.props.navigation;
  55. if (this.locationListener) {
  56. this.locationListener.removeListener();
  57. this.locationListener = undefined;
  58. }
  59. if (!isIOS) {
  60. this.locationListener = new LocationPermission.LocationListener();
  61. if (this.locationListener) {
  62. this.locationListener.addListener();
  63. }
  64. }
  65. navigation.addListener('focus', () => {
  66. console.log('------Home------', "Focus")
  67. this.onCloseInfo();
  68. SettingUtil.getSettings(set => {
  69. //console.log("获取设置信息", set);
  70. this.settingInfo = set;
  71. this.init();
  72. })
  73. this.isHide = false;
  74. if (!app.isWhitelabel) {
  75. MyStatusBar.setStatusBarThemes(MyStatusBar.DARK_STYLE, colorLight);
  76. }
  77. });
  78. navigation.addListener('blur', () => {
  79. //toastShort('onStop')
  80. this.isHide = true;
  81. if (!app.isWhitelabel) {
  82. MyStatusBar.setStatusBarThemes(themeStatusBar, colorPrimaryDark);
  83. }
  84. setTimeout(() => {
  85. navigation.closeDrawer();
  86. }, 200);
  87. });
  88. this.stateListener = AppState.addEventListener("change", state => {
  89. if (state == 'active' && this.forceUpdateDialog) {
  90. this.showUpdateDialog(this.forceUpdateDialog);
  91. }
  92. });
  93. BackHandler.addEventListener('hardwareBackPress', this.toExit)
  94. this.isHide = false;
  95. this.checkUpdateVersion();
  96. }
  97. componentWillUnmount() {
  98. /*console.log("componentWillUnmount")
  99. if (this.unsubscribe) {
  100. this.unsubscribe();
  101. }*/
  102. this.backSeconds = 0;
  103. if (this.stateListener) {
  104. this.stateListener.remove();
  105. }
  106. BackHandler.removeEventListener("hardwareBackPress", this.toExit);
  107. }
  108. init() {
  109. if (this.state.stopList.length == 0 && !this.refresh) {
  110. storage.getStorage(KEY_STORE_LIST).then(data => {
  111. console.log("获取站点缓存", "成功");
  112. if (data) {
  113. const list = JSON.parse(data);
  114. this.setState({
  115. stopList: list
  116. }, () => {
  117. this.checkPermission2Geo(true);
  118. })
  119. } else {
  120. this.checkPermission2Geo(true);
  121. }
  122. }).catch(err => {
  123. console.log("获取站点缓存-err", err);
  124. this.checkPermission2Geo(true);
  125. })
  126. }
  127. }
  128. toExit = () => {
  129. //console.log("beforeRemove", this.isHide);
  130. if (this.state.stationInfo?.id) {
  131. this.setState({
  132. stationInfo: {}
  133. });
  134. return true;
  135. }
  136. if (!this.isHide) {
  137. if (isIOS) {
  138. //e.preventDefault();
  139. } else {
  140. let time = new Date().getTime();
  141. //console.log("beforeRemove2", time, this.backSeconds);
  142. if (time - this.backSeconds < 2000) {
  143. BackHandler.exitApp();
  144. } else {
  145. toastShort($t("home.backAgainTips"));
  146. this.backSeconds = time;
  147. //e.preventDefault();
  148. }
  149. }
  150. return true;
  151. }
  152. }
  153. checkGPS() {
  154. if (this.locationListener) {
  155. this.locationListener.check();
  156. }
  157. }
  158. onMapReady() {
  159. this.setState({
  160. mapReady: true
  161. }, () => {
  162. this.init();
  163. });
  164. }
  165. checkUpdateVersion() {
  166. apiBase.checkUpdate().then(res => {
  167. if (res.data.versionCode > app.versionCode) {
  168. var desc = JSON.parse(res.data?.versionDesc ?? "{}");
  169. if (desc) {
  170. const upLog = i18nUtil.analyzeLocaleData(desc);
  171. if (typeof upLog == "string") {
  172. const prps = {
  173. title: $t("home.versionUpdate"),
  174. message: $t("home.newVersionName") + res.data.versionName + "\n\n" + upLog,
  175. showCancel: !(res.data.force),
  176. ok: $t("home.updateNow"),
  177. cancel: $t("home.later"),
  178. callback: btn => {
  179. if (btn == Dialog.BUTTON_OK) {
  180. //TODO 跳转到应用商店
  181. const uri = isIOS
  182. ? app.storeUrl.ios
  183. : app.storeUrl.android
  184. Linking.openURL(uri);
  185. }
  186. }
  187. }
  188. if (res.data.force) {
  189. prps.onBackPress = () => {
  190. }
  191. this.forceUpdateDialog = prps;
  192. }
  193. this.showUpdateDialog(prps);
  194. }
  195. }
  196. }
  197. }).catch(err => {
  198. })
  199. }
  200. showUpdateDialog(options) {
  201. if (Dialog.isShowing()) {
  202. setTimeout(() => {
  203. this.showUpdateDialog(options)
  204. }, 500);
  205. return;
  206. }
  207. Dialog.showDialog(options)
  208. }
  209. getPermission() {
  210. LocationPermission.requestPermission(hasPermission => {
  211. if (hasPermission) {
  212. this.setState({
  213. hasPermission: true
  214. });
  215. this.checkPermission2Geo();
  216. } else {
  217. this.noPermissionSite();
  218. }
  219. })
  220. }
  221. checkPermission2Geo(refresh) {
  222. if (this.refresh == refresh) {
  223. return;
  224. }
  225. if (refresh) {
  226. //避免关闭自动移动地图后无法点击按钮移动地图
  227. this.refresh = true
  228. }
  229. console.log("checkPermission2Geo", refresh);
  230. this.checkGPS();
  231. if (this.state.hasPermission) {
  232. this.infoGeoLocation();
  233. } else {
  234. LocationPermission.checkPermission((hasPermission, canRequestPermission) => {
  235. if (hasPermission) {
  236. this.setState({
  237. hasPermission: true
  238. });
  239. this.infoGeoLocation();
  240. } else {
  241. if (canRequestPermission) {
  242. if (this.denied) {
  243. this.denied = false;
  244. this.getPermission();
  245. } else {
  246. //避免多次请求
  247. this.denied = true;
  248. this.noPermissionSite();
  249. }
  250. } else {
  251. this.noPermissionSite();
  252. }
  253. }
  254. })
  255. }
  256. }
  257. noPermissionSite() {
  258. console.log("未获取权限也获取站点列表");
  259. this.setState({
  260. hasPermission: false,
  261. permissionDenied: true
  262. });
  263. let first = this.state.stopList.length == 0;//是否第一次加载
  264. this.getStationList(this.state.region, first);
  265. }
  266. /**
  267. * 获取定位数据
  268. * @param {*} again
  269. */
  270. infoGeoLocation(again) {
  271. if (this.state.mapReady) {
  272. navigator.geolocation.getCurrentPosition(location => {
  273. let region = {
  274. latitude: location.coords.latitude,
  275. longitude: location.coords.longitude,
  276. latitudeDelta: 0.0922,
  277. longitudeDelta: 0.0421
  278. }
  279. //console.log("getGeoLocation", region);
  280. //if (this.state.stopList.length == 0) { //只加载一次
  281. let time = new Date().getTime();
  282. let interval = this.settingInfo.refreshInterval * 1000;//重复加载时间
  283. let first = this.state.stopList.length == 0;//是否第一次加载
  284. if (first || this.refresh || time - this.refreshTime > interval) { //一分钟(10秒)加载一次
  285. this.getStationList(region, first || this.refresh);
  286. this.refreshTime = time;
  287. } else if (this.settingInfo.moveMyLocation) {
  288. this.setState({
  289. region: region
  290. });
  291. }
  292. }, error => {
  293. console.info("getGeoLocation", error);
  294. if (!again) {
  295. setTimeout(() => {
  296. this.infoGeoLocation(true);
  297. }, 2000);
  298. }
  299. });
  300. }
  301. }
  302. findFilter(data) {
  303. this.filter = data;
  304. console.log("筛选站点", data);
  305. this.getStationList();
  306. }
  307. /**
  308. * 获取所有充电桩
  309. * @param {Location} region 当前位置
  310. * @param {boolean} first 是否初始化
  311. */
  312. getStationList(region, first) {
  313. //Dialog.showProgressDialog();
  314. if (getUserId()) {
  315. this.filter.operaUserId = getUserId()
  316. }
  317. if (region && (this.settingInfo.moveMyLocation || first)) {
  318. this.setState({
  319. region: region
  320. });
  321. }
  322. apiStation.getAllStation(this.filter).then(res => {
  323. Dialog.dismissLoading();
  324. if (res.data.sites) {
  325. const list = [];
  326. res.data.sites.forEach(item => {
  327. let available = false
  328. if (item.allConnector && item.allConnector.available) {
  329. available = true
  330. }
  331. list.push({
  332. id: item.sitePk,
  333. name: item.siteName,
  334. //address: item.siteAddress,
  335. available: available,
  336. siteType: item.siteType,
  337. latitude: item.locationLatitude,
  338. longitude: item.locationLongitude,
  339. favorite: item?.favorite ? true : false,
  340. hasLabel: item?.hasLabel,
  341. upcoming: item?.upcoming
  342. /*acConnector: item.acConnector,
  343. allConnector: item.allConnector,
  344. dcConnector: item.dcConnector,
  345. distance: utils.getDistance(item.distance)*/
  346. });
  347. });
  348. this.setState({
  349. stopList: list
  350. });
  351. if (this.filter.connectorType == "" && this.filter.parkingFee == "ALL") {
  352. storage.setStorageJson(KEY_STORE_LIST, list);
  353. }
  354. }
  355. this.refresh = false;
  356. }).catch(err => {
  357. Dialog.dismissLoading();
  358. toastShort(err);
  359. /*this.setState({
  360. stopList: []
  361. });*/
  362. this.refresh = false;
  363. })
  364. }
  365. //点击Marker获取StaionInfo
  366. viewChargeStation(id) {
  367. //console.log('info', this.state.stopList[index]);
  368. //const stationInfo = this.state.stopList[index];
  369. if (this.state.stationId == id) {
  370. return;
  371. }
  372. this.setState({
  373. stationId: id,
  374. stationInfo: {},
  375. showStation: true
  376. });
  377. if (app.modules.bookmarks) {
  378. for (let i = 0; i < this.state.stopList.length; i++) {
  379. let info = this.state.stopList[i];
  380. if (info.id == id) {
  381. this.viewIndex = i;
  382. break;
  383. }
  384. }
  385. }
  386. if (this.state.hasPermission) {
  387. navigator.geolocation.getCurrentPosition(location => {
  388. let region = {
  389. latitude: location.coords.latitude,
  390. longitude: location.coords.longitude,
  391. latitudeDelta: 0.0922,
  392. longitudeDelta: 0.0421
  393. }
  394. setTimeout(() => {
  395. this.getStationInfo(id, region);
  396. }, 500);
  397. /*if (this.settingInfo.alwaysLocation) {
  398. this.setState({
  399. region: region
  400. });
  401. }*/
  402. });
  403. } else {
  404. //无定位权限仍然显示
  405. setTimeout(() => {
  406. this.getStationInfo(id, this.state.region);
  407. }, 500);
  408. }
  409. //startPage(PageList.chargeDetail, {...this.state.stopList[index]});
  410. }
  411. getStationInfo(id, location) {
  412. apiStation.getStationRate({
  413. sitePk: id,
  414. lat: location?.latitude,
  415. lng: location?.longitude
  416. }).then(res => {
  417. if (res.data.sitePk) {
  418. var info = utils.getSiteInfo(res.data);
  419. this.setState({
  420. stationInfo: info
  421. });
  422. } else {
  423. this.setState({
  424. stationInfo: {}
  425. });
  426. }
  427. }).catch(err => {
  428. this.setState({
  429. stationInfo: {}
  430. });
  431. toastShort(err);
  432. });
  433. }
  434. onCloseInfo() {
  435. this.viewIndex = -1;
  436. this.setState({
  437. stationId: "",
  438. stationInfo: {},
  439. showStation: false
  440. });
  441. }
  442. favoriteSite() {
  443. if (this.state.stationInfo?.id) {
  444. Dialog.showProgressDialog();
  445. apiStation.bookmarkSite(this.state.stationInfo.id).then(res => {
  446. const info = {...this.state.stationInfo, favorite: !this.state.stationInfo.favorite}
  447. if (this.viewIndex >= 0) {
  448. const list = [...this.state.stopList]
  449. const inf = {...list[this.viewIndex], favorite: info.favorite};
  450. list[this.viewIndex] = inf;
  451. this.setState({
  452. stopList: list,
  453. stationInfo: info
  454. });
  455. } else {
  456. this.setState({
  457. stationInfo: info
  458. });
  459. }
  460. }).catch(err => {
  461. toastShort(err);
  462. console.log(err)
  463. }).finally(() => {
  464. Dialog.dismissLoading();
  465. })
  466. }
  467. }
  468. hidePermissionPanel() {
  469. this.setState({
  470. permissionDenied: false
  471. })
  472. }
  473. render() {
  474. return (
  475. <View style={ui.flex1}>
  476. { app.isLumiWhitelabel
  477. ? <MapUILumi
  478. state={this.state}
  479. navigation={this.props.navigation}
  480. onFilter={data => this.findFilter(data)}
  481. onMapReady={() => this.onMapReady()}
  482. onFavorite={() => this.favoriteSite()}
  483. onCloseInfo={() => this.onCloseInfo()}
  484. onLocation={() => this.checkPermission2Geo(true)}
  485. useApplesMap={this.settingInfo.useApplesMap}
  486. showUserLocation={this.state.hasPermission && this.settingInfo.alwaysLocation}
  487. viewChargeStation={id => this.viewChargeStation(id)}/>
  488. : <MapUI
  489. state={this.state}
  490. navigation={this.props.navigation}
  491. onFilter={data => this.findFilter(data)}
  492. onMapReady={() => this.onMapReady()}
  493. onFavorite={() => this.favoriteSite()}
  494. onCloseInfo={() => this.onCloseInfo()}
  495. onLocation={() => this.checkPermission2Geo(true)}
  496. useApplesMap={this.settingInfo.useApplesMap}
  497. showUserLocation={this.state.hasPermission && this.settingInfo.alwaysLocation}
  498. viewChargeStation={id => this.viewChargeStation(id)}
  499. />
  500. }
  501. <LocationPermission.VIEW
  502. visible={this.state.permissionDenied}
  503. onView={() => this.hidePermissionPanel()}/>
  504. <View style={styles.drawerLeftTouchView}></View>
  505. </View>
  506. );
  507. }
  508. }
  509. const styles = StyleSheet.create({
  510. drawerLeftTouchView: {
  511. top: 0,
  512. left: 0,
  513. bottom: 0,
  514. width: 4,
  515. zIndex: 5,
  516. position: 'absolute',
  517. backgroundColor: 'rgba(0,0,0,0)'
  518. }
  519. })