Vue3重构:公众号统计

This commit is contained in:
zhangjinlong 2023-03-27 18:18:29 +08:00
parent 1a4311309d
commit 51344a5967
2 changed files with 482 additions and 1 deletions

79
src/utils/dateUtils.ts Normal file
View File

@ -0,0 +1,79 @@
/**
* xx
*
* @param ms
* @returns {string}
*/
export function getDate(ms) {
const day = Math.floor(ms / (24 * 60 * 60 * 1000))
const hour = Math.floor(ms / (60 * 60 * 1000) - day * 24)
const minute = Math.floor(ms / (60 * 1000) - day * 24 * 60 - hour * 60)
const second = Math.floor(ms / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - minute * 60)
if (day > 0) {
return day + '天' + hour + '小时' + minute + '分钟'
}
if (hour > 0) {
return hour + '小时' + minute + '分钟'
}
if (minute > 0) {
return minute + '分钟'
}
if (second > 0) {
return second + '秒'
} else {
return 0 + '秒'
}
}
export function beginOfDay(date) {
return new Date(date.getFullYear(), date.getMonth(), date.getDate())
}
export function endOfDay(date) {
return new Date(date.getFullYear(), date.getMonth(), date.getDate(), 23, 59, 59, 999)
}
export function betweenDay(date1, date2) {
date1 = convertDate(date1)
date2 = convertDate(date2)
// 计算差值
return Math.floor((date2.getTime() - date1.getTime()) / (24 * 3600 * 1000))
}
export function formatDate(date, fmt) {
date = convertDate(date)
const o = {
'M+': date.getMonth() + 1, //月份
'd+': date.getDate(), //日
'H+': date.getHours(), //小时
'm+': date.getMinutes(), //分
's+': date.getSeconds(), //秒
'q+': Math.floor((date.getMonth() + 3) / 3), //季度
S: date.getMilliseconds() //毫秒
}
if (/(y+)/.test(fmt)) {
// 年份
fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length))
}
for (const k in o) {
if (new RegExp('(' + k + ')').test(fmt)) {
fmt = fmt.replace(
RegExp.$1,
RegExp.$1.length === 1 ? o[k] : ('00' + o[k]).substr(('' + o[k]).length)
)
}
}
return fmt
}
export function addTime(date, time) {
date = convertDate(date)
return new Date(date.getTime() + time)
}
export function convertDate(date) {
if (typeof date === 'string') {
return new Date(date)
}
return date
}

View File

@ -1,3 +1,405 @@
<template> <template>
<span>开发中</span> <!-- 搜索工作栏 -->
<content-wrap>
<el-form ref="queryForm" class="-mb-15px" :inline="true" label-width="68px">
<el-form-item label="公众号" prop="accountId">
<el-select v-model="accountId" @change="getSummary">
<el-option
v-for="item in accounts"
:key="parseInt(item.id)"
:label="item.name"
:value="parseInt(item.id)"
/>
</el-select>
</el-form-item>
<el-form-item label="时间范围" prop="dateRange">
<el-date-picker
v-model="dateRange"
style="width: 260px"
type="daterange"
range-separator="-"
start-placeholder="开始日期"
end-placeholder="结束日期"
:default-time="defaultTime"
@change="getSummary"
/>
</el-form-item>
</el-form>
</content-wrap>
<!-- 图表 -->
<content-wrap>
<el-row>
<el-col :span="12" class="card-box">
<el-card>
<template #header>
<div>
<span>用户增减数据</span>
</div>
</template> </template>
<div class="el-table el-table--enable-row-hover el-table--medium">
<div ref="userSummaryChartRef" style="height: 420px"></div>
</div>
</el-card>
</el-col>
<el-col :span="12" class="card-box">
<el-card>
<template #header>
<div>
<span>累计用户数据</span>
</div>
</template>
<div class="el-table el-table--enable-row-hover el-table--medium">
<div ref="userCumulateChartRef" style="height: 420px"></div>
</div>
</el-card>
</el-col>
<el-col :span="12" class="card-box">
<el-card>
<template #header>
<div>
<span>消息概况数据</span>
</div>
</template>
<div class="el-table el-table--enable-row-hover el-table--medium">
<div ref="upstreamMessageChartRef" style="height: 420px"></div>
</div>
</el-card>
</el-col>
<el-col :span="12" class="card-box">
<el-card>
<template #header>
<div>
<span>接口分析数据</span>
</div>
</template>
<div class="el-table el-table--enable-row-hover el-table--medium">
<div ref="interfaceSummaryChartRef" style="height: 420px"></div>
</div>
</el-card>
</el-col>
</el-row>
</content-wrap>
</template>
<script setup lang="ts" name="MpStatistics">
//
import * as echarts from 'echarts'
import {
getInterfaceSummary,
getUpstreamMessage,
getUserCumulate,
getUserSummary
} from '@/api/mp/statistics'
import { addTime, beginOfDay, betweenDay, endOfDay, formatDate } from '@/utils/dateUtils'
import { getSimpleAccountList } from '@/api/mp/account'
const message = useMessage() //
const defaultTime = ref<[Date, Date]>([
new Date(2000, 1, 1, 0, 0, 0),
new Date(2000, 2, 1, 23, 59, 59)
])
const dateRange = ref([
beginOfDay(new Date(new Date().getTime() - 3600 * 1000 * 24 * 7)),
endOfDay(new Date(new Date().getTime() - 3600 * 1000 * 24))
]) // -1
const accountId = ref()
const accounts = ref([
{
id: '0',
name: ''
}
])
const userSummaryChartRef = ref()
const userCumulateChartRef = ref()
const upstreamMessageChartRef = ref()
const interfaceSummaryChartRef = ref()
const xAxisDate = ref([] as any[]) // X
const userSummaryOption = reactive({
//
color: ['#67C23A', '#e5323e'],
legend: {
data: ['新增用户', '取消关注的用户']
},
tooltip: {},
xAxis: {
data: [] as any[] // X
},
yAxis: {
minInterval: 1
},
series: [
{
name: '新增用户',
type: 'bar',
label: {
show: true
},
barGap: 0,
data: [] as any[] //
},
{
name: '取消关注的用户',
type: 'bar',
label: {
show: true
},
data: [] as any[] //
}
]
})
const userCumulateOption = reactive({
//
legend: {
data: ['累计用户量']
},
xAxis: {
type: 'category',
data: [] as any[]
},
yAxis: {
minInterval: 1
},
series: [
{
name: '累计用户量',
data: [] as any[], //
type: 'line',
smooth: true,
label: {
show: true
}
}
]
})
const upstreamMessageOption = reactive({
//
color: ['#67C23A', '#e5323e'],
legend: {
data: ['用户发送人数', '用户发送条数']
},
tooltip: {},
xAxis: {
data: [] as any[] // X
},
yAxis: {
minInterval: 1
},
series: [
{
name: '用户发送人数',
type: 'line',
smooth: true,
label: {
show: true
},
data: [] as any[] //
},
{
name: '用户发送条数',
type: 'line',
smooth: true,
label: {
show: true
},
data: [] as any[] //
}
]
})
const interfaceSummaryOption = reactive({
//
color: ['#67C23A', '#e5323e', '#E6A23C', '#409EFF'],
legend: {
data: ['被动回复用户消息的次数', '失败次数', '最大耗时', '总耗时']
},
tooltip: {},
xAxis: {
data: [] as any[] // X
},
yAxis: {},
series: [
{
name: '被动回复用户消息的次数',
type: 'bar',
label: {
show: true
},
barGap: 0,
data: [] as any[] //
},
{
name: '失败次数',
type: 'bar',
label: {
show: true
},
data: [] as any[] //
},
{
name: '最大耗时',
type: 'bar',
label: {
show: true
},
data: [] as any[] //
},
{
name: '总耗时',
type: 'bar',
label: {
show: true
},
data: [] as any[] //
}
]
})
onMounted(async () => {
//
await getAccountList()
//
getSummary()
})
const getAccountList = async () => {
const data = await getSimpleAccountList()
accounts.value = data
//
if (accounts.value.length > 0) {
accountId.value = accounts.value[0].id
}
}
const getSummary = () => {
//
if (!accountId) {
message.error('未选中公众号,无法统计数据')
return false
}
// 7 7
if (betweenDay(dateRange.value[0], dateRange.value[1]) >= 7) {
message.error('时间间隔 7 天以内,请重新选择')
return false
}
xAxisDate.value = []
const days = betweenDay(dateRange.value[0], dateRange.value[1]) //
for (let i = 0; i <= days; i++) {
xAxisDate.value.push(
formatDate(addTime(dateRange.value[0], 3600 * 1000 * 24 * i), 'yyyy-MM-dd')
)
}
//
initUserSummaryChart()
initUserCumulateChart()
initUpstreamMessageChart()
interfaceSummaryChart()
}
const initUserSummaryChart = async () => {
userSummaryOption.xAxis.data = []
userSummaryOption.series[0].data = []
userSummaryOption.series[1].data = []
try {
const data = await getUserSummary({
accountId: accountId.value,
date: [
formatDate(dateRange.value[0], 'yyyy-MM-dd HH:mm:ss'),
formatDate(dateRange.value[1], 'yyyy-MM-dd HH:mm:ss')
]
})
userSummaryOption.xAxis.data = xAxisDate.value
//
xAxisDate.value.forEach((date, index) => {
data.forEach((item) => {
//
const refDate = formatDate(new Date(item.refDate), 'yyyy-MM-dd')
if (refDate.indexOf(date) === -1) {
return
}
//
userSummaryOption.series[0].data[index] = item.newUser
userSummaryOption.series[1].data[index] = item.cancelUser
})
})
//
const userSummaryChart = echarts.init(userSummaryChartRef.value)
userSummaryChart.setOption(userSummaryOption)
} catch {}
}
const initUserCumulateChart = async () => {
userCumulateOption.xAxis.data = []
userCumulateOption.series[0].data = []
//
try {
const data = await getUserCumulate({
accountId: accountId.value,
date: [
formatDate(dateRange.value[0], 'yyyy-MM-dd HH:mm:ss'),
formatDate(dateRange.value[1], 'yyyy-MM-dd HH:mm:ss')
]
})
userCumulateOption.xAxis.data = xAxisDate.value
//
data.forEach((item, index) => {
userCumulateOption.series[0].data[index] = item.cumulateUser
})
//
const userCumulateChart = echarts.init(userCumulateChartRef.value)
userCumulateChart.setOption(userCumulateOption)
} catch {}
}
const initUpstreamMessageChart = async () => {
upstreamMessageOption.xAxis.data = []
upstreamMessageOption.series[0].data = []
upstreamMessageOption.series[1].data = []
//
try {
const data = await getUpstreamMessage({
accountId: accountId.value,
date: [
formatDate(dateRange.value[0], 'yyyy-MM-dd HH:mm:ss'),
formatDate(dateRange.value[1], 'yyyy-MM-dd HH:mm:ss')
]
})
upstreamMessageOption.xAxis.data = xAxisDate.value
//
data.forEach((item, index) => {
upstreamMessageOption.series[0].data[index] = item.messageUser
upstreamMessageOption.series[1].data[index] = item.messageCount
})
//
const upstreamMessageChart = echarts.init(upstreamMessageChartRef.value)
upstreamMessageChart.setOption(upstreamMessageOption)
} catch {}
}
const interfaceSummaryChart = async () => {
interfaceSummaryOption.xAxis.data = []
interfaceSummaryOption.series[0].data = []
interfaceSummaryOption.series[1].data = []
interfaceSummaryOption.series[2].data = []
interfaceSummaryOption.series[3].data = []
//
try {
const data = await getInterfaceSummary({
accountId: accountId.value,
date: [
formatDate(dateRange.value[0], 'yyyy-MM-dd HH:mm:ss'),
formatDate(dateRange.value[1], 'yyyy-MM-dd HH:mm:ss')
]
})
interfaceSummaryOption.xAxis.data = xAxisDate.value
//
data.forEach((item, index) => {
interfaceSummaryOption.series[0].data[index] = item.callbackCount
interfaceSummaryOption.series[1].data[index] = item.failCount
interfaceSummaryOption.series[2].data[index] = item.maxTimeCost
interfaceSummaryOption.series[3].data[index] = item.totalTimeCost
})
//
const interfaceSummaryChart = echarts.init(interfaceSummaryChartRef.value)
interfaceSummaryChart.setOption(interfaceSummaryOption)
} catch {}
}
</script>