home.js 15.2 KB
/**
 * Created by Cassie on 2018/03/21
 */
import React, { Component } from 'react';
import {
    StyleSheet,
    View,
    Text,
    TouchableOpacity,
    Image,
    TextInput,
    Animated,
    ActivityIndicator,
    NativeModule
} from 'react-native';

import {zoomW,zoomH} from '../utils/getSize';
import {clock,xnToast,timeReduce,timePlus} from '../utils/utils';
import xnService from "../service/AppService";

const address = require('../img/address.png');
const circle = require('../img/circle.png');
const circleBtn = require('../img/circleBtn.png');
const addressG = require('../img/addressG.png');
const remind = require('../img/remind.png');

// const nativeBridge = NativeModules.BlueToolManage;
// const NativeModule = new NativeEventEmitter(nativeBridge);

export default class Home extends Component {
    static navigationOptions = ({navigation}) => ({
        title:'安全护航员',
    });

    constructor(props){
        super(props);
        this.state = {
            obj:{},
            nowTime:clock(),
            canEdit:false,
            loading:true,
            thisTime:'00:00:00',
            allTime:'0天0小时0分钟0秒',
            deviceCode:'E2C56DB5-DFFB-48D2-B060-D0F5A71096E0|1726|1237',
            rss:'-10'
        };
    };

    componentWillMount(){
        xnService.isAuto({id:'978091015737315328'}).then((data) => {
            this.setState({
                loading:false
            });
            if(data.message){
                xnToast(data.message);
                return;
            }
            if(data.errors.length > 0){
                xnToast(data.errors[0].message);
            }else{
                this.setState({
                    isAuto:data.safetyEscort.autoLaid,
                    isLaid:data.safetyEscort.isLaid,
                    id:data.safetyEscort.id,
                    todayEscortTime:data.todayEscortTime,
                    escortTime:data.escortTime
                },function(){
                    this.setState({
                        timer:setInterval(() => {
                            this.setState({
                                nowTime:clock(),
                                thisTime:timeReduce(1522050677845,this.state.todayEscortTime),
                                allTime:timePlus(1522050677845,this.state.escortTime)
                            })
                        },1000)
                    });
                })
            }
        });
        this.getAddress(this.state.deviceCode,this.state.rss)
    };

    componentDidMount(){
        // this.listener = NativeModule.addListener('BlueToolData',(data) => this.setState({obj:data}));
    }

    /*获取上岗地点*/
    getAddress(deviceCode,RSS){
        xnService.getAddress({deviceCode:deviceCode,RSS:RSS}).then((data) => {
            if(data.message){
                xnToast(data.message);
                return;
            }
            if(data.errors.length > 0){
                xnToast(data.errors[0].message);
            }else{
                alert(data);
            }
        })
    };
    /*修正上岗地点*/
    changeAddress(){
        this.setState({
            canEdit:!this.state.canEdit
        })
    };
    /*自动上下岗*/
    switch(){
        if(this.state.isAuto){
            this.changeAuto(false);
        }else{
            this.changeAuto(true);
        }
    };
    changeAuto(type){
        this.setState({
            loading:true
        });
        xnService.changeAuto({id:this.state.id,autoLaid:type}).then((data) => {
            this.setState({
                loading:false
            });
            if(data.message){
                xnToast(data.message);
                return;
            }
            if(data.errors.length > 0){
                xnToast(data.errors[0].message);
            }else{
                this.setState({
                    isAuto:!this.state.isAuto
                })
            }
        })
    };
    /*清除定时器*/
    componentWillUnmount(){
        clearInterval(this.state.timer);
        // this.listener.remove()
    };

    render(){
        return (
            <View style={styles.background}>
                <View style={styles.top}>
                    <View style={styles.topLeft}>
                        <Text style={{fontSize:13,color:'#616161'}}>当前状态:</Text>
                        <View style={{flex:1,flexDirection:'row',justifyContent:'center'}}>
                            {this.state.isLaid && <Text style={{fontSize:18,color:'#3e6db7'}}>{this.state.isLaid ? '已上岗':'待上岗'}</Text>}
                        </View>
                    </View>
                    <View style={styles.topRight}>
                        <View style={{width:(96/zoomW),height:(25/zoomH),display:'flex',justifyContent:'center',alignItems:'center'}}>
                            <Text style={{fontSize:13,color:'#616161'}}>自动上、下岗</Text>
                        </View>
                        <TouchableOpacity activeOpacity={0.8} style={[styles.topBtn,{backgroundColor:this.state.isAuto ? '#1fbe6e':'#c9c7c7'}]} onPress={() => this.switch()}>
                            <Animated.View style={[styles.circle,{left:this.state.isAuto ? new Animated.Value(14/zoomW):new Animated.Value(2/zoomW)}]} />
                        </TouchableOpacity>
                    </View>
                </View>
                <View style={styles.content}>
                    {/*<View style={styles.errorTip}>*/}
                        {/*<Image style={{width:(18/zoomW),height:(18/zoomW)}} source={remind} resizeMode="cover" />*/}
                        {/*<Text style={{fontSize:14,color:'#f26e6e',marginLeft:(17/zoomW)}}>请确认已进入车厢</Text>*/}
                    {/*</View>*/}
                    <View style={[styles.contentItem,{height:(94/zoomH)}]}>
                        <Image style={styles.contentCircle} source={circle} resizeMode="cover" />
                        <View style={{height:(28/zoomH),display:'flex',flexDirection:'row',alignItems:'center'}}>
                            <View style={styles.contentTip}>
                                <Text style={{fontSize:14,color:'#636363'}}>上岗</Text>
                            </View>
                            <Text style={{fontSize:14,color:'#707070'}}>03-20 08:38:42</Text>
                        </View>
                        <View style={{height:(28/zoomH),display:'flex',flexDirection:'row',alignItems:'center'}}>
                            <View style={styles.contentTip}>
                                <Image style={{width:(17/zoomW),height:(17/zoomW)}} source={address} resizeMode="center" />
                            </View>
                            {!this.state.canEdit && <Text style={{fontSize:14,color:'#707070'}}>1号线00018号车厢</Text>}
                            {this.state.canEdit && <View style={{display:'flex',flexDirection:'row',alignItems:'center'}}>
                                <View style={[styles.canEdit,{width:(20/zoomW)}]}>
                                    <TextInput autoFocus={true}  maxLength={2} underlineColorAndroid="transparent" style={{padding:0,width:(16/zoomW)}} />
                                </View>
                                <Text style={{fontSize:14,color:'#707070'}}>号线</Text>
                                <View style={[styles.canEdit,{width:(50/zoomW)}]}>
                                    <TextInput maxLength={5} underlineColorAndroid="transparent" style={{padding:0,width:(40/zoomW)}} />
                                </View>
                                <Text style={{fontSize:14,color:'#707070'}}>号车厢</Text>
                            </View>}
                            <TouchableOpacity style={{marginLeft:(20/zoomW)}} onPress={() => this.changeAddress()}>
                                <Text style={{fontSize:12,color:'#4b85e0'}}>修正</Text>
                            </TouchableOpacity>
                        </View>
                    </View>
                    {!this.state.isAuto && <View style={[styles.contentItem,{height:(246/zoomH),borderBottomWidth:0}]}>
                        <Image style={styles.contentCircle} source={circle} resizeMode="cover" />
                        <View style={{height:(28/zoomH),display:'flex',flexDirection:'row',alignItems:'center'}}>
                            <View style={styles.contentTip}>
                                <Text style={{fontSize:14,color:'#636363'}}>上岗</Text>
                            </View>
                        </View>
                        <View style={{height:(30/zoomH),display:'flex',flexDirection:'row',alignItems:'flex-end',justifyContent:'center',marginLeft:-(35/zoomW)}}>
                            <Text style={{fontSize:12,color:'#4b85e0'}}>点击下岗</Text>
                        </View>
                        <View style={styles.circleBtnBox}>
                            <TouchableOpacity style={{width:(110/zoomW),height:(110/zoomW),display:'flex',alignItems:'center',justifyContent:'center'}} activeOpacity={0.8}>
                                <Image style={styles.circleBtn} source={circleBtn} resizeMode="center" />
                                <Text style={{fontSize:14,color:'#fff'}}>下岗</Text>
                                <Text style={{fontSize:14,color:'#fff'}}>{this.state.nowTime}</Text>
                            </TouchableOpacity>
                        </View>
                        <View style={{marginLeft:-(35/zoomW),display:'flex',flexDirection:'row',justifyContent:'center',alignItems:'center'}}>
                            <Image style={{width:(17/zoomW),height:(17/zoomW),marginRight:(7/zoomW)}} source={addressG} resizeMode="center" />
                            <Text style={{fontSize:12,color:'#a0a0a0'}}>当前下岗位置:1号线00018号车厢</Text>
                            {/*<Text style={{fontSize:14,color:'#f26e6e'}}>请确认已进入车厢</Text>*/}
                        </View>
                    </View>}
                    {this.state.isAuto && <View style={[styles.contentItem,{height:(94/zoomH),borderBottomWidth:0}]}>
                        <Image style={styles.contentCircle} source={circle} resizeMode="cover" />
                        <View style={{height:(28/zoomH),display:'flex',flexDirection:'row',alignItems:'center'}}>
                            <View style={styles.contentTip}>
                                <Text style={{fontSize:14,color:'#636363'}}>在岗</Text>
                            </View>
                            <Text style={{fontSize:14,color:'#707070'}}>03-20 08:38:42</Text>
                        </View>
                        <View style={{height:(28/zoomH),display:'flex',flexDirection:'row',alignItems:'center'}}>
                            <View style={styles.contentTip}>
                                <Image style={{width:(17/zoomW),height:(17/zoomW)}} source={addressG} resizeMode="center" />
                            </View>
                            <Text style={{fontSize:14,color:'#707070'}}>1号线00018号车厢</Text>
                        </View>
                    </View>}
                </View>
                <View style={styles.bottom}>
                    <View style={styles.bottomItem}>
                        <View style={{width:(124/zoomW)}}>
                            <Text style={{fontSize:14,color:'#9f9f9f'}}>今日累计在岗时间:</Text>
                        </View>
                        <View style={{flex:1}}>
                            <Text style={{fontSize:14,color:'#4b85e0'}}>{this.state.thisTime}</Text>
                        </View>
                    </View>
                    <View style={[styles.bottomItem,{borderBottomWidth:0}]}>
                        <View style={{width:(124/zoomW)}}>
                            <Text style={{fontSize:14,color:'#9f9f9f'}}>总公益时间:</Text>
                        </View>
                        <View style={{flex:1}}>
                            <Text style={{fontSize:14,color:'#4b85e0'}}>{this.state.allTime}</Text>
                        </View>
                    </View>
                </View>
                {this.state.loading && <View style={styles.loadingBg}>
                    <ActivityIndicator size="large" />
                </View>}
            </View>
        );
    }
}

const styles = StyleSheet.create({
    background:{
        flex:1,
        backgroundColor:'#f6f6f6',
        display:'flex',
        alignItems:'center',
        paddingLeft:(15/zoomW),
        paddingRight:(15/zoomW)
    },
    top:{
        width:'100%',
        height:(44/zoomH),
        backgroundColor:'#fff',
        borderRadius:8,
        marginTop:(15/zoomH),
        marginBottom:(15/zoomH),
        paddingLeft:(15/zoomW),
        paddingRight:(15/zoomW),
        elevation:2,
        shadowColor:'#a9a9a9',
        shadowOffset:{width:0,height:2},
        shadowOpacity:0.5,
        shadowRadius:4,
        display:'flex',
        flexDirection:'row',
        alignItems:'center'
    },
    topLeft:{
        width:(190/zoomW),
        height:(25/zoomH),
        display:'flex',
        flexDirection:'row',
        alignItems:'center',
        borderRightWidth:1,
        borderColor:'#f6f6f6',
        borderStyle:'solid'
    },
    topRight:{
        flex:1,
        height:(25/zoomW),
        display:'flex',
        flexDirection:'row',
        alignItems:'center'
    },
    topBtn:{
        width:(30/zoomW),
        height:(16/zoomH),
        borderRadius:(8/zoomH),
        display:'flex',
        justifyContent:'center'
    },
    circle:{
        width:(14/zoomH),
        height:(14/zoomH),
        borderRadius:(7/zoomH),
        backgroundColor:'#fff',
        position:'absolute'
    },
    content:{
        width:'100%',
        backgroundColor:'#fff',
        paddingLeft:(15/zoomW),
        paddingRight:(15/zoomW)
    },
    contentItem:{
        width:'100%',
        borderBottomWidth:1,
        borderColor:'#f6f6f6',
        borderStyle:'solid',
        paddingTop:(20/zoomH),
        paddingLeft:(35/zoomW)
    },
    contentTip:{
        width:(28/zoomW),
        display:'flex',
        alignItems:'center',
        marginRight:(15/zoomW)
    },
    contentCircle:{
        width:(18/zoomW),
        height:(18/zoomW),
        position:'absolute',
        top:(35/zoomH)
    },
    circleBtnBox:{
        height:(110/zoomH),
        marginLeft:-(35/zoomW),
        marginBottom:(6/zoomH),
        display:'flex',
        alignItems:'center'
    },
    circleBtn:{
        width:'100%',
        height:'100%',
        position:'absolute',
        top:0
    },
    bottom:{
        width:'100%',
        backgroundColor:'#fff',
        marginTop:(15/zoomH),
        paddingLeft:(15/zoomW),
        paddingRight:(15/zoomW)
    },
    bottomItem:{
        width:'100%',
        height:(44/zoomH),
        borderBottomWidth:1,
        borderColor:'#f6f6f6',
        borderStyle:'solid',
        display:'flex',
        flexDirection:'row',
        alignItems:'center'
    },
    canEdit:{
        display:'flex',
        alignItems:'center'
    },
    errorTip:{
        display:'flex',
        flexDirection:'row',
        alignItems:'center',
        paddingTop:(20/zoomH)
    },
    loadingBg:{
        width:'100%',
        height:'100%',
        position:'absolute',
        display:'flex',
        alignItems:'center',
        justifyContent:'center'
    }
});