通过微信开发者工具 商城模板 创建新小程序
This commit is contained in:
262
mini-program/pages/goods/result/index.js
Normal file
262
mini-program/pages/goods/result/index.js
Normal file
@@ -0,0 +1,262 @@
|
||||
/* eslint-disable no-param-reassign */
|
||||
import { getSearchResult } from '../../../services/good/fetchSearchResult';
|
||||
import Toast from 'tdesign-miniprogram/toast/index';
|
||||
|
||||
const initFilters = {
|
||||
overall: 1,
|
||||
sorts: '',
|
||||
};
|
||||
|
||||
Page({
|
||||
data: {
|
||||
goodsList: [],
|
||||
sorts: '',
|
||||
overall: 1,
|
||||
show: false,
|
||||
minVal: '',
|
||||
maxVal: '',
|
||||
minSalePriceFocus: false,
|
||||
maxSalePriceFocus: false,
|
||||
filter: initFilters,
|
||||
hasLoaded: false,
|
||||
keywords: '',
|
||||
loadMoreStatus: 0,
|
||||
loading: true,
|
||||
},
|
||||
|
||||
total: 0,
|
||||
pageNum: 1,
|
||||
pageSize: 30,
|
||||
|
||||
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 { sorts, overall } = filter;
|
||||
const params = {
|
||||
sort: 0, // 0 综合,1 价格
|
||||
pageNum: 1,
|
||||
pageSize: 30,
|
||||
keyword: keywords,
|
||||
};
|
||||
|
||||
if (sorts) {
|
||||
params.sort = 1;
|
||||
params.sortType = sorts === 'desc' ? 1 : 0;
|
||||
}
|
||||
if (overall) {
|
||||
params.sort = 0;
|
||||
} else {
|
||||
params.sort = 1;
|
||||
}
|
||||
params.minPrice = minVal ? minVal * 100 : 0;
|
||||
params.maxPrice = maxVal ? maxVal * 100 : undefined;
|
||||
if (reset) return params;
|
||||
return {
|
||||
...params,
|
||||
pageNum: pageNum + 1,
|
||||
pageSize,
|
||||
};
|
||||
},
|
||||
|
||||
async init(reset = true) {
|
||||
const { loadMoreStatus, goodsList = [] } = this.data;
|
||||
const params = this.generalQueryData(reset);
|
||||
if (loadMoreStatus !== 0) return;
|
||||
this.setData({
|
||||
loadMoreStatus: 1,
|
||||
loading: true,
|
||||
});
|
||||
try {
|
||||
const result = await getSearchResult(params);
|
||||
const code = 'Success';
|
||||
const data = result;
|
||||
if (code.toUpperCase() === 'SUCCESS') {
|
||||
const { spuList, totalCount = 0 } = data;
|
||||
if (totalCount === 0 && reset) {
|
||||
this.total = totalCount;
|
||||
this.setData({
|
||||
emptyInfo: {
|
||||
tip: '抱歉,未找到相关商品',
|
||||
},
|
||||
hasLoaded: true,
|
||||
loadMoreStatus: 0,
|
||||
loading: false,
|
||||
goodsList: [],
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
const _goodsList = reset ? spuList : goodsList.concat(spuList);
|
||||
_goodsList.forEach((v) => {
|
||||
v.tags = v.spuTagList.map((u) => u.title);
|
||||
v.hideKey = { desc: true };
|
||||
});
|
||||
const _loadMoreStatus = _goodsList.length === totalCount ? 2 : 0;
|
||||
this.pageNum = params.pageNum || 1;
|
||||
this.total = totalCount;
|
||||
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() {
|
||||
this.setData(
|
||||
{
|
||||
goodsList: [],
|
||||
loadMoreStatus: 0,
|
||||
},
|
||||
() => {
|
||||
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 { 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();
|
||||
},
|
||||
);
|
||||
},
|
||||
});
|
||||
15
mini-program/pages/goods/result/index.json
Normal file
15
mini-program/pages/goods/result/index.json
Normal file
@@ -0,0 +1,15 @@
|
||||
{
|
||||
"navigationBarTitleText": "搜索",
|
||||
"usingComponents": {
|
||||
"t-search": "tdesign-miniprogram/search/search",
|
||||
"t-input": "tdesign-miniprogram/input/input",
|
||||
"t-empty": "tdesign-miniprogram/empty/empty",
|
||||
"t-toast": "tdesign-miniprogram/toast/toast",
|
||||
"goods-list": "/components/goods-list/index",
|
||||
"filter": "/components/filter/index",
|
||||
"filter-popup": "/components/filter-popup/index",
|
||||
"load-more": "/components/load-more/index",
|
||||
"t-icon": "tdesign-miniprogram/icon/icon"
|
||||
},
|
||||
"onReachBottomDistance": 50
|
||||
}
|
||||
66
mini-program/pages/goods/result/index.wxml
Normal file
66
mini-program/pages/goods/result/index.wxml
Normal file
@@ -0,0 +1,66 @@
|
||||
<view class="result-container">
|
||||
<t-search
|
||||
t-class="t-search"
|
||||
t-class-input-container="t-class__input-container"
|
||||
t-class-left="t-search__left-icon"
|
||||
t-class-input="t-search__input"
|
||||
value="{{keywords}}"
|
||||
leftIcon=""
|
||||
placeholder="iPhone12pro"
|
||||
bind:submit="handleSubmit"
|
||||
>
|
||||
<t-icon slot="left-icon" prefix="wr" name="search" size="40rpx" color="#bbb" />
|
||||
</t-search>
|
||||
<filter
|
||||
wr-class="filter-container"
|
||||
bind:change="handleFilterChange"
|
||||
layout="{{layout}}"
|
||||
sorts="{{sorts}}"
|
||||
overall="{{overall}}"
|
||||
bind:showFilterPopup="showFilterPopup"
|
||||
>
|
||||
<filter-popup
|
||||
show="{{show}}"
|
||||
slot="filterPopup"
|
||||
bind:showFilterPopupClose="showFilterPopupClose"
|
||||
bind:reset="reset"
|
||||
bind:confirm="confirm"
|
||||
>
|
||||
<view class="price-container" slot="filterSlot">
|
||||
<view class="price-between">价格区间</view>
|
||||
<view class="price-ipts-wrap">
|
||||
<t-input
|
||||
type="number"
|
||||
t-class="price-ipt"
|
||||
t-class-input="t-class-input"
|
||||
placeholder="最低价"
|
||||
value="{{minVal}}"
|
||||
bindchange="onMinValAction"
|
||||
/>
|
||||
<view class="price-divided">-</view>
|
||||
<t-input
|
||||
type="number"
|
||||
t-class="price-ipt"
|
||||
t-class-input="t-class-input"
|
||||
placeholder="最高价"
|
||||
value="{{maxVal}}"
|
||||
bindchange="onMaxValAction"
|
||||
/>
|
||||
</view>
|
||||
</view>
|
||||
</filter-popup>
|
||||
</filter>
|
||||
<view class="empty-wrap" wx:if="{{goodsList.length === 0 && hasLoaded}}">
|
||||
<t-empty t-class="empty-tips" size="240rpx" description="暂无相关商品" />
|
||||
</view>
|
||||
<view class="category-goods-list" wx:if="{{goodsList.length}}">
|
||||
<goods-list
|
||||
wr-class="wr-goods-list"
|
||||
goodsList="{{goodsList}}"
|
||||
bind:click="gotoGoodsDetail"
|
||||
bind:addcart="handleAddCart"
|
||||
/>
|
||||
</view>
|
||||
<load-more wx:if="{{goodsList.length > 0}}" status="{{loadMoreStatus}}" no-more-text="没有更多了" />
|
||||
</view>
|
||||
<t-toast id="t-toast" />
|
||||
114
mini-program/pages/goods/result/index.wxss
Normal file
114
mini-program/pages/goods/result/index.wxss
Normal file
@@ -0,0 +1,114 @@
|
||||
page {
|
||||
background-color: #fff;
|
||||
}
|
||||
|
||||
page view {
|
||||
box-sizing: border-box;
|
||||
}
|
||||
|
||||
.result-container {
|
||||
display: block;
|
||||
}
|
||||
|
||||
.result-container .t-search {
|
||||
padding: 0 30rpx;
|
||||
background-color: #fff;
|
||||
}
|
||||
|
||||
.result-container .t-class__input-container {
|
||||
height: 64rpx !important;
|
||||
border-radius: 32rpx !important;
|
||||
}
|
||||
|
||||
.result-container .t-search__left-icon {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
}
|
||||
|
||||
.result-container .t-search__input {
|
||||
font-size: 28rpx !important;
|
||||
color: #333 !important;
|
||||
}
|
||||
|
||||
.result-container .category-goods-list {
|
||||
background-color: #f2f2f2;
|
||||
overflow-y: scroll;
|
||||
padding: 20rpx 24rpx;
|
||||
-webkit-overflow-scrolling: touch;
|
||||
}
|
||||
|
||||
.result-container .wr-goods-list {
|
||||
background: #f2f2f2 !important;
|
||||
}
|
||||
|
||||
.result-container .t-image__mask {
|
||||
display: flex !important;
|
||||
}
|
||||
|
||||
.result-container .empty-wrap {
|
||||
margin-top: 184rpx;
|
||||
margin-bottom: 120rpx;
|
||||
height: 300rpx;
|
||||
}
|
||||
|
||||
.result-container .empty-wrap .empty-tips .empty-content .content-text {
|
||||
margin-top: 40rpx;
|
||||
}
|
||||
|
||||
.result-container .price-container {
|
||||
padding: 32rpx;
|
||||
height: 100vh;
|
||||
max-width: 632rpx;
|
||||
background-color: #fff;
|
||||
border-radius: 30rpx 0 0 30rpx;
|
||||
}
|
||||
|
||||
.result-container .price-between {
|
||||
font-size: 26rpx;
|
||||
font-weight: 500;
|
||||
color: rgba(51, 51, 51, 1);
|
||||
}
|
||||
|
||||
.result-container .price-ipts-wrap {
|
||||
width: 100%;
|
||||
display: flex;
|
||||
flex-direction: row;
|
||||
justify-content: space-around;
|
||||
margin-top: 24rpx;
|
||||
}
|
||||
|
||||
.result-container .price-ipts-wrap .price-divided {
|
||||
position: relative;
|
||||
width: 22rpx;
|
||||
margin: 0 20rpx;
|
||||
color: #222427;
|
||||
}
|
||||
|
||||
.result-container .price-ipts-wrap .price-ipt {
|
||||
box-sizing: border-box;
|
||||
width: 246rpx;
|
||||
font-size: 24rpx;
|
||||
height: 56rpx;
|
||||
padding: 0 24rpx;
|
||||
text-align: center;
|
||||
border-radius: 8rpx;
|
||||
color: #333;
|
||||
background: rgba(245, 245, 245, 1);
|
||||
}
|
||||
|
||||
.t-class-input {
|
||||
font-size: 24rpx !important;
|
||||
}
|
||||
|
||||
.t-search__clear {
|
||||
font-size: 40rpx !important;
|
||||
}
|
||||
|
||||
.result-container .price-ipts-wrap .price-ipt::after {
|
||||
border: none !important;
|
||||
}
|
||||
|
||||
.result-container .t-input__control {
|
||||
font-size: 24rpx !important;
|
||||
text-align: center;
|
||||
}
|
||||
Reference in New Issue
Block a user