Home.js 12 KB

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