SameCity.js 3.91 KB
import React, {Component} from 'react';
import {DeviceEventEmitter, Image, NativeModules, StyleSheet, Text, View} from 'react-native';
import HomeList from '../home/HomeList';
import AppService from '../../service/AppService';
import {xnToast} from "../../utils/utils";

var Geolocation = require('Geolocation');
export default class Mine extends Component {
    static navigationOptions = ({ navigation, screenProps }) => ({
        header: null
    });
    constructor(props) {
        super(props);
        this.state = {
            network: false,
            location: '',
            cityName: this.props.cityName,
            cityCode: this.props.cityCode,
            cityList:''
        }
    }

    componentWillMount(){
        const _this = this;
        NativeModules.system.getLocation().then(data => {
        });
        this.getCityList();

        // 获取网络状态
        // NativeModules.system.getLocation().then((data)=>{
        //     console.log(this.state.location);
        // })

    };
    //因为android原生网络请求需要引入一堆文件,故获取城市列表的数据放到RN来做
    getCityList(){

        let params = {
            pageSize: 0,
        }
        AppService.citySearch(params).then((data)=>{
            if (data.message) {
                xnToast(data.message);
                return;
            }
            if (data.errors.length > 0) {
                xnToast(data.errors[0].message);
            } else {
                if (data.result !=null && data.result.length>0) {
                    this.setState({
                        cityList:JSON.stringify(data.result),
                    })
                }

            }

        }).catch((error)=>{
            xnToast(error);
            console.log("===",error)
        })


    }

    getLocation() {
        const _this = this;
        if (global.__IOS__){
            NativeModules.system.cityChoose(this.state.cityName).then((result) => {
                var data = JSON.parse(result) || {};
                global.cityName = data.name || '';
                _this.setState({
                    cityName: data.name || '',
                    cityCode: data.cityCode || ''
                },function () {
                    DeviceEventEmitter.emit('refreshHomeList');
                    DeviceEventEmitter.emit('refreshCityName');
                    DeviceEventEmitter.emit('refreshHeader');
                })

            });
        }else {
            NativeModules.system.cityChoose(this.state.cityName,this.state.cityList).then((result) => {
                var data = JSON.parse(result) || {};
                global.cityName = data.name || '';
                _this.setState({
                    cityName: data.name || '',
                    cityCode: data.cityCode || ''
                },function () {
                    DeviceEventEmitter.emit('refreshHomeList');
                    DeviceEventEmitter.emit('refreshCityName');
                    DeviceEventEmitter.emit('refreshHeader');
                })

            });
        }


    }

    render() {
        return(
            <View style={styles.background}>
                <View style={styles.addressContainer}>
                    <Image style={{height: 12, width: 10,marginRight:5}} source={require('../../img/dw.png')} />
                    <Text onPress={this.getLocation.bind(this)}>切换城市</Text>
                </View>
                <HomeList tabLabel= {''}  arr_keyValue = {[{'boardCityCode': this.state.cityCode},{tabIndex:this.props.tabIndex}]} navigation={this.props.navigation}/>
            </View>
        )
    }
}

const styles = StyleSheet.create({
    background:{
        flex:1,
        backgroundColor:'#F3F5F6',
        display:'flex',
    },
    addressContainer: {
        display: 'flex',
        flexDirection: 'row',
        alignItems: 'center',
        justifyContent: 'center',
        height: 34,
        backgroundColor: '#F3F5F6'
    }
});