diff --git a/src/service/api/test.ts b/src/service/api/test.ts index b544c89..c224885 100644 --- a/src/service/api/test.ts +++ b/src/service/api/test.ts @@ -37,10 +37,14 @@ export function withoutToken() { export function dictData() { return request.Get('/getDictData', { transformData(rawData, _headers) { - const { data } = rawData as any + const response = rawData as any return { - gender: data.gender === 0 ? '男' : '女', - status: `状态是${data.status}`, + ...response, + data: { + ...response.data, + gender: response.data.gender === 0 ? '男' : '女', + status: `状态是${response.data.status}`, + }, } }, }) @@ -57,10 +61,15 @@ export function getBlob(url: string) { /* 带进度的下载文件 */ export function downloadFile(url: string) { - return blankInstance.Get(url, { + const methodInstance = blankInstance.Get(url, { // 开启下载进度 enableDownload: true, }) + methodInstance.meta = { + // 标识为bolb数据 + isBlob: true, + } + return methodInstance } /* 测试状态码500失败 */ export function FailedRequest() { diff --git a/src/store/route.ts b/src/store/route.ts index 7b4582a..75ea41d 100644 --- a/src/store/route.ts +++ b/src/store/route.ts @@ -135,7 +135,6 @@ export const useRouteStore = defineStore('route-store', { }) resultRouter = arrayToTree(resultRouter) as AppRoute.Route[] - console.warn('🚀 ~ createRoutes ~ resultRouter:', resultRouter) this.setRedirect(resultRouter) const appRootRoute: RouteRecordRaw = { path: '/appRoot', diff --git a/src/views/plugin/fetch/index.vue b/src/views/plugin/fetch/index.vue index 7a9ce57..3844b47 100644 --- a/src/views/plugin/fetch/index.vue +++ b/src/views/plugin/fetch/index.vue @@ -112,24 +112,16 @@ function getDictData() { msg.value = res }) } + +const filePath = ref('https://images.unsplash.com/photo-1663529628961-80aa6ebcd157?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=764&q=80') // 模拟获取二进制文件 -const imagePath = ref('https://images.unsplash.com/photo-1663529628961-80aa6ebcd157?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=764&q=80') function getBlobFile() { - getBlob(imagePath.value).then((res) => { - msg.value = 'this is blob!' - const link = URL.createObjectURL(res) - const eleLink = document.createElement('a') - eleLink.download = 'okk' - eleLink.style.display = 'none' - eleLink.href = link - document.body.appendChild(eleLink) - eleLink.click() - document.body.removeChild(eleLink) + getBlob(filePath.value).then((res) => { + downloadLink(res, 'BlobOk') }) } -// 下载大文件获取进度 -const downloadPath = ref('https://suqiqi.oss-cn-beijing.aliyuncs.com/test/video/1.mp4') -const { downloading, abort: abortDownloadFile, send: sendDownloadFile } = useRequest(downloadFile(downloadPath.value), { +// 下载文件获取进度 +const { downloading, abort: abortDownloadFile, send: sendDownloadFile } = useRequest(downloadFile(filePath.value), { // 当immediate为false时,默认不发出 immediate: false, }) @@ -138,6 +130,21 @@ const downloadProcess = computed(() => { return 0 return Math.floor(downloading.value.loaded / downloading.value.total * 100) }) +async function handleDownloadFile() { + const res = await sendDownloadFile() + downloadLink(res, 'fileOk') +} + +function downloadLink(data: Blob, name: string) { + const link = URL.createObjectURL(data) + const eleLink = document.createElement('a') + eleLink.download = name + eleLink.style.display = 'none' + eleLink.href = link + document.body.appendChild(eleLink) + eleLink.click() + document.body.removeChild(eleLink) +}