Skip to content

Commit 24fa1c8

Browse files
committed
feat: support search
1 parent 70626b2 commit 24fa1c8

File tree

12 files changed

+205
-3
lines changed

12 files changed

+205
-3
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ module.exports = {
8181

8282
### v2.0.0
8383

84-
- [ ] 支持搜索
84+
- [x] 支持搜索 `v1.1.0`
8585
- [ ] 支持个人分享 (可能受限个人小程序无法做到)
8686

8787
## 补充

miniprogram/app.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
"pages/changelog/detail",
99
"pages/learn/index",
1010
"pages/learn/detail",
11-
"pages/cart/index"
11+
"pages/cart/index",
12+
"pages/search/index"
1213
],
1314
"window": {
1415
"backgroundColor": "#f6f6f6",
@@ -34,6 +35,8 @@
3435
"t-tab-panel": "tdesign-miniprogram/tabs/tab-panel",
3536
"t-loading": "tdesign-miniprogram/loading/loading",
3637
"t-message": "tdesign-miniprogram/message/message",
38+
"t-navbar": "tdesign-miniprogram/navbar/navbar",
39+
"t-search": "tdesign-miniprogram/search/search",
3740
"t-switch": "tdesign-miniprogram/switch/switch",
3841
"t-steps": "tdesign-miniprogram/steps/steps",
3942
"t-step": "tdesign-miniprogram/steps/step-item"

miniprogram/pages/changelog/log.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
11
export default [{
2+
version: '1.1.0',
3+
detail: [{
4+
type: 'feature',
5+
value: '支持搜索'
6+
}],
7+
date: '2022-03-19'
8+
}, {
29
version: '1.0.0',
310
detail: [{
411
type: 'feature',

miniprogram/pages/index/index.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ Page({
66
data: {
77
list: [],
88
value: 'index',
9+
searchKeyword: '',
910
tabbars: [{
1011
text: '首页',
1112
value: 'index',
@@ -60,6 +61,15 @@ Page({
6061
})
6162
},
6263

64+
handleToSearch() {
65+
const content = this.data.searchKeyword.trim();
66+
wx.navigateTo({
67+
url: '/pages/search/index?keyword=' + content,
68+
}).then(() => {
69+
this.setData({ searchKeyword: '' })
70+
})
71+
},
72+
6373
onShareAppMessage() {
6474
return {
6575
title: '程序员做饭指南',

miniprogram/pages/index/index.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{
22
"navigationBarTitleText": "程序员做饭",
3+
"navigationStyle": "custom",
34
"usingComponents": {
45
}
56
}

miniprogram/pages/index/index.less

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,33 @@
11
page {
2-
padding: 48rpx 32rpx 200rpx;
2+
padding: 0 0 200rpx;
3+
}
4+
5+
.brand {
6+
display: flex;
7+
align-items: center;
8+
}
9+
10+
.logo {
11+
width: 50rpx;
12+
height: 50rpx;
13+
}
14+
15+
.name {
16+
display: flex;
17+
align-items: center;
18+
font-size: 28rpx;
19+
}
20+
21+
.header {
22+
// background-color: #fff;
23+
padding: 24rpx 32rpx 0;
24+
}
25+
26+
.search {
27+
background: rgba(221, 221, 221, .7) !important;
28+
}
29+
30+
.container {
31+
margin-top: 32rpx;
32+
padding: 0 32rpx;
333
}

miniprogram/pages/index/index.wxml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,15 @@
11
<!--index.wxml-->
2+
<t-navbar home-icon="slot" title="" >
3+
<view class="brand" slot="home-icon">
4+
<image class="logo" src="cloud://cloud1-9g17b7v0e010c809.636c-cloud1-9g17b7v0e010c809-1309933988/primary/240_F_292003584_6Zc3rf7qmIdGY2FMGrA1pQ7hjxYAZs0V.jpeg" />
5+
</view>
6+
<view class="name" slot="home-icon">做饭指南</view>
7+
</t-navbar>
8+
9+
<view class="header">
10+
<t-search t-class-input-container="search" model:value="{{searchKeyword}}" shape="round" placeholder="搜索菜谱" center bind:submit="handleToSearch" />
11+
</view>
12+
213
<view class="container">
314
<list
415
wx:for="{{list}}"

miniprogram/pages/search/index.js

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
import cookbook from '../../data.js';
2+
import config from '../../config/index'
3+
4+
Page({
5+
data: {
6+
keyword: '',
7+
result: [],
8+
isLoading: false,
9+
focus: false,
10+
chineseMap: config.chineseMap,
11+
themeMap: {
12+
dessert: 'danger',
13+
breakfast: 'success',
14+
staple: 'warning'
15+
}
16+
},
17+
18+
onLoad(options) {
19+
let keyword = options.keyword.trim()
20+
if (keyword) {
21+
this.setData({ keyword });
22+
this.doSearch(keyword)
23+
}
24+
},
25+
26+
handleSearchClear() {
27+
this.setData({
28+
result: [],
29+
keyword: '',
30+
focus: true
31+
})
32+
},
33+
34+
handleSearch({ detail }) {
35+
if (detail.value == '') {
36+
this.setData({ result: [] })
37+
return
38+
};
39+
40+
this.setData({ isLoading: true })
41+
if (this.lastTrigger) {
42+
clearTimeout(this.lastTrigger)
43+
}
44+
this.lastTrigger = setTimeout(() => {
45+
this.doSearch(detail.value)
46+
}, 500)
47+
},
48+
49+
doSearch(keyword) {
50+
const fuzzySearch = (key) => {
51+
if (typeof key == 'string') return key.indexOf(keyword) > -1
52+
if (Array.isArray(key)) {
53+
return key.some(item => fuzzySearch(item))
54+
}
55+
if (typeof key == 'object') {
56+
return Object.keys(key).some(item => fuzzySearch(item))
57+
}
58+
return false
59+
}
60+
const result = cookbook.filter(cb => {
61+
const isTitleMatch = cb.name.indexOf(keyword) > -1;
62+
// const isDescMatch = fuzzySearch(cb.desc)
63+
64+
return isTitleMatch;
65+
})
66+
this.setData({
67+
result,
68+
isLoading: false
69+
})
70+
this.updateViews(keyword)
71+
},
72+
73+
updateViews(key) {
74+
wx.cloud.callFunction({
75+
name: 'updateViews',
76+
data: {
77+
id: key,
78+
type: 'search'
79+
},
80+
})
81+
},
82+
83+
onShareAppMessage() {
84+
return {
85+
title: '我推荐的菜谱',
86+
path: '/pages/search/index?keyword=' + this.data.keyword
87+
}
88+
}
89+
})
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"usingComponents": {}
3+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
page {
2+
background-color: #fff;
3+
}
4+
5+
.container {
6+
padding: 32rpx;
7+
}
8+
9+
.result {
10+
margin-top: 24rpx;
11+
}
12+
13+
.tips {
14+
text-align: center;
15+
font-size: 28rpx;
16+
color: #999;
17+
margin-bottom: 24rpx;
18+
}
19+
20+
.loading-box {
21+
margin-top: 24rpx;
22+
display: flex;
23+
justify-content: center;
24+
}

0 commit comments

Comments
 (0)