index.js
7.14 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
export default function() {
return {
namespaced: true,
state: {
productList: {
showErrorPage: false,
isFetching: false,
error: null,
page: 0, // 当前页
page_size: 10, // 每页数量
page_total: 0, // 总共多少页
total: 0, // 总共多少条
endReached: false, // 到达底部
list: [], // 商品列表
isEmpty: false,
},
searchParams: {
type: 0, // type:0,推荐;1,热销;2,即将发售; 3,品类; 4,品牌;5,系列;6,搜索 7, 收藏
order: '', // 指定排序
productPool: null, // 商品池id
sort: null, // 品类id
brand: null, // 品牌id
series: null, // 系列id
gender: null, // 性别
size: null, // 尺码id
isSoonSale: null, // 是否是即将售卖
query: null, // 搜索词
limit: null, // 每页记录数
page: null, // 当前页号
coupon_token: null, // 优惠券token
},
filterParams: {
sort: [], // 品类id
brand: [], // 品牌id
gender: [], // 性别
size: [], // 尺码id
},
filterData: [],
filterVisible: false,
searchWord: [],
defaultSearchWord: [],
searchSuggestList: [],
},
mutations: {
addProductList(state, {data}) {
let {productList} = state;
if (typeof data === 'object' && Object.keys(data).length) {
for (let key in data) {
if (key === 'product_list') {
productList.list = data.page > 1 ? productList.list.concat(data.product_list) : data.product_list;
} else {
productList[key] = data[key];
}
}
state.productList = productList;
}
},
setSearchParams(state, {params}) {
console.log(params)
state.searchParams = params || {};
},
addFilterData(state, {filter}) {
state.filterData = filter;
},
setFilterParam(state, {filter}) {
let {filterParams} = state;
if (typeof filter === 'object' && Object.keys(filter).length) {
for (let key in filter) {
filterParams[key] = filter[key];
}
console.log(filterParams);
state.filterParams = filterParams;
}
},
setSearchQuery(state, {query}) {
state.searchParams = Object.assign(state.searchParams, {query});
},
setSearchSuggest(state, {searchSuggestList}) {
console.log(searchSuggestList);
state.searchSuggestList = searchSuggestList.items;
},
setSearchWords(state, {searchWord}) {
state.searchWord = searchWord;
},
setDefaultSearchWords(state, {defaultSearchWord}) {
state.defaultSearchWord = defaultSearchWord;
},
initData(state) {
state = {
productList: {
showErrorPage: false,
isFetching: false,
error: null,
page: 0, // 当前页
page_size: 10, // 每页数量
page_total: 0, // 总共多少页
total: 0, // 总共多少条
endReached: false, // 到达底部
list: [], // 商品列表
isEmpty: false,
},
searchParams: {
type: 0, // type:0,推荐;1,热销;2,即将发售; 3,品类; 4,品牌;5,系列;6,搜索 7, 收藏
order: '', // 指定排序
productPool: null, // 商品池id
sort: null, // 品类id
brand: null, // 品牌id
series: null, // 系列id
gender: null, // 性别
size: null, // 尺码id
isSoonSale: null, // 是否是即将售卖
query: null, // 搜索词
limit: null, // 每页记录数
page: null, // 当前页号
coupon_token: null, // 优惠券token
},
filterParams: {
sort: [], // 品类id
brand: [], // 品牌id
gender: [], // 性别
size: [], // 尺码id
},
filterData: [],
filterVisible: false,
searchWord: [],
defaultSearchWord: [],
searchSuggestList: [],
};
}
},
actions: {
//查询商品列表
fetchProductList: async function({commit, state}, params) {
let searchParams = state.searchParams;
let list = state.productList;
let pageSize = list.page_size;
let isReset = false, isHome = false;
if (params && params.isReset) {
isReset = true;
delete params.isReset;
}
if (params && params.isHome) {
isHome = true;
delete params.isHome;
}
if (!isHome && params && Object.keys(params).length) {
searchParams = Object.assign({...searchParams}, params)
commit('setSearchParams', {params: searchParams});
}
isHome && (searchParams = params || {});
if (!isReset && (list.endReached || (!list.endReached && list.page_total === 1))) {
return;
}
let page = isReset ? 1 : (list.page + 1);
for (let key in searchParams) {
if (!searchParams[key]) {
delete searchParams[key];
}
}
let result = await this.$api.get('/api/ufo/list/productList', {
...searchParams,
page,
pageSize
});
if (result.code === 200) {
let {data} = result;
data.endReached = (data.page === data.page_total) && (data.page_size !== 1);
commit('addProductList', {data});
}
return result.data ? result.data.length : 0;
},
//查询筛选列表
fetchFilterData: async function({commit, state}) {
let searchParams = state.searchParams;
let result = await this.$api.get('/api/ufo/list/filterData', {...searchParams});
if (result.code === 200) {
let {data} = result;
commit('addFilterData', {filter: data.filter});
}
},
// 设置筛选参数
setFilterParam: function({commit}, params) {
commit('setFilterParam', {filter: params});
},
// 设置查询参数
setSearchQuery: function({commit}, query) {
commit('setSearchQuery', {query});
},
// 请求查询关键词
fetchSearchWords: async function({commit}) {
let result = await this.$api.get('/api/ufo/list/searchWords', { word_type: 0});
commit('setSearchWords', {searchWord: result.data});
},
// 请求默认查询关键词
fetchDefaultSearchWords: async function({commit}) {
let result = await this.$api.get('/api/ufo/list/searchWords', { word_type: 1});
commit('setDefaultSearchWords', {defaultSearchWord: result.data});
},
// 联动查询输入关键词
fetchSearchSuggest: async function({commit}, query) {
let result = await this.$api.get('/api/ufo/list/searchSuggest', {query});
commit('setSearchSuggest', {searchSuggestList: result.data});
},
initData: function({commit}) {
commit('initData');
}
}
};
}