monthlyRating.vue 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. <template>
  2. <div>
  3. <el-card>
  4. <span class="title">监控ASIN每月平均评分</span>
  5. <div style="display: flex; align-items: baseline; justify-content: space-between">
  6. <div style="display: flex; gap: 25px; margin-bottom: 10px">
  7. <div>
  8. <span style="font-size: 14px; padding-right: 4px; color: #95969a">ASIN:</span>
  9. <el-input v-model="searchAsin" clearable style="width: 180px" @change="fetchData"></el-input>
  10. </div>
  11. <div>
  12. <span style="font-size: 14px; padding-right: 4px; color: #95969a">SKU:</span>
  13. <el-input v-model="searchSku" clearable style="width: 180px" @change="fetchData"></el-input>
  14. </div>
  15. <div>
  16. <span style="font-size: 14px; color: #95969a">时间选择:</span>
  17. <el-date-picker
  18. v-model="asinMonthDate"
  19. :clearable="false"
  20. :disabled-date="disabledDate"
  21. placeholder="选择月"
  22. style="width: 170px"
  23. type="month"
  24. >
  25. </el-date-picker>
  26. </div>
  27. </div>
  28. <div>
  29. <el-button circle :icon="Download" plain @click="handleDownload"></el-button>
  30. </div>
  31. </div>
  32. <el-card body-style="padding: 0" shadow="never">
  33. <vxe-grid :cell-style="cellStyle" :header-cell-style="headerStyle" v-bind="gridOptions" v-on="gridEvents" show-overflow="tooltip"></vxe-grid>
  34. </el-card>
  35. </el-card>
  36. </div>
  37. </template>
  38. <script lang="ts" setup>
  39. import { getTableData, getTableDownloadData } from '/src/views/score-statistics/api';
  40. import { Download } from '@element-plus/icons-vue';
  41. import { scoreStatisticsColumns } from '/src/views/score-statistics/Columns';
  42. const gridOptions = reactive({
  43. border: 'inner',
  44. loading: false,
  45. height: 700,
  46. pagerConfig: {
  47. enabled: true,
  48. total: 20,
  49. currentPage: 1,
  50. pageSize: 20,
  51. pageSizes: [10, 20, 30],
  52. },
  53. rowConfig: {
  54. height: 38,
  55. },
  56. columns: scoreStatisticsColumns,
  57. data: [],
  58. });
  59. const asinMonthDate = ref(dayjs().subtract(1, 'month').startOf('month').format('YYYY-MM-DD'))
  60. const disabledDate = (time: Date) => {
  61. // 禁用当前月和当前月之后的日期
  62. const today = new Date(); // 获取当前日期
  63. const currentYear = today.getFullYear();
  64. const currentMonth = today.getMonth() + 1; // 获取月份,注意要 +1
  65. // 通过比较年份和月份来禁用
  66. return time.getFullYear() > currentYear || (time.getFullYear() === currentYear && time.getMonth() + 2 > currentMonth);
  67. };
  68. const searchAsin = ref('');
  69. const searchSku = ref('');
  70. const averageData = ref([]);
  71. const gridEvents = {
  72. pageChange({ pageSize, currentPage }) {
  73. gridOptions.pagerConfig.currentPage = currentPage;
  74. gridOptions.pagerConfig.pageSize = pageSize;
  75. fetchData();
  76. },
  77. };
  78. async function fetchData() {
  79. gridOptions.loading = true; // 显示加载状态
  80. try {
  81. const params = {
  82. limit: gridOptions.pagerConfig.pageSize,
  83. page: gridOptions.pagerConfig.currentPage,
  84. review_date: asinMonthDate.value,
  85. asin : searchAsin.value,
  86. sku : searchSku.value,
  87. };
  88. const resp = await getTableData(params);
  89. gridOptions.data = resp.data;
  90. console.log("=>(monthlyRating.vue:121) gridOptions.data", gridOptions.data);
  91. gridOptions.pagerConfig.total = resp.total;
  92. } catch (error) {
  93. console.log(error);
  94. } finally {
  95. gridOptions.loading = false;
  96. }
  97. }
  98. async function handleDownload() {
  99. gridOptions.loading = true; // 显示加载状态
  100. try {
  101. const params = {
  102. review_date: asinMonthDate.value,
  103. asin : searchAsin.value,
  104. sku : searchSku.value,
  105. };
  106. const resp = await getTableDownloadData(params);
  107. const url = window.URL.createObjectURL(resp.data);
  108. const link = document.createElement('a');
  109. link.href = url;
  110. link.setAttribute('download', asinMonthDate.slice(0, 7) + '平均评分.xlsx');
  111. document.body.appendChild(link);
  112. link.click();
  113. document.body.removeChild(link);
  114. window.URL.revokeObjectURL(url);
  115. gridOptions.loading = false;
  116. ElMessage.success('下载成功!');
  117. } catch (error) {
  118. ElMessage.error('下载失败!');
  119. } finally {
  120. gridOptions.loading = false;
  121. }
  122. }
  123. function cellStyle() {
  124. return {
  125. fontSize: '13px',
  126. fontWeight: '500',
  127. };
  128. }
  129. function headerStyle() {
  130. return {
  131. fontSize: '13px',
  132. backgroundColor: '#f0f1f3',
  133. height: 10,
  134. //color: '#b4b6ba',
  135. //fontWeight: '500',
  136. };
  137. }
  138. watch(asinMonthDate, (newVal) => {
  139. const date = dayjs(newVal).startOf('month').format('YYYY-MM-DD');
  140. console.log("=>(monthlyRating.vue:180) date", date);
  141. if (date !== asinMonthDate.value) {
  142. asinMonthDate.value = date;
  143. console.log("=>(monthlyRating.vue:183) asinMonthDate.value", asinMonthDate.value);
  144. fetchData();
  145. }
  146. }
  147. );
  148. onMounted(() => {
  149. fetchData();
  150. });
  151. </script>
  152. <style scoped>
  153. .tool {
  154. padding-right: 25px;
  155. }
  156. .title {
  157. display: flex;
  158. align-items: center; /* 垂直居中对齐 */
  159. /* justify-content: space-between; !* 水平居中对齐 *! */
  160. font-size: 16px;
  161. font-weight: 500;
  162. margin-bottom: 20px;
  163. }
  164. </style>