FileUploadSection.js 10.3 KB
import React,{PropTypes} from 'react';
import ReactDOM from 'react-dom';
import cx from 'classnames';

import UploadWrap from '../UploadWrap';
import { Row, Col } from 'antd';
import {downloadFile, getToken, showPic} from '../../../utils/fileUtil';
const s ={}
export class OssImg extends React.Component {
    constructor(props) {
        super(props);
        this.state={
            url:'',
            id:'',
        }
    }

    componentDidMount(){
        const {file}=this.props,self=this;
        console.log('componendidmount',file)

        getToken(file.object_id).then( data=>{
            var client = new OSS({
                endpoint: "https://oss-cn-beijing.aliyuncs.com",
                accessKeyId: data.access_key_id,//'SX9nVDsfn54Tsl2K',
                accessKeySecret: data.access_key_secret,//'VoYVvrMZyWBfVG9sBgA2UCqSczJnoL',
                stsToken: data.security_token,
                // bucket:"krhr"
            });
            var url = client.signatureUrl(data.object_path, {
                expires: 3600,
                'content-disposition': 'attachment; filename="' + name + '"'
            });
            self.setState({
                url:url,
                id:file.object_id
            })
        })

    }

    componentWillReceiveProps(nextProps){

    }
    render() {
        const {url,id}=this.state;
        return (
            <img src={url?url:'./img/电子文件.png'} alt="电子文件"/>
        );
    }
}


export class OssDataImg extends React.Component {
    constructor(props) {
        super(props);
        this.state={
            url:'',
            id:'',
        }
    }

    componentDidMount(){
        const {file}=this.props,self=this;
        getToken(file.uuid).then( data=>{
            var client = new OSS({
                endpoint: "https://oss-cn-beijing.aliyuncs.com",
                accessKeyId: data.access_key_id,//'SX9nVDsfn54Tsl2K',
                accessKeySecret: data.access_key_secret,//'VoYVvrMZyWBfVG9sBgA2UCqSczJnoL',
                stsToken: data.security_token,
                // bucket:"krhr"
            });

            var url = client.signatureUrl(data.object_path, {
                expires: 3600,
                'content-disposition': 'attachment; filename="' + name + '"'
            });
            self.setState({
                url:url,
                id:file.uuid
            })
        })

    }
    componentWillReceiveProps(nextProps) {
        const file=nextProps.file,self=this;
        getToken(file.uuid).then( data=>{
            var client = new OSS({
                endpoint: "https://oss-cn-beijing.aliyuncs.com",
                accessKeyId: data.access_key_id,//'SX9nVDsfn54Tsl2K',
                accessKeySecret: data.access_key_secret,//'VoYVvrMZyWBfVG9sBgA2UCqSczJnoL',
                stsToken: data.security_token,
                // bucket:"krhr"
            });

            var url = client.signatureUrl(data.object_path, {
                expires: 3600,
                'content-disposition': 'attachment; filename="' + name + '"'
            });
            self.setState({
                url:url,
                id:file.uuid
            })
        })
    }
    render() {
        const {url,id}=this.state;
        return (
            <div style={this.props.style ? this.props.style : null}>
            {
                url && <img src={url} alt="图片" style={this.props.style ? this.props.style : null}/>
            }
            </div>

        );
    }
}


export class FileUploadSection extends React.Component {
	constructor(props) {
        super(props);
        this.state={
             // fileUploadSection:[]
        };
    }
    static propTypes = {
        fileUploadSection:PropTypes.array,
        fields:PropTypes.object,
        readOnly:PropTypes.bool
    }
    static defaultProps={
        readOnly:false
    }
    handlePreview(file){
        const name=file.path.substring(file.path.lastIndexOf('/')+1);
        downloadFile(file.bucket,file.path,name,file.uuid);
    }

    fileChange(file){

    }
    fileUploading(file){

    }
    fileCallBack(files,field,info){
        const newFiles=files.concat();
        // console.log(fileDefind);
        // console.log(info);
        newFiles.push(info);
        field.onChange(JSON.stringify(newFiles));
    }
    handleDelete(index,files,field){
        const newFiles=files.concat();
        newFiles.splice(index,1);
        field.onChange(JSON.stringify(newFiles));
    }


    componentDidMount(){

    }
    // shouldComponentUpdate(nextProps, nextState){
    //     console.log('-----nextProps',nextProps)
    //     console.log('-----nextState',nextState)
    //     return false;
    //     // if(nextProps.uuid==nextState.uuid){
    //     //     return false;
    //     // }else{
    //     //     return true;
    //     // }
    // }
    render(){
    	const self=this;
        const {fileUploadSection,fields,readOnly,employee}=this.props;
        if(readOnly){
            return (
                <div  style={{float:'left', width:'100%',padding:'0 40px'}}>
                    {fileUploadSection.map((fileDefind,i)=>{
                        let files=[];
                        if(fileDefind&&fileDefind.name&&employee&&employee[fileDefind.name]){
                            files=JSON.parse(employee[fileDefind.name]);
                        }
                        return (
                            <div key={i} className={cx(s.file_item_wrap)}>
                                <div className={cx(s.part, s.clearfix)}>
                                    <p style={{margin:'10px 20px',height:'18px'}}>{fileDefind.display_name}</p>
                                    <div className={cx(s.partContain, s.clearfix)}>
                                        {files.map((f,i)=>{
                                            return (<Col key={i} span={4}>
                                                <div className={cx(s.imgBox)}>
                                                    <div className={cx('kr_icon')}>
                                                        <div className={cx(s.preview)} onClick={this.handlePreview.bind(this,f)}>
                                                          <OssImg file={f}></OssImg>
                                                          <div className={cx(s.miss)} >
                                                              <div className={cx(s.shadow)}></div>
                                                              <i>&#xe691;</i>
                                                              <b>点击预览</b>
                                                          </div>
                                                        </div>
                                                    </div>
                                                    <div className={cx(s.textBox)}>
                                                      <span >{f.name}</span>
                                                    </div>
                                                </div>
                                            </Col>);
                                        })}
                                    </div>
                                </div>
                            </div>
                        )
                    })}
                </div>
            )
        }else{
            return(
                <div  style={{float:'left', width:'100%',padding:'0 40px'}}>
                    {fileUploadSection.map((fileDefind,i)=>{
                        const field=fields[fileDefind.name]||{};
                        const value=field.value||'[]';
                        const files=JSON.parse(value);
                        return (
                            <div key={i} className={cx(s.file_item_wrap)}>
                                <div className={cx(s.part, s.clearfix)}>
                                    <p style={{margin:'10px 20px',height:'18px'}}>{fileDefind.display_name}</p>
                                    <div className={cx(s.partContain, s.clearfix)}>
                                        {files.map((f,i)=>{

                                            return (<Col key={i} span={4}>
                                                <div className={cx(s.imgBox)}>
                                                    <div className={cx('kr_icon')}>
                                                        <div className={cx(s.preview)} onClick={this.handlePreview.bind(this,f)}>
                                                          <OssImg file={f}></OssImg>
                                                          <div className={cx(s.miss)} >
                                                              <div className={cx(s.shadow)}></div>
                                                              <i>&#xe691;</i>
                                                              <b>点击预览</b>
                                                          </div>
                                                        </div>
                                                        <span className={cx(s.remove_btn)}  onClick={this.handleDelete.bind(this,i,files,field)}>&#xe699;</span>
                                                    </div>
                                                    <div className={cx(s.textBox)}>
                                                      <span >{f.name}</span>
                                                    </div>
                                                </div>
                                            </Col>);
                                        })}
                                        <Col span={4}>
                                        <div className={cx(s.iconE)}>
                                            <UploadWrap only={true} autoUpload={true} type='electronic_record' keyNum={i} onChange={self.fileChange} backResult={self.fileUploading} endCallBack={self.fileCallBack.bind(self,files,field)}>
                                                <i className={cx('kr_icon')}> &#xe621; </i>
                                            </UploadWrap>
                                        </div>
                                        </Col>
                                    </div>
                                </div>
                            </div>
                        )
                    })}
                </div>
            )
        }
    }
}

export default FileUploadSection;