Pārlūkot izejas kodu

feat: 下载文件

luoyali 8 mēneši atpakaļ
vecāks
revīzija
004af1c991

+ 4 - 3
src/api/codeGenerate/database.ts

@@ -1,6 +1,7 @@
 import request from '@/utils/request'
 import axios from 'axios'
 import { AxiosPromise } from 'axios'
+import { ls } from '@/utils'
 
 // apiUrl 数据库管理
 const api = {
@@ -30,11 +31,11 @@ function codePreviewApi(data: any): AxiosPromise {
  */
 function codeDownloadApi(data: any) {
 	const url = import.meta.env.VITE_APP_BASE_API + api.codeDownload + `?t=${new Date().getTime()}`
+	const token = ls.get('token')
 	try {
 		const response = axios.post(url, data, {
-			headers: {
-				'Content-Type': 'application/zip'
-			}
+			responseType: 'arraybuffer',
+			headers: { accessToken: token, 'Content-Type': 'application/json; application/octet-stream' }
 		})
 		return response // 提取数据
 	} catch (error) {

+ 4 - 3
src/views/codeGenerate/database/CodeGenDrawer.vue

@@ -173,11 +173,12 @@ const generateCode = async () => {
 
 		const res = await database.codeDownloadApi({ ...form.value, templateIds: form.value.templateIds })
 		console.log('d', res.data)
-		const blob = new Blob([res.data], { type: 'application/zip' })
-		const url = window.URL.createObjectURL(blob)
+		// const blob = new Blob([res.data], { type: 'application/zip' })
+		const url = window.URL.createObjectURL(new Blob([res.data], { type: 'application/zip' }))
+		// const url = window.URL.createObjectURL(blob)
 		const a = document.createElement('a')
 		a.href = url
-		a.download = 'generated_code.zip' // 根据需要调整文件名
+		a.setAttribute('download', 'generated_code.zip')
 		document.body.appendChild(a)
 		a.click()
 		a.remove()