Files
kangyuan-mobile/pages/index/components/aistore_earnings_carousel.vue
T
2026-06-26 17:20:30 +08:00

375 lines
8.8 KiB
Vue

<template>
<view class="earnings-carousel">
<view class="earnings-header">
<view class="earnings-title">
<view class="earnings-title__icon">
<image class="earnings-title__icon-img" src="/static/icon/medal.png" mode="aspectFit" />
</view>
<view class="earnings-title__text">销量风云榜</view>
</view>
</view>
<swiper
class="earnings-swiper"
:current="currentPageIndex"
:autoplay="autoplay"
:interval="interval"
:duration="duration"
:circular="swiperPages.length > 1"
:disable-touch="true"
:indicator-dots="false"
@change="onSwiperChange"
>
<swiper-item v-for="(page, pageIndex) in swiperPages" :key="pageIndex">
<view class="earnings-page">
<view class="earnings-row">
<view class="rank-card" v-for="(item, i) in page" :key="item.orderItemId || i">
<view class="rank-top">
<image class="rank-avatar" :src="getAvatar(item)" mode="aspectFill" />
</view>
<view class="rank-name lineOne">{{ item.nickname || maskMobile(item.mobile) || '-' }}</view>
<view class="rank-goods">
<image class="rank-goods__img" :src="getGoodsPic(item)" mode="aspectFill" />
<view class="rank-goods__mask">
<view class="rank-rate">{{ formatPercent(item.progress) }}</view>
<view class="rank-progress">
<view class="rank-progress__bar" :style="{ width: `${clampProgress(item.progress)}%` }" />
</view>
</view>
</view>
<view class="rank-goods__name lineOne">{{ item.spuName || '-' }}</view>
</view>
</view>
</view>
</swiper-item>
</swiper>
</view>
</template>
<script setup>
import { computed, onMounted, ref, watch } from 'vue';
import sheep from '@/sheep';
import { earningsPage } from '@/sheep/api/ai/index';
const props = defineProps({
active: {
type: Boolean,
default: true,
},
pageSize: {
type: Number,
default: 30,
},
autoplay: {
type: Boolean,
default: true,
},
interval: {
type: Number,
default: 5000,
},
duration: {
type: Number,
default: 450,
},
});
const MAX_PAGES = 30;
const DISPLAY_SIZE = 5;
const currentPageIndex = ref(0);
const total = ref(0);
const pagesMap = ref({});
const loadingMap = ref({});
const normalizeSize = (val, def) => {
const n = Math.floor(Number(val));
return Number.isFinite(n) && n > 0 ? n : def;
};
const autoplay = computed(() => props.autoplay && swiperPages.value.length > 1);
const interval = computed(() => {
const n = Number(props.interval);
return Number.isFinite(n) && n > 0 ? n : 5000;
});
const duration = computed(() => {
const n = Number(props.duration);
return Number.isFinite(n) && n >= 0 ? n : 450;
});
const totalPages = computed(() => MAX_PAGES);
const swiperPages = computed(() => {
return pages.value;
});
const normalizePageNo = (pageNo) => {
const n = Math.floor(Number(pageNo));
const tp = totalPages.value || 1;
if (!Number.isFinite(n) || tp <= 0) return 1;
if (n <= 0) return tp;
if (n > tp) return 1;
return n;
};
const normalizePageList = (list) => {
const arr = Array.isArray(list) ? list : [];
if (arr.length === DISPLAY_SIZE) return arr;
if (arr.length > DISPLAY_SIZE) return arr.slice(0, DISPLAY_SIZE);
return [...arr, ...Array.from({ length: DISPLAY_SIZE - arr.length }, () => ({}))];
};
const pages = computed(() => {
const arr = [];
const pageCount = totalPages.value || 1;
for (let i = 0; i < pageCount; i += 1) {
const key = String(i + 1);
arr.push(normalizePageList(pagesMap.value[key]));
}
return arr;
});
const fetchPage = async (pageNo) => {
const res = await earningsPage({
pageNo,
pageSize: normalizeSize(props.pageSize, 30),
}).catch(() => ({ code: -1 }));
if (res?.code !== 0) return;
const data = res?.data || {};
total.value = Number(data?.total) || 0;
const list = Array.isArray(data?.list) ? data.list : [];
pagesMap.value[String(pageNo)] = list;
};
const ensurePageLoaded = (pageNo, force = false) => {
const no = normalizePageNo(pageNo);
const key = String(no);
if (!force && pagesMap.value[key]) return Promise.resolve();
if (loadingMap.value[key]) return loadingMap.value[key];
const p = fetchPage(no).finally(() => {
loadingMap.value[key] = null;
});
loadingMap.value[key] = p;
return p;
};
const onSwiperChange = (e) => {
const next = Number(e?.detail?.current);
if (!Number.isFinite(next)) return;
currentPageIndex.value = next;
const currentPageNo = next + 1;
ensurePageLoaded(currentPageNo, true);
ensurePageLoaded(currentPageNo + 1);
ensurePageLoaded(currentPageNo + 2);
};
const clampProgress = (val) => {
const n = Number(val);
if (!Number.isFinite(n)) return 0;
return Math.max(0, Math.min(100, n));
};
const formatPercent = (val) => {
const n = clampProgress(val);
return `${Math.round(n)}%`;
};
const getAvatar = (item) => {
const url = String(item?.avatar || '').trim();
return url ? sheep.$url.cdn(url) : '/static/head_null.png';
};
const getGoodsPic = (item) => {
const url = String(item?.picUrl || '').trim();
return url ? sheep.$url.cdn(url) : '/static/product_null.png';
};
const maskMobile = (mobile) => {
const s = String(mobile || '').trim();
if (!/^1\d{10}$/.test(s)) return '';
return `${s.slice(0, 3)}****${s.slice(7)}`;
};
const reset = async () => {
currentPageIndex.value = 0;
total.value = 0;
pagesMap.value = {};
loadingMap.value = {};
await ensurePageLoaded(1, true);
ensurePageLoaded(2);
};
watch(
() => props.active,
(val) => {
if (val) {
reset();
}
},
{ immediate: true },
);
watch(
() => props.pageSize,
() => {
if (props.active) reset();
},
);
onMounted(() => {
if (props.active) reset();
});
</script>
<style scoped lang="scss">
.earnings-carousel {
margin-top: 18rpx;
padding: 16rpx 12rpx 0;
border-radius: 24rpx;
background: rgba(255, 255, 255, 0.75);
box-shadow: 0 16rpx 32rpx rgba(200, 213, 248, 0.8);
box-sizing: border-box;
}
.earnings-header {
display: flex;
align-items: center;
padding: 0 6rpx 0;
}
.earnings-title {
display: flex;
align-items: center;
gap: 10rpx;
}
.earnings-title__icon {
width: 40rpx;
height: 40rpx;
border-radius: 20rpx;
background: linear-gradient(180deg, #93c5fd 0%, #1d4ed8 100%);
display: flex;
align-items: center;
justify-content: center;
}
.earnings-title__icon-img {
width: 28rpx;
height: 28rpx;
display: block;
}
.earnings-title__text {
font-size: 32rpx;
font-weight: 900;
color: #1d4ed8;
}
.earnings-swiper {
height: 280rpx;
}
.earnings-page {
width: 100%;
height: 270rpx;
display: flex;
align-items: center;
}
.earnings-row {
width: 100%;
display: flex;
justify-content: space-between;
gap: 14rpx;
padding: 0 8rpx;
box-sizing: border-box;
}
.rank-card {
flex: 1;
min-width: 0;
border-radius: 12rpx;
background: rgba(255, 255, 255, 0.95);
padding: 16rpx 12rpx;
box-sizing: border-box;
box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1)
}
.rank-top {
display: flex;
align-items: center;
justify-content: center;
gap: 8rpx;
}
.rank-avatar {
width: 54rpx;
height: 54rpx;
border-radius: 32rpx;
background: #f2f3f6;
flex-shrink: 0;
}
.rank-name {
margin-top: 10rpx;
font-size: 22rpx;
color: #111827;
text-align: center;
}
.rank-rate {
font-size: 24rpx;
font-weight: 900;
color: #ffffff;
line-height: 1;
}
.rank-progress {
margin-top: 8rpx;
height: 12rpx;
border-radius: 999rpx;
background: rgba(255, 255, 255, 0.5);
overflow: hidden;
}
.rank-progress__bar {
height: 100%;
border-radius: 999rpx;
background: linear-gradient(90deg, #ffb347 0%, #ff8a00 100%);
}
.rank-goods {
margin-top: 14rpx;
position: relative;
border-radius: 14rpx;
background: #f3f5f8;
overflow: hidden;
height: 66rpx;
}
.rank-goods__img {
width: 100%;
height: 100%;
}
.rank-goods__mask {
position: absolute;
inset: 0;
display: flex;
flex-direction: column;
justify-content: center;
padding: 0 10rpx;
background: rgba(17, 24, 39, 0.32);
box-sizing: border-box;
}
.rank-goods__name {
margin-top: 10rpx;
font-size: 22rpx;
color: #111827;
text-align: center;
}
</style>