Browse Source

feat: 下载

luoyali 8 months ago
parent
commit
b2bf6fad2b

+ 9 - 5
src/api/codeGenerate/database.ts

@@ -1,4 +1,5 @@
 import request from '@/utils/request'
+import axios from 'axios'
 import { AxiosPromise } from 'axios'
 
 // apiUrl 数据库管理
@@ -28,11 +29,14 @@ function codePreviewApi(data: any): AxiosPromise {
  * 代码生成 - 下载代码
  */
 function codeDownloadApi(data: any) {
-	return request({
-		url: api.codeDownload + `?t=${new Date().getTime()}`,
-		method: 'post',
-		data
-	})
+	const url = import.meta.env.VITE_APP_BASE_API + api.codeDownload + `?t=${new Date().getTime()}`
+	try {
+		const response = axios.post(url, data)
+		return response // 提取数据
+	} catch (error) {
+		console.error('Error downloading code:', error)
+		throw error // 处理错误
+	}
 }
 
 /**

+ 0 - 70
src/layout/components/Header/components/Message1.vue

@@ -1,70 +0,0 @@
-<template>
-	<el-popover placement="bottom" :width="310" trigger="click">
-		<template #reference>
-			<div class="menu--message-trigger">
-				<el-tooltip :content="$t('le.message.txt')" effect="dark" placement="bottom">
-					<div class="menu--message menu-item le-hover-effect--bg">
-						<el-badge :value="90" :max="99">
-							<i class="le-iconfont le-notice"></i>
-						</el-badge>
-					</div>
-				</el-tooltip>
-			</div>
-		</template>
-		<ul>
-			<li v-for="(item, idx) in receivedData" :key="idx">{{ item }}</li>
-		</ul>
-	</el-popover>
-</template>
-<script setup>
-import { onMounted, ref, onBeforeUnmount } from 'vue'
-import { ls } from '@/utils'
-const token = ls.get('token')
-const { VITE_APP_BASE_API } = import.meta.env
-// const serviceUrlApi = ref(`${VITE_APP_BASE_API}/sys/sse/connect`);
-const serviceUrlApi = ref(`http://localhost:3000/events`)
-let sseService = ref(null)
-let receivedData = ref([])
-
-// 创建一个新的 EventSource,但首先使用 fetch API 发送带有 Authorization 头的请求
-function connectSSEWithHeaders(url, headers = {}) {
-	return fetch(url, {
-		method: 'GET',
-		headers: headers,
-		cache: 'no-cache', // *default, no-cache, reload, force-cache, only-if-cached
-		mode: 'cors', // no-cors, cors, *same-origin
-		credentials: 'same-origin', // include, *same-origin, omit
-		redirect: 'follow', // manual, *follow, error
-		referrerPolicy: 'no-referrer' // no-referrer, *client
-	})
-		.then(response => {
-			if (!response.ok) {
-				throw new Error('Network response was not ok')
-			}
-			return new EventSource(url)
-		})
-		.catch(error => {
-			console.error('There was a problem with your fetch operation:', error)
-		})
-}
-
-onMounted(() => {
-	connectSSEWithHeaders(serviceUrlApi.value, { accessToken: token }).then(eventSource => {
-		sseService.value = eventSource
-		eventSource.onmessage = function (e) {
-			console.log('后端返回的数据:', e.data)
-			receivedData.value.push(e.data)
-		}
-		eventSource.onerror = function (err) {
-			console.error('EventSource failed:', err)
-		}
-	})
-})
-
-onBeforeUnmount(() => {
-	if (sseService.value) {
-		sseService.value.close()
-		sseService.value = null
-	}
-})
-</script>

+ 0 - 145
src/layout/components/Header/components/Message2.vue

@@ -1,145 +0,0 @@
-<template>
-	<el-popover placement="bottom" :width="310" trigger="click">
-		<template #reference>
-			<div class="menu--message-trigger">
-				<el-tooltip :content="$t('le.message.txt')" effect="dark" placement="bottom">
-					<div class="menu--message menu-item le-hover-effect--bg">
-						<el-badge :value="total" :max="99">
-							<i class="le-iconfont le-notice"></i>
-						</el-badge>
-					</div>
-				</el-tooltip>
-			</div>
-		</template>
-		<el-tabs class="message-tabs" v-model="activeTab">
-			<el-tab-pane v-for="v of tabsConfig" :key="v.name" :name="v.name">
-				<template #label> {{ v.label }}({{ v.list.length }}) </template>
-				<template v-if="v.list.length">
-					<div class="message-list">
-						<div v-for="item of v.list" :key="item.id" class="message-item" @click="jumpMessageDetail(item.id)">
-							<!--<img src="" alt="" class="message-icon" />-->
-							<div class="message-content">
-								<div class="message-title">
-									<span class="txt text-overflow_ellipsis" :title="item.title">{{ item.title }}</span>
-									<span class="message-date">{{ item.createTime }}</span>
-								</div>
-								<span class="message-txt">{{ item.content }}</span>
-							</div>
-						</div>
-						<div class="message-fix-item" @click="jumpMessageInfo">查看更多</div>
-					</div>
-				</template>
-				<template v-else>
-					<LeNoData />
-				</template>
-			</el-tab-pane>
-		</el-tabs>
-	</el-popover>
-</template>
-
-<script setup lang="ts">
-import { ref, reactive } from 'vue'
-import message from '@/api/system/message'
-import router from '@/router'
-// noticeList  通知     messageList   消息     todoList   待办
-const tabsConfig = reactive({
-	noticeList: {
-		name: 'noticeList',
-		label: '通知',
-		list: []
-	},
-	messageList: {
-		name: 'messageList',
-		label: '消息',
-		list: []
-	},
-	todoList: {
-		name: 'todoList',
-		label: '待办',
-		list: []
-	}
-})
-const activeTab = ref('noticeList')
-const total = ref(0)
-message.getMessage().then(res => {
-	let _total = 0
-	Object.keys(tabsConfig).map(key => {
-		if (res[key]) {
-			tabsConfig[key].list = res[key]
-			_total += res[key].length
-		} else {
-			res[key].list = []
-		}
-	})
-	total.value = _total
-})
-
-const jumpMessageInfo = () => {
-	router.push('/message/list')
-}
-
-const jumpMessageDetail = (id: any) => {
-	router.push('/message/list?id=' + id)
-}
-</script>
-
-<style scoped lang="scss">
-.message-list {
-	display: flex;
-	flex-direction: column;
-	max-height: 390px;
-	overflow-y: auto;
-	.message-item {
-		display: flex;
-		align-items: center;
-		padding: 8px 0;
-		border-bottom: 1px solid var(--el-border-color-light);
-		cursor: pointer;
-		&:last-child {
-			border: none;
-		}
-		.message-content {
-			display: flex;
-			flex-direction: column;
-			width: 100%;
-			.message-title {
-				display: flex;
-				align-items: center;
-				justify-content: space-between;
-				margin-bottom: 5px;
-				font-weight: 600;
-				color: var(--el-text-color-primary);
-			}
-			.message-date {
-				font-size: 12px;
-				color: var(--el-text-color-secondary);
-				margin-left: 10px;
-				font-weight: 400;
-				flex-shrink: 0;
-			}
-			.message-txt {
-				//margin-bottom: 5px;
-				color: var(--el-text-color-regular);
-			}
-		}
-	}
-	.message-fix-item {
-		// flex 居中
-		display: flex;
-		align-items: center;
-		justify-content: center;
-		height: 40px;
-		cursor: pointer;
-	}
-}
-.menu--message-trigger {
-	height: 100%;
-}
-.menu--message {
-	width: 40px;
-	justify-content: center;
-	//.le-notice {
-	//.le-iconfont {
-	//}
-}
-</style>

+ 17 - 12
src/views/codeGenerate/database/CodeGenDrawer.vue

@@ -55,6 +55,7 @@
 </template>
 
 <script lang="ts" setup>
+import axios from 'axios';
 import ItemDrawer from '../template/ItemDrawer.vue'
 import { ref, watch } from 'vue'
 import database from '@/api/codeGenerate/database'
@@ -102,7 +103,7 @@ const emit = defineEmits(['update:modelValue', 'confirm', 'cancel'])
 
 const drawerVisible = ref(props.modelValue)
 const dataSourceOptions = ref<Array<{ id: string; title: string }>>([])
-	const templateOptions = ref<Array<{ id: string; name: string }>>([])
+const templateOptions = ref<Array<{ id: string; name: string }>>([])
 const previewCodeVisible = ref(false)
 const activeData = ref<{ record: any; type: 'edit' | 'create' | 'detail'; visible: boolean }>({
 	record: {},
@@ -169,22 +170,26 @@ const openDrawer = (type: 'edit' | 'create' | 'detail', record = {}) => {
 const generateCode = async () => {
 	try {
 		await formRef.value.validate()
-		await database.codeDownloadApi({ ...form.value, templateIds: form.value.templateIds })
-		// const blob = new Blob([response.data], { type: 'application/octet-stream' })
-		// const url = window.URL.createObjectURL(blob)
-		// const a = document.createElement('a')
-		// a.href = url
-		// a.download = 'generated_code.zip' // Adjust the file name as needed
-		// document.body.appendChild(a)
-		// a.click()
-		// a.remove()
-		// window.URL.revokeObjectURL(url)
-		// console.log('文件下载成功')
+
+		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 a = document.createElement('a')
+		a.href = url
+		a.download = 'generated_code.zip' // 根据需要调整文件名
+		document.body.appendChild(a)
+		a.click()
+		a.remove()
+		window.URL.revokeObjectURL(url)
+		console.log('文件下载成功')
 	} catch (error) {
 		console.error('文件下载失败:', error)
 	}
 }
 
+const updateParams = () => {}
+
 getDataBaseOptions()
 loadTemplates()
 </script>