search.vue
8.49 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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
<template>
<view class="content">
<view class="search-box">
<!-- mSearch组件 如果使用原样式,删除组件元素-->
<mSearch class="mSearch-input-box" :mode="2" button="inside" :placeholder="defaultKeyword" @search="doSearch(false)"
@input="inputChange" @confirm="doSearch(false)" v-model="keyword"></mSearch>
<!-- 原样式 如果使用原样式,恢复下方注销代码 -->
<!--
<view class="input-box">
<input type="text" :adjust-position="true" :placeholder="defaultKeyword" @input="inputChange" v-model="keyword" @confirm="doSearch(false)"
placeholder-class="placeholder-class" confirm-type="search">
</view>
<view class="search-btn" @tap="doSearch(false)">搜索</view>
-->
<!-- 原样式 end -->
</view>
<view class="search-keyword">
<scroll-view class="keyword-list-box" v-show="isShowKeywordList" scroll-y>
<block v-for="(row,index) in keywordList" :key="index">
<view class="keyword-entry" hover-class="keyword-entry-tap">
<view class="keyword-text" @tap.stop="doSearch(keywordList[index].keyword)">
<rich-text :nodes="row.htmlStr"></rich-text>
</view>
<view class="keyword-img" @tap.stop="setKeyword(keywordList[index].keyword)">
<image src="/static/img/search/back.png"></image>
</view>
</view>
</block>
</scroll-view>
<scroll-view class="keyword-box" v-show="!isShowKeywordList" scroll-y>
<view class="keyword-block" v-if="oldKeywordList.length>0">
<view class="keyword-list-header">
<view>历史搜索</view>
<view>
<image @tap="oldDelete" src="/static/img/search/delete.png"></image>
</view>
</view>
<view class="keyword">
<view v-for="(keyword,index) in oldKeywordList" @tap="doSearch(keyword)" :key="index">{{keyword}}</view>
</view>
</view>
</scroll-view>
</view>
</view>
</template>
<script>
//引用mSearch组件,如不需要删除即可
import mSearch from '@/components/mehaotian-search-revision/mehaotian-search-revision.vue';
export default {
data() {
return {
defaultKeyword: "",
keyword: "",
oldKeywordList: [],
keywordList: [],
isShowKeywordList: false
}
},
onLoad() {
this.init();
},
components: {
//引用mSearch组件,如不需要删除即可
mSearch
},
methods: {
init() {
this.loadDefaultKeyword();
this.loadOldKeyword();
this.loadHotKeyword();
},
blur() {
uni.hideKeyboard()
},
//加载默认搜索关键字
loadDefaultKeyword() {
//定义默认搜索关键字,可以自己实现ajax请求数据再赋值,用户未输入时,以水印方式显示在输入框,直接不输入内容搜索会搜索默认关键字
this.defaultKeyword = "默认关键字";
},
//加载历史搜索,自动读取本地Storage
loadOldKeyword() {
uni.getStorage({
key: 'OldKeys',
success: (res) => {
var OldKeys = JSON.parse(res.data);
this.oldKeywordList = OldKeys;
}
});
},
//监听输入
inputChange(event) {
//兼容引入组件时传入参数情况
var keyword = event.detail ? event.detail.value : event;
if (!keyword) {
this.keywordList = [];
this.isShowKeywordList = false;
return;
}
this.isShowKeywordList = true;
//以下示例截取淘宝的关键字,请替换成你的接口
uni.request({
url: 'https://suggest.taobao.com/sug?code=utf-8&q=' + keyword, //仅为示例
success: (res) => {
this.keywordList = [];
this.keywordList = this.drawCorrelativeKeyword(res.data.result, keyword);
}
});
},
//高亮关键字
drawCorrelativeKeyword(keywords, keyword) {
var len = keywords.length,
keywordArr = [];
for (var i = 0; i < len; i++) {
var row = keywords[i];
//定义高亮#9f9f9f
var html = row[0].replace(keyword, "<span style='color: #9f9f9f;'>" + keyword + "</span>");
html = '<div>' + html + '</div>';
var tmpObj = {
keyword: row[0],
htmlStr: html
};
keywordArr.push(tmpObj)
}
return keywordArr;
},
//顶置关键字
setKeyword(index) {
this.keyword = this.keywordList[index].keyword;
},
//清除历史搜索
oldDelete() {
uni.showModal({
content: '确定清除历史搜索记录?',
success: (res) => {
if (res.confirm) {
console.log('用户点击确定');
this.oldKeywordList = [];
uni.removeStorage({
key: 'OldKeys'
});
} else if (res.cancel) {
console.log('用户点击取消');
}
}
});
},
//执行搜索
doSearch(keyword) {
keyword = keyword === false ? this.keyword : keyword;
this.keyword = keyword;
this.saveKeyword(keyword); //保存为历史
uni.showToast({
title: keyword,
icon: 'none',
duration: 2000
});
//以下是示例跳转淘宝搜索,可自己实现搜索逻辑
/*
//#ifdef APP-PLUS
plus.runtime.openURL(encodeURI('taobao://s.taobao.com/search?q=' + keyword));
//#endif
//#ifdef H5
window.location.href = 'taobao://s.taobao.com/search?q=' + keyword
//#endif
*/
},
//保存关键字到历史记录
saveKeyword(keyword) {
uni.getStorage({
key: 'OldKeys',
success: (res) => {
var OldKeys = JSON.parse(res.data);
var findIndex = OldKeys.indexOf(keyword);
if (findIndex == -1) {
OldKeys.unshift(keyword);
} else {
OldKeys.splice(findIndex, 1);
OldKeys.unshift(keyword);
}
//最多10个纪录
OldKeys.length > 10 && OldKeys.pop();
uni.setStorage({
key: 'OldKeys',
data: JSON.stringify(OldKeys)
});
this.oldKeywordList = OldKeys; //更新历史搜索
},
fail: (e) => {
var OldKeys = [keyword];
uni.setStorage({
key: 'OldKeys',
data: JSON.stringify(OldKeys)
});
this.oldKeywordList = OldKeys; //更新历史搜索
}
});
}
}
}
</script>
<style>
view {
display: block;
}
.search-box {
width: 95%;
background-color: rgb(242, 242, 242);
padding: 15rpx 2.5%;
display: flex;
justify-content: space-between;
position: sticky;
top: 0;
}
.search-box .mSearch-input-box {
width: 100%;
}
.search-box .input-box {
width: 85%;
flex-shrink: 1;
display: flex;
justify-content: center;
align-items: center;
}
.search-box .search-btn {
width: 15%;
margin: 0 0 0 2%;
display: flex;
justify-content: center;
align-items: center;
flex-shrink: 0;
font-size: 28rpx;
color: #fff;
background: linear-gradient(to right, #ff9801, #ff570a);
border-radius: 60rpx;
}
.search-box .input-box>input {
width: 100%;
height: 60rpx;
font-size: 32rpx;
border: 0;
border-radius: 60rpx;
-webkit-appearance: none;
-moz-appearance: none;
appearance: none;
padding: 0 3%;
margin: 0;
background-color: #ffffff;
}
.placeholder-class {
color: #9e9e9e;
}
.search-keyword {
width: 100%;
background-color: rgb(242, 242, 242);
}
.keyword-list-box {
height: calc(100vh - 110rpx);
padding-top: 10rpx;
border-radius: 20rpx 20rpx 0 0;
background-color: #fff;
}
.keyword-entry-tap {
background-color: #eee;
}
.keyword-entry {
width: 94%;
height: 80rpx;
margin: 0 3%;
font-size: 30rpx;
color: #333;
display: flex;
justify-content: space-between;
align-items: center;
border-bottom: solid 1rpx #e7e7e7;
}
.keyword-entry image {
width: 60rpx;
height: 60rpx;
}
.keyword-entry .keyword-text,
.keyword-entry .keyword-img {
height: 80rpx;
display: flex;
align-items: center;
}
.keyword-entry .keyword-text {
width: 90%;
}
.keyword-entry .keyword-img {
width: 10%;
justify-content: center;
}
.keyword-box {
height: calc(100vh - 110rpx);
border-radius: 20rpx 20rpx 0 0;
background-color: #fff;
}
.keyword-box .keyword-block {
padding: 10rpx 0;
}
.keyword-box .keyword-block .keyword-list-header {
width: 94%;
padding: 10rpx 3%;
font-size: 27rpx;
color: #333;
display: flex;
justify-content: space-between;
}
.keyword-box .keyword-block .keyword-list-header image {
width: 40rpx;
height: 40rpx;
}
.keyword-box .keyword-block .keyword {
width: 94%;
padding: 3px 3%;
display: flex;
flex-flow: wrap;
justify-content: flex-start;
}
.keyword-box .keyword-block .keyword>view {
display: flex;
justify-content: center;
align-items: center;
border-radius: 60rpx;
padding: 0 20rpx;
margin: 10rpx 20rpx 10rpx 0;
height: 60rpx;
font-size: 28rpx;
background-color: rgb(242, 242, 242);
color: #6b6b6b;
}
</style>