mall-uniapp/pages/goods_search/index.vue

267 lines
6.8 KiB
Vue
Raw Normal View History

2020-08-13 08:12:57 +00:00
<template>
<view>
<view class='searchGood'>
<view class='search acea-row row-between-wrapper'>
<view class='input acea-row row-between-wrapper'>
<text class='iconfont icon-sousuo2'></text>
2023-06-23 14:52:45 +00:00
<input type='text' :value='searchValue' :focus="focus" placeholder='点击搜索商品'
placeholder-class='placeholder' @input="setValue" />
2020-08-13 08:12:57 +00:00
</view>
<view class='bnt' @tap='searchBut'>搜索</view>
</view>
2023-06-23 14:52:45 +00:00
<!-- 搜索词 -->
2020-08-13 08:12:57 +00:00
<view class='title'>热门搜索</view>
<view class='list acea-row'>
<block v-for="(item,index) in hotSearchList" :key="index">
<view class='item' @tap='setHotSearchValue(item.title)'>{{item.title}}</view>
</block>
</view>
<view class='line'></view>
2023-06-23 14:52:45 +00:00
<!-- 搜索结果 -->
<goodList :bastList="bastList" v-if="bastList.length > 0" />
2020-08-13 08:12:57 +00:00
<view class='loadingicon acea-row row-center-wrapper' v-if="bastList.length > 0">
2023-06-23 14:52:45 +00:00
<text class='loading iconfont icon-jiazai' :hidden='!loading' /> {{loadTitle}}
2020-08-13 08:12:57 +00:00
</view>
</view>
2023-06-23 14:52:45 +00:00
<!-- 商品推荐 -->
2020-08-13 08:12:57 +00:00
<view class='noCommodity'>
2023-06-23 14:52:45 +00:00
<view class='pictrue' v-if="bastList.length === 0 && isbastList">
2020-08-13 08:12:57 +00:00
<image src='../../static/images/noSearch.png'></image>
</view>
2023-06-23 14:52:45 +00:00
<recommend :hostProduct='hostProduct' v-if="bastList.length === 0"></recommend>
2020-08-13 08:12:57 +00:00
</view>
</view>
</template>
<script>
2023-06-23 14:52:45 +00:00
import { getSearchKeyword } from '@/api/store.js';
2020-08-13 08:12:57 +00:00
import goodList from '@/components/goodList';
import recommend from '@/components/recommend';
2023-06-23 14:52:45 +00:00
import * as ProductSpuApi from '@/api/product/spu.js';
import * as PromotionActivityApi from '@/api/promotion/activity.js';
import * as ProductUtil from '@/utils/product.js';
export default {
2020-08-13 08:12:57 +00:00
components: {
goodList,
recommend
},
data() {
return {
2023-06-23 14:52:45 +00:00
// ========== 商品搜索 ==========
hotSearchList: [], // 热门搜索词
searchValue: '',
2020-08-13 08:12:57 +00:00
focus: true,
2023-06-23 14:52:45 +00:00
bastList: [], // 商品搜索结果
2020-08-13 08:12:57 +00:00
limit: 8,
page: 1,
loading: false,
loadend: false,
loadTitle: '加载更多',
2023-06-23 14:52:45 +00:00
isbastList: false,
// ========== 商品推荐 ==========
hostProduct: [],
hotPage: 1,
isScroll: true,
};
2020-08-13 08:12:57 +00:00
},
onShow: function() {
this.getRoutineHotSearch();
this.getHostProduct();
},
onReachBottom: function() {
2023-06-23 14:52:45 +00:00
if(this.bastList.length > 0){
2020-08-13 08:12:57 +00:00
this.getProductList();
2023-06-23 14:52:45 +00:00
} else {
2020-08-13 08:12:57 +00:00
this.getHostProduct();
}
},
methods: {
2023-06-23 14:52:45 +00:00
/**
* TODO 芋艿待接入
*/
2020-08-13 08:12:57 +00:00
getRoutineHotSearch: function() {
getSearchKeyword().then(res => {
2023-06-23 14:52:45 +00:00
this.$set(this, 'hotSearchList', res.data);
2020-08-13 08:12:57 +00:00
});
},
2023-06-23 14:52:45 +00:00
/**
* 输入搜索热词
*
* @param searchValue 搜索热词
*/
setHotSearchValue: function(searchValue) {
this.setValue(searchValue);
this.page = 1;
this.loadend = false;
this.$set(this, 'bastList', []);
this.getProductList();
},
/**
* 输入搜索词
*
* @param searchValue 搜索词
*/
setValue: function(searchValue) {
this.$set(this, 'searchValue', searchValue.detail.value);
},
/**
* 点击搜索
*/
searchBut: function() {
this.focus = false;
if (this.searchValue.length > 0) {
this.page = 1;
this.loadend = false;
this.$set(this, 'bastList', []);
uni.showLoading({
title: '正在搜索中'
});
this.getProductList();
uni.hideLoading();
} else {
return this.$util.Tips({
title: '请输入要搜索的商品',
icon: 'none',
duration: 1000,
mask: true,
});
}
},
/**
* 执行搜索商品
*/
2020-08-13 08:12:57 +00:00
getProductList: function() {
2023-06-23 14:52:45 +00:00
if (this.loadend || this.loading) {
return;
}
this.loading = true;
this.loadTitle = '';
ProductSpuApi.getSpuPage({
keyword: this.searchValue,
pageNo: this.page,
pageSize: this.limit
2020-08-13 08:12:57 +00:00
}).then(res => {
2023-06-23 14:52:45 +00:00
const good_list = res.data.list;
const loadend = good_list.length < this.limit;
this.loading = false;
this.loadend = loadend;
this.loadTitle = loadend ? "😕人家是有底线的~~" : "加载更多";
this.page = this.page + 1;
this.isbastList = true;
// 设置营销活动
const spuIds = good_list.map(item => item.id);
2023-06-25 03:20:41 +00:00
if (spuIds.length > 0) {
PromotionActivityApi.getActivityListBySpuIds(spuIds).then(res => {
ProductUtil.setActivityList(good_list, res.data);
this.bastList = this.$util.SplitArray(good_list, this.bastList); // 放在此处,避免 Vue 监控不到数组里的元素变化
});
}
2023-06-23 14:52:45 +00:00
}).catch(err => {
this.loading = false
this.loadTitle = '加载更多'
2020-08-13 08:12:57 +00:00
});
},
2023-06-23 14:52:45 +00:00
/**
* 获得热门推荐
*/
2020-08-13 08:12:57 +00:00
getHostProduct: function() {
2023-06-23 14:52:45 +00:00
if (!this.isScroll) {
return
}
ProductSpuApi.getSpuPage({
recommendType: 'hot',
pageNo: this.hotPage,
pageSize: this.limit
}).then(res => {
const good_list = res.data.list;
this.isScroll = good_list.length >= this.limit
this.hotPage += 1;
// 设置营销活动
const spuIds = good_list.map(item => item.id);
2023-06-25 03:20:41 +00:00
if (spuIds.length > 0) {
PromotionActivityApi.getActivityListBySpuIds(spuIds).then(res => {
ProductUtil.setActivityList(good_list, res.data);
this.hostProduct = this.hostProduct.concat(good_list); // 放在此处,避免 Vue 监控不到数组里的元素变化
});
}
2020-08-13 08:12:57 +00:00
});
}
}
}
</script>
<style lang="scss">
page {
background-color: #fff !important;
}
.searchGood .search {
padding-left: 30rpx;
background-color: #fff !important;
2020-08-13 08:12:57 +00:00
}
.searchGood .search {
padding-top: 20rpx;
2020-08-13 08:12:57 +00:00
}
.searchGood .search .input {
width: 598rpx;
background-color: #f7f7f7;
border-radius: 33rpx;
padding: 0 35rpx;
box-sizing: border-box;
height: 66rpx;
}
.searchGood .search .input input {
width: 472rpx;
font-size: 26rpx;
2020-08-13 08:12:57 +00:00
}
.searchGood .search .input .placeholder {
color: #bbb;
}
.searchGood .search .input .iconfont {
color: #000;
font-size: 35rpx;
}
.searchGood .search .bnt {
width: 120rpx;
text-align: center;
height: 66rpx;
line-height: 66rpx;
font-size: 30rpx;
color: #282828;
}
.searchGood .title {
font-size: 28rpx;
color: #999;
margin: 50rpx 30rpx 25rpx 30rpx;
}
.searchGood .list {
padding-left: 10rpx;
}
.searchGood .list .item {
font-size: 26rpx;
color: #454545;
padding: 0 21rpx;
height: 60rpx;
border-radius: 30rpx;
2020-08-13 08:12:57 +00:00
line-height: 60rpx;
border: 1rpx solid #aaa;
margin: 0 0 20rpx 20rpx;
}
.searchGood .line {
border-bottom: 1rpx solid #eee;
margin: 20rpx 30rpx 0 30rpx;
}
</style>