1091 lines
31 KiB
Vue
1091 lines
31 KiB
Vue
<template>
|
||
<view class="page">
|
||
<!-- 顶部区:问候 + 日期 / 身份摘要 + 刷新 -->
|
||
<view class="hero">
|
||
<view class="hero__deco hero__deco--lg"></view>
|
||
<view class="hero__deco hero__deco--sm"></view>
|
||
|
||
<view class="hero__row">
|
||
<view class="hero__text">
|
||
<view class="hero__greeting">{{ greeting }}</view>
|
||
<view class="hero__name">{{ heroName }}</view>
|
||
<view class="hero__desc">{{ heroDesc }}</view>
|
||
</view>
|
||
|
||
<view
|
||
v-if="canShowBoard"
|
||
class="hero__refresh"
|
||
:class="{ 'hero__refresh--busy': refreshing }"
|
||
hover-class="hero__refresh--active"
|
||
:hover-stay-time="60"
|
||
@click="refreshAll()"
|
||
>
|
||
<up-icon name="reload" color="#ffffff" size="18"></up-icon>
|
||
</view>
|
||
</view>
|
||
</view>
|
||
|
||
<!-- 未登录 / 微信已登录未绑定:页内引导(绝不自动跳登录页,微信审核要求) -->
|
||
<view v-if="!canShowBoard" class="gate">
|
||
<view class="gate__icon">
|
||
<up-icon :name="gateIcon" color="#2979ff" size="34"></up-icon>
|
||
</view>
|
||
<view class="gate__title">{{ gateTitle }}</view>
|
||
<view class="gate__desc">{{ gateDesc }}</view>
|
||
<up-button
|
||
type="primary"
|
||
shape="circle"
|
||
size="small"
|
||
:text="gateActionText"
|
||
:custom-style="{ marginTop: '28rpx', width: '320rpx' }"
|
||
@click="onGateAction"
|
||
></up-button>
|
||
</view>
|
||
|
||
<template v-else>
|
||
<!-- ① 今日订单 / 未分配快递单号 -->
|
||
<view v-if="statsError" class="panel-error">
|
||
<view class="panel-error__text">{{ statsError }}</view>
|
||
<view class="panel-error__retry" @click="loadStats">点击重试</view>
|
||
</view>
|
||
|
||
<view v-else class="stats">
|
||
<view v-for="card in statCards" :key="card.key" class="stat">
|
||
<view class="stat__head">
|
||
<view class="stat__icon" :class="`stat__icon--${card.tone}`">
|
||
<up-icon :name="card.icon" :color="toneColor(card.tone)" size="16"></up-icon>
|
||
</view>
|
||
<text class="stat__label">{{ card.label }}</text>
|
||
</view>
|
||
|
||
<view class="stat__value">{{ card.value }}</view>
|
||
|
||
<view class="stat__foot">
|
||
<template v-if="card.footType === 'trend'">
|
||
<up-icon
|
||
:name="card.footUp ? 'arrow-up' : 'arrow-down'"
|
||
:color="card.footUp ? '#19be6b' : '#f56c6c'"
|
||
size="10"
|
||
></up-icon>
|
||
<text class="stat__trend" :class="card.footUp ? 'is-up' : 'is-down'">
|
||
{{ card.footText }}
|
||
</text>
|
||
<text class="stat__hint">较昨日</text>
|
||
</template>
|
||
|
||
<text
|
||
v-else-if="card.footType === 'tag'"
|
||
class="stat__tag"
|
||
:class="`stat__tag--${card.tone}`"
|
||
>
|
||
{{ card.footText }}
|
||
</text>
|
||
|
||
<text v-else-if="card.footType === 'rate'" class="stat__hint">{{ card.footText }}</text>
|
||
|
||
<view v-else-if="card.footType === 'progress'" class="stat__progress">
|
||
<view
|
||
class="stat__progress-inner"
|
||
:style="{
|
||
width: (card.footPercent || 0) + '%',
|
||
backgroundColor: progressColor(card.footPercent || 0)
|
||
}"
|
||
></view>
|
||
</view>
|
||
|
||
<text v-else class="stat__hint">{{ statsLoading ? '加载中…' : '—' }}</text>
|
||
</view>
|
||
</view>
|
||
</view>
|
||
|
||
<!-- ② 平台分布 -->
|
||
<ratio-card
|
||
title="平台分布"
|
||
:items="platform.items"
|
||
:loading="platform.loading"
|
||
:error="platform.error"
|
||
empty-text="暂无平台订单数据"
|
||
@retry="loadPlatform"
|
||
/>
|
||
|
||
<!-- ③ 快递占比 -->
|
||
<ratio-card
|
||
title="快递占比"
|
||
:items="express.items"
|
||
:loading="express.loading"
|
||
:error="express.error"
|
||
empty-text="暂无快递数据"
|
||
@retry="loadExpress"
|
||
/>
|
||
|
||
<!-- ④ 热销商品排行(Top10) -->
|
||
<view class="rank">
|
||
<view class="rank__head">
|
||
<text class="rank__title">热销商品排行</text>
|
||
<text class="rank__sub">Top{{ RANK_TOP }}</text>
|
||
</view>
|
||
|
||
<view v-if="rank.loading" class="rank__state">加载中…</view>
|
||
|
||
<view v-else-if="rank.error" class="rank__state rank__state--error" @click="loadRank">
|
||
<text class="rank__state-text">{{ rank.error }}</text>
|
||
<text class="rank__retry">点击重试</text>
|
||
</view>
|
||
|
||
<view v-else-if="!rank.items.length" class="rank__state">今日暂无销量数据</view>
|
||
|
||
<template v-else>
|
||
<view class="rank__header">
|
||
<text class="rank__cell rank__cell--index">排名</text>
|
||
<text class="rank__cell rank__cell--name">商品</text>
|
||
<text class="rank__cell rank__cell--value">销量</text>
|
||
<text class="rank__cell rank__cell--trend">环比</text>
|
||
</view>
|
||
|
||
<view
|
||
v-for="(item, index) in rank.items"
|
||
:key="`${item.id}-${item.sku}-${index}`"
|
||
class="rank__item"
|
||
>
|
||
<view class="rank__cell rank__cell--index">
|
||
<view class="rank__badge" :class="rankClass(index)">{{ index + 1 }}</view>
|
||
</view>
|
||
<view class="rank__cell rank__cell--name">
|
||
<text class="rank__product">{{ item.name }}</text>
|
||
<text class="rank__sku">SKU: {{ item.sku || '—' }}</text>
|
||
</view>
|
||
<text class="rank__cell rank__cell--value">{{ item.value }}</text>
|
||
<view class="rank__cell rank__cell--trend">
|
||
<text class="rank__trend" :class="item.trend >= 0 ? 'is-up' : 'is-down'">
|
||
{{ item.trend >= 0 ? '+' : '-' }}{{ Math.abs(item.trend) }}%
|
||
</text>
|
||
</view>
|
||
</view>
|
||
</template>
|
||
</view>
|
||
</template>
|
||
|
||
<!-- 登录抽屉:未登录点「去登录」就地弹出,不跳页 -->
|
||
<auth-modal></auth-modal>
|
||
</view>
|
||
</template>
|
||
|
||
<script setup lang="ts">
|
||
/**
|
||
* 首页 = 经营看板(tabBar 页)
|
||
*
|
||
* 与 new3 的 `views/dashboard` 对齐,按移动端只保留 4 个面板:
|
||
* ① 今日发货统计(new3 第一行的**全部 8 项**):今日订单 / 未分配快递单号 / 未打印 /
|
||
* 未拣货 / 已复核 / 已打包 / 拦截订单 / 异常订单(仓库侧为 待发货 / 今日出库 替换 已复核 / 已打包)
|
||
* ② 平台分布(dashboard.platform_distribution)
|
||
* ③ 快递占比(dashboard.express_ratio)
|
||
* ④ 热销商品排行 Top10(dashboard.hot_order_rank,body 带 `{ top: 10 }`)
|
||
*
|
||
* ⚠️ 三条项目约定:
|
||
* 1. **不能一打开就强制登录**(微信审核):未登录渲染引导卡片,用户点按钮才弹抽屉;
|
||
* 因此本页必须挂 `<auth-modal />`(`goLogin()` 只负责置状态,抽屉由页面渲染)。
|
||
* 2. 面板各自独立加载 / 失败重试,**一个接口失败不影响其它面板**。
|
||
* 3. 切换 tab 只触发 `onShow`:用 `lastLoadAt` 做 60s 节流,避免来回切 tab 反复请求。
|
||
*/
|
||
|
||
import { computed, reactive, ref, watch } from 'vue'
|
||
import { onPullDownRefresh, onShow } from '@dcloudio/uni-app'
|
||
import { useUserStore } from '@/store/modules/user'
|
||
import { usePageAuth } from '@/composables/usePageAuth'
|
||
import { useEntryScene } from '@/composables/useEntryScene'
|
||
import AuthModal from '@/components/auth-modal/auth-modal.vue'
|
||
import RatioCard from '@/components/ratio-card/ratio-card.vue'
|
||
import {
|
||
fetchDashboardStats,
|
||
fetchExpressRatio,
|
||
fetchHotOrderRank,
|
||
fetchPlatformDistribution
|
||
} from '@/api/dashboard/dashboard'
|
||
import type {
|
||
DashboardRankItem,
|
||
DashboardRatioItem,
|
||
DashboardStats,
|
||
DashboardStatsMethod
|
||
} from '@/api/dashboard/dashboard'
|
||
|
||
/** 热销排行取前 10(与 new3 `HotOrderRank.topCount` 默认值一致) */
|
||
const RANK_TOP = 10
|
||
|
||
/** 切 tab 回本页的重载节流(毫秒) */
|
||
const REFRESH_INTERVAL = 60 * 1000
|
||
|
||
const userStore = useUserStore()
|
||
const { isLogin, goLogin } = usePageAuth('public')
|
||
|
||
/** 可以展示看板:已登录 + 已绑定 WMS 账号(微信登录态未绑定时没有业务数据) */
|
||
const canShowBoard = computed(() => userStore.isLogin && !userStore.isWechatUnbound)
|
||
|
||
/* ───────────────────────── 顶部文案 ───────────────────────── */
|
||
|
||
const greeting = computed(() => {
|
||
const hour = new Date().getHours()
|
||
if (hour < 6) return '夜深了'
|
||
if (hour < 12) return '早上好'
|
||
if (hour < 14) return '中午好'
|
||
if (hour < 18) return '下午好'
|
||
return '晚上好'
|
||
})
|
||
|
||
const heroName = computed(() => {
|
||
if (!isLogin.value) return '经营看板'
|
||
if (userStore.isWechatUnbound) return userStore.wechatNickname || '微信用户'
|
||
return userStore.userName || '经营看板'
|
||
})
|
||
|
||
const today = computed(() => {
|
||
const now = new Date()
|
||
const week = ['周日', '周一', '周二', '周三', '周四', '周五', '周六'][now.getDay()]
|
||
const pad = (n: number) => String(n).padStart(2, '0')
|
||
return `${now.getFullYear()}-${pad(now.getMonth() + 1)}-${pad(now.getDate())} ${week}`
|
||
})
|
||
|
||
const heroDesc = computed(() => {
|
||
if (!isLogin.value) return `${today.value} · 登录后查看今日经营数据`
|
||
if (userStore.isWechatUnbound) return `${today.value} · 微信已登录 · 待绑定 WMS 账号`
|
||
const parts = [userStore.roleTypeLabel, userStore.currentEmployee?.store_name].filter(Boolean)
|
||
return [today.value, ...parts].join(' · ')
|
||
})
|
||
|
||
/* ───────────────────── 未登录 / 未绑定引导 ───────────────────── */
|
||
|
||
const gateIcon = computed(() => (userStore.isWechatUnbound ? 'account' : 'lock'))
|
||
const gateTitle = computed(() =>
|
||
userStore.isWechatUnbound ? '绑定 WMS 账号后查看数据' : '登录后查看经营看板'
|
||
)
|
||
const gateDesc = computed(() =>
|
||
userStore.isWechatUnbound
|
||
? '当前是微信登录状态,绑定 WMS 账号后即可查看今日订单、平台分布等数据'
|
||
: '今日订单、平台分布、快递占比与热销排行,登录后自动展示'
|
||
)
|
||
const gateActionText = computed(() => (userStore.isWechatUnbound ? '去绑定账号' : '登录'))
|
||
|
||
function onGateAction(): void {
|
||
if (userStore.isWechatUnbound) {
|
||
uni.switchTab({ url: '/pages/mine/mine' })
|
||
return
|
||
}
|
||
goLogin()
|
||
}
|
||
|
||
/* ───────────────────────── 发货统计 ───────────────────────── */
|
||
|
||
const stats = ref<DashboardStats>({})
|
||
const statsLoading = ref(false)
|
||
const statsError = ref('')
|
||
|
||
/**
|
||
* 统计接口选择:货主侧用 `dashboard.storeStats`(真实数据),
|
||
* 仓库侧权限才用 `dashboard.warehouseStats`(后端目前是演示数据)。
|
||
* 两个权限点都没有时默认货主侧 —— 与小程序现有业务页(都是 `*Store` 货主侧接口)保持一致。
|
||
*/
|
||
const statsMethod = computed<DashboardStatsMethod>(() => {
|
||
const perms = userStore.permissions || []
|
||
if (perms.includes('dashboard.storeBaseData')) return 'dashboard.storeStats'
|
||
if (perms.includes('dashboard.warehouseBaseData')) return 'dashboard.warehouseStats'
|
||
return 'dashboard.storeStats'
|
||
})
|
||
|
||
/* ───────────────────────── 指标卡片(new3 第一行全部 8 项) ───────────────────────── */
|
||
|
||
type StatTone = 'primary' | 'warning' | 'info' | 'success' | 'danger'
|
||
|
||
interface StatCardMeta {
|
||
/** `dashboard.*Stats` 的字段名 */
|
||
key: keyof DashboardStats
|
||
label: string
|
||
/** uview-plus 图标名(名字必须真实存在,否则格子里是空白) */
|
||
icon: string
|
||
tone: StatTone
|
||
/** 数值 > 0 时显示的提示标签(待分配 / 待打印 / 需及时处理…) */
|
||
tagText?: string
|
||
}
|
||
|
||
/** 货主侧(`dashboard.storeStats`)—— 与 new3 `StatsCardsStore.vue` 的 8 张卡一致 */
|
||
const STORE_CARDS: StatCardMeta[] = [
|
||
{ key: 'todayOrders', label: '今日订单', icon: 'order', tone: 'primary' },
|
||
{ key: 'unassignedOrders', label: '未分配快递单号', icon: 'bag', tone: 'warning', tagText: '待分配' },
|
||
{ key: 'unprintedOrders', label: '未打印', icon: 'file-text', tone: 'info', tagText: '待打印' },
|
||
{ key: 'unpickedOrders', label: '未拣货', icon: 'list-dot', tone: 'info', tagText: '待拣货' },
|
||
{ key: 'recheckedOrders', label: '已复核', icon: 'checkmark-circle', tone: 'success' },
|
||
{ key: 'packedOrders', label: '已打包', icon: 'bag-fill', tone: 'success' },
|
||
{ key: 'interceptedOrders', label: '拦截订单', icon: 'error-circle', tone: 'danger', tagText: '需及时处理' },
|
||
{ key: 'exceptionOrders', label: '异常订单', icon: 'error', tone: 'danger', tagText: '需及时处理' }
|
||
]
|
||
|
||
/** 仓库侧(`dashboard.warehouseStats`)—— 与 new3 `StatsCardsWarehouse.vue` 的 8 张卡一致 */
|
||
const WAREHOUSE_CARDS: StatCardMeta[] = [
|
||
{ key: 'todayOrders', label: '今日订单', icon: 'order', tone: 'primary' },
|
||
{ key: 'unassignedOrders', label: '未分配快递单号', icon: 'bag', tone: 'warning', tagText: '待分配' },
|
||
{ key: 'unprintedOrders', label: '未打印', icon: 'file-text', tone: 'info', tagText: '待打印' },
|
||
{ key: 'unpickedOrders', label: '未拣货', icon: 'list-dot', tone: 'info', tagText: '待拣货' },
|
||
{ key: 'pendingShipments', label: '待发货', icon: 'car', tone: 'success' },
|
||
{ key: 'todayOutbound', label: '今日出库', icon: 'car-fill', tone: 'success' },
|
||
{ key: 'interceptedOrders', label: '拦截订单', icon: 'error-circle', tone: 'danger', tagText: '需及时处理' },
|
||
{ key: 'exceptionOrders', label: '异常订单', icon: 'error', tone: 'danger', tagText: '需及时处理' }
|
||
]
|
||
|
||
/** 卡片渲染模型(把底部的「趋势 / 标签 / 进度 / 完成率」拍平成字段,模板里不必做类型收窄) */
|
||
interface StatCard extends StatCardMeta {
|
||
value: string
|
||
footType: 'trend' | 'tag' | 'rate' | 'progress' | 'none'
|
||
footText?: string
|
||
footUp?: boolean
|
||
footPercent?: number
|
||
}
|
||
|
||
const TONE_COLORS: Record<StatTone, string> = {
|
||
primary: '#2979ff',
|
||
warning: '#ff9900',
|
||
info: '#909399',
|
||
success: '#19be6b',
|
||
danger: '#f56c6c'
|
||
}
|
||
|
||
function toneColor(tone: StatTone): string {
|
||
return TONE_COLORS[tone]
|
||
}
|
||
|
||
function num(value: unknown): number {
|
||
const n = Number(value)
|
||
return Number.isFinite(n) ? n : 0
|
||
}
|
||
|
||
function isPresent(value: unknown): boolean {
|
||
return value !== undefined && value !== null && value !== ''
|
||
}
|
||
|
||
function clampPercent(value: number): number {
|
||
if (!Number.isFinite(value)) return 0
|
||
return Math.min(100, Math.max(0, value))
|
||
}
|
||
|
||
/** 进度条配色(与 new3 的 getProgressColor 一致:≥80 绿 / ≥60 橙 / 其余红) */
|
||
function progressColor(percent: number): string {
|
||
if (percent >= 80) return '#19be6b'
|
||
if (percent >= 60) return '#ff9900'
|
||
return '#f56c6c'
|
||
}
|
||
|
||
/** 复核进度(后端有 recheckProgress 用它,否则 已复核/今日订单 兜底) */
|
||
const recheckPercent = computed(() => {
|
||
const raw = stats.value.recheckProgress
|
||
if (isPresent(raw)) return clampPercent(num(raw))
|
||
const today = num(stats.value.todayOrders)
|
||
return today ? clampPercent((num(stats.value.recheckedOrders) / today) * 100) : 0
|
||
})
|
||
|
||
/** 发货进度(后端有 shipmentProgress 用它,否则 (今日订单-待发货)/今日订单 兜底) */
|
||
const shipmentPercent = computed(() => {
|
||
const raw = stats.value.shipmentProgress
|
||
if (isPresent(raw)) return clampPercent(num(raw))
|
||
const today = num(stats.value.todayOrders)
|
||
return today ? clampPercent(((today - num(stats.value.pendingShipments)) / today) * 100) : 0
|
||
})
|
||
|
||
/** 打包完成率文案(已打包 / 今日订单) */
|
||
const packRateText = computed(() => {
|
||
const raw = stats.value.packRate
|
||
const rate = isPresent(raw)
|
||
? num(raw)
|
||
: (() => {
|
||
const today = num(stats.value.todayOrders)
|
||
return today ? (num(stats.value.packedOrders) / today) * 100 : 0
|
||
})()
|
||
return `完成率 ${rate.toFixed(2)}%`
|
||
})
|
||
|
||
/** 出库完成率文案(今日出库 / 今日订单 兜底) */
|
||
const outboundRateText = computed(() => {
|
||
const raw = stats.value.outboundRate
|
||
const rate = isPresent(raw)
|
||
? num(raw)
|
||
: (() => {
|
||
const today = num(stats.value.todayOrders)
|
||
return today ? (num(stats.value.todayOutbound) / today) * 100 : 0
|
||
})()
|
||
return `完成率 ${rate.toFixed(2)}%`
|
||
})
|
||
|
||
function buildCard(meta: StatCardMeta): StatCard {
|
||
const raw = stats.value[meta.key]
|
||
const present = isPresent(raw)
|
||
const value = num(raw)
|
||
|
||
const card: StatCard = {
|
||
...meta,
|
||
value: present ? String(raw) : '—',
|
||
footType: 'none'
|
||
}
|
||
|
||
if (!present) return card
|
||
|
||
switch (meta.key) {
|
||
case 'todayOrders':
|
||
if (isPresent(stats.value.orderTrend)) {
|
||
card.footType = 'trend'
|
||
card.footUp = num(stats.value.orderTrend) >= 0
|
||
card.footText = `${Math.abs(num(stats.value.orderTrend))}%`
|
||
}
|
||
break
|
||
case 'recheckedOrders':
|
||
case 'pendingShipments':
|
||
card.footType = 'progress'
|
||
card.footPercent = meta.key === 'recheckedOrders' ? recheckPercent.value : shipmentPercent.value
|
||
break
|
||
case 'packedOrders':
|
||
card.footType = 'rate'
|
||
card.footText = packRateText.value
|
||
break
|
||
case 'todayOutbound':
|
||
card.footType = 'rate'
|
||
card.footText = outboundRateText.value
|
||
break
|
||
default:
|
||
if (meta.tagText && value > 0) {
|
||
card.footType = 'tag'
|
||
card.footText = meta.tagText
|
||
}
|
||
}
|
||
|
||
return card
|
||
}
|
||
|
||
/** 当前统计接口对应的卡片列表(货主 8 张 / 仓库 8 张) */
|
||
const statCards = computed<StatCard[]>(() => {
|
||
const metas = statsMethod.value === 'dashboard.warehouseStats' ? WAREHOUSE_CARDS : STORE_CARDS
|
||
return metas.map(buildCard)
|
||
})
|
||
|
||
async function loadStats(): Promise<void> {
|
||
if (!canShowBoard.value) return
|
||
statsLoading.value = true
|
||
statsError.value = ''
|
||
try {
|
||
stats.value = await fetchDashboardStats(statsMethod.value)
|
||
} catch {
|
||
stats.value = {}
|
||
statsError.value = '统计数据加载失败'
|
||
} finally {
|
||
statsLoading.value = false
|
||
}
|
||
}
|
||
|
||
/* ───────────────────────── 占比面板 ───────────────────────── */
|
||
|
||
interface RatioPane {
|
||
items: DashboardRatioItem[]
|
||
loading: boolean
|
||
error: string
|
||
}
|
||
|
||
const platform = reactive<RatioPane>({ items: [], loading: false, error: '' })
|
||
const express = reactive<RatioPane>({ items: [], loading: false, error: '' })
|
||
|
||
async function loadRatio(pane: RatioPane, fetcher: () => Promise<DashboardRatioItem[]>, label: string) {
|
||
if (!canShowBoard.value) return
|
||
pane.loading = true
|
||
pane.error = ''
|
||
try {
|
||
pane.items = await fetcher()
|
||
} catch {
|
||
pane.items = []
|
||
pane.error = `${label}加载失败`
|
||
} finally {
|
||
pane.loading = false
|
||
}
|
||
}
|
||
|
||
function loadPlatform(): Promise<void> {
|
||
return loadRatio(platform, fetchPlatformDistribution, '平台分布')
|
||
}
|
||
|
||
function loadExpress(): Promise<void> {
|
||
return loadRatio(express, fetchExpressRatio, '快递占比')
|
||
}
|
||
|
||
/* ───────────────────────── 热销排行 ───────────────────────── */
|
||
|
||
const rank = reactive<{ items: DashboardRankItem[]; loading: boolean; error: string }>({
|
||
items: [],
|
||
loading: false,
|
||
error: ''
|
||
})
|
||
|
||
async function loadRank(): Promise<void> {
|
||
if (!canShowBoard.value) return
|
||
rank.loading = true
|
||
rank.error = ''
|
||
try {
|
||
rank.items = await fetchHotOrderRank(RANK_TOP)
|
||
} catch {
|
||
rank.items = []
|
||
rank.error = '热销排行加载失败'
|
||
} finally {
|
||
rank.loading = false
|
||
}
|
||
}
|
||
|
||
function rankClass(index: number): string {
|
||
if (index === 0) return 'rank__badge--gold'
|
||
if (index === 1) return 'rank__badge--silver'
|
||
if (index === 2) return 'rank__badge--bronze'
|
||
return ''
|
||
}
|
||
|
||
/* ───────────────────────── 加载编排 ───────────────────────── */
|
||
|
||
const refreshing = ref(false)
|
||
/** 上次成功触发加载的时间(节流用) */
|
||
let lastLoadAt = 0
|
||
|
||
/**
|
||
* 确保 `user.info` 已就绪
|
||
*
|
||
* 为什么要等它:`isWechatUnbound` 只能从 `user.info` 的 `is_bound` 看出来。
|
||
* 冷启动时 store 还是空的,若直接请求看板,微信登录态(未绑定)会因权限不足失败,
|
||
* 甚至触发 401 → 自动弹登录抽屉 —— 违背「不打断浏览」的约定。
|
||
*/
|
||
async function ensureUserInfo(): Promise<void> {
|
||
if (userStore.hasUserInfo) return
|
||
try {
|
||
await userStore.getInfo()
|
||
} catch {
|
||
// 失败不阻塞:看板接口自己会失败并给出面板级重试
|
||
}
|
||
}
|
||
|
||
async function refreshAll(): Promise<void> {
|
||
if (!isLogin.value) return
|
||
// 并发保护:下拉刷新 + onShow + 登录 watch 可能同时触发
|
||
if (refreshing.value) return
|
||
lastLoadAt = Date.now()
|
||
refreshing.value = true
|
||
try {
|
||
await ensureUserInfo()
|
||
// 微信登录态未绑定 → 没有业务数据,切回页内引导(不请求看板接口)
|
||
if (!canShowBoard.value) {
|
||
resetAll()
|
||
return
|
||
}
|
||
// user.info 都没拿到(网络 / token 失效):不再连发 4 个必然失败的请求,
|
||
// 直接把各面板置为可重试状态(401 的抽屉处理由 request 层负责)
|
||
if (!userStore.hasUserInfo) {
|
||
stats.value = {}
|
||
statsError.value = '统计数据加载失败'
|
||
platform.items = []
|
||
platform.error = '平台分布加载失败'
|
||
express.items = []
|
||
express.error = '快递占比加载失败'
|
||
rank.items = []
|
||
rank.error = '热销排行加载失败'
|
||
return
|
||
}
|
||
await Promise.all([loadStats(), loadPlatform(), loadExpress(), loadRank()])
|
||
} finally {
|
||
refreshing.value = false
|
||
}
|
||
}
|
||
|
||
function resetAll(): void {
|
||
stats.value = {}
|
||
statsError.value = ''
|
||
platform.items = []
|
||
platform.error = ''
|
||
express.items = []
|
||
express.error = ''
|
||
rank.items = []
|
||
rank.error = ''
|
||
lastLoadAt = 0
|
||
}
|
||
|
||
// 登录 / 绑定成功后(抽屉就地登录,不会触发 onShow)→ 自动拉数据
|
||
watch(canShowBoard, (visible) => {
|
||
if (visible) {
|
||
refreshAll()
|
||
} else {
|
||
resetAll()
|
||
}
|
||
})
|
||
|
||
// tabBar 页切换只触发 onShow;用时间戳节流,避免来回切 tab 反复请求
|
||
onShow(() => {
|
||
if (!isLogin.value) return
|
||
// 已确认是「微信已登录未绑定」→ 不请求(页内已给绑定引导)
|
||
if (userStore.hasUserInfo && userStore.isWechatUnbound) return
|
||
if (Date.now() - lastLoadAt < REFRESH_INTERVAL) return
|
||
refreshAll()
|
||
})
|
||
|
||
onPullDownRefresh(async () => {
|
||
try {
|
||
await refreshAll()
|
||
} finally {
|
||
uni.stopPullDownRefresh()
|
||
}
|
||
})
|
||
|
||
// 消费扫码 / 链接入口(spm → 扫码登录 / 扫码绑定 / 打开指定页)
|
||
// 新首页是扫码进入的默认落地页,待处理入口由最先显示的页面消费(`takePendingEntry` 只取一次)
|
||
useEntryScene()
|
||
</script>
|
||
|
||
<style lang="scss" scoped>
|
||
.page {
|
||
min-height: 100vh;
|
||
background-color: #f5f6fa;
|
||
padding-bottom: 32rpx;
|
||
}
|
||
|
||
/* ───────── 顶部 ───────── */
|
||
.hero {
|
||
position: relative;
|
||
overflow: hidden;
|
||
padding: 44rpx 32rpx 56rpx;
|
||
background: linear-gradient(135deg, #2979ff 0%, #4b93ff 100%);
|
||
border-radius: 0 0 32rpx 32rpx;
|
||
|
||
&__deco {
|
||
position: absolute;
|
||
border-radius: 50%;
|
||
background-color: rgba(255, 255, 255, 0.1);
|
||
|
||
&--lg {
|
||
top: -140rpx;
|
||
right: -80rpx;
|
||
width: 300rpx;
|
||
height: 300rpx;
|
||
}
|
||
|
||
&--sm {
|
||
bottom: -70rpx;
|
||
left: -40rpx;
|
||
width: 160rpx;
|
||
height: 160rpx;
|
||
background-color: rgba(255, 255, 255, 0.07);
|
||
}
|
||
}
|
||
|
||
&__row {
|
||
position: relative;
|
||
z-index: 1;
|
||
display: flex;
|
||
align-items: center;
|
||
}
|
||
|
||
&__text {
|
||
flex: 1;
|
||
min-width: 0;
|
||
}
|
||
|
||
&__greeting {
|
||
font-size: 24rpx;
|
||
color: rgba(255, 255, 255, 0.85);
|
||
}
|
||
|
||
&__name {
|
||
margin-top: 10rpx;
|
||
font-size: 44rpx;
|
||
font-weight: 600;
|
||
color: #ffffff;
|
||
overflow: hidden;
|
||
text-overflow: ellipsis;
|
||
white-space: nowrap;
|
||
}
|
||
|
||
&__desc {
|
||
margin-top: 10rpx;
|
||
font-size: 24rpx;
|
||
color: rgba(255, 255, 255, 0.8);
|
||
}
|
||
|
||
&__refresh {
|
||
flex-shrink: 0;
|
||
display: flex;
|
||
align-items: center;
|
||
justify-content: center;
|
||
width: 68rpx;
|
||
height: 68rpx;
|
||
margin-left: 20rpx;
|
||
border-radius: 50%;
|
||
background-color: rgba(255, 255, 255, 0.22);
|
||
|
||
&--active {
|
||
background-color: rgba(255, 255, 255, 0.36);
|
||
}
|
||
|
||
&--busy {
|
||
opacity: 0.6;
|
||
}
|
||
}
|
||
}
|
||
|
||
/* ───────── 未登录引导 ───────── */
|
||
.gate {
|
||
display: flex;
|
||
flex-direction: column;
|
||
align-items: center;
|
||
margin: -20rpx 24rpx 0;
|
||
padding: 56rpx 40rpx 64rpx;
|
||
background-color: #ffffff;
|
||
border-radius: 24rpx;
|
||
box-shadow: 0 8rpx 24rpx rgba(41, 121, 255, 0.08);
|
||
|
||
&__icon {
|
||
display: flex;
|
||
align-items: center;
|
||
justify-content: center;
|
||
width: 108rpx;
|
||
height: 108rpx;
|
||
border-radius: 50%;
|
||
background-color: #eef4ff;
|
||
}
|
||
|
||
&__title {
|
||
margin-top: 26rpx;
|
||
font-size: 32rpx;
|
||
font-weight: 600;
|
||
color: #303133;
|
||
}
|
||
|
||
&__desc {
|
||
margin-top: 14rpx;
|
||
font-size: 26rpx;
|
||
line-height: 1.6;
|
||
color: #909399;
|
||
text-align: center;
|
||
}
|
||
}
|
||
|
||
/* ───────── 统计卡片(8 项,两列) ───────── */
|
||
/* ⚠️ 不要用 `width: calc(50% - 10rpx)`:WXSS 的 calc() 里 **rpx 不会被换算**,
|
||
实机上整条 width 声明会被丢弃 → 卡片退化成 width:auto,一行只剩一张(2026-10-21 真机踩到)。
|
||
这里只用百分比宽 + `space-between` 造列间距,与 `entry-sections` 的九宫格同一套写法。 */
|
||
.stats {
|
||
display: flex;
|
||
flex-wrap: wrap;
|
||
justify-content: space-between;
|
||
margin: 24rpx 24rpx 0;
|
||
}
|
||
|
||
.stat {
|
||
/* 两列:48.6% × 2 = 97.2%,剩下的 2.8%(≈20rpx)由 space-between 分配为列间距 */
|
||
/* flex-basis 与 width 双写:任意一条生效即可保证两列(不依赖 calc) */
|
||
flex: 0 0 48.6%;
|
||
width: 48.6%;
|
||
margin-bottom: 20rpx;
|
||
padding: 24rpx;
|
||
box-sizing: border-box;
|
||
background-color: #ffffff;
|
||
border-radius: 20rpx;
|
||
box-shadow: 0 8rpx 24rpx rgba(41, 121, 255, 0.06);
|
||
|
||
&__head {
|
||
display: flex;
|
||
align-items: center;
|
||
}
|
||
|
||
&__icon {
|
||
flex-shrink: 0;
|
||
display: flex;
|
||
align-items: center;
|
||
justify-content: center;
|
||
width: 44rpx;
|
||
height: 44rpx;
|
||
margin-right: 10rpx;
|
||
border-radius: 12rpx;
|
||
|
||
&--primary {
|
||
background-color: #eef4ff;
|
||
}
|
||
|
||
&--warning {
|
||
background-color: #fff6e8;
|
||
}
|
||
|
||
&--info {
|
||
background-color: #f2f3f5;
|
||
}
|
||
|
||
&--success {
|
||
background-color: #e8f9ef;
|
||
}
|
||
|
||
&--danger {
|
||
background-color: #fef0f0;
|
||
}
|
||
}
|
||
|
||
&__label {
|
||
flex: 1;
|
||
min-width: 0;
|
||
font-size: 24rpx;
|
||
color: #606266;
|
||
overflow: hidden;
|
||
text-overflow: ellipsis;
|
||
white-space: nowrap;
|
||
}
|
||
|
||
&__value {
|
||
margin-top: 14rpx;
|
||
font-size: 44rpx;
|
||
font-weight: 600;
|
||
line-height: 1.1;
|
||
color: #303133;
|
||
}
|
||
|
||
&__foot {
|
||
display: flex;
|
||
align-items: center;
|
||
min-height: 30rpx;
|
||
margin-top: 10rpx;
|
||
font-size: 22rpx;
|
||
}
|
||
|
||
&__trend {
|
||
margin-left: 4rpx;
|
||
font-weight: 600;
|
||
|
||
&.is-up {
|
||
color: #19be6b;
|
||
}
|
||
|
||
&.is-down {
|
||
color: #f56c6c;
|
||
}
|
||
}
|
||
|
||
&__hint {
|
||
margin-left: 6rpx;
|
||
font-size: 22rpx;
|
||
color: #909399;
|
||
}
|
||
|
||
&__tag {
|
||
padding: 2rpx 12rpx;
|
||
font-size: 20rpx;
|
||
border-radius: 8rpx;
|
||
|
||
&--warning {
|
||
color: #ff9900;
|
||
background-color: #fff6e8;
|
||
}
|
||
|
||
&--info {
|
||
color: #909399;
|
||
background-color: #f2f3f5;
|
||
}
|
||
|
||
&--danger {
|
||
color: #f56c6c;
|
||
background-color: #fef0f0;
|
||
}
|
||
|
||
&--primary {
|
||
color: #2979ff;
|
||
background-color: #eef4ff;
|
||
}
|
||
|
||
&--success {
|
||
color: #19be6b;
|
||
background-color: #e8f9ef;
|
||
}
|
||
}
|
||
|
||
&__progress {
|
||
flex: 1;
|
||
min-width: 0;
|
||
height: 8rpx;
|
||
border-radius: 999rpx;
|
||
overflow: hidden;
|
||
background-color: #f0f2f5;
|
||
}
|
||
|
||
&__progress-inner {
|
||
height: 100%;
|
||
border-radius: 999rpx;
|
||
transition: width 0.3s;
|
||
}
|
||
}
|
||
|
||
/* 统计面板级失败 */
|
||
.panel-error {
|
||
display: flex;
|
||
flex-direction: column;
|
||
align-items: center;
|
||
margin: 24rpx 24rpx 0;
|
||
padding: 40rpx 24rpx;
|
||
background-color: #ffffff;
|
||
border-radius: 24rpx;
|
||
|
||
&__text {
|
||
font-size: 26rpx;
|
||
color: #909399;
|
||
}
|
||
|
||
&__retry {
|
||
margin-top: 12rpx;
|
||
font-size: 26rpx;
|
||
color: #2979ff;
|
||
}
|
||
}
|
||
|
||
/* ───────── 热销排行 ───────── */
|
||
.rank {
|
||
margin: 24rpx 24rpx 0;
|
||
padding: 28rpx 28rpx 12rpx;
|
||
background-color: #ffffff;
|
||
border-radius: 24rpx;
|
||
box-shadow: 0 8rpx 24rpx rgba(41, 121, 255, 0.06);
|
||
|
||
&__head {
|
||
display: flex;
|
||
align-items: baseline;
|
||
justify-content: space-between;
|
||
}
|
||
|
||
&__title {
|
||
font-size: 30rpx;
|
||
font-weight: 600;
|
||
color: #303133;
|
||
}
|
||
|
||
&__sub {
|
||
font-size: 24rpx;
|
||
color: #909399;
|
||
}
|
||
|
||
&__header {
|
||
display: flex;
|
||
align-items: center;
|
||
padding: 20rpx 0 12rpx;
|
||
border-bottom: 1rpx solid #f0f2f5;
|
||
}
|
||
|
||
&__cell {
|
||
font-size: 22rpx;
|
||
color: #a8abb2;
|
||
}
|
||
|
||
&__item {
|
||
display: flex;
|
||
align-items: center;
|
||
padding: 20rpx 0;
|
||
border-bottom: 1rpx solid #f5f7fa;
|
||
|
||
&:last-child {
|
||
border-bottom: none;
|
||
}
|
||
}
|
||
|
||
&__cell--index {
|
||
width: 72rpx;
|
||
flex-shrink: 0;
|
||
text-align: center;
|
||
}
|
||
|
||
&__cell--name {
|
||
flex: 1;
|
||
min-width: 0;
|
||
display: flex;
|
||
flex-direction: column;
|
||
}
|
||
|
||
&__cell--value {
|
||
width: 100rpx;
|
||
flex-shrink: 0;
|
||
font-size: 26rpx;
|
||
color: #2979ff;
|
||
text-align: right;
|
||
}
|
||
|
||
&__cell--trend {
|
||
width: 120rpx;
|
||
flex-shrink: 0;
|
||
text-align: right;
|
||
}
|
||
|
||
&__badge {
|
||
display: inline-flex;
|
||
align-items: center;
|
||
justify-content: center;
|
||
width: 40rpx;
|
||
height: 40rpx;
|
||
font-size: 22rpx;
|
||
font-weight: 600;
|
||
color: #ffffff;
|
||
background-color: #c0c4cc;
|
||
border-radius: 50%;
|
||
|
||
&--gold {
|
||
background: linear-gradient(135deg, #ffd700 0%, #ffa500 100%);
|
||
}
|
||
|
||
&--silver {
|
||
background: linear-gradient(135deg, #c0c0c0 0%, #a9a9a9 100%);
|
||
}
|
||
|
||
&--bronze {
|
||
background: linear-gradient(135deg, #cd7f32 0%, #a0522d 100%);
|
||
}
|
||
}
|
||
|
||
&__product {
|
||
font-size: 26rpx;
|
||
color: #303133;
|
||
overflow: hidden;
|
||
text-overflow: ellipsis;
|
||
white-space: nowrap;
|
||
}
|
||
|
||
&__sku {
|
||
margin-top: 4rpx;
|
||
font-size: 20rpx;
|
||
color: #a8abb2;
|
||
overflow: hidden;
|
||
text-overflow: ellipsis;
|
||
white-space: nowrap;
|
||
}
|
||
|
||
&__trend {
|
||
font-size: 22rpx;
|
||
|
||
&.is-up {
|
||
color: #19be6b;
|
||
}
|
||
|
||
&.is-down {
|
||
color: #f56c6c;
|
||
}
|
||
}
|
||
|
||
&__state {
|
||
display: flex;
|
||
flex-direction: column;
|
||
align-items: center;
|
||
padding: 56rpx 0;
|
||
font-size: 26rpx;
|
||
color: #a8abb2;
|
||
|
||
&--error {
|
||
color: #f56c6c;
|
||
}
|
||
}
|
||
|
||
&__state-text {
|
||
font-size: 26rpx;
|
||
color: #909399;
|
||
}
|
||
|
||
&__retry {
|
||
margin-top: 12rpx;
|
||
font-size: 26rpx;
|
||
color: #2979ff;
|
||
}
|
||
}
|
||
</style>
|