search.js 6.35 KB
import React from 'react';
import { connect } from 'dva'
import { SearchBar, Button, Tabs ,List ,Toast} from 'antd-mobile';
import styles from './Home.css'
import shareHide from "../../utils/shareHide";
import NoData from "../../components/NoData";
import $ from 'jquery';
import { routerRedux } from 'dva/router';
const Item = List.Item;
const { TabPane } = Tabs;

const tabs = [
  { title: '全部', sub: '1' },
  { title: '资料库', sub: '2' },
  { title: '行业动态', sub: '3' },
];
class Index extends React.Component{
  constructor(props) {
    super(props);
    this.state = {
      searchValue: "",
      allList:null,
      currentTab:"1",//默认全部
      inputType:'',
      isFirst:true,
    }
  }
  componentDidMount() {
    document.title = '华夏O2O智慧工作平台';
    shareHide();
    let that= this;
    let searchObj = sessionStorage.getItem("searchObj") ? JSON.parse(sessionStorage.getItem("searchObj")) : null;
    if(searchObj){
      this.setState({searchValue:searchObj.value,currentTab:searchObj.tab},()=>{
        this.getAllData(searchObj.value,searchObj.tab);
      })
    }else {
      this.getAllData("","1");
    }
    $('#inputElem').focus();
    //监听手机键盘输入的内容,输入汉字还是其他的
    $('#inputElem').on('compositionstart', function () {
      that.setState({inputType:'字母'})
    });
    $('#inputElem').on('compositionend', function (e) { 
      that.setState({inputType:'汉字'})
      that.getAllData(that.state.searchValue,that.state.currentTab);
    });
  }
  componentWillUnmount(){
    document.title= ''
    $('#inputElem').off('compositionstart');
    $('#inputElem').off('compositionend');
  }
  //获取查询的结果
  getAllData = (key,type) =>{
    let that = this;
    let otherKey={};
    if(type === "1"){
      otherKey.searchType = "102"
      if(key === ''){
        otherKey.searchType = "101"
      }
    }
    if(type === "2"){
      otherKey.searchType = "100100"
    }
    if(type === "3"){
      otherKey.searchType = "100110"
    }
    Toast.loading('loading...',0);
    this.props.dispatch({
          type: 'home/getSearchList',
          payload: {
              "key":key,
              ...otherKey
          },
          callback(res) {
            let data = res || [];
            that.setState({
              allList: data,
              isFirst: key !== "" ? false : true
            })
            Toast.hide();
          },
          error(err) {
            Toast.error(err.message)
          }
        })

  }
  onChange= (value) => {
    this.setState({ searchValue:value });
    //this.setState({ isFirst: value !== "" ? false : true });
    if(this.state.inputType){
      if(this.state.inputType == '汉字'){
        this.getAllData(value,this.state.currentTab);
      }
    }else{
       this.getAllData(value,this.state.currentTab);
    }
  }
  goTargetPage =(item) =>{
    console.log('goTargetPage----->',item)
    let searchType = item.searchType + "";
    if(searchType === '100100'){//资料库
      if(item.dataPath){
        let type = 'pdf';
        switch (item.dataFormat.toLowerCase()) {
          case 'pdf':
            type = 'pdf';
            break;
          case 'mp4':
            type = 'video';
            break;
          default:
            type = '';
            break;
        }
        if(/doc|docx/i.test(item.dataFormat)) type = 'word';
        if(/ppt|pptx/i.test(item.dataFormat)) type = 'ppt';
        if(/png|jpg|gif/i.test(item.dataFormat)) type = 'img';
        if(/xls|xlsx/i.test(item.dataFormat)) type = 'excel';
        if (type === 'pdf') {
          window.location.href = item.dataPath
        } else if (/doc|docx|xls|xlsx|ppt|pptx/i.test(item.dataFormat)) {
          window.location.href = 'https://view.officeapps.live.com/op/view.aspx?src=' + item.dataPath
        }  else {
            this.props.dispatch(routerRedux.push({
              pathname: '/databank/preview',
              state: {
                url:item.dataPath
              },
            }));
          }
      }
    }
    if(searchType === '100110'){//行业动态
      this.props.history.push({ pathname: "/industrynews/detail", search: 'id=' + item.id });
    }
    sessionStorage.setItem("searchObj",JSON.stringify({tab:this.state.currentTab,value:this.state.searchValue}))//跳转的时候先缓存下来
  }
  clickTab = (tab) =>{
    console.log('clickTab', tab.sub);
    let value = this.state.searchValue;
    this.setState({ currentTab:tab.sub });
    this.getAllData(value,tab.sub);
    sessionStorage.setItem("searchObj",JSON.stringify({tab:this.state.currentTab,value:this.state.searchValue}))
  }
  _extraContent(item){
    return(
        <div><span className={item.dataType ? styles["tipsRight"+item.dataType] : styles.tipsRight5}>{item.dataTypeName}</span></div>
      )
  }
  render() {
    const {allList,currentTab,searchValue,isFirst} = this.state;
    const content=(
        <div onClick={()=>{this.goTargetPage()}}>资料</div>
      );
    let tab = this.state.currentTab -1;//页签默认展示的值
    return (
        <div className={styles.searchWarp}>
          <div className={styles.search}>
            <input
              onChange={(e) => {
                this.onChange(e.target.value);
              }}
              value={this.state.searchValue}
              type="search"
              id="inputElem"
              ref={ref => this.autoFocusInst = ref}
              placeholder={"请输入关键字"} />
              <div className={styles.cancelButton} onClick={()=>{this.props.history.go(-1)}}>取消</div>
          </div>
          <div className={styles.searchContent}>
            <div className={styles.tabsHeader}>
              {tabs.map((item,index)=>{
                return <button className={tab === index ? styles.active : ""} key={item.sub} onClick={()=>{this.clickTab(item)}}>{item.title}</button>
              })}
            </div>
            <div className={styles.tabsContent}>
              {allList !== null &&(
                  allList.length>0 ? <List style={{width:'100%'}}>
                    {allList.map((item,index)=>{
                      return <Item key={index} extra={this._extraContent(item)} onClick={()=>{this.goTargetPage(item)}}>{item.dataName}</Item>
                    })}
                  </List> : <NoData />
                )
              }
            </div>
          </div>
        </div>
      )
  }
}

export default connect(({home})=>({home}))(Index);