infoList.js 5.15 KB
import React, { Component, Fragment } from 'react'
import { connect } from 'dva'
import { routerRedux } from 'dva/router';
import { Toast } from 'antd-mobile';
import shareHide from "../../utils/shareHide";

import css from './databank.less';

class InfoList extends Component {
  constructor(){
    super()
    this.state={
      dataList: [],
      filter: [],
      chooseIdx: 0,
      productId:''
    }
  }

  componentDidMount() {

    const { search } = this.props.location;
    const _this = this;
    let key = Number(sessionStorage.getItem('key'));
    shareHide();
    Toast.loading('loading...');
    if (search) {
      const matchs = decodeURI(search).match(/riskCode=(.*)&riskName=(.*)/i)
      let riskCode = matchs[1]
      let riskName = matchs[2]
      document.title = riskName
        this.props.dispatch({
        type: 'addUser/Dictionaries',
        payload: {
          configType: 'dataConfig'
        },
        callback(data) {
          if (data.length) {
            _this.setState({
              filter: data,
              productId:riskCode
            });
            _this.props.dispatch({
              type: 'databank/getDataInfoList',
              payload: {
                productId: riskCode,
                dataType: data[key].configCode,
                orgId: window.localStorage.getItem('orgId'),
                proId: window.localStorage.getItem('project')
              },
              callback(res) {
                if (res.status) {
                  Toast.hide();
                  _this.setState({
                    dataList: res.data
                  })
                }
              }
            })
          }
        }
      })
    }
  }

  filterData(configCode) {
    const _this = this;
    Toast.loading('loading...');
    _this.props.dispatch({
      type: 'databank/getDataInfoList',
      payload: {
        productId:this.state.productId,
        dataType: configCode,
        orgId: window.localStorage.getItem('orgId'),
        proId: window.localStorage.getItem('project')
      },
      callback(res) {
        if (res.status) {
          Toast.hide();
          _this.setState({
            dataList: res.data
          })
        }
      }
    })
  }


  render() {
    let chooseIdx = 0;
    if(sessionStorage.getItem("key")){
      chooseIdx = Number(sessionStorage.getItem("key"))
    }
    const { dataList } = this.state;
    return (
      <Fragment>
        <div className={css.pList}>
          <div className={css.pTagList}>
            <ul>
              {
                this.state.filter.length > 0 && this.state.filter.map((el, i) => <li key={i} className={chooseIdx === i ? 'on' : ''} onClick={() => {
                  this.filterData(el.configCode)
                  this.setState({
                    chooseIdx: i
                  })
                  sessionStorage.setItem( "key", i);
                }}>{el.configName}</li>)
              }
            </ul>
          </div>
          <ul className={css.fileList}>
            {
              dataList && dataList.map((el, i) => {
                let type = 'pdf';
                switch (el.dataFormat.toLowerCase()) {
                  case 'pdf':
                    type = 'pdf';
                    break;
                  case 'mp4':
                    type = 'video';
                    break;
                  default:
                    type = '';
                    break;
                }
                if(/doc|docx/i.test(el.dataFormat)) type = 'word';
                if(/ppt|pptx/i.test(el.dataFormat)) type = 'ppt';
                if(/png|jpg|gif/i.test(el.dataFormat)) type = 'img';
                if(/xls|xlsx/i.test(el.dataFormat)) type = 'excel';

                return <li key={i} className={type} onClick={() => {
                    //记录点击数
                    this.props.dispatch({
                      type:"home/setClickRecord",
                      payload: {
                        "operCode": el.dataCode,
                        "operFunction": 100100,
                        "operTitle": el.dataName,
                        "operType": 201,
                      },
                      callback(data){
                        console.log('资料库点击一次') 
                      }
                    });
                    if (type === 'pdf') {
                      window.location.href = el.dataPath
                    } else if (/doc|docx|xls|xlsx|ppt|pptx/i.test(el.dataFormat)) {
                      window.location.href = 'https://view.officeapps.live.com/op/view.aspx?src=' + el.dataPath
                    }  else {
                        this.props.dispatch(routerRedux.push({
                          pathname: '/databank/preview',
                          state: {
                            url:el.dataPath
                          },
                        }));
                      }
                  }}>
                  <span>{el.dataName+'.'+el.dataFormat.toLowerCase()}</span>
                </li>
              })
            }
          </ul>
          <div className={css.nomore}>没有更多啦</div>
        </div>
      </Fragment>
    )
  }
}


export default connect(({ databank }) => ({ databank }))(InfoList)