PersonnelTrend.vue 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. <template>
  2. <div class="panel personnel-trend">
  3. <div class="border-beam"></div>
  4. <div class="panel-header">
  5. <div class="panel-header-icon">👥</div>
  6. <span class="panel-title">实验室进入人数统计及走势</span>
  7. </div>
  8. <div class="panel-body">
  9. <!-- Flip counter digits -->
  10. <div class="flip-counters">
  11. <div class="flip-counter">
  12. <div class="fc-label">今日进入总人数</div>
  13. <div class="fc-digits">
  14. <div
  15. v-for="(digit, idx) in totalDigits"
  16. :key="'total-' + idx"
  17. class="flip-digit"
  18. >{{ digit }}</div>
  19. </div>
  20. </div>
  21. <!-- 等待后台老师数据相关-开始 -->
  22. <!--<div class="flip-counter">-->
  23. <!--<div class="fc-label">当前在场实验人数</div>-->
  24. <!--<div class="fc-digits">-->
  25. <!--<div-->
  26. <!--v-for="(digit, idx) in presentDigits"-->
  27. <!--:key="'present-' + idx"-->
  28. <!--class="flip-digit"-->
  29. <!--&gt;{{ digit }}</div>-->
  30. <!--</div>-->
  31. <!--</div>-->
  32. <!-- 等待后台老师数据相关-结束 -->
  33. </div>
  34. <!-- Line chart -->
  35. <div class="chart-wrap">
  36. <div ref="chart" class="chart"></div>
  37. </div>
  38. </div>
  39. </div>
  40. </template>
  41. <script>
  42. import * as echarts from 'echarts'
  43. import { getPassOutTrend } from '@/api/screen'
  44. export default {
  45. name: 'PersonnelTrend',
  46. data() {
  47. return {
  48. totalEntry: 0,
  49. currentPresent: 0,
  50. displayTotal: 0,
  51. displayPresent: 0,
  52. chart: null,
  53. animTimerTotal: null,
  54. animTimerPresent: null,
  55. pollTimer: null
  56. }
  57. },
  58. computed: {
  59. totalDigits() {
  60. return String(this.displayTotal).padStart(4, '0').split('')
  61. },
  62. presentDigits() {
  63. return String(this.displayPresent).padStart(4, '0').split('')
  64. }
  65. },
  66. mounted() {
  67. this.fetchData()
  68. this.pollTimer = setInterval(this.fetchData, 5 * 60 * 1000)
  69. },
  70. beforeDestroy() {
  71. if (this.pollTimer) clearInterval(this.pollTimer)
  72. if (this.animTimerTotal) cancelAnimationFrame(this.animTimerTotal)
  73. if (this.animTimerPresent) cancelAnimationFrame(this.animTimerPresent)
  74. if (this.chart) {
  75. this.chart.dispose()
  76. this.chart = null
  77. }
  78. },
  79. methods: {
  80. async fetchData() {
  81. try {
  82. const res = await getPassOutTrend()
  83. if (res.code === 200) {
  84. const d = res.data
  85. this.totalEntry = d.todayEntryCount
  86. this.currentPresent = d.currentLabUserCount
  87. const hours = Array.from({ length: 24 }, (_, i) => i)
  88. const entryMap = {}
  89. const presentMap = {}
  90. ;(d.entryTrend || []).forEach(item => { entryMap[item.hour] = item.count })
  91. ;(d.labUserTrend || []).forEach(item => { presentMap[item.hour] = item.count })
  92. const trend = {
  93. times: hours.map(h => `${String(h).padStart(2, '0')}:00`),
  94. entry: hours.map(h => entryMap[h] || 0),
  95. present: hours.map(h => presentMap[h] || 0)
  96. }
  97. this.$nextTick(() => {
  98. setTimeout(() => this.animateCount('total', 0, this.totalEntry, 2000), 600)
  99. setTimeout(() => this.animateCount('present', 0, this.currentPresent, 1500), 900)
  100. if (this.chart) {
  101. this.chart.dispose()
  102. this.chart = null
  103. }
  104. this.initChart(trend)
  105. })
  106. }
  107. } catch (e) {
  108. // 错误已由拦截器处理
  109. }
  110. },
  111. /** Animate count from `from` to `to` over `duration` ms */
  112. animateCount(type, from, to, duration) {
  113. const startTime = Date.now()
  114. const tick = () => {
  115. const elapsed = Date.now() - startTime
  116. const progress = Math.min(elapsed / duration, 1)
  117. const current = Math.round(from + (to - from) * progress)
  118. if (type === 'total') {
  119. this.displayTotal = current
  120. } else {
  121. this.displayPresent = current
  122. }
  123. if (progress < 1) {
  124. if (type === 'total') {
  125. this.animTimerTotal = requestAnimationFrame(tick)
  126. } else {
  127. this.animTimerPresent = requestAnimationFrame(tick)
  128. }
  129. }
  130. }
  131. tick()
  132. },
  133. /** 初始化面积折线图 - 展示24小时进入/在场人数走势 */
  134. initChart(trend) {
  135. this.chart = echarts.init(this.$refs.chart, null, {
  136. renderer: 'canvas',
  137. devicePixelRatio: 2
  138. })
  139. const TOOLTIP_CFG = {
  140. backgroundColor: 'rgba(3,14,42,0.92)',
  141. borderColor: 'rgba(30,144,255,0.3)',
  142. textStyle: { color: '#a8cce8', fontSize: 28 }
  143. }
  144. const option = {
  145. backgroundColor: 'transparent',
  146. tooltip: {
  147. trigger: 'axis',
  148. ...TOOLTIP_CFG
  149. },
  150. legend: {
  151. data: ['进入人数', '在场人数'],
  152. top: 0,
  153. right: 0,
  154. textStyle: { color: '#a8cce8', fontSize: 25 },
  155. icon: 'circle',
  156. itemWidth: 20,
  157. itemHeight: 20
  158. },
  159. grid: {
  160. left: 28,
  161. right: 8,
  162. top: 45,
  163. bottom: 50,
  164. containLabel: true
  165. },
  166. xAxis: {
  167. type: 'category',
  168. data: trend.times,
  169. axisLabel: { color: '#5890b8', fontSize: 22 },
  170. axisLine: { lineStyle: { color: 'rgba(30,144,255,0.2)' } },
  171. axisTick: { show: false }
  172. },
  173. yAxis: {
  174. type: 'value',
  175. axisLabel: { color: '#5890b8', fontSize: 22 },
  176. axisLine: { show: false },
  177. splitLine: { lineStyle: { color: 'rgba(30,144,255,0.1)' } }
  178. },
  179. series: [
  180. {
  181. name: '进入人数',
  182. type: 'line',
  183. data: trend.entry,
  184. smooth: true,
  185. symbol: 'circle',
  186. symbolSize: 12,
  187. lineStyle: { color: '#1e90ff', width: 5 },
  188. itemStyle: { color: '#1e90ff' },
  189. areaStyle: {
  190. color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
  191. { offset: 0, color: 'rgba(30,144,255,0.32)' },
  192. { offset: 1, color: 'rgba(30,144,255,0.02)' }
  193. ])
  194. }
  195. },
  196. // 等待后台老师数据相关-开始
  197. // {
  198. // name: '在场人数',
  199. // type: 'line',
  200. // data: trend.present,
  201. // smooth: true,
  202. // symbol: 'circle',
  203. // symbolSize: 12,
  204. // lineStyle: { color: '#ffd740', width: 5 },
  205. // itemStyle: { color: '#ffd740' },
  206. // areaStyle: {
  207. // color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
  208. // { offset: 0, color: 'rgba(255,215,64,0.22)' },
  209. // { offset: 1, color: 'rgba(255,215,64,0.01)' }
  210. // ])
  211. // }
  212. // }
  213. // 等待后台老师数据相关-结束
  214. ]
  215. }
  216. this.chart.setOption(option)
  217. }
  218. }
  219. }
  220. </script>
  221. <style lang="scss" scoped>
  222. .personnel-trend {
  223. display: flex;
  224. flex-direction: column;
  225. }
  226. .panel-body {
  227. flex: 1;
  228. min-height: 0;
  229. display: flex;
  230. flex-direction: column;
  231. padding: 20px 25px;
  232. }
  233. // Flip counters section
  234. .flip-counters {
  235. display: flex;
  236. gap: 25px;
  237. }
  238. .flip-counter {
  239. flex: 1;
  240. text-align: center;
  241. padding: 18px 15px;
  242. border-radius: 10px;
  243. background: $bg-card;
  244. border: 1px solid $border;
  245. .fc-label {
  246. font-size: 25px;
  247. color: $text-dim;
  248. margin-bottom: 12px;
  249. letter-spacing: 2px;
  250. }
  251. .fc-digits {
  252. display: flex;
  253. gap: 8px;
  254. justify-content: center;
  255. }
  256. }
  257. .flip-digit {
  258. width: 50px;
  259. height: 75px;
  260. border-radius: 8px;
  261. background: linear-gradient(180deg, rgba(4, 18, 55, 0.9), rgba(2, 10, 32, 0.95));
  262. border: 1px solid rgba(30, 144, 255, 0.3);
  263. display: flex;
  264. align-items: center;
  265. justify-content: center;
  266. font-size: 45px;
  267. font-weight: 700;
  268. color: $gold;
  269. font-variant-numeric: tabular-nums;
  270. position: relative;
  271. overflow: hidden;
  272. &::after {
  273. content: '';
  274. position: absolute;
  275. left: 0;
  276. right: 0;
  277. top: 50%;
  278. height: 2px;
  279. background: rgba(0, 0, 0, 0.4);
  280. }
  281. }
  282. // Chart area
  283. .chart-wrap {
  284. flex: 1;
  285. min-height: 0;
  286. margin-top: 12px;
  287. .chart {
  288. width: 100%;
  289. height: 100%;
  290. }
  291. }
  292. </style>