123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234 |
- <!-- 我的商品收藏 -->
- <template>
- <s-layout :title="$t('common.favorite')">
- <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">
-
- <text class="goods-number ui-TC-Main ss-flex">
- {{ $t('common.total_goods',{number:state.pagination.total}) }}
- </text>
- </view>
- <view class="header-right">
- <button
- v-if="state.editMode && state.pagination.total"
- class="ss-reset-button"
- @tap="state.editMode = false"
- >
- {{ $t('common.cancel') }}
- </button>
- <button
- v-if="!state.editMode && state.pagination.total"
- class="ss-reset-button ui-TC-Main"
- @tap="state.editMode = true"
- >
- {{ $t('common.edit') }}
- </button>
- </view>
- </view>
- <!-- 内容 -->
- <view class="cart-content">
- <view
- class="goods-box ss-r-10 "
- v-for="item in state.pagination.list"
- :key="item.id"
- >
- <view class="ss-flex ss-col-center">
- <label
- class="check-box ss-flex ss-col-center ss-p-l-10"
- v-if="state.editMode"
- @tap="onSelect(item.spuId)"
- >
- <radio
- :checked="state.selectedCollectList.includes(item.spuId)"
- color="var(--ui-BG-Main)"
- style="transform: scale(0.8)"
- @tap.stop="onSelect(item.spuId)"
- />
- </label>
- <s-goods-item
- :title="item.spuName"
- :img="item.picUrl"
- :price="item.price"
- priceColor="#FF3000"
- :titleWidth="400"
- @tap="
- sheep.$router.go('/pages/goods/index', {
- id: item.spuId,
- })
- "
- />
- </view>
- </view>
- </view>
- <!-- 底部 -->
- <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">
- <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> {{ $t('common.all') }} </view>
- </label>
- </view>
- <view class="footer-right">
- <button
- class="ss-reset-button ui-BG-Main-Gradient pay-btn ss-font-28 ui-Shadow-Main"
- @tap="onCancel">
- {{ $t('common.delete') }}
- </button>
- </view>
- </view>
- </su-fixed>
- </view>
- <uni-load-more
- v-if="state.pagination.total > 0"
- :status="state.loadStatus"
- :content-text="{
- contentdown: '上拉加载更多',
- }"
- @tap="loadMore"
- />
- <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';
- import FavoriteApi from '@/sheep/api/product/favorite';
- import { resetPagination } from '@/sheep/util';
- const sys_navBar = sheep.$platform.navbar;
- const state = reactive({
- pagination: {
- list: [],
- total: 0,
- pageNo: 1,
- pageSize: 6,
- },
- loadStatus: '',
- editMode: false,
- selectedCollectList: [], // 选中的 SPU 数组
- selectAll: false,
- });
- async function getData() {
- state.loadStatus = 'loading';
- const { code, data } = await FavoriteApi.getFavoritePage({
- pageNo: state.pagination.pageNo,
- pageSize: state.pagination.pageSize,
- });
- 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';
- }
- // 单选选中
- const onSelect = (spuId) => {
- if (!state.selectedCollectList.includes(spuId)) {
- state.selectedCollectList.push(spuId);
- } else {
- state.selectedCollectList.splice(state.selectedCollectList.indexOf(spuId), 1);
- }
- state.selectAll = state.selectedCollectList.length === state.pagination.list.length;
- };
- // 全选
- const onSelectAll = () => {
- state.selectAll = !state.selectAll;
- if (!state.selectAll) {
- state.selectedCollectList = [];
- } else {
- state.selectedCollectList = state.pagination.list.map((item) => item.spuId);
- }
- };
- async function onCancel() {
- if (!state.selectedCollectList) {
- return;
- }
- // 取消收藏
- for (const spuId of state.selectedCollectList) {
- await FavoriteApi.deleteFavorite(spuId);
- }
- // 清空选择 + 重新加载
- state.editMode = false;
- state.selectedCollectList = [];
- state.selectAll = false;
- resetPagination(state.pagination);
- await getData();
- }
- // 加载更多
- function loadMore() {
- if (state.loadStatus === 'noMore') {
- return
- }
- state.pagination.pageNo++;
- getData();
- }
- onReachBottom(() => {
- loadMore();
- });
- 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>
|