Home.js 13 KB

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