mall-uniapp/pages/coupon/list.vue

223 lines
5.6 KiB
Vue
Raw Normal View History

2023-12-13 03:35:02 +00:00
<!-- 优惠券中心 -->
<template>
2023-12-16 02:40:04 +00:00
<s-layout title="优惠券" :bgStyle="{ color: '#f2f2f2' }">
<su-sticky bgColor="#fff">
<su-tabs
:list="tabMaps"
:scrollable="false"
@change="onTabsChange"
:current="state.currentTab"
/>
</su-sticky>
<s-empty
v-if="state.pagination.total === 0"
icon="/static/coupon-empty.png"
text="暂无优惠券"
/>
<!-- 情况一领劵中心 -->
<template v-if="state.currentTab === 0">
2023-12-16 02:40:04 +00:00
<view v-for="item in state.pagination.list" :key="item.id">
<s-coupon-list
:data="item"
2023-12-16 10:32:07 +00:00
@tap="sheep.$router.go('/pages/coupon/detail', { id: item.id })"
2023-12-16 02:40:04 +00:00
>
<template #default>
<button
class="ss-reset-button card-btn ss-flex ss-row-center ss-col-center"
:class="!item.canTake ? 'border-btn' : ''"
@click.stop="getBuy(item.id)"
:disabled="!item.canTake"
>
{{ item.canTake ? '立即领取' : '已领取' }}
</button>
</template>
</s-coupon-list>
</view>
</template>
<!-- 情况二我的优惠劵 -->
<template v-else>
<view v-for="item in state.pagination.list" :key="item.id">
<s-coupon-list
:data="item"
type="user"
@tap="sheep.$router.go('/pages/coupon/detail', { couponId: item.id })"
2023-12-16 02:40:04 +00:00
>
<template #default>
<button
class="ss-reset-button card-btn ss-flex ss-row-center ss-col-center"
:class="item.status !== 1 ? 'disabled-btn' : ''"
2023-12-16 02:40:04 +00:00
:disabled="item.status !== 1"
2023-12-16 10:32:07 +00:00
@click.stop="sheep.$router.go('/pages/coupon/detail', { couponId: item.id })"
2023-12-16 02:40:04 +00:00
>
{{ item.status === 1 ? '立即使用' : item.status === 2 ? '已使用' : '已过期' }}
</button>
</template>
</s-coupon-list>
</view>
</template>
2023-12-13 03:35:02 +00:00
<uni-load-more
v-if="state.pagination.total > 0"
:status="state.loadStatus"
:content-text="{
2023-12-13 03:35:02 +00:00
contentdown: '上拉加载更多',
}"
@tap="loadMore"
/>
2023-12-16 02:40:04 +00:00
</s-layout>
2023-12-13 03:35:02 +00:00
</template>
<script setup>
2023-12-16 02:40:04 +00:00
import sheep from '@/sheep';
import { onLoad, onReachBottom } from '@dcloudio/uni-app';
import { reactive } from 'vue';
import _ from 'lodash-es';
2023-12-16 02:40:04 +00:00
import { resetPagination } from '@/sheep/util';
import CouponApi from '@/sheep/api/promotion/coupon';
// 数据
const state = reactive({
currentTab: 0, // 当前 tab
type: '1',
pagination: {
list: [],
total: 0,
pageNo: 1,
pageSize: 5,
2023-12-16 02:40:04 +00:00
},
loadStatus: '',
});
const tabMaps = [
{
name: '领券中心',
value: 'all',
},
{
name: '已领取',
value: '1',
},
{
name: '已使用',
value: '2',
},
{
name: '已失效',
value: '3',
},
];
2023-12-13 03:35:02 +00:00
2023-12-16 02:40:04 +00:00
function onTabsChange(e) {
state.currentTab = e.index;
state.type = e.value;
resetPagination(state.pagination);
if (state.currentTab === 0) {
getData();
2023-12-16 02:40:04 +00:00
} else {
getCoupon();
}
}
2023-12-13 03:35:02 +00:00
2023-12-16 02:40:04 +00:00
// 获得优惠劵模版列表
async function getData() {
state.loadStatus = 'loading';
const { data, code } = await CouponApi.getCouponTemplatePage({
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';
}
2023-12-13 03:35:02 +00:00
2023-12-16 02:40:04 +00:00
// 获得我的优惠劵
async function getCoupon() {
state.loadStatus = 'loading';
const { data, code } = await CouponApi.getCouponPage({
pageNo: state.pagination.pageNo,
pageSize: state.pagination.pageSize,
status: state.type,
2023-12-16 02:40:04 +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';
}
2023-12-13 03:35:02 +00:00
2023-12-16 02:40:04 +00:00
// 领取优惠劵
async function getBuy(id) {
const { code } = await CouponApi.takeCoupon(id);
if (code !== 0) {
return;
}
uni.showToast({
title: '领取成功',
});
setTimeout(() => {
resetPagination(state.pagination);
getData();
}, 1000);
}
2023-12-13 03:35:02 +00:00
2023-12-16 02:40:04 +00:00
// 加载更多
function loadMore() {
if (state.loadStatus === 'noMore') {
return;
}
state.pagination.pageNo++;
if (state.currentTab === 0) {
getData();
} else {
getCoupon();
}
}
2023-12-13 03:35:02 +00:00
2023-12-16 02:40:04 +00:00
onLoad((Option) => {
// 领劵中心
if (Option.type === 'all' || !Option.type) {
getData();
// 我的优惠劵
2023-12-16 02:40:04 +00:00
} else {
Option.type === 'geted'
? (state.currentTab = 1)
: Option.type === 'used'
? (state.currentTab = 2)
: (state.currentTab = 3);
state.type = state.currentTab;
2023-12-16 02:40:04 +00:00
getCoupon();
}
});
2023-12-13 03:35:02 +00:00
2023-12-16 02:40:04 +00:00
onReachBottom(() => {
loadMore();
});
2023-12-13 03:35:02 +00:00
</script>
<style lang="scss" scoped>
2023-12-16 02:40:04 +00:00
.card-btn {
// width: 144rpx;
padding: 0 16rpx;
height: 50rpx;
border-radius: 40rpx;
background: linear-gradient(90deg, var(--ui-BG-Main), var(--ui-BG-Main-gradient));
color: #ffffff;
font-size: 24rpx;
font-weight: 400;
}
2023-12-13 03:35:02 +00:00
2023-12-16 02:40:04 +00:00
.border-btn {
background: linear-gradient(90deg, var(--ui-BG-Main-opacity-4), var(--ui-BG-Main-light));
color: #fff !important;
}
2023-12-13 03:35:02 +00:00
2023-12-16 02:40:04 +00:00
.disabled-btn {
background: #cccccc;
background-color: #cccccc !important;
color: #fff !important;
}
</style>