From 65fa961a7f8cd7d68d17e675abe177562342eefe Mon Sep 17 00:00:00 2001 From: vertoryao <673441990@qq.com> Date: Sun, 18 May 2025 01:17:03 +0800 Subject: [PATCH] =?UTF-8?q?refactor(@vben/web-antd):=20=E5=AE=8C=E6=88=90?= =?UTF-8?q?=E7=88=AC=E8=99=AB=E9=83=A8=E5=88=86=E6=9C=AA=E8=A7=A3=E5=86=B3?= =?UTF-8?q?=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/web-antd/src/api/core/spider.ts | 2 +- apps/web-antd/vite.config.mts | 14 +- .../src/ui/spider/spider-preview.vue | 3 +- .../src/ui/spider/spider-work-view.vue | 201 +++++++++--------- 4 files changed, 118 insertions(+), 102 deletions(-) diff --git a/apps/web-antd/src/api/core/spider.ts b/apps/web-antd/src/api/core/spider.ts index dce46ad..0830223 100644 --- a/apps/web-antd/src/api/core/spider.ts +++ b/apps/web-antd/src/api/core/spider.ts @@ -2,7 +2,7 @@ import { requestClient } from '#/api/request'; // 全国公共资源爬虫 export function runSpider(data: any) { - return requestClient.post(`/spider/run`, { data }); + return requestClient.post(`/spider/run`, data); } export function getSpiderStatus() { diff --git a/apps/web-antd/vite.config.mts b/apps/web-antd/vite.config.mts index d3b3a30..cfe1479 100644 --- a/apps/web-antd/vite.config.mts +++ b/apps/web-antd/vite.config.mts @@ -10,8 +10,8 @@ export default defineConfig(async () => { changeOrigin: true, rewrite: (path) => path.replace(/^\/api/, ''), // mock代理目标地址 - // target: 'http://localhost:8081/api', - target: 'http://43.139.10.64:8082/api', + target: 'http://localhost:8081/api', + // target: 'http://43.139.10.64:8082/api', // target: 'http://192.168.3.238:8081/api', ws: true, }, @@ -20,11 +20,21 @@ export default defineConfig(async () => { rewrite: (path) => path.replace(/^\/docx/, ''), target: 'http://47.112.173.8:6805/static', }, + '/guangzhou': { + changeOrigin: true, + rewrite: (path) => path.replace(/^\/guangzhou/, ''), + target: 'http://47.112.173.8:6800/', + }, '/pptx': { changeOrigin: true, rewrite: (path) => path.replace(/^\/pptx/, ''), target: 'http://47.112.173.8:6802/static', }, + '/spider': { + changeOrigin: true, + rewrite: (path) => path.replace(/^\/spider/, ''), + target: 'http://47.112.173.8:6806/static', + }, '/v1': { changeOrigin: true, rewrite: (path) => path.replace(/^\/v1/, ''), diff --git a/packages/effects/common-ui/src/ui/spider/spider-preview.vue b/packages/effects/common-ui/src/ui/spider/spider-preview.vue index 6a407ae..5b71c97 100644 --- a/packages/effects/common-ui/src/ui/spider/spider-preview.vue +++ b/packages/effects/common-ui/src/ui/spider/spider-preview.vue @@ -36,7 +36,8 @@ const [Drawer, drawerApi] = useVbenDrawer({ const data = drawerApi.getData>(); if (data) { isLoading.value = false; // 出错时也关闭加载状态 - docx.value = `/docx/${data}`; // 更新 docx 的值 + // docx.value = `/spider/${data}`; // 更新 docx 的值 + docx.value = data; // 更新 docx 的值 } // url.value = drawerApi.getData>(); } diff --git a/packages/effects/common-ui/src/ui/spider/spider-work-view.vue b/packages/effects/common-ui/src/ui/spider/spider-work-view.vue index c868b7e..ebbbcb9 100644 --- a/packages/effects/common-ui/src/ui/spider/spider-work-view.vue +++ b/packages/effects/common-ui/src/ui/spider/spider-work-view.vue @@ -130,6 +130,87 @@ function extractDocxFilename(url: string): null | string { const match = url.match(/\/static\/([^/]+\.docx)$/i); // 匹配 /static/ 后任意非斜杠字符加 .docx return match ? match[1] : null; } +const startChecking = () => { + statusPollingInterval = window.setInterval(startPolling, 1000); +}; +let statusPollingInterval: null | number; +let pollingCount = 0; +const maxPollingAttempts = 300; // 最多轮询300秒 + +const startPolling = async () => { + try { + const response = await props.getSpiderStatus(); + + if (!response.data.is_running) { + clearInterval(statusPollingInterval!); + statusPollingInterval = null; + + if (response.data.download_url) { + const download_url = response.data.download_url; + fetchResult.value = download_url; + notification.success({ message: '获取成功', duration: 3 }); + + let filename = ''; + if (isDocxURL(download_url)) { + filename = extractDocxFilename(download_url); + } + + fetchResult.value = `/spider/${filename}`; + resultItems.value.push({ + key: resultItems.value.length + 1, + role: 'ai', + content: '文档已生成', + footer: h(Flex, null, [ + h( + Button, + { + size: 'normal', + type: 'primary', + onClick: () => { + openPreviewDrawer('right', fetchResult); + }, + }, + '文档预览', + ), + h( + Button, + { + size: 'normal', + type: 'primary', + style: { marginLeft: '10px' }, + onClick: () => { + const link = document.createElement('a'); + link.href = download_url; + link.download = fetchResult.value; + document.body.append(link); + link.click(); + link.remove(); + }, + }, + '文档下载', + ), + ]), + }); + + isFetching.value = false; + fetchStatus.value = 'completed'; + } else { + isFetching.value = false; + fetchResult.value = ''; + message.error('抓取无结果'); + } + } else if (++pollingCount >= maxPollingAttempts) { + clearInterval(statusPollingInterval!); + statusPollingInterval = null; + props.stopSpider(); + message.warn('轮询超时,请稍后再试'); + } + } catch (error) { + clearInterval(statusPollingInterval!); + statusPollingInterval = null; + message.error(`状态查询失败:${error}`); + } +}; const startFetching = async () => { // 更新状态为“抓取中” @@ -149,7 +230,7 @@ const startFetching = async () => { try { notification.info({ message: '正在获取中...', - duration: 3, + duration: 5, }); const res = await props.runSpider({ @@ -158,94 +239,19 @@ const startFetching = async () => { llm_api_key: props.item.id, }); - if (res.code !== 0) { + if (res.code === 0) { + notification.info({ + message: res.msg, + duration: 5, + }); + startChecking(); + } else { isFetching.value = false; fetchResult.value = ''; message.error('抓取无结果'); return; } - let statusPollingInterval: null | number = null; - let pollingCount = 0; - const maxPollingAttempts = 300; // 最多轮询300秒 - - const startPolling = async () => { - try { - const response = await props.getSpiderStatus(); - - if (!response.data.is_running) { - clearInterval(statusPollingInterval!); - statusPollingInterval = null; - - if (response.data.download_url) { - const download_url = response.data.download_url; - fetchResult.value = download_url; - notification.success({ message: '获取成功', duration: 3 }); - - let filename = ''; - if (isDocxURL(download_url)) { - filename = extractDocxFilename(download_url); - } - - fetchResult.value = `/static/${filename}`; - resultItems.value.push({ - key: resultItems.value.length + 1, - role: 'ai', - content: '文档已生成', - footer: h(Flex, null, [ - h( - Button, - { - size: 'normal', - type: 'primary', - onClick: () => { - openPreviewDrawer('right', safeFilename); - }, - }, - '文档预览', - ), - h( - Button, - { - size: 'normal', - type: 'primary', - style: { marginLeft: '10px' }, - onClick: () => { - const link = document.createElement('a'); - link.href = download_url; - link.download = safeFilename; - document.body.append(link); - link.click(); - link.remove(); - }, - }, - '文档下载', - ), - ]), - }); - - isFetching.value = false; - fetchStatus.value = 'completed'; - } else { - isFetching.value = false; - fetchResult.value = ''; - message.error('抓取无结果'); - } - } else if (++pollingCount >= maxPollingAttempts) { - clearInterval(statusPollingInterval!); - statusPollingInterval = null; - props.stopSpider(); - message.warn('轮询超时,请稍后再试'); - } - } catch (error) { - clearInterval(statusPollingInterval!); - statusPollingInterval = null; - message.error(`状态查询失败:${error}`); - } - }; - - statusPollingInterval = window.setInterval(startPolling, 1000); - // 组件卸载时清理定时器(需配合 onUnmounted 生命周期) // 示例: // onUnmounted(() => { @@ -288,25 +294,24 @@ const startFetching = async () => { }); const fileUrl = ref(''); - fileUrl.value = `http://47.112.173.8:6800${fetchResult.value}`; + fileUrl.value = `/guangzhou${fetchResult.value}`; - // 保存抓取结果 url http://47.112.173.8:6802/static/66f3cfd95e364a239d8036390db658ae.docx resultItems.value.push({ key: resultItems.value.length + 1, role: 'ai', - content: '文档已生成', + content: '文档已生成,有效时间5分钟,请及时查看或下载', footer: h(Flex, null, [ - // h( - // Button, - // { - // size: 'nomarl', - // type: 'primary', - // onClick: () => { - // openPreviewDrawer('right', fetchResult); - // }, - // }, - // '文档预览', - // ), + h( + Button, + { + size: 'nomarl', + type: 'primary', + onClick: () => { + openPreviewDrawer('right', fileUrl); + }, + }, + '文档预览', + ), h( Button, { @@ -318,7 +323,7 @@ const startFetching = async () => { onClick: () => { // 创建隐藏的 标签用于触发下载 const link = document.createElement('a'); - link.href = fileUrl; // 设置下载链接 + link.href = fileUrl.value; // 设置下载链接 link.download = '广州公共资源交易中心数据获取'; // 设置下载文件名 document.body.append(link); // 将 标签添加到页面中 link.click(); // 触发点击事件开始下载