Home.js 13 KB

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