index.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. <template>
  2. <div class="app-container">
  3. <doc-alert title="公众号统计" url="https://doc.iocoder.cn/mp/statistics/" />
  4. <!-- 搜索工作栏 -->
  5. <el-form ref="queryForm" size="small" :inline="true" label-width="68px">
  6. <el-form-item label="公众号" prop="accountId">
  7. <el-select v-model="accountId" @change="getSummary">
  8. <el-option v-for="item in accounts" :key="parseInt(item.id)" :label="item.name" :value="parseInt(item.id)" />
  9. </el-select>
  10. </el-form-item>
  11. <el-form-item label="时间范围" prop="date">
  12. <el-date-picker v-model="date" style="width: 260px" value-format="yyyy-MM-dd HH:mm:ss" type="daterange"
  13. range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期"
  14. :picker-options="datePickerOptions" :default-time="['00:00:00', '23:59:59']"
  15. @change="getSummary">
  16. </el-date-picker>
  17. </el-form-item>
  18. </el-form>
  19. <!-- 图表 -->
  20. <el-row>
  21. <el-col :span="12" class="card-box">
  22. <el-card>
  23. <div slot="header">
  24. <span>用户增减数据</span>
  25. </div>
  26. <div class="el-table el-table--enable-row-hover el-table--medium">
  27. <div ref="userSummaryChart" style="height: 420px" />
  28. </div>
  29. </el-card>
  30. </el-col>
  31. <el-col :span="12" class="card-box">
  32. <el-card>
  33. <div slot="header">
  34. <span>累计用户数据</span>
  35. </div>
  36. <div class="el-table el-table--enable-row-hover el-table--medium">
  37. <div ref="userCumulateChart" style="height: 420px" />
  38. </div>
  39. </el-card>
  40. </el-col>
  41. <el-col :span="12" class="card-box">
  42. <el-card>
  43. <div slot="header">
  44. <span>消息概况数据</span>
  45. </div>
  46. <div class="el-table el-table--enable-row-hover el-table--medium">
  47. <div ref="upstreamMessageChart" style="height: 420px" />
  48. </div>
  49. </el-card>
  50. </el-col>
  51. <el-col :span="12" class="card-box">
  52. <el-card>
  53. <div slot="header">
  54. <span>接口分析数据</span>
  55. </div>
  56. <div class="el-table el-table--enable-row-hover el-table--medium">
  57. <div ref="interfaceSummaryChart" style="height: 420px" />
  58. </div>
  59. </el-card>
  60. </el-col>
  61. </el-row>
  62. </div>
  63. </template>
  64. <script>
  65. // 引入基本模板
  66. import * as echarts from 'echarts'
  67. // 引入柱状图组件
  68. require('echarts/lib/chart/bar')
  69. // 引入柱拆线组件
  70. require('echarts/lib/chart/line')
  71. // 引入提示框和title组件
  72. require('echarts/lib/component/tooltip')
  73. require('echarts/lib/component/title')
  74. require('echarts/lib/component/legend')
  75. import { getInterfaceSummary, getUserSummary, getUserCumulate, getUpstreamMessage} from '@/api/mp/statistics'
  76. import { datePickerOptions } from "@/utils/constants";
  77. import {addTime, beginOfDay, betweenDay, endOfDay, formatDate} from "@/utils/dateUtils";
  78. import { getSimpleAccounts } from "@/api/mp/account";
  79. export default {
  80. name: 'MpStatistics',
  81. data() {
  82. return {
  83. date : [beginOfDay(new Date(new Date().getTime() - 3600 * 1000 * 24 * 7)), // -7 天
  84. endOfDay(new Date(new Date().getTime() - 3600 * 1000 * 24))], // -1 天
  85. accountId: undefined,
  86. accounts: [],
  87. xAxisDate: [], // X 轴的日期范围
  88. userSummaryOption: { // 用户增减数据
  89. color: ['#67C23A', '#e5323e'],
  90. legend: {
  91. data: ['新增用户','取消关注的用户']
  92. },
  93. tooltip: {},
  94. xAxis: {
  95. data: [] // X 轴的日期范围
  96. },
  97. yAxis: {
  98. minInterval: 1
  99. },
  100. series: [{
  101. name: '新增用户',
  102. type: 'bar',
  103. label: {
  104. normal: {
  105. show: true
  106. }
  107. },
  108. barGap: 0,
  109. data: [] // 新增用户的数据
  110. }, {
  111. name: '取消关注的用户',
  112. type: 'bar',
  113. label: {
  114. normal: {
  115. show: true
  116. }
  117. },
  118. data: [] // 取消关注的用户的数据
  119. }]
  120. },
  121. userCumulateOption: { // 累计用户数据
  122. legend: {
  123. data: ['累计用户量']
  124. },
  125. xAxis: {
  126. type: 'category',
  127. data: []
  128. },
  129. yAxis: {
  130. minInterval: 1
  131. },
  132. series: [{
  133. name:'累计用户量',
  134. data: [], // 累计用户量的数据
  135. type: 'line',
  136. smooth: true,
  137. label: {
  138. normal: {
  139. show: true
  140. }
  141. }
  142. }]
  143. },
  144. upstreamMessageOption: { // 消息发送概况数据
  145. color: ['#67C23A', '#e5323e'],
  146. legend: {
  147. data: ['用户发送人数', '用户发送条数']
  148. },
  149. tooltip: {},
  150. xAxis: {
  151. data: [] // X 轴的日期范围
  152. },
  153. yAxis: {
  154. minInterval: 1
  155. },
  156. series: [{
  157. name: '用户发送人数',
  158. type: 'line',
  159. smooth: true,
  160. label: {
  161. normal: {
  162. show: true
  163. }
  164. },
  165. data: [] // 用户发送人数的数据
  166. }, {
  167. name: '用户发送条数',
  168. type: 'line',
  169. smooth: true,
  170. label: {
  171. normal: {
  172. show: true
  173. }
  174. },
  175. data: [] // 用户发送条数的数据
  176. }]
  177. },
  178. interfaceSummaryOption: { // 接口分析况数据
  179. color: ['#67C23A', '#e5323e', '#E6A23C', '#409EFF'],
  180. legend: {
  181. data: ['被动回复用户消息的次数','失败次数', '最大耗时','总耗时']
  182. },
  183. tooltip: {},
  184. xAxis: {
  185. data: [] // X 轴的日期范围
  186. },
  187. yAxis: {},
  188. series: [{
  189. name: '被动回复用户消息的次数',
  190. type: 'bar',
  191. label: {
  192. normal: {
  193. show: true
  194. }
  195. },
  196. barGap: 0,
  197. data: [] // 被动回复用户消息的次数的数据
  198. }, {
  199. name: '失败次数',
  200. type: 'bar',
  201. label: {
  202. normal: {
  203. show: true
  204. }
  205. },
  206. data: [] // 失败次数的数据
  207. }, {
  208. name: '最大耗时',
  209. type: 'bar',
  210. label: {
  211. normal: {
  212. show: true
  213. }
  214. },
  215. data: [] // 最大耗时的数据
  216. }, {
  217. name: '总耗时',
  218. type: 'bar',
  219. label: {
  220. normal: {
  221. show: true
  222. }
  223. },
  224. data: [] // 总耗时的数据
  225. }]
  226. },
  227. // 静态变量
  228. datePickerOptions: datePickerOptions,
  229. }
  230. },
  231. created() {
  232. getSimpleAccounts().then(response => {
  233. this.accounts = response.data;
  234. // 默认选中第一个
  235. if (this.accounts.length > 0) {
  236. this.accountId = this.accounts[0].id;
  237. }
  238. // 加载数据
  239. this.getSummary();
  240. })
  241. },
  242. methods: {
  243. getSummary() {
  244. // 如果没有选中公众号账号,则进行提示。
  245. if (!this.accountId) {
  246. this.$message.error('未选中公众号,无法统计数据')
  247. return false
  248. }
  249. // 必须选择 7 天内,因为公众号有时间跨度限制为 7
  250. if (betweenDay(this.date[0], this.date[1]) >= 7) {
  251. this.$message.error('时间间隔 7 天以内,请重新选择')
  252. return false
  253. }
  254. this.xAxisDate = []
  255. const days = betweenDay(this.date[0], this.date[1]) // 相差天数
  256. for(let i = 0; i <= days; i++){
  257. this.xAxisDate.push(formatDate(addTime(this.date[0], 3600 * 1000 * 24 * i), 'yyyy-MM-dd'));
  258. }
  259. // 初始化图表
  260. this.initUserSummaryChart();
  261. this.initUserCumulateChart();
  262. this.initUpstreamMessageChart();
  263. this.interfaceSummaryChart();
  264. },
  265. initUserSummaryChart() {
  266. this.userSummaryOption.xAxis.data = [];
  267. this.userSummaryOption.series[0].data = [];
  268. this.userSummaryOption.series[1].data = [];
  269. getUserSummary({
  270. accountId: this.accountId,
  271. date: [formatDate(this.date[0], 'yyyy-MM-dd HH:mm:ss'), formatDate(this.date[1], 'yyyy-MM-dd HH:mm:ss'),]
  272. }).then(response => {
  273. this.userSummaryOption.xAxis.data = this.xAxisDate;
  274. // 处理数据
  275. this.xAxisDate.forEach((date, index) => {
  276. response.data.forEach((item) => {
  277. // 匹配日期
  278. const refDate = formatDate(new Date(item.refDate), 'yyyy-MM-dd');
  279. if (refDate.indexOf(date) === -1) {
  280. return;
  281. }
  282. // 设置数据到对应的位置
  283. this.userSummaryOption.series[0].data[index] = item.newUser;
  284. this.userSummaryOption.series[1].data[index] = item.cancelUser;
  285. })
  286. })
  287. // 绘制图表
  288. const userSummaryChart = echarts.init(this.$refs.userSummaryChart);
  289. userSummaryChart.setOption(this.userSummaryOption)
  290. }).catch(() => {})
  291. },
  292. initUserCumulateChart() {
  293. this.userCumulateOption.xAxis.data = [];
  294. this.userCumulateOption.series[0].data = [];
  295. // 发起请求
  296. getUserCumulate({
  297. accountId: this.accountId,
  298. date: [formatDate(this.date[0], 'yyyy-MM-dd HH:mm:ss'), formatDate(this.date[1], 'yyyy-MM-dd HH:mm:ss'),]
  299. }).then(response => {
  300. this.userCumulateOption.xAxis.data = this.xAxisDate;
  301. // 处理数据
  302. response.data.forEach((item, index) => {
  303. this.userCumulateOption.series[0].data[index] = item.cumulateUser;
  304. })
  305. // 绘制图表
  306. const userCumulateChart = echarts.init(this.$refs.userCumulateChart);
  307. userCumulateChart.setOption(this.userCumulateOption)
  308. }).catch(() => {})
  309. },
  310. initUpstreamMessageChart() {
  311. this.upstreamMessageOption.xAxis.data = [];
  312. this.upstreamMessageOption.series[0].data = [];
  313. this.upstreamMessageOption.series[1].data = [];
  314. // 发起请求
  315. getUpstreamMessage({
  316. accountId: this.accountId,
  317. date: [formatDate(this.date[0], 'yyyy-MM-dd HH:mm:ss'), formatDate(this.date[1], 'yyyy-MM-dd HH:mm:ss'),]
  318. }).then(response => {
  319. this.upstreamMessageOption.xAxis.data = this.xAxisDate;
  320. // 处理数据
  321. response.data.forEach((item, index) => {
  322. this.upstreamMessageOption.series[0].data[index] = item.messageUser;
  323. this.upstreamMessageOption.series[1].data[index] = item.messageCount;
  324. })
  325. // 绘制图表
  326. const upstreamMessageChart = echarts.init(this.$refs.upstreamMessageChart);
  327. upstreamMessageChart.setOption(this.upstreamMessageOption);
  328. }).catch(() => {})
  329. },
  330. interfaceSummaryChart() {
  331. this.interfaceSummaryOption.xAxis.data = [];
  332. this.interfaceSummaryOption.series[0].data = [];
  333. this.interfaceSummaryOption.series[1].data = [];
  334. this.interfaceSummaryOption.series[2].data = [];
  335. this.interfaceSummaryOption.series[3].data = [];
  336. // 发起请求
  337. getInterfaceSummary({
  338. accountId: this.accountId,
  339. date: [formatDate(this.date[0], 'yyyy-MM-dd HH:mm:ss'), formatDate(this.date[1], 'yyyy-MM-dd HH:mm:ss'),]
  340. }).then(response => {
  341. this.interfaceSummaryOption.xAxis.data = this.xAxisDate;
  342. // 处理数据
  343. response.data.forEach((item, index) => {
  344. this.interfaceSummaryOption.series[0].data[index] = item.callbackCount;
  345. this.interfaceSummaryOption.series[1].data[index] = item.failCount;
  346. this.interfaceSummaryOption.series[2].data[index] = item.maxTimeCost;
  347. this.interfaceSummaryOption.series[3].data[index] = item.totalTimeCost;
  348. })
  349. // 绘制图表
  350. const interfaceSummaryChart = echarts.init(this.$refs.interfaceSummaryChart);
  351. interfaceSummaryChart.setOption(this.interfaceSummaryOption);
  352. }).catch(() => {})
  353. }
  354. }
  355. }
  356. </script>