This commit is contained in:
yin 2026-10-27 17:55:41 +08:00
parent 739e1d81db
commit 7ff6520061
2 changed files with 146 additions and 20 deletions

View File

@ -1,12 +1,26 @@
<template>
<view class="ratio-card">
<view class="ratio-card__head">
<!-- 头部:整行可点,点一下展开/收起明细 -->
<view
class="ratio-card__head"
hover-class="ratio-card__head--active"
:hover-stay-time="60"
@click="toggle"
>
<text class="ratio-card__title">{{ title }}</text>
<text v-if="!loading && !error && items.length" class="ratio-card__total">
共 {{ total }} 单
</text>
<view class="ratio-card__toggle" :class="{ 'ratio-card__toggle--open': expanded }">
<up-icon name="arrow-down" color="#909399" size="13"></up-icon>
</view>
</view>
<!-- 收起时的简短信息:共 N 单 + 占比最高的项 -->
<text
v-if="!expanded && !loading && !error && items.length"
class="ratio-card__summary"
>
{{ summary }}
</text>
<!-- 加载中 -->
<view v-if="loading" class="ratio-card__state">加载中…</view>
@ -19,7 +33,7 @@
<!-- 空数据 -->
<view v-else-if="!items.length" class="ratio-card__state">{{ emptyText }}</view>
<!-- 占比条 + 明细 -->
<!-- 占比条(始终显示)+ 明细(展开时) -->
<template v-else>
<view class="ratio-card__bar">
<view
@ -30,7 +44,7 @@
></view>
</view>
<view class="ratio-card__list">
<view v-if="expanded" class="ratio-card__list">
<view
v-for="(item, index) in items"
:key="`row-${item.name}-${index}`"
@ -54,6 +68,9 @@
* canvas 适配都是成本),移动端窄屏上「**顶部堆叠占比条 + 下方明细**」反而更易读:
* 每行都能同时看到 名称 / 单量 / 百分比。
*
* 默认**收起**:只显示 标题 + 概要(共 N 单 · 占比最高项)+ 堆叠占比条,
* 点标题行展开后才渲染明细列表(首页不再一次性铺满,省很多竖向空间)。
*
* 用法:
* ```vue
* <ratio-card
@ -66,7 +83,7 @@
* ```
*/
import { computed } from 'vue'
import { computed, ref } from 'vue'
import type { DashboardRatioItem } from '@/api/dashboard/dashboard'
const props = withDefaults(
@ -87,6 +104,13 @@ const props = withDefaults(
const emit = defineEmits<{ retry: [] }>()
/** 是否展开明细(默认收起,只给概要) */
const expanded = ref(false)
function toggle(): void {
expanded.value = !expanded.value
}
/** 兜底色板:后端没下发 `itemStyle.color` 时按顺序取色 */
const PALETTE = [
'#2979ff',
@ -103,6 +127,23 @@ const total = computed(() =>
props.items.reduce((sum, item) => sum + (Number(item.value) || 0), 0)
)
/** 占比最高的项(后端已降序,这里再算一次以防未排序) */
const topItem = computed(() =>
props.items.reduce<DashboardRatioItem | undefined>((top, item) => {
if (!top) return item
return (Number(item.value) || 0) > (Number(top.value) || 0) ? item : top
}, undefined)
)
/** 收起时的简短信息:共 N 单 · 占比最高项 */
const summary = computed(() => {
if (!props.items.length) return ''
const top = topItem.value
const parts = [`共 ${total.value} 单`]
if (top) parts.push(`${top.name} ${percent(top)}%`)
return parts.join(' · ')
})
function percent(item: DashboardRatioItem): string {
if (!total.value) return '0'
const value = ((Number(item.value) || 0) / total.value) * 100
@ -119,7 +160,7 @@ function colorOf(item: DashboardRatioItem, index: number): string {
<style lang="scss" scoped>
.ratio-card {
margin: 0 24rpx 24rpx;
padding: 28rpx 28rpx 8rpx;
padding: 28rpx 28rpx 20rpx;
background-color: #ffffff;
border-radius: 24rpx;
box-shadow: 0 8rpx 24rpx rgba(41, 121, 255, 0.06);
@ -128,6 +169,34 @@ function colorOf(item: DashboardRatioItem, index: number): string {
display: flex;
align-items: center;
justify-content: space-between;
&--active {
opacity: 0.6;
}
}
&__toggle {
flex-shrink: 0;
display: flex;
align-items: center;
justify-content: center;
width: 44rpx;
height: 44rpx;
transition: transform 0.2s;
&--open {
transform: rotate(180deg);
}
}
&__summary {
display: block;
margin-top: 10rpx;
font-size: 24rpx;
color: #909399;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
&__title {
@ -136,11 +205,6 @@ function colorOf(item: DashboardRatioItem, index: number): string {
color: #303133;
}
&__total {
font-size: 24rpx;
color: #909399;
}
/* 堆叠占比条 */
&__bar {
display: flex;

View File

@ -118,13 +118,25 @@
@retry="loadExpress"
/>
<!-- ④ 热销商品排行(Top10) -->
<!-- ④ 热销商品排行(Top10):默认收起,只给「共 N 件 · 榜首 xxx」 -->
<view class="rank">
<view class="rank__head">
<text class="rank__title">热销商品排行</text>
<text class="rank__sub">Top{{ RANK_TOP }}</text>
<view
class="rank__head"
hover-class="rank__head--active"
:hover-stay-time="60"
@click="rankExpanded = !rankExpanded"
>
<view class="rank__head-main">
<text class="rank__title">热销商品排行</text>
<text class="rank__sub">Top{{ RANK_TOP }}</text>
</view>
<view class="rank__toggle" :class="{ 'rank__toggle--open': rankExpanded }">
<up-icon name="arrow-down" color="#909399" size="13"></up-icon>
</view>
</view>
<text v-if="!rankExpanded && rankSummary" class="rank__summary">{{ rankSummary }}</text>
<view v-if="rank.loading" class="rank__state">加载中…</view>
<view v-else-if="rank.error" class="rank__state rank__state--error" @click="loadRank">
@ -134,7 +146,7 @@
<view v-else-if="!rank.items.length" class="rank__state">今日暂无销量数据</view>
<template v-else>
<template v-else-if="rankExpanded">
<view class="rank__header">
<text class="rank__cell rank__cell--index">排名</text>
<text class="rank__cell rank__cell--name">商品</text>
@ -516,6 +528,20 @@ const rank = reactive<{ items: DashboardRankItem[]; loading: boolean; error: str
error: ''
})
/** 是否展开 Top10 明细(默认收起,只显示「共 N 件 · 榜首 xxx」) */
const rankExpanded = ref(false)
/** 今日销量合计(收起时的简短信息之一) */
const rankTotal = computed(() =>
rank.items.reduce((sum, item) => sum + (Number(item.value) || 0), 0)
)
const rankSummary = computed(() => {
if (!rank.items.length) return ''
const top = rank.items[0]
return `共 ${rankTotal.value} 件 · 榜首 ${top.name}`
})
async function loadRank(): Promise<void> {
if (!canShowBoard.value) return
rank.loading = true
@ -937,15 +963,25 @@ useEntryScene()
/* ───────── 热销排行 ───────── */
.rank {
margin: 24rpx 24rpx 0;
padding: 28rpx 28rpx 12rpx;
padding: 28rpx 28rpx 20rpx;
background-color: #ffffff;
border-radius: 24rpx;
box-shadow: 0 8rpx 24rpx rgba(41, 121, 255, 0.06);
&__head {
display: flex;
align-items: baseline;
align-items: center;
justify-content: space-between;
&--active {
opacity: 0.6;
}
}
&__head-main {
display: flex;
align-items: baseline;
min-width: 0;
}
&__title {
@ -955,10 +991,36 @@ useEntryScene()
}
&__sub {
margin-left: 12rpx;
font-size: 24rpx;
color: #909399;
}
&__toggle {
flex-shrink: 0;
display: flex;
align-items: center;
justify-content: center;
width: 44rpx;
height: 44rpx;
transition: transform 0.2s;
&--open {
transform: rotate(180deg);
}
}
/* 收起时的简短信息:今日共 N 件 · 榜首 xxx */
&__summary {
display: block;
margin-top: 10rpx;
font-size: 24rpx;
color: #909399;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
&__header {
display: flex;
align-items: center;