index.js
3.24 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
import Taro, { Component } from '@tarojs/taro';
import { View, Text} from '@tarojs/components';
import { SearchItem, SearchBar, PageTitle } from '../../components';
import {search as searchModel} from '../../models';
import './index.scss';
import router from '../../router/index';
import Yas from '../../utils/yas';
let yas;
export default class Search extends Component {
constructor() {
super(...arguments);
this.state = {
hotSearch: [],
latelySearch: [],
hotSearchTitle: '热门推荐',
latelySearchTitle: '最近搜索',
fixed: false
};
}
config = {
navigationBarTitleText: '搜索'
}
componentWillMount() {
yas = new Yas(this.$scope);
yas.pageOpenReport();
}
componentDidMount () {
const list = Taro.getStorageSync('latelySearch');
console.log(list);
this.setState({
latelySearch: list || []
})
searchModel.getSearchWord().then(ret => {
if (ret && ret.code === 200) {
this.setState({
hotSearch: ret.data || []
});
}
})
}
componentDidShow() {
const list = Taro.getStorageSync('latelySearch');
this.setState({
latelySearch: list || []
})
}
onScroll(e) {
if (e.detail.scrollTop > 48) {
this.setState({
fixed: true
});
} else {
this.setState({
fixed: false
});
}
}
onScrolltoupper() {
this.setState({
fixed: false
});
}
onComplate(e) {
if (e.detail.value === '') {
return;
}
this.saveLateSearch(e);
router.go('productList', {
query: e.detail.value
});
}
saveLateSearch(e) {
const latelySearch = this.state.latelySearch;
if (latelySearch.length >= 10) {
latelySearch.pop();
}
let setList = this.removeSameSearch(latelySearch, e.detail.value);
setList.unshift({search_word: e.detail.value});
try {
Taro.setStorageSync('latelySearch', setList);
} catch (error) {
console.log(error);
}
// this.setState({
// latelySearch
// })
}
removeSameSearch(arr, search_word) {
let hash = [];
for (var i = 0; i < arr.length; i++) {
if (arr[i].search_word !== search_word) {
hash.push(arr[i]);
}
}
return hash;
}
onRemoveAllSearch() {
try {
Taro.setStorageSync('latelySearch', []);
} catch (error) {
console.log(error);
}
this.setState({
latelySearch: []
})
}
render () {
let {hotSearchTitle, hotSearch, latelySearchTitle, latelySearch} = this.state;
let value = '';
return (
<View>
<View className='search-out-box'>
<SearchBar onComplate={this.onComplate} value={value}></SearchBar>
</View>
<ScrollView
className='search-page'
scrollY
scrollWithAnimation
scrollTop='0'
lowerThreshold='20'
upperThreshold='20'
onScrolltoupper={this.onScrolltoupper}
onScroll={this.onScroll}>
{/* <PageTitle pageTitle="搜索"></PageTitle> */}
{
latelySearch.length > 0 &&
<View className='latelySearch'>
<SearchItem title={latelySearchTitle} list={latelySearch} isSearch={true} onRemoveAllSearch={this.onRemoveAllSearch}></SearchItem>
</View>
}
{
hotSearch.length > 0 &&
<View className={latelySearch.length > 0 ? '' : 'hotSearch'}>
<SearchItem title={hotSearchTitle} list={hotSearch} ></SearchItem>
</View>
}
</ScrollView>
</View>
)
}
}