DataTable.vue 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. <script lang="ts" setup>
  2. /**
  3. * @Name: Table.vue
  4. * @Description: 商品监控表格
  5. * @Author: Cheney
  6. */
  7. import { Delete, Download, InfoFilled, Plus, Refresh, Upload } from '@element-plus/icons-vue';
  8. import { ElMessage } from 'element-plus';
  9. import { usePagination } from '/@/utils/usePagination';
  10. import { useTableData } from '/@/utils/useTableData';
  11. import { useResponse } from '/@/utils/useResponse';
  12. import { ProductMonitorColumns } from '/@/views/product-manage/Columns';
  13. import DataTableSlot from '/@/views/product-manage/product-monitor/component/DataTableSlot.vue';
  14. import PermissionButton from '/src/components/PermissionButton/index.vue';
  15. import VerticalDivider from '/src/components/VerticalDivider/index.vue';
  16. import ImportButton from '/src/components/ImportButton/index.vue';
  17. import EditDrawer from './EditDrawer.vue';
  18. import CreateDialog from '/@/views/product-manage/product-monitor/component/createDialog.vue';
  19. import * as api from '../api';
  20. interface Parameter {
  21. country: string,
  22. brand: string,
  23. group: string,
  24. status: string,
  25. shop: string
  26. asin: string,
  27. sku: string,
  28. platformId: string,
  29. scoreNumber: string,
  30. commentNumber: string,
  31. displayScore: string,
  32. }
  33. const queryParameter: Parameter | undefined = inject('query-parameter');
  34. const { tableOptions, handlePageChange } = usePagination(fetchList);
  35. const gridRef = ref();
  36. const gridOptions: any = reactive({
  37. id: 'product-monitor-table',
  38. keepSource: true,
  39. size: 'mini',
  40. border: false,
  41. round: true,
  42. stripe: true,
  43. showHeader: true,
  44. currentRowHighLight: true,
  45. height: '100%',
  46. customConfig: {
  47. storage: true,
  48. },
  49. toolbarConfig: {
  50. size: 'large',
  51. custom: true,
  52. slots: {
  53. buttons: 'toolbar_buttons',
  54. tools: 'toolbar_tools'
  55. }
  56. },
  57. rowConfig: {
  58. isHover: true
  59. },
  60. columnConfig: {
  61. resizable: true
  62. },
  63. pagerConfig: {
  64. total: tableOptions.value.total,
  65. page: tableOptions.value.page,
  66. limit: tableOptions.value.limit
  67. },
  68. loading: false,
  69. loadingConfig: {
  70. icon: 'vxe-icon-indicator roll',
  71. text: '正在拼命加载中...'
  72. },
  73. columns: '',
  74. data: ''
  75. });
  76. const checkedList = ref<Set<number>>(new Set());
  77. const editOpen = ref(false);
  78. const createOpen = ref(false);
  79. const rowData = ref({});
  80. const templateType = ref('item1');
  81. onMounted(() => {
  82. fetchList();
  83. });
  84. async function fetchList() {
  85. gridOptions.data = [];
  86. gridOptions.columns = [];
  87. const query = {
  88. country_code: queryParameter?.country,
  89. brand: queryParameter?.brand,
  90. tag: queryParameter?.group,
  91. status: queryParameter?.status,
  92. shop_id: queryParameter?.shop,
  93. asin: queryParameter?.asin,
  94. sku: queryParameter?.sku,
  95. platform_number: queryParameter?.platformId,
  96. scoreNumber: queryParameter?.scoreNumber,
  97. commentNumber: queryParameter?.commentNumber,
  98. displayScore: queryParameter?.displayScore
  99. };
  100. await useTableData(api.getTableData, query, gridOptions);
  101. await gridRef.value.loadColumn(ProductMonitorColumns);
  102. gridOptions.showHeader = Boolean(gridOptions.data?.length);
  103. }
  104. function handleRefresh() {
  105. fetchList();
  106. }
  107. async function handleDownload() {
  108. gridOptions.loading = true;
  109. try {
  110. const query = {
  111. country_code: queryParameter?.country,
  112. goods__brand: queryParameter?.brand,
  113. goods__tag: queryParameter?.group,
  114. status: queryParameter?.status,
  115. shop_id: queryParameter?.shop,
  116. asin: queryParameter?.asin,
  117. goods__sku: queryParameter?.sku,
  118. platform_number: queryParameter?.platformId,
  119. goods__all_ratings: queryParameter?.scoreNumber,
  120. goods__all_reviews: queryParameter?.commentNumber,
  121. goods__all_score: queryParameter?.displayScore
  122. };
  123. const response = await api.exportData(query);
  124. const url = window.URL.createObjectURL(new Blob([response.data]));
  125. const link = document.createElement('a');
  126. link.href = url;
  127. link.setAttribute('download', '商品监控数据.xlsx');
  128. document.body.appendChild(link);
  129. link.click();
  130. ElMessage.success('数据导出成功!');
  131. } catch (error) {
  132. ElMessage.error('数据导出失败,请重试!');
  133. console.error(error);
  134. } finally {
  135. gridOptions.loading = false; // 结束加载状态
  136. }
  137. }
  138. async function batchDelete() {
  139. const ids = Array.from(checkedList.value);
  140. const res = await useResponse(api.batchDeleteRow, { keys: ids });
  141. checkedList.value.clear();
  142. if (res.code === 2000) {
  143. ElMessage.success({ message: '删除成功', plain: true });
  144. handleRefresh();
  145. }
  146. }
  147. function selectChangeEvent({ checked, row }: any) {
  148. if (checked) {
  149. checkedList.value.add(row.id); // 获取单个数据
  150. } else {
  151. checkedList.value.delete(row.id);
  152. }
  153. }
  154. function selectAllChangeEvent({ checked }: any) {
  155. const $grid = gridRef.value;
  156. if ($grid) {
  157. const records = $grid.getData(); // 获取所有数据
  158. if (checked) {
  159. records.forEach((item: any) => {
  160. checkedList.value.add(item.id);
  161. });
  162. } else {
  163. checkedList.value.clear();
  164. }
  165. }
  166. }
  167. function handleEdit(row: any) {
  168. editOpen.value = true;
  169. rowData.value = row;
  170. }
  171. async function singleDelete(row: any) {
  172. const res = await useResponse(api.deleteRow, row);
  173. if (res.code === 2000) {
  174. ElMessage.success({ message: '删除成功', plain: true });
  175. handleRefresh();
  176. }
  177. }
  178. function handleCreate() {
  179. createOpen.value = true;
  180. }
  181. function downloadTemplate() {
  182. // console.log('111=> ');
  183. }
  184. const gridEvents = {
  185. custom ({ type }: any) {
  186. if (type == 'confirm') {
  187. fetchList();
  188. }
  189. }
  190. }
  191. defineExpose({ fetchList });
  192. </script>
  193. <template>
  194. <vxe-grid ref="gridRef" v-bind="gridOptions" v-on="gridEvents"
  195. @checkbox-change="selectChangeEvent"
  196. @checkbox-all="selectAllChangeEvent">
  197. <template #toolbar_buttons>
  198. <div class="flex gap-2">
  199. <el-popconfirm
  200. :icon="InfoFilled"
  201. icon-color="#626AEF"
  202. title="你确定要删除此项吗?"
  203. width="220"
  204. @confirm="batchDelete"
  205. >
  206. <template #reference>
  207. <PermissionButton :disabled="!checkedList.size" :icon="Delete" plain round type="danger">
  208. 批量删除
  209. </PermissionButton>
  210. </template>
  211. <template #actions="{ confirm, cancel }">
  212. <el-button size="small" @click="cancel">No!</el-button>
  213. <el-button size="small" type="danger" @click="confirm">Yes?</el-button>
  214. </template>
  215. </el-popconfirm>
  216. <PermissionButton :icon="Plus" plain round type="primary" @click="handleCreate">
  217. 新 增
  218. </PermissionButton>
  219. <div class="custom-el-input">
  220. <el-select v-model="templateType" style="width: 190px">
  221. <template #prefix>
  222. <div class="flex items-center">
  223. <el-button size="small" type="success" text
  224. style="margin-left: -7px; font-size: 14px; border-radius: 29px;"
  225. @click.stop="downloadTemplate">
  226. 下载
  227. </el-button>
  228. <VerticalDivider style="margin-left: 7px" />
  229. </div>
  230. </template>
  231. <el-option label="商品通知模板" value="item1" />
  232. <el-option label="商品模板" value="item2" />
  233. <el-option label="指导价格模板" value="item3" />
  234. </el-select>
  235. </div>
  236. <VerticalDivider class="px-1" style="margin-left: 7px;" />
  237. <ImportButton :icon="Upload" :uploadFunction="api.upload" bg text>导 入</ImportButton>
  238. </div>
  239. </template>
  240. <template #toolbar_tools>
  241. <el-button circle class="toolbar-btn" @click="handleRefresh">
  242. <el-icon>
  243. <Refresh />
  244. </el-icon>
  245. </el-button>
  246. <el-popconfirm
  247. width="220"
  248. :icon="InfoFilled"
  249. icon-color="#626AEF"
  250. title="是否确认导出当前时间内所有数据项?"
  251. @confirm="handleDownload"
  252. >
  253. <template #reference>
  254. <el-button circle class="mr-3 toolbar-btn">
  255. <el-icon>
  256. <Download />
  257. </el-icon>
  258. </el-button>
  259. </template>
  260. <template #actions="{ confirm, cancel }">
  261. <el-button size="small" @click="cancel">No!</el-button>
  262. <el-button
  263. type="danger"
  264. size="small"
  265. @click="confirm"
  266. >
  267. Yes?
  268. </el-button>
  269. </template>
  270. </el-popconfirm>
  271. </template>
  272. <template #top>
  273. <div class="mb-2"></div>
  274. </template>
  275. <template #pager>
  276. <vxe-pager
  277. v-model:currentPage="gridOptions.pagerConfig.page"
  278. v-model:pageSize="gridOptions.pagerConfig.limit"
  279. :total="gridOptions.pagerConfig.total"
  280. class="mt-1.5"
  281. @page-change="handlePageChange"
  282. />
  283. </template>
  284. <!-- 自定义列插槽 -->
  285. <template v-for="col in ProductMonitorColumns" #[`${col.field}`]="{ row }">
  286. <DataTableSlot :key="row.id" :field="col.field" :row="row" @edit-row="handleEdit" @handle-delete="singleDelete" />
  287. </template>
  288. </vxe-grid>
  289. <EditDrawer v-if="editOpen" v-model="editOpen" :row-data="rowData" @refresh="handleRefresh" />
  290. <CreateDialog v-if="createOpen" v-model="createOpen" @refresh="fetchList" />
  291. </template>
  292. <style scoped>
  293. .toolbar-btn {
  294. width: 34px;
  295. height: 34px;
  296. font-size: 18px
  297. }
  298. :deep(.custom-el-input .el-select__wrapper) {
  299. border-radius: 20px;
  300. }
  301. </style>