search.js
6.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
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);