336 lines
6.1 KiB
JavaScript
336 lines
6.1 KiB
JavaScript
/* eslint-disable no-param-reassign */
|
||
import {
|
||
getGoodsList
|
||
} from '~/services/home/getGoodsList';
|
||
import Toast from 'tdesign-miniprogram/toast/index';
|
||
|
||
const initFilters = {
|
||
overall: 1,
|
||
sorts: '',
|
||
};
|
||
|
||
Page({
|
||
data: {
|
||
goodsList: [],
|
||
layout: 0,
|
||
priceSorts: '',
|
||
soldSorts: '',
|
||
overall: 1,
|
||
show: false,
|
||
minVal: '',
|
||
maxVal: '',
|
||
filter: initFilters,
|
||
hasLoaded: false,
|
||
keywords: '',
|
||
loadMoreStatus: 0,
|
||
loading: true,
|
||
},
|
||
|
||
total: 0,
|
||
pageNum: 1,
|
||
pageSize: 10,
|
||
|
||
onLoad(options) {
|
||
const {
|
||
searchValue = ''
|
||
} = options || {};
|
||
this.setData({
|
||
keywords: searchValue,
|
||
},
|
||
() => {
|
||
this.init(true);
|
||
},
|
||
);
|
||
},
|
||
|
||
generalQueryData(reset = false) {
|
||
const {
|
||
filter,
|
||
keywords,
|
||
minVal,
|
||
maxVal
|
||
} = this.data;
|
||
const {
|
||
pageNum,
|
||
pageSize
|
||
} = this;
|
||
const {
|
||
priceSorts,
|
||
soldSorts,
|
||
overall
|
||
} = filter;
|
||
const params = {
|
||
goodsSort: 0, // 0 综合,1 价格
|
||
pageNum: 1,
|
||
pageSize: 6,
|
||
goodsName: keywords,
|
||
};
|
||
|
||
if (priceSorts) {
|
||
params.goodsSort = 1;
|
||
params.goodsSortType = priceSorts === 'desc' ? 1 : 0;
|
||
}
|
||
|
||
if (soldSorts) {
|
||
params.goodsSort = 2;
|
||
params.goodsSortType = soldSorts === 'desc' ? 1 : 0;
|
||
}
|
||
|
||
if (overall) {
|
||
params.goodsSort = 0;
|
||
} else {
|
||
params.goodsSort = 1;
|
||
}
|
||
console.log(minVal, 'minVal', maxVal, 'maxVal');
|
||
if (minVal) {
|
||
params.minPrice = minVal * 100
|
||
}
|
||
if (maxVal) {
|
||
params.maxPrice = maxVal * 100
|
||
}
|
||
if (reset) return params;
|
||
return {
|
||
...params,
|
||
pageNum: pageNum + 1,
|
||
pageSize,
|
||
};
|
||
},
|
||
|
||
async init(reset = true) {
|
||
const {
|
||
loadMoreStatus,
|
||
goodsList = []
|
||
} = this.data;
|
||
const params = this.generalQueryData(reset);
|
||
console.log(params);
|
||
if (loadMoreStatus !== 0) return;
|
||
this.setData({
|
||
loadMoreStatus: 1,
|
||
loading: true,
|
||
});
|
||
try {
|
||
const result = await getGoodsList(params);
|
||
const data = result.data;
|
||
if (result.code == 200) {
|
||
const {
|
||
result,
|
||
totalPage = 0
|
||
} = data;
|
||
// console.log(data, 'data');
|
||
// console.log(result, 'result');
|
||
// console.log(totalPage, 'totalPage');
|
||
if (totalPage === 0 && reset) {
|
||
this.total = totalPage;
|
||
this.setData({
|
||
emptyInfo: {
|
||
tip: '抱歉,未找到相关商品',
|
||
},
|
||
hasLoaded: true,
|
||
loadMoreStatus: 0,
|
||
loading: false,
|
||
goodsList: [],
|
||
});
|
||
return;
|
||
}
|
||
|
||
const _goodsList = reset ? result : goodsList.concat(result);
|
||
const _loadMoreStatus = _goodsList.length === totalPage ? 2 : 0;
|
||
this.pageNum = params.pageNum || 1;
|
||
this.total = totalPage;
|
||
this.setData({
|
||
goodsList: _goodsList,
|
||
loadMoreStatus: _loadMoreStatus,
|
||
});
|
||
} else {
|
||
this.setData({
|
||
loading: false,
|
||
});
|
||
wx.showToast({
|
||
title: '查询失败,请稍候重试',
|
||
});
|
||
}
|
||
} catch (error) {
|
||
this.setData({
|
||
loading: false,
|
||
});
|
||
}
|
||
this.setData({
|
||
hasLoaded: true,
|
||
loading: false,
|
||
});
|
||
},
|
||
|
||
handleCartTap() {
|
||
wx.switchTab({
|
||
url: '/pages/cart/index',
|
||
});
|
||
},
|
||
|
||
handleSubmit(e) {
|
||
this.setData({
|
||
goodsList: [],
|
||
loadMoreStatus: 0,
|
||
keywords: e.detail.value
|
||
},
|
||
() => {
|
||
this.init(true);
|
||
},
|
||
);
|
||
},
|
||
|
||
onReachBottom() {
|
||
const {
|
||
goodsList
|
||
} = this.data;
|
||
const {
|
||
total = 0
|
||
} = this;
|
||
if (goodsList.length === total) {
|
||
this.setData({
|
||
loadMoreStatus: 2,
|
||
});
|
||
return;
|
||
}
|
||
this.init(false);
|
||
},
|
||
|
||
handleAddCart() {
|
||
Toast({
|
||
context: this,
|
||
selector: '#t-toast',
|
||
message: '点击加购',
|
||
});
|
||
},
|
||
|
||
gotoGoodsDetail(e) {
|
||
const {
|
||
index
|
||
} = e.detail;
|
||
const {
|
||
spuId
|
||
} = this.data.goodsList[index];
|
||
wx.navigateTo({
|
||
url: `/pages/goods/details/index?spuId=${spuId}`,
|
||
});
|
||
},
|
||
|
||
handleFilterChange(e) {
|
||
const {
|
||
layout,
|
||
overall,
|
||
priceSorts,
|
||
soldSorts
|
||
} = e.detail;
|
||
this.pageNum = 1;
|
||
this.setData({
|
||
layout,
|
||
priceSorts,
|
||
soldSorts,
|
||
overall,
|
||
loadMoreStatus: 0,
|
||
});
|
||
this.init(true);
|
||
},
|
||
|
||
// handleFilterChange(e) {
|
||
// const {
|
||
// overall,
|
||
// sorts
|
||
// } = e.detail;
|
||
// const {
|
||
// total
|
||
// } = this;
|
||
// const _filter = {
|
||
// sorts,
|
||
// overall,
|
||
// };
|
||
// this.setData({
|
||
// filter: _filter,
|
||
// sorts,
|
||
// overall,
|
||
// });
|
||
|
||
// this.pageNum = 1;
|
||
// this.setData({
|
||
// goodsList: [],
|
||
// loadMoreStatus: 0,
|
||
// },
|
||
// () => {
|
||
// total && this.init(true);
|
||
// },
|
||
// );
|
||
// },
|
||
|
||
showFilterPopup() {
|
||
this.setData({
|
||
show: true,
|
||
});
|
||
},
|
||
|
||
showFilterPopupClose() {
|
||
this.setData({
|
||
show: false,
|
||
});
|
||
},
|
||
|
||
onMinValAction(e) {
|
||
const {
|
||
value
|
||
} = e.detail;
|
||
this.setData({
|
||
minVal: value
|
||
});
|
||
},
|
||
|
||
onMaxValAction(e) {
|
||
const {
|
||
value
|
||
} = e.detail;
|
||
this.setData({
|
||
maxVal: value
|
||
});
|
||
},
|
||
|
||
reset() {
|
||
this.setData({
|
||
minVal: '',
|
||
maxVal: ''
|
||
});
|
||
},
|
||
|
||
confirm() {
|
||
const {
|
||
minVal,
|
||
maxVal
|
||
} = this.data;
|
||
let message = '';
|
||
if (minVal && !maxVal) {
|
||
message = `价格最小是${minVal}`;
|
||
} else if (!minVal && maxVal) {
|
||
message = `价格范围是0-${minVal}`;
|
||
} else if (minVal && maxVal && minVal <= maxVal) {
|
||
message = `价格范围${minVal}-${this.data.maxVal}`;
|
||
} else {
|
||
message = '请输入正确范围';
|
||
}
|
||
if (message) {
|
||
Toast({
|
||
context: this,
|
||
selector: '#t-toast',
|
||
message,
|
||
});
|
||
}
|
||
this.pageNum = 1;
|
||
this.setData({
|
||
show: false,
|
||
minVal: '',
|
||
goodsList: [],
|
||
loadMoreStatus: 0,
|
||
maxVal: '',
|
||
},
|
||
() => {
|
||
this.init();
|
||
},
|
||
);
|
||
},
|
||
}); |