brandSearch.js
4.16 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
import { getYHStorageSync } from '../../utils/util';
Component({
options: {
multipleSlots: true // 在组件定义时的选项中启用多slot支持
},
/**
* 组件的属性列表
* 用于组件自定义设置
*/
properties: {
all_list: {
type: Object,
value: {},
},
hotKeyword: {
type: Object,
value: {},
}
},
/**
* 私有数据,组件的初始数据
* 可用于模版渲染
*/
data: {
result: {},
showResult: false,
hasResult: false,
searchHistory: [],
},
ready: function () {
let searchHistory = getYHStorageSync('searchHistory', 'brandSearch') ? getYHStorageSync('searchHistory','brandSearch') : [];
this.setData({
searchHistory,
});
},
/**
* 组件的方法列表
* 更新属性和数据的方法与更新页面数据的方法类似
*/
methods: {
//搜索框文字改变
inputChanged: function (event) {
let text = event.detail.value;
text = text.toLowerCase();
if (text) {
let all_list = this.properties.all_list;
let result = {};
let hasResult = false;
for (let k in all_list) {
let list = all_list[k];
let ary = [];
list && list.map((value, key) => {
let name = value.brand_name.toLowerCase();
let cnName = value.brand_name_cn.toLowerCase();
let enName = value.brand_name_en.toLowerCase();
if (value.type == 2 && (name.includes(text)
|| cnName.includes(text)
|| enName.includes(text))) { //type==1 店铺 type==2 品牌
ary.push(value);
hasResult = true;
}
})
if (ary.length > 0) {
result[k] = ary;
}
}
this.setData({
hasResult,
showResult: true,
result,
})
}else {
this.setData({
hasResult: false,
showResult: false,
result: {},
})
}
},
//搜索框获取焦点焦点
focusChanged: function (event) {
// console.log('focusChanged')
},
//搜索框键盘点击确定
inputConfirmed: function (event) {
// console.log('inputConfirmed')
},
//热门搜索点击
hotKeywordItemTapped: function (event) {
let brandItem = event.currentTarget.dataset.item;
this.triggerEvent('dismissSearchView', {});
// console.log(event.currentTarget.dataset)
wx.navigateTo({
url: '../goodsList/brand?brandId=' + brandItem.brandId + '&brandName=' + brandItem.brandName + '&page_name=' + 'brands' + '&page_param=' + ''
});
},
historyKeywordItemTapped: function (event) {
let brandItem = event.currentTarget.dataset.item;
this.triggerEvent('dismissSearchView', {});
// console.log(event.currentTarget.dataset)
wx.navigateTo({
url: '../goodsList/brandStore?shop_id=' + brandItem.shop_id + '&shop_name' + brandItem.brand_name + '&page_name=' + 'brands' + '&page_param=' + ''
});
},
cancelSearch: function (event) {
this.triggerEvent('dismissSearchView', {});
},
clearSearchHistory: function (e) {
try {
let searchHistory = [];
this.setData({
searchHistory,
});
wx.removeStorageSync('searchHistory');
} catch (e) { }
},
search_brandItemTapped: function (e) {
let brandItem = e.currentTarget.dataset.brandItem;
let searchHistory = this.data.searchHistory;
let isHistory = false;
for (var i = 0; i < searchHistory.length; i++) {
let item = searchHistory[i];
if (item.brand_name == brandItem.brand_name) {
isHistory = true;
}
}
if (!isHistory) {
searchHistory.push(brandItem);
this.setData({
searchHistory,
});
try {
wx.setStorageSync('searchHistory', searchHistory);
} catch (e) {
};
}
this.triggerEvent('dismissSearchView', {});
wx.navigateTo({
url: '../goodsList/brandStore?shop_id=' + brandItem.shop_id + '&shop_name' + brandItem.brand_name
});
},
}
})