index.vue 12 KB

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