DataTable.vue 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. <script lang="ts" setup>
  2. /**
  3. * @Name: DataTable.vue
  4. * @Description: 审批查看(直销)数据表格
  5. * @Author: xinyan
  6. */
  7. import { Download, 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 DataTableSlot from './DataTableSlot.vue';
  12. import { uesDownloadFile } from '/@/utils/useDownload';
  13. import { downloadFile } from '/@/utils/service';
  14. import PermissionButton from '/src/components/PermissionButton/index.vue';
  15. import ImportButton from '/src/components/ImportButton/index.vue';
  16. import VerticalDivider from '/src/components/VerticalDivider/index.vue';
  17. import * as api from '../api';
  18. import { useResponse } from '/@/utils/useResponse';
  19. import {
  20. DirectSalesCheckColumns_Regular,
  21. DirectSalesCheckColumns_Special, SupplyCheckColumns_Regular,
  22. SupplyCheckColumns_Special,
  23. } from '/@/views/price-approval/Columns';
  24. import EditDrawer from '/@/views/price-approval/direct-sales/component/EditDrawer.vue';
  25. import router from '/@/router';
  26. import { hasPermission } from '/@/utils/hasPermission';
  27. import { getDept } from '/@/views/price-approval/api';
  28. interface Parameter {
  29. sku: string;
  30. platform: string;
  31. country_code: string;
  32. sales_mode: string;
  33. }
  34. const queryParameter: Parameter | undefined = inject('query-parameter');
  35. const { tableOptions, handlePageChange } = usePagination(fetchList);
  36. const gridRef = ref();
  37. const gridOptions: any = reactive({
  38. size: 'mini',
  39. border: false,
  40. round: true,
  41. stripe: true,
  42. currentRowHighLight: true,
  43. height: '100%',
  44. toolbarConfig: {
  45. size: 'large',
  46. slots: {
  47. buttons: 'toolbar_buttons',
  48. tools: 'toolbar_tools',
  49. },
  50. },
  51. rowConfig: {
  52. isHover: true,
  53. height: 50,
  54. },
  55. columnConfig: {
  56. resizable: true,
  57. },
  58. pagerConfig: {
  59. total: tableOptions.value.total,
  60. page: tableOptions.value.page,
  61. limit: tableOptions.value.limit,
  62. },
  63. loading: false,
  64. loadingConfig: {
  65. icon: 'vxe-icon-indicator roll',
  66. text: '正在拼命加载中...',
  67. },
  68. columns: '',
  69. data: '',
  70. });
  71. const checkedList = ref<Set<number>>(new Set());
  72. const btnLoading = ref(false);
  73. const editOpen = ref(false);
  74. const rowData = ref({});
  75. const dialogVisible = ref(false);
  76. const templateType = ref('cost');
  77. const is_superuser = ref(false);
  78. const roleKey = ref('');
  79. onBeforeMount(() => {
  80. fetchDept();
  81. gridOptions.pagerConfig.limit = 10;
  82. });
  83. onMounted(() => {
  84. fetchList();
  85. });
  86. async function fetchDept() {
  87. const resp = (await useResponse(getDept)).data;
  88. is_superuser.value = resp.is_superuser;
  89. roleKey.value = resp.role_info.length > 0 ? resp.role_info.map((role) => role.key) : '';
  90. console.log('=>(DataTable.vue:100) roleKey.value', roleKey.value);
  91. }
  92. async function fetchList(isQuery = false) {
  93. if (isQuery) {
  94. gridOptions.pagerConfig.page = 1;
  95. }
  96. gridOptions.data = [];
  97. gridOptions.columns = [];
  98. const query = {
  99. sku: queryParameter?.sku,
  100. platform: queryParameter?.platform,
  101. country_code: queryParameter?.country_code,
  102. sales_mode: queryParameter?.sales_mode,
  103. };
  104. await useTableData(api.getTableData, query, gridOptions);
  105. if (gridOptions && gridOptions.data?.length) {
  106. if (is_superuser.value) {
  107. await gridRef.value.loadColumn(DirectSalesCheckColumns_Special);
  108. } else if (!roleKey.value.includes('price.manage')) {
  109. await gridRef.value.loadColumn(DirectSalesCheckColumns_Regular);
  110. }else {
  111. await gridRef.value.loadColumn(DirectSalesCheckColumns_Regular);
  112. }
  113. }
  114. gridOptions.showHeader = Boolean(gridOptions.data?.length);
  115. }
  116. function handleRefresh() {
  117. fetchList();
  118. }
  119. async function handleDownload() {
  120. gridOptions.loading = true;
  121. try {
  122. await uesDownloadFile({
  123. apiMethod: api.exportData,
  124. queryParams: {
  125. sku: queryParameter?.sku,
  126. platform: queryParameter?.platform,
  127. country_code: queryParameter?.country_code,
  128. sales_mode: queryParameter?.sales_mode,
  129. },
  130. fileName: '审批查看(直销)数据.xlsx',
  131. successMessage: () => ElMessage.success('数据导出成功!'),
  132. errorMessage: () => ElMessage.error('数据导出失败,请重试!'),
  133. });
  134. } finally {
  135. gridOptions.loading = false;
  136. }
  137. }
  138. function handleCreate() {
  139. router.push({ path: '/addPage', query: { type: 'direct' } });
  140. }
  141. function handleEdit(row: any) {
  142. editOpen.value = true;
  143. rowData.value = row;
  144. }
  145. async function singleDelete(row: any) {
  146. const res = await useResponse(api.deleteRow, row);
  147. if (res.code === 2000) {
  148. ElMessage.error({ message: '已删除!', plain: true, icon: 'Delete' });
  149. handleRefresh();
  150. }
  151. }
  152. function downloadTemplate() {
  153. const url = '/api/pricing/price_product_direct/import_data/';
  154. const fileName = '审批查看(直销)模板.xlsx';
  155. if (url) {
  156. downloadFile({
  157. url,
  158. method: 'GET',
  159. filename: fileName,
  160. });
  161. } else {
  162. console.error('未知的模板类型:', templateType.value);
  163. }
  164. }
  165. const gridEvents = {
  166. custom({ type }: any) {
  167. if (type == 'confirm') {
  168. fetchList();
  169. }
  170. },
  171. };
  172. function cellStyle() {
  173. return {
  174. fontWeight: 600,
  175. };
  176. }
  177. defineExpose({ fetchList });
  178. </script>
  179. <template>
  180. <vxe-grid ref="gridRef" :cell-style="cellStyle" v-bind="gridOptions" v-on="gridEvents">
  181. <!-- 工具栏左侧 -->
  182. <template #toolbar_buttons>
  183. <div class="flex gap-2">
  184. <div>
  185. <PermissionButton v-if="hasPermission('PRICE_DIRECT_CREATE')" :icon="Plus" plain round type="primary" @click="handleCreate"
  186. >新 增
  187. </PermissionButton>
  188. </div>
  189. <div class="custom-el-input">
  190. <el-select v-model="templateType" style="width: 200px">
  191. <template #prefix>
  192. <div class="flex items-center">
  193. <el-button
  194. size="small"
  195. style="margin-left: -7px; font-size: 14px; border-radius: 29px"
  196. text
  197. type="success"
  198. @click.stop="downloadTemplate"
  199. >
  200. 下载
  201. </el-button>
  202. <VerticalDivider style="margin-left: 7px" />
  203. </div>
  204. </template>
  205. <el-option label="审批查看(直销)" value="cost" />
  206. </el-select>
  207. </div>
  208. <VerticalDivider class="px-1" style="margin-left: 7px" />
  209. <ImportButton v-if="hasPermission('DIRECT_IMPORT_DATA')" :icon="Upload" :uploadFunction="api.upload" bg text>导 入 </ImportButton>
  210. </div>
  211. </template>
  212. <!-- 工具栏右侧 -->
  213. <template #toolbar_tools>
  214. <el-button circle class="toolbar-btn" @click="handleRefresh">
  215. <el-icon>
  216. <Refresh />
  217. </el-icon>
  218. </el-button>
  219. <el-button circle class="toolbar-btn" @click="handleDownload">
  220. <el-icon>
  221. <Download />
  222. </el-icon>
  223. </el-button>
  224. </template>
  225. <template #top>
  226. <div class="mb-2"></div>
  227. </template>
  228. <!-- 分页 -->
  229. <template #pager>
  230. <vxe-pager
  231. v-model:currentPage="gridOptions.pagerConfig.page"
  232. v-model:pageSize="gridOptions.pagerConfig.limit"
  233. :total="gridOptions.pagerConfig.total"
  234. class="mt-1.5"
  235. @page-change="handlePageChange"
  236. />
  237. </template>
  238. <template #empty>
  239. <el-empty description="暂无数据" />
  240. </template>
  241. <!-- 自定义列插槽 -->
  242. <template v-for="col in DirectSalesCheckColumns_Special" #[`${col.field}`]="{ row }">
  243. <DataTableSlot :field="col.field" :row="row" @edit-row="handleEdit" @handle-delete="singleDelete" />
  244. </template>
  245. </vxe-grid>
  246. <EditDrawer v-if="editOpen" v-model="editOpen" :row-data="rowData" @refresh="handleRefresh" />
  247. </template>
  248. <style scoped>
  249. .toolbar-btn {
  250. width: 34px;
  251. height: 34px;
  252. font-size: 18px;
  253. }
  254. :deep(.custom-el-input .el-select__wrapper) {
  255. border-radius: 20px;
  256. }
  257. </style>