addNotice.js 14 KB
/**
 * Created by Cassie on 2018/05/14
 */
import React, { Component } from 'react';
import {
    StyleSheet,
    View,
    Image,
    Text,
    TextInput,
    ScrollView,
    TouchableOpacity,
    DeviceEventEmitter,
    Modal,
    Animated,
} from 'react-native';

// import ImagePicker from 'react-native-image-picker';
import {width,height,zoomW,zoomH} from '../../utils/getSize';

const back = require('../../img/cloaseB.png');
const allDel = require('../../img/allDel.png');
const cross = require('../../img/cross.png');
const avatar = require('../../img/avatar.png');
const bread = require('../../img/bread.png');
import {xnToast,xnBorderWidth} from '../../utils/utils';

export default class AddNotice extends Component {
    static navigationOptions = ({ navigation, screenProps }) => ({
        title: "",
        headerLeft:(<View style={{display:'flex',flexDirection:'row'}}>
            <TouchableOpacity style={styles.topIcon} >
                <Image source={back} style={{width:(16/zoomH),height:(16/zoomH)}} resizeMode="contain" />
            </TouchableOpacity>
        </View>),
        headerRight:(
            <View style={{display:'flex',flexDirection:'row'}}>
                <TouchableOpacity style={styles.topIcon} onPress={navigation.state.params?navigation.state.params.doPost:null}>
                    <Text  style={{fontSize:16,color:'#00BE3C'}} resizeMode="contain" > 发布</Text>
                </TouchableOpacity>
            </View>)
    });

    constructor(props) {
        super(props);
        this.state = {
            focus:false,
            imgList :[],
            showSelect:false,
            bottom:new Animated.Value(-157),
            title:'',
            doEmployee:'',
            employeeCount:0,
            canCreate:false
        }

    }

    componentDidMount(){
        let _this = this;
        //设置头部
        this.props.navigation.setParams({
            doPost:()=>{
                if(!_this.state.title ||_this.state.title.length == 0){
                    xnToast('请填写标题');
                    return;
                }
                if(_this.state.title.length < 4){
                    xnToast('标题至少4个字');
                    return;
                }
            }
        });
        this.reloaAddEmployee = DeviceEventEmitter.addListener('reloaAddEmployee',function(){_this.reloadEmployee()});
    };

    componentWillUnmount(){
        if(this.reloaAddEmployee){
            this.reloaAddEmployee.remove();
        }
    };

    /*刷新员工显示*/
    reloadEmployee(){
        let count =0;
        if(global.doEmployee.length !=0){
            count =global.doEmployee.length;
        }
        this.setState({
            employeeCount:count,

        });
        this.checkCreate();
    };
    /*判断是否可创建*/
    checkCreate(){
        // if(this.state.objectName.length != 0 && global.doEmployee.length != 0){
        if( global.doEmployee.length != 0){
            this.props.navigation.setParams({
                color:'rgba(0,190,60,1)',
                opacity:0.8
            });
            this.setState({
                canCreate:true
            })
        }
    };
    /*打开和关闭选择图片*/
    selectPic(){
        this.setState({
            showSelect:true,
        });
        Animated.timing(
            this.state.bottom,
            {toValue:0}
        ).start();
    };
    closeSelect(){
        this.setState({
            showSelect:false,
            bottom:new Animated.Value(-157)
        });
    };

    /*发布*/
    toPublish(){
        alert("发布");
    }
    /*选择通知人员*/
    toChoosePerson() {
        this.props.navigation.navigate('addPeople');
    }
    /*跳转到选择板块*/
    toSelectPlate(){
        this.props.navigation.navigate('PlateName',{selectPlate:this.selectPlate})
    };

    /*打开相册*/
    toAlbum(){
        alert("打开相册");
        // global.openImg = true;
        // ImagePicker.launchImageLibrary({},(response) => {
        //     this.closeSelect();
        //     if(response.didCancel){}
        //     else{
        //         this.setState({
        //             loading:true
        //         });
        //         let fileName = response.fileName ? response.fileName : response.uri.substring(response.uri.lastIndexOf('/')+1);
                // AppService.getAccess({name:fileName}).then((data) => {
                //     if(data.message){
                //         xnToast(data.message);
                //         return;
                //     }
                //     if(data.errors.length > 0) {
                //         xnToast(data.errors[0].message);
                //     }else{
                //         NativeModules.system.simpleUpload(data.accessKeyId,data.accessKeySecret,data.securityToken,data.info.endpoint,data.info.bucket,fileName,response.path || response.uri).then((res) => {
                //             let extension = res.substring(res.lastIndexOf('.')+1).toLowerCase();
                //             let fileType = '';
                //             if(this.state.imgType.indexOf(extension) != -1){
                //                 fileType = 'IMAGE'
                //             }else if(this.state.videoType.indexOf(extension) != -1){
                //                 fileType = 'VEDIO'
                //             }
                //             let params = {
                //                 businessId:this.state.threadId,
                //                 businessType:'THREAD',
                //                 type:fileType,
                //                 name:res.substring(res.lastIndexOf('/')+1,res.lastIndexOf('.')),
                //                 extension:extension,
                //                 storagePath:res,
                //                 sourceType:'DIRECT'
                //             };
                //             AppService.postAddattact(params).then((data) => {
                //                 this.setState({
                //                     loading:false
                //                 });
                //                 if(data.message){
                //                     xnToast(data.message);
                //                     return;
                //                 }
                //                 if(data.errors.length > 0) {
                //                     xnToast(data.errors[0].message);
                //                 }else{
                //                     let imgList = this.state.imgList;
                //                     imgList.push({url:res,id:data.attachmentId,type:fileType});
                //                     this.setState({
                //                         imgList:imgList
                //                     });
                //                 }
                //             })
                //         })
                //     }
                // })
        //     }
        // });
    };

    render(){
        return(
            <View style={styles.background}>
                <ScrollView keyboardShouldPersistTaps='always'>
                    <View style={{paddingBottom:(15/zoomH),borderBottomWidth:StyleSheet.hairlineWidth,borderBottomColor:'#eee',borderStyle:'solid'}}>
                        <View style={styles.postTitle}>
                            <TextInput underlineColorAndroid="transparent" autoFocus={false} style={styles.titleText} placeholder='公告标题(必填)' maxLength={40} />
                        </View>
                        <View style={styles.content}>
                            <TextInput ref="text" underlineColorAndroid="transparent" multiline={true} style={styles.contentText} placeholder='输入内容' maxLength={1000}  />
                            <View style={styles.addImg}>
                                {/*{this.state.imgList && this.state.imgList.map((item,index) => this.renderImg(item,index))}*/}
                                {this.state.imgList.length < 10 &&<TouchableOpacity activeOpacity={0.6} style={styles.contentImg} onPress={() => this.selectPic()}>
                                    <Image style={{width:(20/zoomW),height:(20/zoomW)}} source={cross} resizeMode="contain" />
                                </TouchableOpacity>}
                            </View>
                        </View>
                        <TouchableOpacity activeOpacity={0.6} style={styles.selectItem} onPress={()=>this.toChoosePerson()}>
                            <View style={{display:'flex',flexDirection:'row',alignItems:'center'}}>
                                <Image source={avatar} style={styles.plateTip} resizeMode="contain" />
                                <Text style={{fontSize:16,color:'#000000'}}>通知人员</Text>
                            </View>
                            <View style={{display:'flex',flexDirection:'row',alignItems:'center'}}>
                                <Text style={{fontSize:16,color:'#000000'}}>{this.state.employeeCount}</Text>
                                <Image source={bread} style={styles.rightArrow} resizeMode="contain" />
                            </View>
                        </TouchableOpacity>
                    </View>
                    <Modal animationType={'none'} transparent={true} visible={this.state.showSelect} onRequestClose={() => {}}>
                        <TouchableOpacity activeOpacity={1} style={styles.modalBg} onPress={() => this.closeSelect()}>
                            <Animated.View style={[styles.selectContent,{bottom:this.state.bottom}]}>
                                <View style={{marginLeft:(20/zoomW),marginRight:(20/zoomW),marginBottom:7,backgroundColor:'#ffffff', borderRadius:12,borderWidth:1, borderColor:'#ddd', borderStyle:'solid'}}>
                                    <TouchableOpacity activeOpacity={.9} onPress={() => this.toAlbum()}>
                                        <View style={styles.selectModal}>
                                            <Text style={{color:'#047BFF',fontSize:21}}>图片</Text>
                                        </View>
                                    </TouchableOpacity>
                                    <Text style = {{height:xnBorderWidth(), backgroundColor:'#eee'}}></Text>
                                    <TouchableOpacity activeOpacity={.9} onPress={() => this.toAlbum()}>
                                        <View style={styles.selectModal}>
                                            <Text style={{color:'#047BFF',fontSize:21}}>文件</Text>
                                        </View>
                                    </TouchableOpacity>
                                </View>
                                <TouchableOpacity style={{marginLeft:(20/zoomW),marginRight:(20/zoomW),marginBottom:7,backgroundColor:'#ffffff', borderRadius:12,borderWidth:1, borderColor:'#ddd', borderStyle:'solid'}}
                                    activeOpacity={.9} onPress={() => this.closeSelect()}>
                                    <View style={styles.selectModal}>
                                        <Text style={{color:'#047BFF',fontSize:21}}>取消</Text>
                                    </View>
                                </TouchableOpacity>
                            </Animated.View>
                        </TouchableOpacity>
                    </Modal>
                </ScrollView>
            </View>
        );
    }

}

const styles = StyleSheet.create({
    topIcon:{
        paddingLeft:(10/zoomW),
        paddingRight:(10/zoomW),
        height:'100%',
        display:'flex',
        alignItems:'center'
    },
    background:{
        width:'100%',
        height:'100%',
        backgroundColor:'#f3f3f3'
    },
    postTitle:{
        width:'100%',
        height:(44/zoomH),
        borderBottomWidth:xnBorderWidth(),
        borderBottomColor:'#eee',
        borderStyle:'solid',
        paddingLeft:(17/zoomW),
        paddingRight:(17/zoomW),
        backgroundColor:'#fff'
    },
    targetTitle:{
        width:width,
        borderBottomWidth:StyleSheet.hairlineWidth,
        borderStyle:'solid',
        borderColor:'#eee',
        paddingLeft:(15/zoomW),
        paddingRight:(15/zoomW),
        display:'flex',
        flexDirection:'row'
    },
    titleText:{
        flex:1,
        padding:0,
        paddingRight:(10/zoomW),
        color:'#333',
        fontSize:18
    },
    addImg:{
        display:'flex',
        flexDirection:'row',
        flexWrap:'wrap',
        marginBottom:(10/zoomH)
    },
    contentImg:{
        width:(80/zoomW),
        height:(80/zoomW),
        borderWidth:1,
        borderColor:'#ddd',
        borderStyle:'solid',
        display:'flex',
        justifyContent:'center',
        alignItems:'center'
    },
    imgItem:{
        width:(80/zoomW),
        height:(80/zoomW),
        marginRight:(5/zoomW),
        marginBottom:(10/zoomH)
    },
    plateTip:{
        width:(20/zoomW),
        height:(20/zoomW),
        marginRight:(10/zoomW)
    },
    rightArrow:{
        width:(10/zoomW),
        height:(10/zoomW),
        marginLeft:(10/zoomW)
    },
    modalBg:{
        width:'100%',
        height:'100%',
        backgroundColor:'rgba(0,0,0,.5)'
    },
    content:{
        width:'100%',
        backgroundColor:'#fff',
        paddingLeft:(17/zoomW),
        paddingRight:(17/zoomW),
    },
    contentText:{
        width:'100%',
        height:(245/zoomH),
        fontSize:16,
        color:'#333',
        textAlignVertical:'top',
        padding:0,
        paddingTop:(10/zoomH),
        paddingBottom:(10/zoomH)
    },
    selectItem:{
        width:'100%',
        height:(44/zoomH),
        backgroundColor:'#ffffff',
        borderStyle:'solid',
        display:'flex',
        flexDirection:'row',
        marginTop:(10/zoomW),
        paddingLeft:(17/zoomW),
        paddingRight:(17/zoomW),
        justifyContent:'space-between',
        alignItems:'center'
    },
    selectContent:{
        width:'100%',
        position:'absolute'
    },
    selectModal:{
        width:'100%',
        height:50,
        display:'flex',
        alignItems:'center',
        justifyContent:'center',
    },
})