HomeModuleKpiCard.vue 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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 home-kpi-card__body">
  11. <component
  12. :is="styleDef.component"
  13. :module-code="moduleCode"
  14. :cells="cells"
  15. :loading="loading"
  16. />
  17. </div>
  18. </section>
  19. </template>
  20. <script setup>
  21. import { ref, computed, onMounted, onUnmounted, watch } from 'vue'
  22. import { fetchHomeGrid } from '../../aidop/api/kanbanData'
  23. import { AIDOP_LAYOUT_SAVED, AIDOP_S4_LAYOUT_SAVED } from '../../aidop/kanban/utils/s4LayoutEvents'
  24. import { getStyleDef, DEFAULT_STYLE_CODE } from './homeCardStyles'
  25. const props = defineProps({
  26. moduleCode: { type: String, required: true },
  27. title: { type: String, default: '' },
  28. icon: { type: [Object, Function], default: null },
  29. factoryId: { type: Number, default: 1 },
  30. /** 透传 smartOps base 查询参数(日期/产品/订单号/产线) */
  31. extra: { type: Object, default: () => ({}) },
  32. })
  33. const emit = defineEmits(['click'])
  34. const apiItems = ref([])
  35. const layoutPattern = ref(DEFAULT_STYLE_CODE)
  36. const loading = ref(false)
  37. function fmtValue(v, unit) {
  38. if (v == null || v === '') return '—'
  39. const n = Number(v)
  40. if (Number.isNaN(n)) return String(v)
  41. if (unit === '%') return `${Math.round(n * 10) / 10}`
  42. if (unit === '天') return `${Math.round(n * 10) / 10}`
  43. if (Math.abs(n - Math.round(n)) < 1e-9) return `${Math.round(n)}`
  44. return `${Math.round(n * 100) / 100}`
  45. }
  46. function fmtTargetDisplay(v, unit) {
  47. if (v == null || v === '') return ''
  48. const n = Number(v)
  49. if (Number.isNaN(n)) return String(v)
  50. const num =
  51. unit === '%'
  52. ? `${Math.round(n * 10) / 10}`
  53. : unit === '天'
  54. ? `${Math.round(n * 10) / 10}`
  55. : Math.abs(n - Math.round(n)) < 1e-9
  56. ? `${Math.round(n)}`
  57. : `${Math.round(n * 100) / 100}`
  58. if (unit === '%') return `${num}%`
  59. if (unit === '天') return `${num} 天`
  60. if (unit) return `${num} ${unit}`
  61. return `${num}`
  62. }
  63. function mapItem(item) {
  64. const arrow = item.gapArrow
  65. const trendArrow = arrow === 'up' ? 'up' : arrow === 'down' ? 'down' : 'flat'
  66. const lv = String(item.achievementLevel || 'green')
  67. const trendTone = lv === 'red' ? 'bad' : lv === 'yellow' ? 'warn' : 'good'
  68. const unit = item.unit ?? ''
  69. const ft =
  70. item.formulaText != null && String(item.formulaText).trim()
  71. ? String(item.formulaText).trim()
  72. : ''
  73. return {
  74. key: item.rowId || item.metricCode,
  75. label: item.displayName || item.metricCode,
  76. formulaText: ft,
  77. value: fmtValue(item.currentValue, unit),
  78. unit,
  79. targetDisplay: fmtTargetDisplay(item.targetValue, unit),
  80. trend: item.gapLabel || '—',
  81. trendArrow,
  82. trendTone,
  83. }
  84. }
  85. const cells = computed(() =>
  86. Array.isArray(apiItems.value) ? apiItems.value.map(mapItem) : []
  87. )
  88. const styleDef = computed(() => getStyleDef(layoutPattern.value))
  89. async function load() {
  90. const mc = String(props.moduleCode || '').toUpperCase()
  91. if (!mc) return
  92. loading.value = true
  93. try {
  94. const grid = await fetchHomeGrid(mc, props.factoryId, props.extra || {})
  95. apiItems.value = grid?.items && grid.items.length > 0 ? grid.items : []
  96. layoutPattern.value = grid?.layoutPattern || DEFAULT_STYLE_CODE
  97. } catch {
  98. apiItems.value = []
  99. layoutPattern.value = DEFAULT_STYLE_CODE
  100. } finally {
  101. loading.value = false
  102. }
  103. }
  104. function onLayoutSaved(e) {
  105. const detailMc = String(e?.detail?.moduleCode || '').toUpperCase()
  106. if (!detailMc || detailMc === String(props.moduleCode || '').toUpperCase()) load()
  107. }
  108. function onS4LayoutSavedLegacy() {
  109. if (String(props.moduleCode || '').toUpperCase() === 'S4') load()
  110. }
  111. function onCardClick(e) {
  112. emit('click', e)
  113. }
  114. watch(
  115. () => [props.moduleCode, props.factoryId, props.extra],
  116. () => {
  117. load()
  118. },
  119. { deep: true }
  120. )
  121. onMounted(() => {
  122. load()
  123. if (typeof window !== 'undefined') {
  124. window.addEventListener(AIDOP_LAYOUT_SAVED, onLayoutSaved)
  125. window.addEventListener(AIDOP_S4_LAYOUT_SAVED, onS4LayoutSavedLegacy)
  126. }
  127. })
  128. onUnmounted(() => {
  129. if (typeof window !== 'undefined') {
  130. window.removeEventListener(AIDOP_LAYOUT_SAVED, onLayoutSaved)
  131. window.removeEventListener(AIDOP_S4_LAYOUT_SAVED, onS4LayoutSavedLegacy)
  132. }
  133. })
  134. defineExpose({ reload: load })
  135. </script>
  136. <style scoped>
  137. .dash-card {
  138. display: flex;
  139. flex-direction: column;
  140. background: rgba(30, 41, 59, 0.55);
  141. border: 1px solid #334155;
  142. border-radius: 8px;
  143. padding: 8px 10px;
  144. cursor: pointer;
  145. transition: border-color 0.2s, box-shadow 0.2s;
  146. min-height: 0;
  147. overflow: hidden;
  148. }
  149. .dash-card:hover {
  150. border-color: #64748b;
  151. box-shadow: 0 6px 24px rgba(0, 0, 0, 0.35);
  152. }
  153. .dash-card__head {
  154. flex-shrink: 0;
  155. display: flex;
  156. align-items: center;
  157. flex-wrap: nowrap;
  158. gap: 6px;
  159. width: 100%;
  160. margin-bottom: 6px;
  161. padding-bottom: 6px;
  162. border-bottom: 1px solid #334155;
  163. }
  164. .dash-card__module-icon {
  165. margin-left: auto;
  166. flex-shrink: 0;
  167. color: #5eead4;
  168. filter: drop-shadow(0 0 8px rgba(94, 234, 212, 0.45));
  169. }
  170. .dash-card__code {
  171. font-size: 13px;
  172. font-weight: 800;
  173. color: #60a5fa;
  174. letter-spacing: 0.5px;
  175. }
  176. .dash-card__title {
  177. font-size: 13px;
  178. font-weight: 700;
  179. color: #e2e8f0;
  180. }
  181. .dash-card__body {
  182. flex: 1;
  183. min-height: 0;
  184. display: flex;
  185. flex-direction: column;
  186. overflow: hidden;
  187. }
  188. .home-kpi-card__body {
  189. padding: 2px 10px 4px;
  190. box-sizing: border-box;
  191. }
  192. </style>