mall-uniapp/pages/user/goods-collect.vue

232 lines
6.2 KiB
Vue
Raw Normal View History

2023-12-14 16:01:54 +00:00
<!-- 我的商品收藏 -->
2022-11-22 07:45:36 +00:00
<template>
<s-layout title="商品收藏">
<view class="cart-box ss-flex ss-flex-col ss-row-between">
<!-- 头部 -->
<view class="cart-header ss-flex ss-col-center ss-row-between ss-p-x-30">
<view class="header-left ss-flex ss-col-center ss-font-26">
2023-12-14 16:01:54 +00:00
<text class="goods-number ui-TC-Main ss-flex">{{ state.pagination.total }}</text> 件商品
2022-11-22 07:45:36 +00:00
</view>
<view class="header-right">
<button
v-if="state.editMode && state.pagination.total"
class="ss-reset-button"
@tap="state.editMode = false"
>
取消
</button>
<button
v-if="!state.editMode && state.pagination.total"
class="ss-reset-button ui-TC-Main"
@tap="state.editMode = true"
>
2023-12-14 16:01:54 +00:00
编辑
</button>
2022-11-22 07:45:36 +00:00
</view>
</view>
<!-- 内容 -->
<view class="cart-content">
<view
class="goods-box ss-r-10 ss-m-b-14"
2023-12-14 16:01:54 +00:00
v-for="item in state.pagination.list"
2022-11-22 07:45:36 +00:00
:key="item.id"
>
<view class="ss-flex ss-col-center">
2023-03-21 11:03:23 +00:00
<label
2022-11-22 07:45:36 +00:00
class="check-box ss-flex ss-col-center ss-p-l-10"
2023-03-21 11:03:23 +00:00
v-if="state.editMode"
@tap="onSelect(item.spuId)"
2022-11-22 07:45:36 +00:00
>
2023-03-21 11:03:23 +00:00
<radio
:checked="state.selectedCollectList.includes(item.spuId)"
2023-03-21 11:03:23 +00:00
color="var(--ui-BG-Main)"
style="transform: scale(0.8)"
@tap.stop="onSelect(item.spuId)"
2023-03-21 11:03:23 +00:00
/>
</label>
2022-11-22 07:45:36 +00:00
<s-goods-item
:title="item.spuName"
:img="item.picUrl"
:price="item.price"
2022-11-22 07:45:36 +00:00
priceColor="#FF3000"
:titleWidth="400"
@tap="
sheep.$router.go('/pages/goods/index', {
id: item.spuId,
2022-11-22 07:45:36 +00:00
})
"
2023-12-14 16:01:54 +00:00
/>
2022-11-22 07:45:36 +00:00
</view>
</view>
</view>
2023-12-14 16:01:54 +00:00
2022-11-22 07:45:36 +00:00
<!-- 底部 -->
<su-fixed bottom :val="0" placeholder v-show="state.editMode">
<view class="cart-footer ss-flex ss-col-center ss-row-between ss-p-x-30 border-bottom">
<view class="footer-left ss-flex ss-col-center">
2023-03-21 11:03:23 +00:00
<label class="check-box ss-flex ss-col-center ss-p-r-30" @tap="onSelectAll">
<radio
:checked="state.selectAll"
color="var(--ui-BG-Main)"
style="transform: scale(0.7)"
@tap.stop="onSelectAll"
/>
<view> 全选 </view>
</label>
2022-11-22 07:45:36 +00:00
</view>
<view class="footer-right">
<button
class="ss-reset-button ui-BG-Main-Gradient pay-btn ss-font-28 ui-Shadow-Main"
2023-12-14 16:01:54 +00:00
@tap="onCancel">
取消收藏
</button>
2022-11-22 07:45:36 +00:00
</view>
</view>
</su-fixed>
</view>
<uni-load-more
v-if="state.pagination.total > 0"
:status="state.loadStatus"
:content-text="{
contentdown: '上拉加载更多',
}"
2023-12-14 16:01:54 +00:00
@tap="loadMore"
2022-11-22 07:45:36 +00:00
/>
<s-empty v-if="state.pagination.total === 0" text="暂无收藏" icon="/static/collect-empty.png" />
</s-layout>
</template>
<script setup>
import sheep from '@/sheep';
import { reactive } from 'vue';
import { onLoad, onReachBottom } from '@dcloudio/uni-app';
import _ from 'lodash';
2023-12-14 16:01:54 +00:00
import FavoriteApi from '@/sheep/api/product/favorite';
import { resetPagination } from '@/sheep/util';
2022-11-22 07:45:36 +00:00
const sys_navBar = sheep.$platform.navbar;
2023-12-14 16:01:54 +00:00
2022-11-22 07:45:36 +00:00
const state = reactive({
pagination: {
2023-12-14 16:01:54 +00:00
list: [],
total: 0,
pageNo: 1,
pageSize: 6,
2022-11-22 07:45:36 +00:00
},
loadStatus: '',
2023-12-14 16:01:54 +00:00
2022-11-22 07:45:36 +00:00
editMode: false,
2023-12-14 16:01:54 +00:00
selectedCollectList: [], // 选中的 SPU 数组
2022-11-22 07:45:36 +00:00
selectAll: false,
});
2023-12-14 16:01:54 +00:00
async function getData() {
2022-11-22 07:45:36 +00:00
state.loadStatus = 'loading';
2023-12-14 16:01:54 +00:00
const { code, data } = await FavoriteApi.getFavoritePage({
pageNo: state.pagination.pageNo,
pageSize: state.pagination.pageSize,
2022-11-22 07:45:36 +00:00
});
2023-12-14 16:01:54 +00:00
if (code !== 0) {
return;
2022-11-22 07:45:36 +00:00
}
2023-12-14 16:01:54 +00:00
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';
2022-11-22 07:45:36 +00:00
}
// 单选选中
2023-12-14 16:01:54 +00:00
const onSelect = (spuId) => {
if (!state.selectedCollectList.includes(spuId)) {
state.selectedCollectList.push(spuId);
2022-11-22 07:45:36 +00:00
} else {
2023-12-14 16:01:54 +00:00
state.selectedCollectList.splice(state.selectedCollectList.indexOf(spuId), 1);
2022-11-22 07:45:36 +00:00
}
2023-12-14 16:01:54 +00:00
state.selectAll = state.selectedCollectList.length === state.pagination.list.length;
2022-11-22 07:45:36 +00:00
};
2023-12-14 16:01:54 +00:00
2022-11-22 07:45:36 +00:00
// 全选
const onSelectAll = () => {
state.selectAll = !state.selectAll;
if (!state.selectAll) {
state.selectedCollectList = [];
} else {
2023-12-14 16:01:54 +00:00
state.selectedCollectList = state.pagination.list.map((item) => item.spuId);
2022-11-22 07:45:36 +00:00
}
};
2023-12-14 16:01:54 +00:00
2022-11-22 07:45:36 +00:00
async function onCancel() {
2023-12-14 16:01:54 +00:00
if (!state.selectedCollectList) {
return;
2022-11-22 07:45:36 +00:00
}
2023-12-14 16:01:54 +00:00
// 取消收藏
for (const spuId of state.selectedCollectList) {
await FavoriteApi.deleteFavorite(spuId);
}
// 清空选择 + 重新加载
state.editMode = false;
state.selectedCollectList = [];
state.selectAll = false;
resetPagination(state.pagination);
await getData();
2022-11-22 07:45:36 +00:00
}
2023-12-14 16:01:54 +00:00
2022-11-22 07:45:36 +00:00
// 加载更多
2023-12-14 16:01:54 +00:00
function loadMore() {
if (state.loadStatus === 'noMore') {
return
2022-11-22 07:45:36 +00:00
}
2023-12-14 16:01:54 +00:00
state.pagination.pageNo++;
getData();
2022-11-22 07:45:36 +00:00
}
2023-12-14 16:01:54 +00:00
2022-11-22 07:45:36 +00:00
onReachBottom(() => {
2023-12-14 16:01:54 +00:00
loadMore();
2022-11-22 07:45:36 +00:00
});
2023-12-14 16:01:54 +00:00
2022-11-22 07:45:36 +00:00
onLoad(() => {
getData();
});
</script>
<style lang="scss" scoped>
.cart-box {
.cart-header {
height: 70rpx;
background-color: #f6f6f6;
width: 100%;
position: fixed;
left: 0;
top: v-bind('sys_navBar') rpx;
z-index: 1000;
box-sizing: border-box;
}
.cart-footer {
height: 100rpx;
background-color: #fff;
.pay-btn {
height: 80rpx;
line-height: 80rpx;
border-radius: 40rpx;
padding: 0 40rpx;
min-width: 200rpx;
}
}
.cart-content {
width: 100%;
margin-top: 70rpx;
padding: 0 20rpx;
box-sizing: border-box;
.goods-box {
background-color: #fff;
&:last-child {
margin-bottom: 40rpx;
}
}
}
}
</style>