Analysis.vue 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <script setup lang="ts">
  2. import PanelGroup from './components/PanelGroup.vue'
  3. import { ElRow, ElCol, ElCard, ElSkeleton } from 'element-plus'
  4. import { Echart } from '@/components/Echart'
  5. import { pieOptions, barOptions, lineOptions } from './echarts-data'
  6. import { ref, reactive } from 'vue'
  7. import {
  8. getUserAccessSourceApi,
  9. getWeeklyUserActivityApi,
  10. getMonthlySalesApi
  11. } from '@/api/dashboard/analysis'
  12. import { set } from 'lodash-es'
  13. import { EChartsOption } from 'echarts'
  14. import { useI18n } from '@/hooks/web/useI18n'
  15. const { t } = useI18n()
  16. const loading = ref(true)
  17. const pieOptionsData = reactive<EChartsOption>(pieOptions) as EChartsOption
  18. // 用户来源
  19. const getUserAccessSource = async () => {
  20. const res = await getUserAccessSourceApi().catch(() => {})
  21. if (res) {
  22. set(
  23. pieOptionsData,
  24. 'legend.data',
  25. res.data.map((v) => t(v.name))
  26. )
  27. set(pieOptionsData, 'series.data', res.data)
  28. }
  29. }
  30. const barOptionsData = reactive<EChartsOption>(barOptions) as EChartsOption
  31. // 周活跃量
  32. const getWeeklyUserActivity = async () => {
  33. const res = await getWeeklyUserActivityApi().catch(() => {})
  34. if (res) {
  35. set(
  36. barOptionsData,
  37. 'xAxis.data',
  38. res.data.map((v) => t(v.name))
  39. )
  40. set(barOptionsData, 'series', [
  41. {
  42. name: t('analysis.activeQuantity'),
  43. data: res.data.map((v) => v.value),
  44. type: 'bar'
  45. }
  46. ])
  47. }
  48. }
  49. const lineOptionsData = reactive<EChartsOption>(lineOptions) as EChartsOption
  50. // 每月销售总额
  51. const getMonthlySales = async () => {
  52. const res = await getMonthlySalesApi().catch(() => {})
  53. if (res) {
  54. set(
  55. lineOptionsData,
  56. 'xAxis.data',
  57. res.data.map((v) => t(v.name))
  58. )
  59. set(lineOptionsData, 'series', [
  60. {
  61. name: t('analysis.estimate'),
  62. smooth: true,
  63. type: 'line',
  64. data: res.data.map((v) => v.estimate),
  65. animationDuration: 2800,
  66. animationEasing: 'cubicInOut'
  67. },
  68. {
  69. name: t('analysis.actual'),
  70. smooth: true,
  71. type: 'line',
  72. itemStyle: {},
  73. data: res.data.map((v) => v.actual),
  74. animationDuration: 2800,
  75. animationEasing: 'quadraticOut'
  76. }
  77. ])
  78. }
  79. }
  80. const getAllApi = async () => {
  81. await Promise.all([getUserAccessSource(), getWeeklyUserActivity(), getMonthlySales()])
  82. loading.value = false
  83. }
  84. getAllApi()
  85. </script>
  86. <template>
  87. <PanelGroup />
  88. <ElRow :gutter="20" justify="space-between">
  89. <ElCol :xl="10" :lg="10" :md="24" :sm="24" :xs="24">
  90. <ElCard shadow="hover" class="mb-20px">
  91. <ElSkeleton :loading="loading" animated>
  92. <Echart :options="pieOptionsData" :height="300" />
  93. </ElSkeleton>
  94. </ElCard>
  95. </ElCol>
  96. <ElCol :xl="14" :lg="14" :md="24" :sm="24" :xs="24">
  97. <ElCard shadow="hover" class="mb-20px">
  98. <ElSkeleton :loading="loading" animated>
  99. <Echart :options="barOptionsData" :height="300" />
  100. </ElSkeleton>
  101. </ElCard>
  102. </ElCol>
  103. <ElCol :span="24">
  104. <ElCard shadow="hover" class="mb-20px">
  105. <ElSkeleton :loading="loading" animated :rows="4">
  106. <Echart :options="lineOptionsData" :height="350" />
  107. </ElSkeleton>
  108. </ElCard>
  109. </ElCol>
  110. </ElRow>
  111. </template>