Dashboard.vue 11.3 KB
<!-- 工作台:今日补货单、时间线、取数端与 DMS 登录、未处理告警、近 7 天、实时动态。
  普通账号(客户)不显示取数端 / DMS 登录状态和实时动态(运行日志)。 -->
<script setup lang="ts">
import { Refresh } from '@element-plus/icons-vue'
import { ElMessage } from 'element-plus'
import { computed, onMounted, onUnmounted, ref } from 'vue'
import { useRouter } from 'vue-router'
import { api } from '@/api'
import EmptyState from '@/components/EmptyState.vue'
import PageHeader from '@/components/PageHeader.vue'
import Panel from '@/components/Panel.vue'
import StatCard from '@/components/StatCard.vue'
import StatusPill from '@/components/StatusPill.vue'
import { fmtAge, fmtMoney, fmtShort, LEVEL_TONE, LOGIN_STATE, STAGE, TRIGGER } from '@/format'
import { session } from '@/session'

const router = useRouter()
const d = ref<any>(null)
const evs = ref<any[]>([])
const alerts = ref<any[]>([])
let timer: any = null

async function load() {
  try {
    const [t, e, a] = await Promise.all([api.get('/api/restock/today'),
      session.isAdmin ? api.get('/api/events?limit=30') : Promise.resolve([]),
      api.get('/api/alerts?status=open&size=5')])
    d.value = t
    evs.value = e.slice().reverse()
    alerts.value = a.rows
  } catch (err: any) { ElMessage.error(err.message) }
}
onMounted(() => { load(); timer = setInterval(load, 8000) })
onUnmounted(() => clearInterval(timer))

const run = computed(() => d.value?.run)
const login = computed(() => {
  const m = d.value?.edge?.dms_login || {}
  const k = Object.keys(m)[0]
  return k ? m[k] : null
})

const weekday = (s: string) => '日一二三四五六'[new Date(s + 'T00:00:00').getDay()]
</script>

<template>
  <div class="page">
    <PageHeader title="工作台" :desc="d ? `今天 ${d.now.slice(0, 10)} · ${d.config.auto_push ? `每天 ${d.config.push_time} 自动推送` : '已关闭每天自动执行'} · ${d.config.cutoff} 截单` : ''">
      <template #actions>
        <el-button :icon="Refresh" @click="load">刷新</el-button>
      </template>
    </PageHeader>

    <template v-if="d">
      <div v-if="!d.enabled" class="notice warn mb-3">库存补货功能还没有开通,到「系统设置 → 功能开通」里打开。</div>
      <div class="grid" :class="session.isAdmin ? 'grid-4' : 'grid-2'">
        <StatCard label="今日补货单" :tone="run ? STAGE[run.status]?.tone : 'info'">
          {{ run ? STAGE[run.status]?.text || run.stage_cn : '未开始' }}
          <template #hint>{{ run ? `${run.doc_no} · ${TRIGGER[run.trigger] || run.trigger}` : (d.config.auto_push ? `${d.config.push_time} 自动推送` : '每天自动执行已关闭') }}</template>
        </StatCard>
        <StatCard label="可订" :value="run ? `${run.summary.order_count} 项 / ${run.summary.order_qty} 件` : '—'"
                  :hint="run ? `约 ${fmtMoney(run.summary.amount)} · 出库 ${run.summary.total} 种(常备 ${run.summary.stock_count})` : `分类表 ${d.category_count} 种 · SO 常备 ${d.so_part_count} 种 · 今天单次 / 批量补货 ${d.single_today} 次`" />
        <StatCard v-if="session.isAdmin" label="门店取数端" :tone="d.edge.online ? 'ok' : 'err'">
          {{ d.edge.online ? '在线' : '离线' }}
          <template #hint>{{ d.edge.name || '未登记设备' }} · {{ d.edge.online ? `心跳 ${fmtAge(d.edge.last_seen_age)}` : d.edge.registered ? `最后在线 ${fmtAge(d.edge.last_seen_age)}` : '还没有连接过' }}</template>
        </StatCard>
        <StatCard v-if="session.isAdmin" label="DMS 登录" :tone="login ? LOGIN_STATE[login.state]?.tone : 'info'">
          {{ login ? LOGIN_STATE[login.state]?.text || login.state : '—' }}
          <template #hint>{{ login ? `今日登录 ${login.today_logins || 0} 次${login.token_left_s ? ' · 登录态剩 ' + (login.token_left_s / 3600).toFixed(1) + ' 小时' : ''}` : '取数端在线后显示' }}</template>
        </StatCard>
      </div>

      <div class="grid main-grid">
        <div class="stack">
          <Panel title="今日补货单" :desc="run ? `${run.doc_no} · ${run.triggered_by || ''}` : '还没有补货单'">
            <template #extra>
              <el-button v-if="run" link type="primary" @click="router.push('/runs/' + run.id)">查看详情</el-button>
            </template>
            <div v-if="run" class="run">
              <div class="run-status">
                <StatusPill :tone="STAGE[run.status]?.tone">{{ STAGE[run.status]?.text || run.stage_cn }}</StatusPill>
                <span v-if="run.order_no" class="sub">采购单 <b class="mono">{{ run.order_no }}</b>(待提交)</span>
                <span v-if="run.message" class="muted small">{{ run.message }}</span>
              </div>
              <div class="figs">
                <div><span class="fig-label">要订</span><span class="fig-value">{{ run.summary.order_count }} 项</span></div>
                <div><span class="fig-label">合计</span><span class="fig-value">{{ run.summary.order_qty }} 件</span></div>
                <div><span class="fig-label">金额</span><span class="fig-value">{{ fmtMoney(run.summary.amount) }}</span></div>
                <div><span class="fig-label">查询失败</span><span class="fig-value" :class="{ err: run.summary.errors.length }">{{ run.summary.errors.length }} 项</span></div>
              </div>
              <div v-if="run.status === 'awaiting_confirm'" class="notice warn">待计划员确认 · <a :href="run.link" target="_blank">打开单据页</a></div>
              <div v-else-if="run.status === 'awaiting_submit'" class="notice warn">已建单 {{ run.order_no }},待确认提交 · <a :href="run.link" target="_blank">打开单据页</a></div>
            </div>
            <EmptyState v-else title="今天还没有补货单" :desc="d.config.auto_push ? `${d.config.push_time} 自动推送` : '每天自动执行已关闭'">
              <el-button type="primary" @click="router.push('/runs')">去补货单</el-button>
            </EmptyState>
          </Panel>

          <Panel title="今日时间线" desc="按门店本地时间">
            <ol class="timeline">
              <li v-for="s in d.timeline" :key="s.kind + s.time" :class="{ passed: s.passed }">
                <span class="tl-time num">{{ s.time }}</span>
                <span class="tl-dot" />
                <span class="tl-title">{{ s.title }}</span>
              </li>
            </ol>
          </Panel>

          <Panel title="近 7 天">
            <div class="week">
              <div v-for="w in d.week" :key="w.date" class="day" :class="w.status ? STAGE[w.status]?.tone : 'none'">
                <div class="day-date num">{{ w.date.slice(5) }}</div>
                <div class="day-week muted small">周{{ weekday(w.date) }}</div>
                <div class="day-stage">{{ w.stage_cn || '—' }}</div>
              </div>
            </div>
          </Panel>
        </div>

        <div class="stack">
          <Panel title="未处理告警">
            <template #extra><el-button link type="primary" @click="router.push('/alerts')">全部</el-button></template>
            <div v-if="alerts.length" class="alerts">
              <div v-for="a in alerts" :key="a.id" class="alert">
                <StatusPill :tone="a.severity === 'warn' ? 'warn' : a.severity === 'info' ? 'info' : 'err'">{{ a.kind_cn || a.kind }}</StatusPill>
                <div class="alert-title">{{ a.title }}</div>
                <div class="muted small">{{ fmtShort(a.last_at) }} · 发生 {{ a.occur_count }} 次</div>
              </div>
            </div>
            <EmptyState v-else title="没有未处理的告警" />
          </Panel>
          <Panel v-if="session.isAdmin" title="实时动态" desc="系统、取数端、企微、智能体的运行记录">
            <div class="events">
              <div v-for="e in evs" :key="e.id" class="ev">
                <span class="ev-dot" :class="LEVEL_TONE[e.level] || 'info'" />
                <div class="ev-body">
                  <div class="ev-msg">{{ e.msg }}</div>
                  <div class="muted small">{{ fmtShort(e.ts) }} · {{ e.source }}</div>
                </div>
              </div>
              <EmptyState v-if="!evs.length" title="暂无动态" />
            </div>
          </Panel>
        </div>
      </div>
    </template>
    <el-skeleton v-else :rows="8" animated />
  </div>
</template>

<style scoped>
.main-grid { grid-template-columns: minmax(0, 1.6fr) minmax(0, 1fr); margin-top: 16px; align-items: start; }
@media (max-width: 1100px) { .main-grid { grid-template-columns: minmax(0, 1fr); } }
.run { display: flex; flex-direction: column; gap: 14px; }
.run-status { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
.run .figs .fig-value { font-size: 18px; }
.timeline { list-style: none; margin: 0; padding: 0; display: flex; gap: 0; flex-wrap: wrap; }
.timeline li { flex: 1; min-width: 110px; display: grid; grid-template-rows: auto 18px auto; justify-items: center; gap: 4px; position: relative; color: var(--c-text-2); }
.timeline li::before { content: ''; position: absolute; top: calc(1.55em + 12px); left: 0; right: 0; height: 2px; background: var(--c-border); z-index: 0; }
.timeline li:first-child::before { left: 50%; }
.timeline li:last-child::before { right: 50%; }
.timeline li.passed::before { background: var(--c-primary-line); }
.tl-time { font-weight: 600; font-size: 13.5px; }
.tl-dot { width: 10px; height: 10px; border-radius: 50%; background: var(--c-surface); border: 2px solid var(--c-border-strong); z-index: 1; align-self: center; }
.passed .tl-dot { background: var(--c-primary); border-color: var(--c-primary); }
.tl-title { font-size: 12.5px; text-align: center; }
.passed { color: var(--c-text-3); }
.week { display: grid; grid-template-columns: repeat(7, minmax(0, 1fr)); gap: 8px; }
.day { border: 1px solid var(--c-border); border-radius: var(--radius-sm); padding: 10px 8px; text-align: center; background: var(--c-surface-2); }
.day-date { font-weight: 600; font-size: 13px; }
.day-stage { margin-top: 6px; font-size: 12px; font-weight: 500; }
.day.ok { background: var(--c-ok-soft); border-color: transparent; color: var(--c-ok); }
.day.warn { background: var(--c-warn-soft); border-color: transparent; color: var(--c-warn); }
.day.err { background: var(--c-err-soft); border-color: transparent; color: var(--c-err); }
.day.primary { background: var(--c-primary-soft); border-color: transparent; color: var(--c-primary); }
.day.info { color: var(--c-text-2); }
.day.none .day-stage { color: var(--c-text-3); }
@media (max-width: 720px) { .week { grid-template-columns: repeat(4, minmax(0, 1fr)); } }
.alerts { display: flex; flex-direction: column; }
.alert { padding: 10px 0; border-bottom: 1px solid var(--c-border); display: grid; gap: 4px; }
.alert:last-child { border-bottom: 0; }
.alert-title { font-weight: 600; }
.events { max-height: 460px; overflow-y: auto; display: flex; flex-direction: column; }
.ev { display: flex; gap: 10px; padding: 8px 0; border-bottom: 1px dashed var(--c-border); }
.ev:last-child { border-bottom: 0; }
.ev-dot { width: 7px; height: 7px; border-radius: 50%; margin-top: 7px; flex-shrink: 0; background: var(--c-info); }
.ev-dot.ok { background: var(--c-ok); } .ev-dot.warn { background: var(--c-warn); } .ev-dot.err { background: var(--c-err); }
.ev-body { min-width: 0; }
.ev-msg { font-size: 13px; word-break: break-all; }
</style>