From ded40d28b221e99bedee51e855db6d6c0cb1e4c1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=98=E7=BF=94=E5=AE=87-=E6=97=85=E7=AE=A1=E5=AE=B6?= <153298343@qq.com> Date: Thu, 15 Jun 2023 11:13:35 +0800 Subject: [PATCH] up --- src/components/XyTable/index.vue | 3 +- src/utils/downloadRequest.js | 92 +++++++++++++++++++ .../activity/components/addDrawPrize.vue | 35 ++++++- src/views/activity/drawPrize.vue | 8 ++ src/views/activity/drawPrizeLog.vue | 71 ++++++++++++-- 5 files changed, 196 insertions(+), 13 deletions(-) create mode 100644 src/utils/downloadRequest.js diff --git a/src/components/XyTable/index.vue b/src/components/XyTable/index.vue index a970359..4838ba6 100644 --- a/src/components/XyTable/index.vue +++ b/src/components/XyTable/index.vue @@ -200,7 +200,8 @@ export default { list, rowKey, deleteClick, - editorClick + editorClick, + selectionChange } = this return (
diff --git a/src/utils/downloadRequest.js b/src/utils/downloadRequest.js new file mode 100644 index 0000000..3dfce31 --- /dev/null +++ b/src/utils/downloadRequest.js @@ -0,0 +1,92 @@ +import axios from 'axios' +import { getToken } from '@/utils/auth' +import { Loading, Message } from 'element-ui' + +/* + * @params {string} url 请求拼接地址 + * @params {object} info 请求参数params或data + */ + +let loading; +export async function download(url,method='get',info,filename){ + + loading = Loading.service({ + lock:true, + background:"rgba(0,0,0,0.4)", + text:"文件正在生成中..." + }) + + let options = { + baseURL:process.env.VUE_APP_BASE_API, + url, + method, + responseType: 'blob', + timeout:10000, + headers: { + Accept: 'application/json','Content-Type':'application/json; charset=utf-8', + withCredentials: true, + Authorization:"Bearer " + getToken() + }, + } + if(method === 'get'){ + Object.defineProperty(options,'params',{ + value:info, + enumerable:true, + writable:false + }) + } + if(method === 'post'){ + Object.defineProperty(options,'data',{ + value:info, + enumerable:true, + writable:false + }) + } + + try { + const response = await axios.request(options) + + loading.close() + + // 提取文件名 + if(!filename){ + filename = response.headers['content-disposition']?.match( + /filename=(.*)/ + )[1] || '' + } + + // 将二进制流转为blob + const blob = new Blob([response.data], { type: 'application/octet-stream' }) + if (typeof window.navigator.msSaveBlob !== 'undefined') { + // 兼容IE,window.navigator.msSaveBlob:以本地方式保存文件 + window.navigator.msSaveBlob(blob, decodeURI(filename)) + } else { + // 创建新的URL并指向File对象或者Blob对象的地址 + const blobURL = window.URL.createObjectURL(blob) + // 创建a标签,用于跳转至下载链接 + const tempLink = document.createElement('a') + tempLink.style.display = 'none' + tempLink.href = blobURL + tempLink.setAttribute('download', decodeURI(filename)) + // 兼容:某些浏览器不支持HTML5的download属性 + if (typeof tempLink.download === 'undefined') { + tempLink.setAttribute('target', '_blank') + } + // 挂载a标签 + document.body.appendChild(tempLink) + tempLink.click() + document.body.removeChild(tempLink) + // 释放blob URL地址 + window.URL.revokeObjectURL(blobURL) + } + }catch (err){ + console.error(err) + loading.close() + Message({ + type:'error', + message:err + }) + } +} + + diff --git a/src/views/activity/components/addDrawPrize.vue b/src/views/activity/components/addDrawPrize.vue index 4167719..671d382 100644 --- a/src/views/activity/components/addDrawPrize.vue +++ b/src/views/activity/components/addDrawPrize.vue @@ -35,6 +35,28 @@
+ + +