mall-uniapp/pages/goods/list.vue

362 lines
8.6 KiB
Vue
Raw Normal View History

2023-12-13 03:35:02 +00:00
<template>
2023-12-13 09:42:12 +00:00
<s-layout navbar="normal" :leftWidth="0" :rightWidth="0" tools="search" :defaultSearch="state.keyword"
@search="onSearch">
<!-- 筛选 -->
<su-sticky bgColor="#fff">
<view class="ss-flex">
<view class="ss-flex-1">
<su-tabs :list="state.tabList" :scrollable="false" @change="onTabsChange"
2023-12-15 14:15:22 +00:00
:current="state.currentTab" />
2023-12-13 09:42:12 +00:00
</view>
<view class="list-icon" @tap="state.iconStatus = !state.iconStatus">
2023-12-15 14:15:22 +00:00
<text v-if="state.iconStatus" class="sicon-goods-list" />
<text v-else class="sicon-goods-card" />
2023-12-13 09:42:12 +00:00
</view>
</view>
</su-sticky>
2023-12-13 03:35:02 +00:00
2023-12-13 09:42:12 +00:00
<!-- 弹窗 -->
<su-popup :show="state.showFilter" type="top" round="10" :space="sys_navBar + 38" backgroundColor="#F6F6F6"
:zIndex="10" @close="state.showFilter = false">
<view class="filter-list-box">
<view class="filter-item" v-for="(item, index) in state.tabList[state.currentTab].list"
2023-12-15 15:46:24 +00:00
:key="item.value" :class="[{ 'filter-item-active': index === state.curFilter }]"
2023-12-13 09:42:12 +00:00
@tap="onFilterItem(index)">
{{ item.label }}
</view>
</view>
</su-popup>
2023-12-15 14:15:22 +00:00
<!-- 情况一单列布局 -->
2023-12-13 09:42:12 +00:00
<view v-if="state.iconStatus && state.pagination.total > 0" class="goods-list ss-m-t-20">
2023-12-15 14:15:22 +00:00
<view class="ss-p-l-20 ss-p-r-20 ss-m-b-20" v-for="item in state.pagination.list" :key="item.id">
<s-goods-column
class=""
size="lg"
:data="item"
:topRadius="10"
:bottomRadius="10"
@click="sheep.$router.go('/pages/goods/index', { id: item.id })"
/>
2023-12-13 09:42:12 +00:00
</view>
</view>
2023-12-15 14:15:22 +00:00
<!-- 情况二双列布局 -->
<view v-if="!state.iconStatus && state.pagination.total > 0"
2023-12-13 09:42:12 +00:00
class="ss-flex ss-flex-wrap ss-p-x-20 ss-m-t-20 ss-col-top">
<view class="goods-list-box">
<view class="left-list" v-for="item in state.leftGoodsList" :key="item.id">
2023-12-15 14:15:22 +00:00
<s-goods-column
class="goods-md-box"
size="md"
:data="item"
:topRadius="10"
:bottomRadius="10"
2023-12-13 09:42:12 +00:00
@click="sheep.$router.go('/pages/goods/index', { id: item.id })"
2023-12-15 15:46:24 +00:00
@getHeight="mountMasonry($event, 'left')"
>
2023-12-13 09:42:12 +00:00
<template v-slot:cart>
2023-12-15 15:46:24 +00:00
<button class="ss-reset-button cart-btn" />
2023-12-13 09:42:12 +00:00
</template>
</s-goods-column>
</view>
</view>
<view class="goods-list-box">
<view class="right-list" v-for="item in state.rightGoodsList" :key="item.id">
2023-12-15 14:15:22 +00:00
<s-goods-column
class="goods-md-box"
size="md"
:topRadius="10"
:bottomRadius="10"
:data="item"
2023-12-13 09:42:12 +00:00
@click="sheep.$router.go('/pages/goods/index', { id: item.id })"
2023-12-15 15:46:24 +00:00
@getHeight="mountMasonry($event, 'right')"
>
2023-12-13 09:42:12 +00:00
<template v-slot:cart>
2023-12-15 14:15:22 +00:00
<button class="ss-reset-button cart-btn" />
2023-12-13 09:42:12 +00:00
</template>
</s-goods-column>
</view>
</view>
</view>
<uni-load-more v-if="state.pagination.total > 0" :status="state.loadStatus" :content-text="{
2023-12-13 03:35:02 +00:00
contentdown: '上拉加载更多',
2023-12-15 14:15:22 +00:00
}" @tap="loadMore" />
<s-empty v-if="state.pagination.total === 0" icon="/static/soldout-empty.png" text="暂无商品" />
2023-12-13 09:42:12 +00:00
</s-layout>
2023-12-13 03:35:02 +00:00
</template>
<script setup>
2023-12-15 14:15:22 +00:00
import { reactive } from 'vue';
import { onLoad, onReachBottom } from '@dcloudio/uni-app';
2023-12-13 09:42:12 +00:00
import sheep from '@/sheep';
import _ from 'lodash';
2023-12-15 14:15:22 +00:00
import { resetPagination } from '@/sheep/util';
import SpuApi from '@/sheep/api/product/spu';
2023-12-13 03:35:02 +00:00
2023-12-13 09:42:12 +00:00
const sys_navBar = sheep.$platform.navbar;
const emits = defineEmits(['close', 'change']);
2023-12-13 03:35:02 +00:00
2023-12-13 09:42:12 +00:00
const state = reactive({
pagination: {
2023-12-15 14:15:22 +00:00
list: [],
total: 0,
pageNo: 1,
pageSize: 6,
2023-12-13 09:42:12 +00:00
},
2023-12-15 15:46:24 +00:00
currentSort: undefined,
currentOrder: undefined,
currentTab: 0, // 当前选中的 tab
curFilter: 0, // 当前选中的 list 筛选项
2023-12-13 09:42:12 +00:00
showFilter: false,
2023-12-15 14:15:22 +00:00
iconStatus: false, // true - 单列布局false - 双列布局
2023-12-15 15:46:24 +00:00
keyword: '',
categoryId: 0,
2023-12-13 09:42:12 +00:00
tabList: [{
name: '综合推荐',
list: [{
2023-12-15 15:46:24 +00:00
label: '综合推荐'
2023-12-13 09:42:12 +00:00
},
{
label: '价格升序',
sort: 'price',
order: true,
},
{
label: '价格降序',
sort: 'price',
order: false,
},
],
},
{
name: '销量',
sort: 'salesCount',
2023-12-15 15:46:24 +00:00
order: false
2023-12-13 09:42:12 +00:00
},
{
name: '新品优先',
2023-12-15 15:46:24 +00:00
value: 'createTime',
order: false
2023-12-13 09:42:12 +00:00
},
],
loadStatus: '',
2023-12-15 14:15:22 +00:00
leftGoodsList: [], // 双列布局 - 左侧商品
rightGoodsList: [], // 双列布局 - 右侧商品
2023-12-13 09:42:12 +00:00
});
2023-12-13 03:35:02 +00:00
2023-12-13 09:42:12 +00:00
// 加载瀑布流
let count = 0;
let leftHeight = 0;
let rightHeight = 0;
2023-12-13 03:35:02 +00:00
2023-12-15 15:46:24 +00:00
// 处理双列布局 leftGoodsList + rightGoodsList
2023-12-13 09:42:12 +00:00
function mountMasonry(height = 0, where = 'left') {
2023-12-15 14:15:22 +00:00
if (!state.pagination.list[count]) {
return;
}
2023-12-13 03:35:02 +00:00
2023-12-13 09:42:12 +00:00
if (where === 'left') {
leftHeight += height;
} else {
rightHeight += height;
}
if (leftHeight <= rightHeight) {
2023-12-15 14:15:22 +00:00
state.leftGoodsList.push(state.pagination.list[count]);
2023-12-13 09:42:12 +00:00
} else {
2023-12-15 14:15:22 +00:00
state.rightGoodsList.push(state.pagination.list[count]);
2023-12-13 09:42:12 +00:00
}
count++;
}
2023-12-13 03:35:02 +00:00
2023-12-15 14:15:22 +00:00
// 清空列表
2023-12-13 09:42:12 +00:00
function emptyList() {
2023-12-15 14:15:22 +00:00
resetPagination(state.pagination);
state.leftGoodsList = [];
2023-12-13 09:42:12 +00:00
state.rightGoodsList = [];
count = 0;
leftHeight = 0;
rightHeight = 0;
}
2023-12-15 14:15:22 +00:00
// 搜索
2023-12-13 09:42:12 +00:00
function onSearch(e) {
state.keyword = e;
emptyList();
2023-12-15 14:15:22 +00:00
getList(state.currentSort, state.currentOrder);
2023-12-13 09:42:12 +00:00
}
2023-12-13 03:35:02 +00:00
2023-12-13 09:42:12 +00:00
// 点击
function onTabsChange(e) {
2023-12-15 15:46:24 +00:00
// 如果点击的是【综合推荐】,则直接展开或者收起筛选项
2023-12-13 09:42:12 +00:00
if (state.tabList[e.index].list) {
state.currentTab = e.index;
state.showFilter = !state.showFilter;
return;
}
2023-12-15 15:46:24 +00:00
state.showFilter = false;
// 如果点击的是【销量】或者【新品优先】,则直接切换 tab
2023-12-13 09:42:12 +00:00
if (e.index === state.currentTab) {
return;
}
2023-12-15 15:46:24 +00:00
state.currentTab = e.index;
state.currentSort = e.sort;
state.currentOrder = e.order;
2023-12-13 09:42:12 +00:00
emptyList();
2023-12-15 14:15:22 +00:00
getList(e.sort, e.order);
2023-12-13 09:42:12 +00:00
}
2023-12-13 03:35:02 +00:00
2023-12-15 15:46:24 +00:00
// 点击 tab 的 list 筛选项
2023-12-13 09:42:12 +00:00
const onFilterItem = (val) => {
2023-12-15 15:46:24 +00:00
// 如果点击的是当前的筛选项,则直接收起筛选项,不要加载数据
// 这里选择 tabList[0] 的原因,是目前只有它有 list
if (state.currentSort === state.tabList[0].list[val].sort
&& state.currentOrder === state.tabList[0].list[val].order) {
2023-12-13 09:42:12 +00:00
state.showFilter = false;
return;
}
2023-12-15 15:46:24 +00:00
state.showFilter = false;
// 设置筛选条件
2023-12-13 09:42:12 +00:00
state.curFilter = val;
state.tabList[0].name = state.tabList[0].list[val].label;
state.currentSort = state.tabList[0].list[val].sort;
state.currentOrder = state.tabList[0].list[val].order;
2023-12-15 15:46:24 +00:00
// 清空 + 加载数据
emptyList();
getList();
}
2023-12-13 03:35:02 +00:00
2023-12-15 15:46:24 +00:00
async function getList() {
2023-12-13 09:42:12 +00:00
state.loadStatus = 'loading';
const { code, data } = await SpuApi.getSpuPage({
2023-12-15 14:15:22 +00:00
pageNo: state.pagination.pageNo,
pageSize: state.pagination.pageSize,
2023-12-15 15:46:24 +00:00
sortField: state.currentSort,
sortAsc: state.currentOrder,
2023-12-15 14:15:22 +00:00
categoryId: state.categoryId,
keyword: state.keyword,
2023-12-13 09:42:12 +00:00
});
2023-12-15 14:15:22 +00:00
if (code !== 0) {
return;
}
state.pagination.list = _.concat(state.pagination.list, data.list)
state.pagination.total = data.total;
state.loadStatus = state.pagination.list.length < state.pagination.total ? 'more' : 'noMore';
mountMasonry();
2023-12-13 09:42:12 +00:00
}
2023-12-15 14:15:22 +00:00
2023-12-13 09:42:12 +00:00
// 加载更多
2023-12-15 14:15:22 +00:00
function loadMore() {
if (state.loadStatus === 'noMore') {
return;
}
state.pagination.pageNo++;
getList(state.currentSort, state.currentOrder);
2023-12-13 09:42:12 +00:00
}
2023-12-15 14:15:22 +00:00
2023-12-13 09:42:12 +00:00
onLoad((options) => {
state.categoryId = options.categoryId;
state.keyword = options.keyword;
2023-12-15 14:15:22 +00:00
getList(state.currentSort, state.currentOrder);
2023-12-13 09:42:12 +00:00
});
2023-12-15 14:15:22 +00:00
2023-12-13 09:42:12 +00:00
// 上拉加载更多
onReachBottom(() => {
2023-12-15 14:15:22 +00:00
loadMore();
2023-12-13 09:42:12 +00:00
});
2023-12-13 03:35:02 +00:00
</script>
<style lang="scss" scoped>
2023-12-13 09:42:12 +00:00
.goods-list-box {
width: 50%;
box-sizing: border-box;
.left-list {
margin-right: 10rpx;
margin-bottom: 20rpx;
}
.right-list {
margin-left: 10rpx;
margin-bottom: 20rpx;
}
}
.goods-box {
&:nth-last-of-type(1) {
margin-bottom: 0 !important;
}
&:nth-child(2n) {
margin-right: 0;
}
}
.list-icon {
width: 80rpx;
.sicon-goods-card {
font-size: 40rpx;
}
.sicon-goods-list {
font-size: 40rpx;
}
}
.goods-card {
margin-left: 20rpx;
}
.list-filter-tabs {
background-color: #fff;
}
.filter-list-box {
padding: 28rpx 52rpx;
.filter-item {
font-size: 28rpx;
font-weight: 500;
color: #333333;
line-height: normal;
margin-bottom: 24rpx;
&:nth-last-child(1) {
margin-bottom: 0;
}
}
.filter-item-active {
color: var(--ui-BG-Main);
}
}
.tab-item {
height: 50px;
position: relative;
z-index: 11;
2023-12-13 03:35:02 +00:00
2023-12-13 09:42:12 +00:00
.tab-title {
font-size: 30rpx;
}
2023-12-13 03:35:02 +00:00
2023-12-13 09:42:12 +00:00
.cur-tab-title {
font-weight: $font-weight-bold;
}
2023-12-13 03:35:02 +00:00
2023-12-13 09:42:12 +00:00
.tab-line {
width: 60rpx;
height: 6rpx;
border-radius: 6rpx;
position: absolute;
left: 50%;
transform: translateX(-50%);
bottom: 10rpx;
background-color: var(--ui-BG-Main);
z-index: 12;
}
}
</style>