HomeModuleKpiCard.vue 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. <template>
  2. <section class="dash-card home-kpi-card" @click="onCardClick">
  3. <header class="dash-card__head">
  4. <span class="dash-card__code">{{ moduleCode }}</span>
  5. <span class="dash-card__title">{{ title }}</span>
  6. <el-icon v-if="icon" class="dash-card__module-icon" :size="22">
  7. <component :is="icon" />
  8. </el-icon>
  9. </header>
  10. <div class="dash-card__body s4-body">
  11. <div
  12. v-if="cells.length > 0"
  13. class="s4-kpi-grid"
  14. :class="{ 's4-kpi-grid--dense': cells.length > 4 }"
  15. >
  16. <div v-for="cell in cells" :key="cell.key" class="s4-kpi-cell">
  17. <el-tooltip v-if="cell.formulaText" placement="top" :show-after="200" :content="cell.formulaText">
  18. <div class="s4-kpi-label s4-kpi-label--formula">{{ cell.label }}</div>
  19. </el-tooltip>
  20. <div v-else class="s4-kpi-label">{{ cell.label }}</div>
  21. <div class="s4-kpi-body">
  22. <div class="s4-kpi-main">
  23. <div class="s4-kpi-value-row">
  24. <span class="s4-kpi-value">{{ cell.value }}</span>
  25. <span v-if="cell.unit" class="s4-kpi-unit">{{ cell.unit }}</span>
  26. </div>
  27. <div v-if="cell.targetDisplay" class="s4-kpi-target">目标: {{ cell.targetDisplay }}</div>
  28. </div>
  29. <span
  30. class="s4-kpi-trend"
  31. :class="{
  32. 's4-kpi-trend--good': cell.trendTone === 'good',
  33. 's4-kpi-trend--warn': cell.trendTone === 'warn',
  34. 's4-kpi-trend--bad': cell.trendTone === 'bad',
  35. }"
  36. >
  37. <span class="s4-kpi-trend__arrow" aria-hidden="true">{{
  38. cell.trendArrow === 'up' ? '▲' : cell.trendArrow === 'down' ? '▼' : '—'
  39. }}</span>
  40. {{ cell.trend }}
  41. </span>
  42. </div>
  43. </div>
  44. </div>
  45. <div v-else class="home-kpi-card__empty">
  46. <span v-if="loading">加载中…</span>
  47. <span v-else>暂无指标配置,点击卡片进入模块后到“运营指标建模”配置 L1 指标</span>
  48. </div>
  49. </div>
  50. </section>
  51. </template>
  52. <script setup>
  53. import { ref, computed, onMounted, onUnmounted, watch } from 'vue'
  54. import { fetchHomeGrid } from '../../aidop/api/kanbanData'
  55. import { AIDOP_LAYOUT_SAVED, AIDOP_S4_LAYOUT_SAVED } from '../../aidop/kanban/utils/s4LayoutEvents'
  56. const props = defineProps({
  57. moduleCode: { type: String, required: true },
  58. title: { type: String, default: '' },
  59. icon: { type: [Object, Function], default: null },
  60. factoryId: { type: Number, default: 1 },
  61. /** 透传 smartOps base 查询参数(日期/产品/订单号/产线) */
  62. extra: { type: Object, default: () => ({}) },
  63. })
  64. const emit = defineEmits(['click'])
  65. const apiItems = ref([])
  66. const loading = ref(false)
  67. function fmtValue(v, unit) {
  68. if (v == null || v === '') return '—'
  69. const n = Number(v)
  70. if (Number.isNaN(n)) return String(v)
  71. if (unit === '%') return `${Math.round(n * 10) / 10}`
  72. if (unit === '天') return `${Math.round(n * 10) / 10}`
  73. if (Math.abs(n - Math.round(n)) < 1e-9) return `${Math.round(n)}`
  74. return `${Math.round(n * 100) / 100}`
  75. }
  76. function fmtTargetDisplay(v, unit) {
  77. if (v == null || v === '') return ''
  78. const n = Number(v)
  79. if (Number.isNaN(n)) return String(v)
  80. const num =
  81. unit === '%'
  82. ? `${Math.round(n * 10) / 10}`
  83. : unit === '天'
  84. ? `${Math.round(n * 10) / 10}`
  85. : Math.abs(n - Math.round(n)) < 1e-9
  86. ? `${Math.round(n)}`
  87. : `${Math.round(n * 100) / 100}`
  88. if (unit === '%') return `${num}%`
  89. if (unit === '天') return `${num} 天`
  90. if (unit) return `${num} ${unit}`
  91. return `${num}`
  92. }
  93. function mapItem(item) {
  94. const arrow = item.gapArrow
  95. const trendArrow = arrow === 'up' ? 'up' : arrow === 'down' ? 'down' : 'flat'
  96. const lv = String(item.achievementLevel || 'green')
  97. const trendTone = lv === 'red' ? 'bad' : lv === 'yellow' ? 'warn' : 'good'
  98. const unit = item.unit ?? ''
  99. const ft =
  100. item.formulaText != null && String(item.formulaText).trim()
  101. ? String(item.formulaText).trim()
  102. : ''
  103. return {
  104. key: item.rowId || item.metricCode,
  105. label: item.displayName || item.metricCode,
  106. formulaText: ft,
  107. value: fmtValue(item.currentValue, unit),
  108. unit,
  109. targetDisplay: fmtTargetDisplay(item.targetValue, unit),
  110. trend: item.gapLabel || '—',
  111. trendArrow,
  112. trendTone,
  113. }
  114. }
  115. const cells = computed(() =>
  116. Array.isArray(apiItems.value) ? apiItems.value.map(mapItem) : []
  117. )
  118. async function load() {
  119. const mc = String(props.moduleCode || '').toUpperCase()
  120. if (!mc) return
  121. loading.value = true
  122. try {
  123. const grid = await fetchHomeGrid(mc, props.factoryId, props.extra || {})
  124. apiItems.value = grid?.items && grid.items.length > 0 ? grid.items : []
  125. } catch {
  126. apiItems.value = []
  127. } finally {
  128. loading.value = false
  129. }
  130. }
  131. function onLayoutSaved(e) {
  132. const detailMc = String(e?.detail?.moduleCode || '').toUpperCase()
  133. if (!detailMc || detailMc === String(props.moduleCode || '').toUpperCase()) load()
  134. }
  135. function onS4LayoutSavedLegacy() {
  136. if (String(props.moduleCode || '').toUpperCase() === 'S4') load()
  137. }
  138. function onCardClick(e) {
  139. emit('click', e)
  140. }
  141. watch(
  142. () => [props.moduleCode, props.factoryId, props.extra],
  143. () => {
  144. load()
  145. },
  146. { deep: true }
  147. )
  148. onMounted(() => {
  149. load()
  150. if (typeof window !== 'undefined') {
  151. window.addEventListener(AIDOP_LAYOUT_SAVED, onLayoutSaved)
  152. window.addEventListener(AIDOP_S4_LAYOUT_SAVED, onS4LayoutSavedLegacy)
  153. }
  154. })
  155. onUnmounted(() => {
  156. if (typeof window !== 'undefined') {
  157. window.removeEventListener(AIDOP_LAYOUT_SAVED, onLayoutSaved)
  158. window.removeEventListener(AIDOP_S4_LAYOUT_SAVED, onS4LayoutSavedLegacy)
  159. }
  160. })
  161. defineExpose({ reload: load })
  162. </script>
  163. <style scoped>
  164. .dash-card {
  165. display: flex;
  166. flex-direction: column;
  167. background: rgba(30, 41, 59, 0.55);
  168. border: 1px solid #334155;
  169. border-radius: 8px;
  170. padding: 8px 10px;
  171. cursor: pointer;
  172. transition: border-color 0.2s, box-shadow 0.2s;
  173. min-height: 0;
  174. overflow: hidden;
  175. }
  176. .dash-card:hover {
  177. border-color: #64748b;
  178. box-shadow: 0 6px 24px rgba(0, 0, 0, 0.35);
  179. }
  180. .dash-card__head {
  181. flex-shrink: 0;
  182. display: flex;
  183. align-items: center;
  184. flex-wrap: nowrap;
  185. gap: 6px;
  186. width: 100%;
  187. margin-bottom: 6px;
  188. padding-bottom: 6px;
  189. border-bottom: 1px solid #334155;
  190. }
  191. .dash-card__module-icon {
  192. margin-left: auto;
  193. flex-shrink: 0;
  194. color: #5eead4;
  195. filter: drop-shadow(0 0 8px rgba(94, 234, 212, 0.45));
  196. }
  197. .dash-card__code {
  198. font-size: 13px;
  199. font-weight: 800;
  200. color: #60a5fa;
  201. letter-spacing: 0.5px;
  202. }
  203. .dash-card__title {
  204. font-size: 13px;
  205. font-weight: 700;
  206. color: #e2e8f0;
  207. }
  208. .dash-card__body {
  209. flex: 1;
  210. min-height: 0;
  211. display: flex;
  212. flex-direction: column;
  213. gap: 6px;
  214. overflow: hidden;
  215. }
  216. .s4-body {
  217. flex: 1;
  218. min-height: 0;
  219. overflow: hidden;
  220. padding: 2px 10px 4px;
  221. box-sizing: border-box;
  222. }
  223. .s4-kpi-grid {
  224. flex: 1;
  225. min-height: 0;
  226. display: grid;
  227. grid-template-columns: 1fr 1fr;
  228. grid-template-rows: 1fr 1fr;
  229. gap: 8px 12px;
  230. align-content: stretch;
  231. }
  232. .s4-kpi-grid--dense {
  233. grid-template-columns: repeat(4, minmax(0, 1fr));
  234. grid-template-rows: repeat(2, minmax(0, 1fr));
  235. }
  236. .s4-kpi-cell {
  237. display: flex;
  238. flex-direction: column;
  239. align-items: flex-start;
  240. justify-content: center;
  241. gap: 6px;
  242. padding: 6px 8px;
  243. min-height: 0;
  244. background: rgba(0, 0, 0, 0.22);
  245. border-radius: 6px;
  246. border: 1px solid rgba(51, 65, 85, 0.35);
  247. }
  248. .s4-kpi-label {
  249. font-size: 11px;
  250. color: #94a3b8;
  251. line-height: 1.3;
  252. font-weight: 500;
  253. }
  254. .s4-kpi-label--formula {
  255. cursor: help;
  256. border-bottom: 1px dashed rgba(148, 163, 184, 0.55);
  257. }
  258. .s4-kpi-body {
  259. display: flex;
  260. align-items: center;
  261. justify-content: space-between;
  262. gap: 8px;
  263. width: 100%;
  264. min-width: 0;
  265. }
  266. .s4-kpi-main {
  267. flex: 1;
  268. min-width: 0;
  269. display: flex;
  270. flex-direction: column;
  271. align-items: flex-start;
  272. gap: 3px;
  273. }
  274. .s4-kpi-value-row {
  275. display: flex;
  276. flex-wrap: wrap;
  277. align-items: baseline;
  278. gap: 4px 6px;
  279. }
  280. .s4-kpi-target {
  281. font-size: 11px;
  282. font-weight: 500;
  283. color: #94a3b8;
  284. line-height: 1.3;
  285. }
  286. .s4-kpi-value {
  287. font-size: clamp(18px, 3.8vw, 24px);
  288. font-weight: 800;
  289. color: #ffffff;
  290. line-height: 1.05;
  291. letter-spacing: 0.02em;
  292. }
  293. .s4-kpi-unit {
  294. font-size: 11px;
  295. color: #e2e8f0;
  296. font-weight: 500;
  297. }
  298. .s4-kpi-trend {
  299. display: inline-flex;
  300. align-items: center;
  301. gap: 2px;
  302. font-size: 11px;
  303. font-weight: 700;
  304. flex-shrink: 0;
  305. align-self: center;
  306. }
  307. .s4-kpi-trend__arrow {
  308. font-size: 9px;
  309. line-height: 1;
  310. transform: translateY(0.5px);
  311. }
  312. .s4-kpi-trend--good {
  313. color: #4ade80;
  314. }
  315. .s4-kpi-trend--warn {
  316. color: #facc15;
  317. }
  318. .s4-kpi-trend--bad {
  319. color: #fb923c;
  320. }
  321. .home-kpi-card__empty {
  322. flex: 1;
  323. display: flex;
  324. align-items: center;
  325. justify-content: center;
  326. color: #94a3b8;
  327. font-size: 11px;
  328. padding: 8px;
  329. text-align: center;
  330. line-height: 1.4;
  331. }
  332. </style>