Home.js 14 KB

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