| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448 |
- /**
- * 首页通用页
- * @邠心vbe on 2021/08/13
- */
- import React, { Component } from 'react';
- import { View, StyleSheet, BackHandler, Linking } from 'react-native';
- import app from '../../../app.json'
- import { i18nUtil } from '../../i18n';
- import apiBase from '../../api/apiBase';
- import apiStation from '../../api/apiStation';
- import Dialog from '../../components/Dialog';
- import utils from '../../utils/utils';
- import { SettingUtil } from '../Settings';
- import MyStatusBar from '../../components/MyStatusBar';
- import LocationPermission from './maps/LocationPermission';
- import MapUI from './MapUI';
- export default class HomePage extends Component {
- constructor(props) {
- super(props);
- this.state = {
- region: {
- latitude: 1.3532623163977149,
- longitude: 103.87092316860532,
- latitudeDelta: 0.0922,
- longitudeDelta: 0.0421
- },
- mapReady: false,
- stationInfo: {},
- stopList: [],
- hasPermission: isIOS,
- permissionDenied: false
- };
- this.isHide = true;
- this.denied = true;
- this.backSeconds = 0;
- this.refreshTime = 0;
- this.settingInfo = {}
- this.viewIndex = -1;
- this.filter = {
- parkingFee: 'ALL',
- connectorType: ''
- };
- }
- componentDidMount() {
- const navigation = this.props.navigation;
- navigation.addListener('focus', () => {
- //toastShort('onResume')
- this.setState({
- stationInfo: {}
- });
- this.viewIndex = -1;
- SettingUtil.getSettings(set => {
- //console.log("获取设置信息", set);
- this.settingInfo = set;
- this.checkPermission2Geo();
- })
- this.isHide = false;
- if (!app.isWhitelabel) {
- MyStatusBar.setStatusBarThemes(MyStatusBar.DARK_STYLE, colorLight);
- }
- });
- navigation.addListener('blur', () => {
- //toastShort('onStop')
- this.isHide = true;
- if (!app.isWhitelabel) {
- MyStatusBar.setStatusBarThemes(MyStatusBar.LIGHT_STYLE, colorPrimaryDark);
- }
- setTimeout(() => {
- navigation.closeDrawer();
- }, 200);
- });
-
- /*navigation.addListener('beforeRemove', (e) => {
- if (isIOS) {
- e.preventDefault();
- } else {
- let time = new Date().getTime();
- if (time - this.backSeconds < 2000) {
- BackHandler.exitApp();
- } else {
- toastShort("Press the back button again to exit the program");
- this.backSeconds = time;
- e.preventDefault();
- }
- }
- });*/
- BackHandler.addEventListener('hardwareBackPress', this.toExit)
- this.isHide = false;
- this.checkUpdateVersion();
- }
- componentWillUnmount() {
- /*console.log("componentWillUnmount")
- if (this.unsubscribe) {
- this.unsubscribe();
- }*/
- this.backSeconds = 0;
- BackHandler.removeEventListener("hardwareBackPress", this.toExit)
- }
- toExit = () => {
- //console.log("beforeRemove", this.isHide);
- if (this.state.stationInfo?.id) {
- this.setState({
- stationInfo: {}
- });
- return true;
- }
- if (!this.isHide) {
- if (isIOS) {
- //e.preventDefault();
- } else {
- let time = new Date().getTime();
- //console.log("beforeRemove2", time, this.backSeconds);
- if (time - this.backSeconds < 2000) {
- BackHandler.exitApp();
- } else {
- toastShort("Press the back button again to exit the program");
- this.backSeconds = time;
- //e.preventDefault();
- }
- }
- return true;
- }
- }
- onMapReady() {
- this.setState({
- mapReady: true
- }, () => {
- this.checkPermission2Geo();
- });
- }
- checkUpdateVersion() {
- apiBase.checkUpdate().then(res => {
- if (res.data.versionCode > app.versionCode) {
- var desc = JSON.parse(res.data?.versionDesc ?? "{}");
- if (desc) {
- const upLog = i18nUtil.analyzeLocaleData(desc);
- if (typeof upLog == "string") {
- this.showUpdateDialog({
- title: $t("home.versionUpdate"),
- message: $t("home.newVersionName") + res.data.versionName + "\n\n" + upLog,
- showCancel: !(res.data.force),
- ok: $t("home.updateNow"),
- cancel: $t("home.later"),
- callback: btn => {
- if (btn == Dialog.BUTTON_OK) {
- //TODO 跳转到应用商店
- const uri = isIOS
- ? app.storeUrl.ios
- : app.storeUrl.android
- Linking.openURL(uri);
- }
- }
- })
- }
- }
- }
- }).catch(err => {
- })
- }
- showUpdateDialog(options) {
- if (Dialog.isShowing()) {
- setTimeout(() => {
- this.showUpdateDialog(options)
- }, 500);
- return;
- }
- Dialog.showDialog(options)
- }
- getPermission() {
- LocationPermission.requestPermission(hasPermission => {
- if (hasPermission) {
- this.setState({
- hasPermission: true
- });
- this.checkPermission2Geo();
- } else {
- this.noPermissionSite();
- }
- })
- }
- checkPermission2Geo(refresh) {
- if (this.state.hasPermission) {
- if (refresh) {
- //避免关闭自动移动地图后无法点击按钮移动地图
- this.state.stopList = []
- }
- this.infoGeoLocation();
- } else {
- LocationPermission.checkPermission((hasPermission, canRequestPermission) => {
- if (hasPermission) {
- this.setState({
- hasPermission: true
- });
- this.infoGeoLocation();
- } else {
- if (canRequestPermission) {
- if (this.denied) {
- this.denied = false;
- this.getPermission();
- } else {
- //避免多次请求
- this.denied = true;
- this.noPermissionSite();
- }
- } else {
- this.noPermissionSite();
- }
- }
- })
- }
- }
- noPermissionSite() {
- console.log("未获取权限也获取站点列表");
- this.setState({
- hasPermission: false,
- permissionDenied: true
- });
- let first = this.state.stopList.length == 0;//是否第一次加载
- this.getStationList(this.state.region, first);
- }
- /**
- * 获取定位数据
- * @param {*} again
- */
- infoGeoLocation(again) {
- if (this.state.mapReady) {
- navigator.geolocation.getCurrentPosition(location => {
- let region = {
- latitude: location.coords.latitude,
- longitude: location.coords.longitude,
- latitudeDelta: 0.0922,
- longitudeDelta: 0.0421
- }
- //console.log("getGeoLocation", region);
- //if (this.state.stopList.length == 0) { //只加载一次
- let time = new Date().getTime();
- let interval = this.settingInfo.refreshInterval * 1000;//重复加载时间
- let first = this.state.stopList.length == 0;//是否第一次加载
- if (first || time - this.refreshTime > interval) { //一分钟(10秒)加载一次
- this.getStationList(region, first);
- this.refreshTime = time;
- } else if (this.settingInfo.moveMyLocation) {
- this.setState({
- region: region
- });
- }
- }, error => {
- console.info("getGeoLocation", error);
- if (!again) {
- setTimeout(() => {
- this.infoGeoLocation(true);
- }, 2000);
- }
- });
- }
- }
- findFilter(data) {
- this.filter = data;
- this.getStationList();
- }
- /**
- * 获取所有充电桩
- * @param {Location} region 当前位置
- * @param {boolean} first 是否初始化
- */
- getStationList(region, first) {
- Dialog.showProgressDialog();
- if (getUserId()) {
- this.filter.operaUserId = getUserId()
- }
- apiStation.getAllStation(this.filter).then(res => {
- Dialog.dismissLoading();
- if (res.data.sites) {
- const list = [];
- res.data.sites.forEach(item => {
- let available = false
- if (item.allConnector && item.allConnector.available) {
- available = true
- }
- list.push({
- id: item.sitePk,
- name: item.siteName,
- //address: item.siteAddress,
- available: available,
- siteType: item.siteType,
- latitude: item.locationLatitude,
- longitude: item.locationLongitude,
- favorite: item?.favorite ? true : false,
- upcoming: false
- /*acConnector: item.acConnector,
- allConnector: item.allConnector,
- dcConnector: item.dcConnector,
- distance: utils.getDistance(item.distance)*/
- });
- });
- this.setState({
- stopList: list
- });
- //this.viewChargeStation(list[0].id) //测试显示站点
- if (region && (this.settingInfo.moveMyLocation || first)) {
- setTimeout(() => {
- this.setState({
- region: region
- });
- }, 500);
- }
- }
- }).catch(err => {
- Dialog.dismissLoading();
- toastShort(err);
- this.setState({
- stopList: []
- });
- })
- }
- //点击Marker获取StaionInfo
- viewChargeStation(id) {
- //console.log('info', this.state.stopList[index]);
- //const stationInfo = this.state.stopList[index];
- if (app.modules.bookmarks) {
- for (let i = 0; i < this.state.stopList.length; i++) {
- let info = this.state.stopList[i];
- if (info.id == id) {
- this.viewIndex = i;
- break;
- }
- }
- }
- if (this.state.hasPermission) {
- navigator.geolocation.getCurrentPosition(location => {
- let region = {
- latitude: location.coords.latitude,
- longitude: location.coords.longitude,
- latitudeDelta: 0.0922,
- longitudeDelta: 0.0421
- }
- this.getStationInfo(id, region);
- /*if (this.settingInfo.alwaysLocation) {
- this.setState({
- region: region
- });
- }*/
- });
- } else {
- //无定位权限仍然显示
- this.getStationInfo(id, this.state.region);
- }
- //startPage(PageList.chargeDetail, {...this.state.stopList[index]});
- }
- getStationInfo(id, location) {
- apiStation.getStationRate({
- sitePk: id,
- lat: location?.latitude,
- lng: location?.longitude
- }).then(res => {
- if (res.data.sitePk) {
- var info = utils.getSiteInfo(res.data);
- this.setState({
- stationInfo: info
- });
- } else {
- this.setState({
- stationInfo: {}
- });
- }
- }).catch(err => {
- this.setState({
- stationInfo: {}
- });
- toastShort(err);
- });
- }
- favoriteSite() {
- if (this.state.stationInfo?.id) {
- Dialog.showProgressDialog();
- apiStation.bookmarkSite(this.state.stationInfo.id).then(res => {
- const info = {...this.state.stationInfo, favorite: !this.state.stationInfo.favorite}
- if (this.viewIndex >= 0) {
- const list = [...this.state.stopList]
- const inf = {...list[this.viewIndex], favorite: info.favorite};
- list[this.viewIndex] = inf;
- this.setState({
- stopList: list,
- stationInfo: info
- });
- } else {
- this.setState({
- stationInfo: info
- });
- }
- }).catch(err => {
- toastShort(err);
- console.log(err)
- }).finally(() => {
- Dialog.dismissLoading();
- })
- }
- }
- render() {
- return (
- <View style={ui.flex1}>
- <MapUI
- state={this.state}
- navigation={this.props.navigation}
- onFilter={data => this.findFilter(data)}
- onMapReady={() => this.onMapReady()}
- onFavorite={() => this.favoriteSite()}
- onLocation={() => this.checkPermission2Geo(true)}
- showUserLocation={this.state.hasPermission && this.settingInfo.alwaysLocation}
- viewChargeStation={id => this.viewChargeStation(id)}
- />
- <LocationPermission.VIEW visible={this.state.permissionDenied}/>
- <View style={styles.drawerLeftTouchView}></View>
- </View>
- );
- }
- }
- const styles = StyleSheet.create({
- drawerLeftTouchView: {
- top: 0,
- left: 0,
- bottom: 0,
- width: 4,
- zIndex: 5,
- position: 'absolute',
- backgroundColor: 'rgba(0,0,0,0)'
- }
- })
|