2025-05-05 16:33:06 +08:00
|
|
|
<script setup lang="ts">
|
|
|
|
import type { SpiderItem } from './typing';
|
|
|
|
|
|
|
|
import { ref } from 'vue';
|
|
|
|
|
|
|
|
import {
|
|
|
|
Button,
|
|
|
|
Card,
|
|
|
|
CardContent,
|
|
|
|
CardFooter,
|
|
|
|
CardHeader,
|
|
|
|
CardTitle,
|
|
|
|
} from '@vben-core/shadcn-ui';
|
|
|
|
|
2025-05-13 16:51:40 +08:00
|
|
|
import { message, RangePicker } from 'ant-design-vue';
|
|
|
|
import dayjs from 'dayjs';
|
2025-05-12 19:52:53 +08:00
|
|
|
|
2025-05-05 16:33:06 +08:00
|
|
|
interface SpiderParams {
|
|
|
|
appid: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
interface SpiderContext {
|
|
|
|
userId: string;
|
|
|
|
conversationId: string;
|
|
|
|
files: unknown[];
|
|
|
|
inputs: Record<string, unknown>;
|
|
|
|
}
|
|
|
|
|
|
|
|
interface SpiderResult {
|
|
|
|
data: {
|
|
|
|
outputs: {
|
|
|
|
result: string;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
interface Props {
|
|
|
|
item?: SpiderItem;
|
|
|
|
title: string;
|
|
|
|
runSpider?: (
|
|
|
|
params: SpiderParams,
|
|
|
|
context: SpiderContext,
|
|
|
|
) => Promise<SpiderResult>;
|
|
|
|
}
|
|
|
|
|
|
|
|
defineOptions({
|
|
|
|
name: 'SpiderWorkView',
|
|
|
|
});
|
|
|
|
|
|
|
|
const props = withDefaults(defineProps<Props>(), {
|
|
|
|
item: () => {
|
|
|
|
return null;
|
|
|
|
},
|
|
|
|
runSpider: () => async () => ({
|
|
|
|
data: {
|
|
|
|
outputs: {
|
|
|
|
result: '',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
});
|
|
|
|
|
2025-05-12 19:52:53 +08:00
|
|
|
const selectedDateRange = ref<string[]>([]);
|
|
|
|
|
2025-05-05 16:33:06 +08:00
|
|
|
const startFetching = async () => {
|
|
|
|
// 更新状态为“抓取中”
|
|
|
|
isFetching.value = true;
|
|
|
|
fetchStatus.value = 'fetching';
|
|
|
|
|
2025-05-13 16:51:40 +08:00
|
|
|
let publish_start_time = ''; // 默认值
|
|
|
|
let publish_end_time = ''; // 默认值
|
2025-05-12 19:52:53 +08:00
|
|
|
|
|
|
|
if (selectedDateRange.value && selectedDateRange.value.length === 2) {
|
2025-05-13 16:51:40 +08:00
|
|
|
publish_start_time =
|
|
|
|
dayjs(selectedDateRange.value[0]).format('YYYYMMDDhhmmss') || '';
|
|
|
|
publish_end_time =
|
|
|
|
dayjs(selectedDateRange.value[1]).format('YYYYMMDDhhmmss') || '';
|
2025-05-12 19:52:53 +08:00
|
|
|
}
|
|
|
|
|
2025-05-05 16:33:06 +08:00
|
|
|
try {
|
|
|
|
const res = await props.runSpider(
|
|
|
|
{
|
2025-05-05 23:35:12 +08:00
|
|
|
appid: props.item.id,
|
2025-05-05 16:33:06 +08:00
|
|
|
},
|
|
|
|
{
|
|
|
|
userId: '1562',
|
|
|
|
conversationId: '',
|
|
|
|
files: [],
|
2025-05-12 19:52:53 +08:00
|
|
|
inputs: {
|
2025-05-13 16:51:40 +08:00
|
|
|
publish_start_time,
|
|
|
|
publish_end_time,
|
2025-05-12 19:52:53 +08:00
|
|
|
},
|
2025-05-05 16:33:06 +08:00
|
|
|
},
|
|
|
|
);
|
2025-05-13 16:51:40 +08:00
|
|
|
if (res.data.outputs.result) {
|
|
|
|
// 保存抓取结果
|
|
|
|
fetchResult.value = res.data.outputs.result;
|
|
|
|
message.success('抓取成功');
|
|
|
|
} else {
|
|
|
|
fetchResult.value = '';
|
|
|
|
message.error('抓取无结果');
|
|
|
|
}
|
2025-05-05 16:33:06 +08:00
|
|
|
} catch (error) {
|
|
|
|
console.error(error);
|
|
|
|
}
|
|
|
|
|
|
|
|
// 模拟抓取完成
|
|
|
|
isFetching.value = false;
|
|
|
|
fetchStatus.value = 'completed';
|
|
|
|
};
|
|
|
|
|
|
|
|
// 下载文件逻辑
|
|
|
|
const downloadFile = () => {
|
2025-05-13 17:24:20 +08:00
|
|
|
if (!fetchResult.value || !fetchResult.value.startsWith('http')) {
|
|
|
|
message.error('无效的文件链接');
|
|
|
|
return;
|
|
|
|
}
|
2025-05-05 16:33:06 +08:00
|
|
|
|
2025-05-13 17:24:20 +08:00
|
|
|
const fileUrl = fetchResult.value;
|
|
|
|
const fileName = decodeURIComponent(
|
|
|
|
fileUrl.slice(Math.max(0, fileUrl.lastIndexOf('/') + 1)),
|
|
|
|
);
|
2025-05-05 16:33:06 +08:00
|
|
|
|
|
|
|
const link = document.createElement('a');
|
2025-05-13 17:24:20 +08:00
|
|
|
link.href = fileUrl;
|
2025-05-05 16:33:06 +08:00
|
|
|
link.download = fileName;
|
|
|
|
link.click();
|
|
|
|
};
|
|
|
|
|
|
|
|
const isFetching = ref(false);
|
|
|
|
const fetchResult = ref('');
|
|
|
|
const fetchStatus = ref('');
|
|
|
|
</script>
|
|
|
|
|
|
|
|
<template>
|
|
|
|
<div class="flex h-full">
|
|
|
|
<Card class="w-full self-end" v-loading="isFetching">
|
|
|
|
<CardHeader class="py-4">
|
|
|
|
<CardTitle class="text-lg">{{ title }}</CardTitle>
|
|
|
|
</CardHeader>
|
|
|
|
<CardContent class="flex flex-wrap pt-0">
|
|
|
|
{{ item ? item.name : '请选择右侧爬虫' }}
|
|
|
|
</CardContent>
|
|
|
|
<CardFooter class="flex justify-end">
|
2025-05-12 19:52:53 +08:00
|
|
|
<RangePicker
|
|
|
|
class="mx-2"
|
|
|
|
v-model:value="selectedDateRange"
|
2025-05-13 16:51:40 +08:00
|
|
|
format="YYYY/MM/DD"
|
2025-05-12 19:52:53 +08:00
|
|
|
/>
|
2025-05-13 17:24:20 +08:00
|
|
|
<Button
|
|
|
|
:disabled="!item || selectedDateRange.length < 2"
|
|
|
|
@click="startFetching"
|
|
|
|
>
|
2025-05-05 16:33:06 +08:00
|
|
|
{{ isFetching ? '抓取中...' : '开始抓取' }}
|
|
|
|
</Button>
|
2025-05-08 19:00:31 +08:00
|
|
|
<Button
|
|
|
|
class="mx-2"
|
|
|
|
:disabled="fetchStatus !== 'completed'"
|
|
|
|
@click="downloadFile"
|
|
|
|
>
|
2025-05-05 16:33:06 +08:00
|
|
|
下载文件
|
|
|
|
</Button>
|
|
|
|
</CardFooter>
|
|
|
|
</Card>
|
|
|
|
</div>
|
|
|
|
</template>
|