index.vue 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. <template>
  2. <div class="app-container">
  3. <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
  4. <el-form-item label="日期">
  5. <el-date-picker
  6. v-model="timeRange"
  7. type="daterange"
  8. placeholder="选择日期"
  9. :value-format="'yyyy-MM-dd'"
  10. :picker-options="pickerOptions"
  11. @change="handleDateData"
  12. ></el-date-picker>
  13. </el-form-item>
  14. <el-form-item>
  15. <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
  16. <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
  17. <el-button
  18. type="warning"
  19. plain
  20. icon="el-icon-download"
  21. size="mini"
  22. :loading="exportLoading"
  23. @click="handleExport"
  24. v-hasPermi="['course:miniappTraffic:export']"
  25. >导出</el-button>
  26. </el-form-item>
  27. </el-form>
  28. <el-table border v-loading="loading" :data="trafficList" show-summary :summary-method="getSummaries">
  29. <el-table-column label="日期" align="center" prop="statDate" />
  30. <el-table-column label="使用流量" align="center">
  31. <template slot-scope="scope">
  32. <span>{{ formatTrafficData(scope.row.totalInternetTraffic) }}</span>
  33. </template>
  34. </el-table-column>
  35. </el-table>
  36. <pagination
  37. v-show="total > 0"
  38. :total="total"
  39. :page.sync="queryParams.pageNum"
  40. :limit.sync="queryParams.pageSize"
  41. @pagination="getList"
  42. />
  43. </div>
  44. </template>
  45. <script>
  46. import { listMiniappTraffic, exportMiniappTraffic } from "@/api/course/miniappTraffic";
  47. export default {
  48. name: "MiniappTraffic",
  49. data() {
  50. return {
  51. timeRange: null,
  52. pickerOptions: {
  53. shortcuts: [
  54. {
  55. text: '今日',
  56. onClick(picker) {
  57. const start = new Date();
  58. const end = new Date();
  59. picker.$emit('pick', [start, end]);
  60. }
  61. },
  62. {
  63. text: '昨日',
  64. onClick(picker) {
  65. const start = new Date();
  66. start.setDate(start.getDate() - 1);
  67. const end = new Date(start);
  68. picker.$emit('pick', [start, end]);
  69. }
  70. },
  71. {
  72. text: '本周',
  73. onClick(picker) {
  74. const now = new Date();
  75. const day = now.getDay() || 7;
  76. const start = new Date(now);
  77. start.setDate(now.getDate() - day + 1);
  78. const end = new Date();
  79. picker.$emit('pick', [start, end]);
  80. }
  81. },
  82. {
  83. text: '本月',
  84. onClick(picker) {
  85. const start = new Date(new Date().setDate(1));
  86. const end = new Date();
  87. picker.$emit('pick', [start, end]);
  88. }
  89. }
  90. ]
  91. },
  92. loading: false,
  93. exportLoading: false,
  94. showSearch: true,
  95. total: 0,
  96. trafficList: [],
  97. queryParams: {
  98. startDate: null,
  99. endDate: null,
  100. pageNum: 1,
  101. pageSize: 31
  102. }
  103. };
  104. },
  105. created() {
  106. this.initCurrentMonth();
  107. this.getList();
  108. },
  109. methods: {
  110. formatDate(date) {
  111. const year = date.getFullYear();
  112. const month = String(date.getMonth() + 1).padStart(2, '0');
  113. const day = String(date.getDate()).padStart(2, '0');
  114. return `${year}-${month}-${day}`;
  115. },
  116. initCurrentMonth() {
  117. const today = new Date();
  118. const start = new Date(today.getFullYear(), today.getMonth(), 1);
  119. const startStr = this.formatDate(start);
  120. const endStr = this.formatDate(today);
  121. this.timeRange = [startStr, endStr];
  122. this.queryParams.startDate = startStr;
  123. this.queryParams.endDate = endStr;
  124. },
  125. getSummaries(param) {
  126. const { columns, data } = param;
  127. const sums = [];
  128. columns.forEach((column, index) => {
  129. if (index === 0) {
  130. sums[index] = '合计';
  131. return;
  132. }
  133. if (index === columns.length - 1) {
  134. if (data && data.length > 0) {
  135. const total = data.reduce((prev, item) => prev + (Number(item.totalInternetTraffic) || 0), 0);
  136. sums[index] = this.formatTrafficData(total);
  137. } else {
  138. sums[index] = '0.0000 GB';
  139. }
  140. } else {
  141. sums[index] = '';
  142. }
  143. });
  144. return sums;
  145. },
  146. handleDateData() {
  147. if (this.timeRange) {
  148. this.queryParams.startDate = this.timeRange[0];
  149. this.queryParams.endDate = this.timeRange[1];
  150. } else {
  151. this.queryParams.startDate = null;
  152. this.queryParams.endDate = null;
  153. }
  154. },
  155. getList() {
  156. this.loading = true;
  157. listMiniappTraffic(this.queryParams).then(res => {
  158. this.trafficList = res.rows || [];
  159. this.total = res.total || 0;
  160. }).finally(() => {
  161. this.loading = false;
  162. });
  163. },
  164. handleQuery() {
  165. this.queryParams.pageNum = 1;
  166. this.getList();
  167. },
  168. resetQuery() {
  169. this.initCurrentMonth();
  170. this.queryParams.pageNum = 1;
  171. this.getList();
  172. },
  173. formatTrafficData(traffic) {
  174. return `${((Number(traffic) || 0) / (1024 ** 3)).toFixed(4)} GB`;
  175. },
  176. handleExport() {
  177. this.$confirm("确认导出数据?", "提示").then(() => {
  178. this.exportLoading = true;
  179. return exportMiniappTraffic(this.queryParams);
  180. }).then(res => {
  181. this.download(res.msg);
  182. }).finally(() => {
  183. this.exportLoading = false;
  184. });
  185. }
  186. }
  187. };
  188. </script>