Home.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  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. Dialog.showProgressDialog();
  267. if (getUserId()) {
  268. this.filter.operaUserId = getUserId()
  269. }
  270. apiStation.getAllStation(this.filter).then(res => {
  271. Dialog.dismissLoading();
  272. if (res.data.sites) {
  273. const list = [];
  274. res.data.sites.forEach(item => {
  275. let available = false
  276. if (item.allConnector && item.allConnector.available) {
  277. available = true
  278. }
  279. list.push({
  280. id: item.sitePk,
  281. name: item.siteName,
  282. //address: item.siteAddress,
  283. available: available,
  284. siteType: item.siteType,
  285. latitude: item.locationLatitude,
  286. longitude: item.locationLongitude,
  287. favorite: item?.favorite ? true : false,
  288. upcoming: false
  289. /*acConnector: item.acConnector,
  290. allConnector: item.allConnector,
  291. dcConnector: item.dcConnector,
  292. distance: utils.getDistance(item.distance)*/
  293. });
  294. });
  295. this.setState({
  296. stopList: list
  297. });
  298. //this.viewChargeStation(list[0].id) //测试显示站点
  299. if (region && (this.settingInfo.moveMyLocation || first)) {
  300. setTimeout(() => {
  301. this.setState({
  302. region: region
  303. });
  304. }, 500);
  305. }
  306. }
  307. }).catch(err => {
  308. Dialog.dismissLoading();
  309. toastShort(err);
  310. this.setState({
  311. stopList: []
  312. });
  313. })
  314. }
  315. //点击Marker获取StaionInfo
  316. viewChargeStation(id) {
  317. //console.log('info', this.state.stopList[index]);
  318. //const stationInfo = this.state.stopList[index];
  319. if (app.modules.bookmarks) {
  320. for (let i = 0; i < this.state.stopList.length; i++) {
  321. let info = this.state.stopList[i];
  322. if (info.id == id) {
  323. this.viewIndex = i;
  324. break;
  325. }
  326. }
  327. }
  328. if (this.state.hasPermission) {
  329. navigator.geolocation.getCurrentPosition(location => {
  330. let region = {
  331. latitude: location.coords.latitude,
  332. longitude: location.coords.longitude,
  333. latitudeDelta: 0.0922,
  334. longitudeDelta: 0.0421
  335. }
  336. this.getStationInfo(id, region);
  337. /*if (this.settingInfo.alwaysLocation) {
  338. this.setState({
  339. region: region
  340. });
  341. }*/
  342. });
  343. } else {
  344. //无定位权限仍然显示
  345. this.getStationInfo(id, this.state.region);
  346. }
  347. //startPage(PageList.chargeDetail, {...this.state.stopList[index]});
  348. }
  349. getStationInfo(id, location) {
  350. apiStation.getStationRate({
  351. sitePk: id,
  352. lat: location?.latitude,
  353. lng: location?.longitude
  354. }).then(res => {
  355. if (res.data.sitePk) {
  356. var info = utils.getSiteInfo(res.data);
  357. this.setState({
  358. stationInfo: info
  359. });
  360. } else {
  361. this.setState({
  362. stationInfo: {}
  363. });
  364. }
  365. }).catch(err => {
  366. this.setState({
  367. stationInfo: {}
  368. });
  369. toastShort(err);
  370. });
  371. }
  372. favoriteSite() {
  373. if (this.state.stationInfo?.id) {
  374. Dialog.showProgressDialog();
  375. apiStation.bookmarkSite(this.state.stationInfo.id).then(res => {
  376. const info = {...this.state.stationInfo, favorite: !this.state.stationInfo.favorite}
  377. if (this.viewIndex >= 0) {
  378. const list = [...this.state.stopList]
  379. const inf = {...list[this.viewIndex], favorite: info.favorite};
  380. list[this.viewIndex] = inf;
  381. this.setState({
  382. stopList: list,
  383. stationInfo: info
  384. });
  385. } else {
  386. this.setState({
  387. stationInfo: info
  388. });
  389. }
  390. }).catch(err => {
  391. toastShort(err);
  392. console.log(err)
  393. }).finally(() => {
  394. Dialog.dismissLoading();
  395. })
  396. }
  397. }
  398. render() {
  399. return (
  400. <View style={ui.flex1}>
  401. <MapUI
  402. state={this.state}
  403. navigation={this.props.navigation}
  404. onFilter={data => this.findFilter(data)}
  405. onMapReady={() => this.onMapReady()}
  406. onFavorite={() => this.favoriteSite()}
  407. onLocation={() => this.checkPermission2Geo(true)}
  408. showUserLocation={this.state.hasPermission && this.settingInfo.alwaysLocation}
  409. viewChargeStation={id => this.viewChargeStation(id)}
  410. />
  411. <LocationPermission.VIEW visible={this.state.permissionDenied}/>
  412. <View style={styles.drawerLeftTouchView}></View>
  413. </View>
  414. );
  415. }
  416. }
  417. const styles = StyleSheet.create({
  418. drawerLeftTouchView: {
  419. top: 0,
  420. left: 0,
  421. bottom: 0,
  422. width: 4,
  423. zIndex: 5,
  424. position: 'absolute',
  425. backgroundColor: 'rgba(0,0,0,0)'
  426. }
  427. })