소스 검색

下载方法修改

xinyan 5 달 전
부모
커밋
385d0912c5

+ 31 - 0
src/utils/useDownload.ts

@@ -0,0 +1,31 @@
+/**
+ * 通用下载方法
+ * @param apiMethod - 调用的 API 方法,返回一个 Promise。
+ * @param  queryParams - 请求参数。
+ * @param fileName - 下载文件的名称。
+ * @param successMessage - 成功时的消息提示函数。
+ * @param  errorMessage - 失败时的消息提示函数。
+ */
+
+export async function uesDownloadFile({
+	apiMethod,
+	queryParams = {},
+	fileName = '下载文件.xlsx',
+	successMessage = () => ElMessage.success('文件下载成功!'),
+	errorMessage = () => ElMessage.error('文件下载失败,请重试!'),
+}) {
+	try {
+		const response = await apiMethod(queryParams);
+		const url = window.URL.createObjectURL(new Blob([response.data]));
+		const link = document.createElement('a');
+		link.href = url;
+		link.setAttribute('download', fileName);
+		document.body.appendChild(link);
+		link.click();
+		document.body.removeChild(link); // 清理 DOM
+		successMessage();
+	} catch (error) {
+		errorMessage();
+		console.error('下载文件出错:', error);
+	}
+}

+ 31 - 34
src/views/price-approval/cost-detail/component/DataTable.vue

@@ -5,13 +5,12 @@
  * @Author: xinyan
  */
 
-import { Delete, Download, InfoFilled, Plus, Refresh, Upload } from '@element-plus/icons-vue';
+import { Download, Plus, Refresh, Upload } from '@element-plus/icons-vue';
 import { ElMessage } from 'element-plus';
 import { usePagination } from '/@/utils/usePagination';
 import { useTableData } from '/@/utils/useTableData';
-import { ProductColumns } from '/@/views/product-manage/Columns';
 import DataTableSlot from './DataTableSlot.vue';
-import { downloadFile } from '/@/utils/service';
+import { uesDownloadFile } from '/@/utils/useDownload';
 import PermissionButton from '/src/components/PermissionButton/index.vue';
 import ImportButton from '/src/components/ImportButton/index.vue';
 import VerticalDivider from '/src/components/VerticalDivider/index.vue';
@@ -19,7 +18,6 @@ import EditDrawer from '/src/views/product-manage/product-list/component/EditDra
 import NoticeDialog from '/src/views/product-manage/product-list/component/NoticeDialog.vue';
 import * as api from '../api';
 import { useResponse } from '/@/utils/useResponse';
-import DownloadError from '/@/views/product-manage/product-list/component/DownloadError.vue';
 import { CostDetailColumns } from '/@/views/price-approval/Columns';
 
 interface Parameter {
@@ -114,27 +112,22 @@ function handleRefresh() {
 }
 
 async function handleDownload() {
-	gridOptions.loading = true;
-	try {
-		const query = {
-			description: queryParameter?.description,
-			platform: queryParameter?.platform,
-			station: queryParameter?.station,
-		};
-		const response = await api.exportData(query);
-		const url = window.URL.createObjectURL(new Blob([response.data]));
-		const link = document.createElement('a');
-		link.href = url;
-		link.setAttribute('download', '商品列表数据.xlsx');
-		document.body.appendChild(link);
-		link.click();
-		ElMessage.success('数据导出成功!');
-	} catch (error) {
-		ElMessage.error('数据导出失败,请重试!');
-		console.error(error);
-	} finally {
-		gridOptions.loading = false; // 结束加载状态
-	}
+	// gridOptions.loading = true;
+	// try {
+	// 	await uesDownloadFile({
+	// 		apiMethod: api.exportData, // 调用的 API 方法
+	// 		queryParams: {
+	// 			description: queryParameter?.description,
+	// 			platform: queryParameter?.platform,
+	// 			station: queryParameter?.station,
+	// 		},
+	// 		fileName: '成本查看数据.xlsx', // 自定义文件名
+	// 		successMessage: () => ElMessage.success('数据导出成功!'),
+	// 		errorMessage: () => ElMessage.error('数据导出失败,请重试!'),
+	// 	});
+	// } finally {
+	// 	gridOptions.loading = false; // 结束加载状态
+	// }
 }
 
 function selectChangeEvent({ checked, row }: any) {
@@ -159,9 +152,7 @@ function selectAllChangeEvent({ checked }: any) {
 	}
 }
 
-function handleCreate() {
-
-}
+function handleCreate() {}
 
 function handleEdit(row: any) {
 	editOpen.value = true;
@@ -176,7 +167,6 @@ async function singleDelete(row: any) {
 	}
 }
 
-
 function downloadTemplate() {
 	const url = '/api/choice/reviews_monitor/import_data/';
 	const fileName = '商品监控模板.xlsx';
@@ -200,17 +190,24 @@ const gridEvents = {
 	},
 };
 
-function cellStyle(){
-	return{
-		fontWeight:500,
-	}
+function cellStyle() {
+	return {
+		fontWeight: 500,
+	};
 }
 
 defineExpose({ fetchList });
 </script>
 
 <template>
-	<vxe-grid ref="gridRef" v-bind="gridOptions" v-on="gridEvents" @checkbox-change="selectChangeEvent" @checkbox-all="selectAllChangeEvent" :cell-style="cellStyle">
+	<vxe-grid
+		ref="gridRef"
+		:cell-style="cellStyle"
+		v-bind="gridOptions"
+		v-on="gridEvents"
+		@checkbox-change="selectChangeEvent"
+		@checkbox-all="selectAllChangeEvent"
+	>
 		<!-- 工具栏左侧 -->
 		<template #toolbar_buttons>
 			<div class="flex gap-2">

+ 4 - 3
src/views/price-approval/cost-detail/index.vue

@@ -46,9 +46,7 @@ onBeforeMount(() => {
 
 async function fetchOptions() {
 	const resp = (await useResponse(api.getCostOptions)).data;
-	console.log("=>(index.vue:49) resp", resp);
 	platformOptions.value = resp.platform_list;
-	console.log("=>(index.vue:51) platformOptions.value", platformOptions.value);
 	stationOptions.value= resp.station_list;
 }
 
@@ -95,7 +93,10 @@ async function resetParameter() {
 							<el-col :span="5" class="flex">
 								<div class="flex items-center">
 									<span class="mr-2">地 区</span>
-									<el-input v-model="formInline.station" clearable placeholder="请输入店铺" />
+									<el-select v-model="formInline.station" placeholder="请选择地区">
+										<el-option v-for="item in stationOptions" :key="item" :label="item" :value="item">
+										</el-option>
+									</el-select>
 								</div>
 							</el-col>
 						</el-row>

+ 25 - 29
src/views/product-manage/competitor-monitor/component/DataTable.vue

@@ -20,6 +20,7 @@ import CreateDialog from '/src/views/product-manage/competitor-monitor/component
 import * as api from '../api';
 import { downloadFile } from '/@/utils/service';
 import HistoricalDetail from '/@/views/product-manage/historical-detail/index.vue';
+import { uesDownloadFile } from '/@/utils/useDownload';
 
 
 interface Parameter {
@@ -136,35 +137,30 @@ function handleRefresh() {
 }
 
 async function handleDownload() {
-  gridOptions.loading = true;
-  try {
-    const query = {
-      country_code: queryParameter?.country,
-      goods__brand: queryParameter?.brand,
-      goods__tag: queryParameter?.group,
-      status: queryParameter?.status,
-      shop: queryParameter?.shop,
-      asin: queryParameter?.asin,
-      goods__sku: queryParameter?.sku,
-      platform_number: queryParameter?.platformId,
-      goods__all_ratings: queryParameter?.scoreNumber,
-      goods__all_reviews: queryParameter?.commentNumber,
-      goods__all_score: queryParameter?.displayScore
-    };
-    const response = await api.exportData(query);
-    const url = window.URL.createObjectURL(new Blob([ response.data ]));
-    const link = document.createElement('a');
-    link.href = url;
-    link.setAttribute('download', '竞品监控数据.xlsx');
-    document.body.appendChild(link);
-    link.click();
-    ElMessage.success('数据导出成功!');
-  } catch (error) {
-    ElMessage.error('数据导出失败,请重试!');
-    console.error(error);
-  } finally {
-    gridOptions.loading = false; // 结束加载状态
-  }
+	gridOptions.loading = true;
+	try {
+		await uesDownloadFile({
+			apiMethod: api.exportData, // 调用的 API 方法
+			queryParams: {
+				country_code: queryParameter?.country,
+				goods__brand: queryParameter?.brand,
+				goods__tag: queryParameter?.group,
+				status: queryParameter?.status,
+				shop: queryParameter?.shop,
+				asin: queryParameter?.asin,
+				goods__sku: queryParameter?.sku,
+				platform_number: queryParameter?.platformId,
+				goods__all_ratings: queryParameter?.scoreNumber,
+				goods__all_reviews: queryParameter?.commentNumber,
+				goods__all_score: queryParameter?.displayScore
+			},
+			fileName: '竞品监控数据.xlsx', // 自定义文件名
+			successMessage: () => ElMessage.success('数据导出成功!'),
+			errorMessage: () => ElMessage.error('数据导出失败,请重试!'),
+		});
+	} finally {
+		gridOptions.loading = false; // 结束加载状态
+	}
 }
 
 async function batchDelete() {

+ 23 - 27
src/views/product-manage/product-list/component/DataTable.vue

@@ -21,6 +21,7 @@ import * as api from '../api';
 import { useResponse } from '/@/utils/useResponse';
 import DownloadError from '/@/views/product-manage/product-list/component/DownloadError.vue';
 import { hasPermission } from '/@/utils/hasPermission';
+import { uesDownloadFile } from '/@/utils/useDownload';
 
 
 interface Parameter {
@@ -129,33 +130,28 @@ function handleRefresh() {
 }
 
 async function handleDownload() {
-  gridOptions.loading = true;
-  try {
-    const query = {
-      country_code: queryParameter?.country,
-      brand: queryParameter?.brand,
-      tag: queryParameter?.group,
-      status: queryParameter?.status,
-      asin: queryParameter?.asin,
-      sku: queryParameter?.sku,
-      shop: queryParameter?.shop,
-      is_competitors: queryParameter?.isCompetitors,
-      platform_number: queryParameter?.platformNumber
-    };
-    const response = await api.exportData(query);
-    const url = window.URL.createObjectURL(new Blob([ response.data ]));
-    const link = document.createElement('a');
-    link.href = url;
-    link.setAttribute('download', '商品列表数据.xlsx');
-    document.body.appendChild(link);
-    link.click();
-    ElMessage.success('数据导出成功!');
-  } catch (error) {
-    ElMessage.error('数据导出失败,请重试!');
-    console.error(error);
-  } finally {
-    gridOptions.loading = false; // 结束加载状态
-  }
+	gridOptions.loading = true;
+	try {
+		await uesDownloadFile({
+			apiMethod: api.exportData, // 调用的 API 方法
+			queryParams: {
+				country_code: queryParameter?.country,
+				brand: queryParameter?.brand,
+				tag: queryParameter?.group,
+				status: queryParameter?.status,
+				asin: queryParameter?.asin,
+				sku: queryParameter?.sku,
+				shop: queryParameter?.shop,
+				is_competitors: queryParameter?.isCompetitors,
+				platform_number: queryParameter?.platformNumber
+			},
+			fileName: '商品列表数据.xlsx', // 自定义文件名
+			successMessage: () => ElMessage.success('数据导出成功!'),
+			errorMessage: () => ElMessage.error('数据导出失败,请重试!'),
+		});
+	} finally {
+		gridOptions.loading = false; // 结束加载状态
+	}
 }
 
 function selectChangeEvent({ checked, row }: any) {

+ 25 - 29
src/views/product-manage/product-monitor/component/DataTable.vue

@@ -20,6 +20,7 @@ import EditDrawer from './EditDrawer.vue';
 import CreateDialog from '/src/views/product-manage/product-monitor/component/CreateDialog.vue';
 import * as api from '../api';
 import HistoricalDetail from '/@/views/product-manage/historical-detail/index.vue';
+import { uesDownloadFile } from '/@/utils/useDownload';
 
 
 interface Parameter {
@@ -132,35 +133,30 @@ function handleRefresh() {
 }
 
 async function handleDownload() {
-  gridOptions.loading = true;
-  try {
-    const query = {
-      country_code: queryParameter?.country,
-      brand: queryParameter?.brand,
-      tag: queryParameter?.group,
-      status: queryParameter?.status,
-      shop_id: queryParameter?.shop,
-      asin: queryParameter?.asin,
-      sku: queryParameter?.sku,
-      platform_number: queryParameter?.platformId,
-      scoreNumber: queryParameter?.scoreNumber,
-      commentNumber: queryParameter?.commentNumber,
-      displayScore: queryParameter?.displayScore
-    };
-    const response = await api.exportData(query);
-    const url = window.URL.createObjectURL(new Blob([ response.data ]));
-    const link = document.createElement('a');
-    link.href = url;
-    link.setAttribute('download', '商品监控数据.xlsx');
-    document.body.appendChild(link);
-    link.click();
-    ElMessage.success('数据导出成功!');
-  } catch (error) {
-    ElMessage.error('数据导出失败,请重试!');
-    console.error(error);
-  } finally {
-    gridOptions.loading = false; // 结束加载状态
-  }
+	gridOptions.loading = true;
+	try {
+		await uesDownloadFile({
+			apiMethod: api.exportData, // 调用的 API 方法
+			queryParams: {
+				country_code: queryParameter?.country,
+				brand: queryParameter?.brand,
+				tag: queryParameter?.group,
+				status: queryParameter?.status,
+				shop_id: queryParameter?.shop,
+				asin: queryParameter?.asin,
+				sku: queryParameter?.sku,
+				platform_number: queryParameter?.platformId,
+				scoreNumber: queryParameter?.scoreNumber,
+				commentNumber: queryParameter?.commentNumber,
+				displayScore: queryParameter?.displayScore
+			},
+			fileName: '商品监控数据.xlsx', // 自定义文件名
+			successMessage: () => ElMessage.success('数据导出成功!'),
+			errorMessage: () => ElMessage.error('数据导出失败,请重试!'),
+		});
+	} finally {
+		gridOptions.loading = false; // 结束加载状态
+	}
 }
 
 async function batchDelete() {

+ 23 - 26
src/views/store-manage/online-merchandise/component/DataTable.vue

@@ -12,6 +12,7 @@ import { OnlineMerchandiseColumns } from '/@/views/store-manage/Columns';
 import DataTableSlot from './DataTableSlot.vue';
 import * as api from '../api';
 import { ElMessage } from 'element-plus';
+import { uesDownloadFile } from '/@/utils/useDownload';
 
 
 interface Parameter {
@@ -82,7 +83,7 @@ async function fetchList(isQuery = false) {
   if (isQuery) {
     gridOptions.pagerConfig.page = 1;
   }
-  
+
   gridOptions.data = [];
   gridOptions.columns = [];
 
@@ -107,31 +108,27 @@ function handleRefresh() {
 }
 
 async function handleDownload() {
-  gridOptions.loading = true;
-  try {
-    const query = {
-      asin: queryParameter?.asin,
-      sku__startswith: queryParameter?.sku,
-      country_code: queryParameter?.country,
-      shop_id: queryParameter?.shop,
-      shop__region: queryParameter?.region,
-      fulfillment_channel: queryParameter?.delivery,
-      status: queryParameter?.status
-    };
-    const response = await api.exportData(query);
-    const url = window.URL.createObjectURL(new Blob([ response.data ]));
-    const link = document.createElement('a');
-    link.href = url;
-    link.setAttribute('download', '在线商品数据.xlsx');
-    document.body.appendChild(link);
-    link.click();
-    ElMessage.success('数据导出成功!');
-  } catch (error) {
-    ElMessage.error('数据导出失败,请重试!');
-    console.error(error);
-  } finally {
-    gridOptions.loading = false;
-  }
+	gridOptions.loading = true;
+	try {
+		await uesDownloadFile({
+			apiMethod: api.exportData, // 调用的 API 方法
+			queryParams: {
+				asin: queryParameter?.asin,
+				sku: queryParameter?.sku,
+				country_code: queryParameter?.country,
+				shop_id: queryParameter?.shop,
+				region: queryParameter?.region,
+				delivery: queryParameter?.delivery,
+				status: queryParameter?.status,
+				platform_number: queryParameter?.platformId,
+			},
+			fileName: '在线商品数据.xlsx', // 自定义文件名
+			successMessage: () => ElMessage.success('数据导出成功!'),
+			errorMessage: () => ElMessage.error('数据导出失败,请重试!'),
+		});
+	} finally {
+		gridOptions.loading = false; // 结束加载状态
+	}
 }
 
 defineExpose({ fetchList });