myInfo.js 7.22 KB
/**
 * Created by Cassie on 2018/05/14
 */
import React, {Component} from "react";
import {StyleSheet,
    View,
    Text,
    TouchableOpacity,
    Image,
    ScrollView,
    DeviceEventEmitter,
    StatusBar,
    ActivityIndicator
} from "react-native";
import moment from "moment";
import {zoomW, zoomH} from "../../utils/getSize";
import {xnToast, NoDoublePress} from "../../utils/utils";
import AppService from "../../service/AppService";

const back = require('../../img/returnB.png');
const avatar = require('../../img/avatar.png');

export default class MyInfo extends Component {
    // static navigationOptions = ({navigation, screenProps}) => ({
    //     title:'消息列表',
    //     headerLeft: (<View style={{flex: 1, display: 'flex', flexDirection: 'row'}}>
    //         <TouchableOpacity style={styles.topIcon}
    //                           onPress={navigation.state.params ? navigation.state.params.back : null}>
    //             <Image source={back} style={{width: (16 / zoomH), height: (16 / zoomH)}} resizeMode="contain"/>
    //         </TouchableOpacity>
    //     </View>),
    //     headerRight: (<View style={{flex: 1, display: 'flex', flexDirection: 'row'}}/>)
    // });

    constructor(props) {
        super(props);
        this.state = {
            sheetList: []
        };
    };

    componentWillMount(){
        this.getInfo();

    };

    componentDidMount() {
        this.props.navigation.setParams({
            title:'消息列表',
            backgroundColor:'white',
            titleColor:'black',
            isBack:true,
            backClick:()=>{this.back()},
        });
    };
    back(){
        NoDoublePress.onPress(() => {
            DeviceEventEmitter.emit('reloadLink');
            this.props.navigation.goBack();
        })
    }

    //获取相关消息
    getInfo(){
        if (global.isConnected) {
            this.setState({
                loading: true
            });
            let params = {
                pageSize:0,
                objectId:global.userId,
                objectType:'USER',
                isReaded:false,
                businessId:this.props.navigation.state.params.id
            };
            AppService.getMyInfo(params).then((data) => {
                this.setState({
                    loading: false
                });
                //查询完之后再去清角标
                this.cleanMyInfo();
                if (data.message) {
                    xnToast(data.message);
                    return;
                }
                if (data.errors.length > 0) {
                    xnToast(data.errors[0].message);
                } else {
                    this.setState({
                        sheetList:data.result
                    })
                }
            }).catch((error) => {
                this.setState({
                    loading: false
                });
                xnToast(error)
            })
        } else {
            xnToast('暂无网络连接,请稍后重试!')
        }
    };
    //我相关的消息清除提醒
    cleanMyInfo(){
        let params = {
            objectId:global.userId,
            objectType:'USER',
            businessId:this.props.navigation.state.params.id
        };
        AppService.cleanMyInfo(params).then((data) => {
            if(data.message){
                xnToast(data.message);
                return;
            }
            if(data.errors.length > 0){
                xnToast(data.errors[0].message);
            }
        }).catch((error) => {
            xnToast(error)
        })
    };

    /*渲染列表*/
    renderItem(item, index) {
        return (
            <TouchableOpacity activeOpacity={1} key={index} style={styles.targetItem}>
                <View style={{
                    borderRadius: (30 / zoomH),
                    borderStyle: 'solid',
                    borderColor: '#ddd',
                    borderWidth: StyleSheet.hairlineWidth,
                    width: (52 / zoomH),
                    height: (52 / zoomH)
                }}>
                    <Image
                        source={item.userAvatar ? {uri: item.userAvatar + '?x-oss-process=image/resize,w_500'} : avatar}
                        style={{width: (52 / zoomH), height: (52 / zoomH), borderRadius: (26 / zoomH)}}
                        resizeMode="cover"/>
                </View>
                <View style={{flex: 1, paddingLeft: (10 / zoomW)}}>
                    <View style={{display:'flex',flexDirection:'row',justifyContent:'space-between',marginBottom:(10/zoomH)}}>
                        <Text style={{fontSize: 14, color: '#576B95'}}>{item.dealObjectName}</Text>
                        <Text style={{fontSize: 14, color: '#666'}}>{moment(Number(item.creationTime)).format('YYYY-MM-DD HH:mm')}</Text>
                    </View>
                    <View>
                        <Text style={{fontSize: 16, color: '#333'}}>{item.businessContent}</Text>
                    </View>
                </View>
            </TouchableOpacity>
        )
    };

    componentWillUnmount() {
        this.setState = (state, callback) => {
            return;
        };
    };

    render() {
        return (
            <View style={styles.background}>
                <StatusBar
                    backgroundColor={'#FFFFFF'}
                    barStyle={"dark-content"}
                    networkActivityIndicatorVisible
                />
                {!this.state.loading && <ScrollView style={{width: '100%',backgroundColor:'#ececf1'}}>
                    <View style={{width: '100%', height: (10 / zoomH)}}/>
                    {this.state.sheetList && this.state.sheetList.map((item, index) => this.renderItem(item, index))}
                </ScrollView>}
                {this.state.loading && <View style={styles.loadingBg}>
                    <View style={styles.loadingBox}>
                        <ActivityIndicator size='large' color='#fff'/>
                        <Text style={{fontSize: 16, color: '#fff', marginTop: (6 / zoomH)}}>加载中...</Text>
                    </View>
                </View>}
            </View>
        );
    }
}

const styles = StyleSheet.create({
    topIcon: {
        paddingLeft: (10 / zoomW),
        paddingRight: (10 / zoomW),
        height: '100%',
        display: 'flex',
        justifyContent: 'center'
    },
    background: {
        flex: 1,
        backgroundColor: '#fff',
        display: 'flex',
        alignItems: 'center'
    },
    targetItem: {
        flex: 1,
        display: 'flex',
        flexDirection: 'row',
        borderStyle: 'solid',
        borderBottomWidth: StyleSheet.hairlineWidth,
        borderBottomColor: '#eee',
        paddingLeft: (15 / zoomW),
        paddingRight: (15 / zoomW),
        paddingTop: (10 / zoomH),
        paddingBottom: (10 / zoomH),
        backgroundColor: '#fff'
    },
    loadingBg: {
        position: 'absolute',
        top: 0,
        width: '100%',
        height: '100%',
        display: 'flex',
        justifyContent: 'center',
        alignItems: 'center'
    },
    loadingBox: {
        width: (100 / zoomW),
        height: (120 / zoomH),
        backgroundColor: 'rgba(0,0,0,.5)',
        borderRadius: 8,
        display: 'flex',
        alignItems: 'center',
        justifyContent: 'center'
    }
});