商品列表:简化部分代码

pull/28/MERGE
YunaiV 2023-12-15 22:15:22 +08:00
parent 9049182843
commit bab41f4553
1 changed files with 77 additions and 70 deletions

View File

@ -6,11 +6,11 @@
<view class="ss-flex"> <view class="ss-flex">
<view class="ss-flex-1"> <view class="ss-flex-1">
<su-tabs :list="state.tabList" :scrollable="false" @change="onTabsChange" <su-tabs :list="state.tabList" :scrollable="false" @change="onTabsChange"
:current="state.currentTab"></su-tabs> :current="state.currentTab" />
</view> </view>
<view class="list-icon" @tap="state.iconStatus = !state.iconStatus"> <view class="list-icon" @tap="state.iconStatus = !state.iconStatus">
<text v-if="state.iconStatus" class="sicon-goods-list"></text> <text v-if="state.iconStatus" class="sicon-goods-list" />
<text v-else class="sicon-goods-card"></text> <text v-else class="sicon-goods-card" />
</view> </view>
</view> </view>
</su-sticky> </su-sticky>
@ -26,17 +26,31 @@
</view> </view>
</view> </view>
</su-popup> </su-popup>
<!-- 情况一单列布局 -->
<view v-if="state.iconStatus && state.pagination.total > 0" class="goods-list ss-m-t-20"> <view v-if="state.iconStatus && state.pagination.total > 0" class="goods-list ss-m-t-20">
<view class="ss-p-l-20 ss-p-r-20 ss-m-b-20" v-for="item in state.pagination.data" :key="item.id"> <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" <s-goods-column
@click="sheep.$router.go('/pages/goods/index', { id: item.id })"></s-goods-column> class=""
size="lg"
:data="item"
:topRadius="10"
:bottomRadius="10"
@click="sheep.$router.go('/pages/goods/index', { id: item.id })"
/>
</view> </view>
</view> </view>
<!-- 情况二双列布局 -->
<view v-if="!state.iconStatus && state.pagination.total > 0" <view v-if="!state.iconStatus && state.pagination.total > 0"
class="ss-flex ss-flex-wrap ss-p-x-20 ss-m-t-20 ss-col-top"> class="ss-flex ss-flex-wrap ss-p-x-20 ss-m-t-20 ss-col-top">
<view class="goods-list-box"> <view class="goods-list-box">
<view class="left-list" v-for="item in state.leftGoodsList" :key="item.id"> <view class="left-list" v-for="item in state.leftGoodsList" :key="item.id">
<s-goods-column class="goods-md-box" size="md" :data="item" :topRadius="10" :bottomRadius="10" <s-goods-column
class="goods-md-box"
size="md"
:data="item"
:topRadius="10"
:bottomRadius="10"
@click="sheep.$router.go('/pages/goods/index', { id: item.id })" @click="sheep.$router.go('/pages/goods/index', { id: item.id })"
@getHeight="mountMasonry($event, 'left')"> @getHeight="mountMasonry($event, 'left')">
<template v-slot:cart> <template v-slot:cart>
@ -47,11 +61,16 @@
</view> </view>
<view class="goods-list-box"> <view class="goods-list-box">
<view class="right-list" v-for="item in state.rightGoodsList" :key="item.id"> <view class="right-list" v-for="item in state.rightGoodsList" :key="item.id">
<s-goods-column class="goods-md-box" size="md" :topRadius="10" :bottomRadius="10" :data="item" <s-goods-column
class="goods-md-box"
size="md"
:topRadius="10"
:bottomRadius="10"
:data="item"
@click="sheep.$router.go('/pages/goods/index', { id: item.id })" @click="sheep.$router.go('/pages/goods/index', { id: item.id })"
@getHeight="mountMasonry($event, 'right')"> @getHeight="mountMasonry($event, 'right')">
<template v-slot:cart> <template v-slot:cart>
<button class="ss-reset-button cart-btn"> </button> <button class="ss-reset-button cart-btn" />
</template> </template>
</s-goods-column> </s-goods-column>
</view> </view>
@ -59,46 +78,37 @@
</view> </view>
<uni-load-more v-if="state.pagination.total > 0" :status="state.loadStatus" :content-text="{ <uni-load-more v-if="state.pagination.total > 0" :status="state.loadStatus" :content-text="{
contentdown: '上拉加载更多', contentdown: '上拉加载更多',
}" @tap="loadmore" /> }" @tap="loadMore" />
<s-empty v-if="state.pagination.total === 0" icon="/static/soldout-empty.png" text="暂无商品"> <s-empty v-if="state.pagination.total === 0" icon="/static/soldout-empty.png" text="暂无商品" />
</s-empty>
</s-layout> </s-layout>
</template> </template>
<script setup> <script setup>
import { import { reactive } from 'vue';
reactive
} from 'vue';
import { import {
onLoad, onLoad,
onReachBottom onReachBottom
} from '@dcloudio/uni-app'; } from '@dcloudio/uni-app';
import sheep from '@/sheep'; import sheep from '@/sheep';
import _ from 'lodash'; import _ from 'lodash';
import { resetPagination } from '@/sheep/util';
const sys_navBar = sheep.$platform.navbar; const sys_navBar = sheep.$platform.navbar;
const emits = defineEmits(['close', 'change']); const emits = defineEmits(['close', 'change']);
const pagination = {
data: [],
current_page: 1,
total: 1,
last_page: 1,
};
const state = reactive({ const state = reactive({
pagination: { pagination: {
data: [], list: [],
current_page: 1, total: 0,
total: 1, pageNo: 1,
last_page: 1, pageSize: 6,
}, },
// currentSort: 'weigh', // currentSort: 'weigh',
// currentOrder: 'desc', // currentOrder: 'desc',
currentTab: 0, currentTab: 0,
filterParams: {},
curFilter: 0, curFilter: 0,
showFilter: false, showFilter: false,
iconStatus: false, iconStatus: false, // true - false -
categoryId: 0, categoryId: 0,
tabList: [{ tabList: [{
name: '综合推荐', name: '综合推荐',
@ -133,8 +143,8 @@
], ],
loadStatus: '', loadStatus: '',
keyword: '', keyword: '',
leftGoodsList: [], leftGoodsList: [], // -
rightGoodsList: [], rightGoodsList: [], // -
}); });
// //
@ -143,7 +153,9 @@
let rightHeight = 0; let rightHeight = 0;
function mountMasonry(height = 0, where = 'left') { function mountMasonry(height = 0, where = 'left') {
if (!state.pagination.data[count]) return; if (!state.pagination.list[count]) {
return;
}
if (where === 'left') { if (where === 'left') {
leftHeight += height; leftHeight += height;
@ -151,26 +163,28 @@
rightHeight += height; rightHeight += height;
} }
if (leftHeight <= rightHeight) { if (leftHeight <= rightHeight) {
state.leftGoodsList.push(state.pagination.data[count]); state.leftGoodsList.push(state.pagination.list[count]);
} else { } else {
state.rightGoodsList.push(state.pagination.data[count]); state.rightGoodsList.push(state.pagination.list[count]);
} }
count++; count++;
} }
//
function emptyList() { function emptyList() {
state.pagination = pagination resetPagination(state.pagination);
state.leftGoodsList = []; state.leftGoodsList = [];
state.rightGoodsList = []; state.rightGoodsList = [];
count = 0; count = 0;
leftHeight = 0; leftHeight = 0;
rightHeight = 0; rightHeight = 0;
} }
// //
function onSearch(e) { function onSearch(e) {
state.keyword = e; state.keyword = e;
emptyList(); emptyList();
getList(state.currentSort, state.currentOrder, state.categoryId, e); getList(state.currentSort, state.currentOrder);
} }
// //
@ -189,7 +203,7 @@
} }
emptyList(); emptyList();
console.log(e, '6666') console.log(e, '6666')
getList(e.sort, e.order, state.categoryId, state.keyword); getList(e.sort, e.order);
} }
// //
@ -207,54 +221,47 @@
state.currentSort = state.tabList[0].list[val].sort; state.currentSort = state.tabList[0].list[val].sort;
state.currentOrder = state.tabList[0].list[val].order; state.currentOrder = state.tabList[0].list[val].order;
emptyList(); emptyList();
getList(state.currentSort, state.currentOrder, state.categoryId, state.keyword); getList(state.currentSort, state.currentOrder);
state.showFilter = false; state.showFilter = false;
}; };
async function getList(Sort, Order, categoryId, keyword, page = 1, list_rows = 6) { async function getList(Sort, Order) {
state.loadStatus = 'loading'; state.loadStatus = 'loading';
const res = await sheep.$api.goods.list({ const { code, data } = await sheep.$api.goods.list({
pageNo: state.pagination.pageNo,
pageSize: state.pagination.pageSize,
sortField: Sort, sortField: Sort,
sortAsc: Order, sortAsc: Order,
category_id: !keyword ? categoryId : '', categoryId: state.categoryId,
pageSize: list_rows, keyword: state.keyword,
keyword: keyword,
pageNo: page,
}); });
if (res.code === 0) { if (code !== 0) {
let couponList = _.concat(state.pagination.data, res.data.list); return;
state.pagination = { }
...res.data, state.pagination.list = _.concat(state.pagination.list, data.list)
data: couponList, state.pagination.total = data.total;
}; state.loadStatus = state.pagination.list.length < state.pagination.total ? 'more' : 'noMore';
mountMasonry(); mountMasonry();
if (state.pagination.current_page < state.pagination.last_page) {
state.loadStatus = 'more';
} else {
state.loadStatus = 'noMore';
}
}
} }
// //
function loadmore() { function loadMore() {
if (state.loadStatus !== 'noMore') { if (state.loadStatus === 'noMore') {
getList( return;
state.currentSort,
state.currentOrder,
state.categoryId,
state.keyword,
state.pagination.current_page + 1,
);
} }
state.pagination.pageNo++;
getList(state.currentSort, state.currentOrder);
} }
onLoad((options) => { onLoad((options) => {
state.categoryId = options.categoryId; state.categoryId = options.categoryId;
state.keyword = options.keyword; state.keyword = options.keyword;
getList(state.currentSort, state.currentOrder, state.categoryId, state.keyword); getList(state.currentSort, state.currentOrder);
}); });
// //
onReachBottom(() => { onReachBottom(() => {
loadmore(); loadMore();
}); });
</script> </script>