refactor: 重构前端代码,拆分组件并优化README文档

This commit is contained in:
dmy
2026-01-12 14:37:18 +08:00
parent 8b2f328981
commit 4f37b0fb61
9 changed files with 660 additions and 477 deletions

View File

@@ -31,167 +31,43 @@
</el-header>
<el-main>
<div v-if="activeIndex === '1'">
<div style="display: flex; justify-content: space-between; align-items: center; margin-bottom: 20px;">
<h2 style="margin: 0;">Dashboard</h2>
<el-button type="primary" :loading="crawling" :disabled="isCrawling" @click="handleCrawl">
<el-icon style="margin-right: 5px"><Refresh /></el-icon>
立刻抓取
</el-button>
</div>
<el-row :gutter="20">
<el-col :span="24">
<el-card class="box-card" shadow="hover">
<template #header>
<div class="card-header">
<span>High Priority Bids</span>
<el-tag type="danger">Top 10</el-tag>
</div>
</template>
<el-table :data="highPriorityBids" style="width: 100%" size="small">
<el-table-column prop="title" label="Title">
<template #default="scope">
<a :href="scope.row.url" target="_blank">{{ scope.row.title }}</a>
</template>
</el-table-column>
<el-table-column prop="source" label="Source" width="240" />
<el-table-column prop="publishDate" label="Date" width="120">
<template #default="scope">{{ formatDate(scope.row.publishDate) }}</template>
</el-table-column>
</el-table>
</el-card>
</el-col>
</el-row>
<el-divider />
<div style="display: flex; justify-content: space-between; align-items: center; margin-bottom: 10px;">
<h3 style="margin: 0;">Today's Bids</h3>
<div style="display: flex; gap: 10px;">
<el-date-picker
v-model="dateRange"
type="daterange"
range-separator="To"
start-placeholder="Start Date"
end-placeholder="End Date"
format="YYYY-MM-DD"
value-format="YYYY-MM-DD"
clearable
style="width: 240px;"
/>
<el-button type="primary" @click="setLast3Days">3天</el-button>
<el-button type="primary" @click="setLast7Days">7天</el-button>
<el-select
v-model="selectedKeywords"
multiple
collapse-tags
collapse-tags-tooltip
placeholder="Filter by Keywords"
clearable
style="width: 300px;"
>
<el-option
v-for="keyword in keywords"
:key="keyword.id"
:label="keyword.word"
:value="keyword.word"
/>
</el-select>
</div>
</div>
<el-table :data="filteredTodayBids" v-loading="loading" style="width: 100%">
<el-table-column prop="title" label="Title">
<template #default="scope">
<a :href="scope.row.url" target="_blank">{{ scope.row.title }}</a>
</template>
</el-table-column>
<el-table-column prop="source" label="Source" width="220" />
<el-table-column prop="publishDate" label="Date" width="150">
<template #default="scope">{{ formatDate(scope.row.publishDate) }}</template>
</el-table-column>
</el-table>
</div>
<Dashboard
v-if="activeIndex === '1'"
:today-bids="todayBids"
:high-priority-bids="highPriorityBids"
:keywords="keywords"
:loading="loading"
:is-crawling="isCrawling"
@refresh="fetchData"
/>
<div v-if="activeIndex === '2'">
<div style="display: flex; justify-content: space-between; align-items: center; margin-bottom: 20px;">
<h2 style="margin: 0;">All Bids</h2>
<el-select v-model="selectedSource" placeholder="Filter by Source" clearable style="width: 200px" @change="currentPage = 1; fetchData()">
<el-option
v-for="source in sourceOptions"
:key="source"
:label="source"
:value="source"
/>
</el-select>
</div>
<el-table :data="bids" v-loading="loading" style="width: 100%">
<el-table-column prop="title" label="Title">
<template #default="scope">
<a :href="scope.row.url" target="_blank">{{ scope.row.title }}</a>
</template>
</el-table-column>
<el-table-column prop="source" label="Source" width="200" />
<el-table-column prop="publishDate" label="Date" width="150">
<template #default="scope">{{ formatDate(scope.row.publishDate) }}</template>
</el-table-column>
</el-table>
<el-pagination
v-model:current-page="currentPage"
v-model:page-size="pageSize"
:page-sizes="[10, 20, 50, 100]"
:total="total"
layout="total, sizes, prev, pager, next, jumper"
@current-change="handlePageChange"
@size-change="handleSizeChange"
style="margin-top: 20px; justify-content: flex-end;"
/>
</div>
<Bids
v-if="activeIndex === '2'"
:bids="bids"
:source-options="sourceOptions"
:loading="loading"
:total="total"
@fetch="handleFetchBids"
/>
<div v-if="activeIndex === '3'">
<div class="card-header" style="display: flex; justify-content: space-between; align-items: center; margin-bottom: 20px;">
<h2>Keyword Management</h2>
<el-button type="primary" @click="dialogVisible = true">Add Keyword</el-button>
</div>
<div v-loading="loading" style="min-height: 200px;">
<el-tag
v-for="keyword in keywords"
:key="keyword.id"
closable
:type="getTagType(keyword.weight)"
@close="handleDeleteKeyword(keyword.id)"
style="margin: 5px;"
>
{{ keyword.word }}
</el-tag>
<el-empty v-if="keywords.length === 0" description="No keywords" />
</div>
</div>
<Keywords
v-if="activeIndex === '3'"
:keywords="keywords"
:loading="loading"
@refresh="fetchData"
/>
</el-main>
</el-container>
<el-dialog v-model="dialogVisible" title="Add Keyword" width="30%">
<el-form :model="form" label-width="120px">
<el-form-item label="Keyword">
<el-input v-model="form.word" />
</el-form-item>
<el-form-item label="Weight">
<el-input-number v-model="form.weight" :min="1" :max="5" />
</el-form-item>
</el-form>
<template #footer>
<span class="dialog-footer">
<el-button @click="dialogVisible = false">Cancel</el-button>
<el-button type="primary" @click="handleAddKeyword">Confirm</el-button>
</span>
</template>
</el-dialog>
</el-container>
</template>
<script setup lang="ts">
import { ref, onMounted, reactive, computed, watch } from 'vue'
import { ref, onMounted } from 'vue'
import axios from 'axios'
import { ElMessage } from 'element-plus'
import { DataBoard, Document, Setting, Refresh } from '@element-plus/icons-vue'
import { DataBoard, Document, Setting } from '@element-plus/icons-vue'
import Dashboard from './components/Dashboard.vue'
import Bids from './components/Bids.vue'
import Keywords from './components/Keywords.vue'
const activeIndex = ref('1')
const bids = ref<any[]>([])
@@ -199,192 +75,32 @@ const todayBids = ref<any[]>([])
const highPriorityBids = ref<any[]>([])
const keywords = ref<any[]>([])
const loading = ref(false)
const crawling = ref(false)
const dialogVisible = ref(false)
const selectedSource = ref('')
const currentPage = ref(1)
const pageSize = ref(10)
const isCrawling = ref(false)
const total = ref(0)
const sourceOptions = ref<string[]>([])
const isCrawling = ref(false)
const selectedKeywords = ref<string[]>([])
const dateRange = ref<[string, string] | null>(null)
// 从 localStorage 加载保存的关键字
const loadSavedKeywords = () => {
const saved = localStorage.getItem('selectedKeywords')
if (saved) {
try {
selectedKeywords.value = JSON.parse(saved)
} catch (e) {
console.error('Failed to parse saved keywords:', e)
}
}
}
// 监听关键字变化并保存到 localStorage
watch(selectedKeywords, (newKeywords) => {
localStorage.setItem('selectedKeywords', JSON.stringify(newKeywords))
}, { deep: true })
// 监听日期范围变化并显示提示
watch(dateRange, () => {
const totalBids = bids.value.length
const filteredCount = filteredTodayBids.value.length
if (totalBids > 0 && filteredCount < totalBids) {
ElMessage.info(`筛选结果:共 ${filteredCount} 条数据(总共 ${totalBids} 条)`)
}
})
const form = reactive({
word: '',
weight: 1
})
// 根据 weight 获取 tag 类型
const getTagType = (weight: number) => {
if (weight >= 5) return 'danger'
if (weight >= 4) return 'warning'
if (weight >= 3) return 'primary'
if (weight >= 2) return 'success'
return 'info'
}
const handleSelect = (key: string) => {
activeIndex.value = key
}
// 处理分页变化
const handlePageChange = (page: number) => {
currentPage.value = page
fetchData()
}
// 处理每页数量变化
const handleSizeChange = (size: number) => {
pageSize.value = size
currentPage.value = 1
fetchData()
}
// 设置日期范围为最近3天
const setLast3Days = () => {
const endDate = new Date()
const startDate = new Date()
startDate.setDate(startDate.getDate() - 2) // 最近3天包括今天
const formatDateForPicker = (date: Date) => {
const year = date.getFullYear()
const month = String(date.getMonth() + 1).padStart(2, '0')
const day = String(date.getDate()).padStart(2, '0')
return `${year}-${month}-${day}`
}
dateRange.value = [formatDateForPicker(startDate), formatDateForPicker(endDate)]
console.log('setLast3Days called, todayBids:', todayBids.value.length, 'dateRange:', dateRange.value)
// 直接计算筛选结果并显示提示(只限制开始时间,不限制结束时间)
const start = new Date(startDate)
start.setHours(0, 0, 0, 0)
let result = todayBids.value
result = result.filter(bid => {
if (!bid.publishDate) return false
const bidDate = new Date(bid.publishDate)
return bidDate >= start
})
const totalBids = todayBids.value.length
const filteredCount = result.length
console.log('setLast3Days result, totalBids:', totalBids, 'filteredCount:', filteredCount)
if (totalBids === 0) {
ElMessage.warning('暂无数据请先抓取数据')
}
}
// 设置日期范围为最近7天
const setLast7Days = () => {
const endDate = new Date()
const startDate = new Date()
startDate.setDate(startDate.getDate() - 6) // 最近7天包括今天
const formatDateForPicker = (date: Date) => {
const year = date.getFullYear()
const month = String(date.getMonth() + 1).padStart(2, '0')
const day = String(date.getDate()).padStart(2, '0')
return `${year}-${month}-${day}`
}
dateRange.value = [formatDateForPicker(startDate), formatDateForPicker(endDate)]
console.log('setLast7Days called, todayBids:', todayBids.value.length, 'dateRange:', dateRange.value)
// 直接计算筛选结果并显示提示(只限制开始时间,不限制结束时间)
const start = new Date(startDate)
start.setHours(0, 0, 0, 0)
let result = todayBids.value
result = result.filter(bid => {
if (!bid.publishDate) return false
const bidDate = new Date(bid.publishDate)
return bidDate >= start
})
const totalBids = todayBids.value.length
const filteredCount = result.length
console.log('setLast7Days result, totalBids:', totalBids, 'filteredCount:', filteredCount)
if (totalBids === 0) {
ElMessage.warning('暂无数据请先抓取数据')
}
}
const formatDate = (dateString: string) => {
if (!dateString) return '-'
return new Date(dateString).toLocaleDateString()
}
// 过滤 Today's Bids只显示包含所选关键字的项目并且在日期范围内
const filteredTodayBids = computed(() => {
let result = todayBids.value
// 按关键字筛选
if (selectedKeywords.value.length > 0) {
result = result.filter(bid => {
return selectedKeywords.value.some(keyword =>
bid.title.toLowerCase().includes(keyword.toLowerCase())
)
const handleFetchBids = async (page: number, limit: number, source?: string) => {
loading.value = true
try {
const res = await axios.get('/api/bids', {
params: {
page,
limit,
source: source || undefined
}
})
bids.value = res.data.items
total.value = res.data.total
} catch (error) {
console.error('Failed to fetch bids:', error)
} finally {
loading.value = false
}
// 按日期范围筛选(只限制开始时间,不限制结束时间)
if (dateRange.value && dateRange.value.length === 2) {
const [startDate] = dateRange.value
result = result.filter(bid => {
if (!bid.publishDate) return false
const bidDate = new Date(bid.publishDate)
const start = new Date(startDate)
// 设置时间为当天的开始
start.setHours(0, 0, 0, 0)
return bidDate >= start
})
}
return result
})
// 监听筛选结果变化并显示提示
watch(filteredTodayBids, (newFilteredBids) => {
const totalBids = todayBids.value.length
const filteredCount = newFilteredBids.length
if (totalBids > 0 && filteredCount < totalBids) {
ElMessage.info(`筛选结果:共 ${filteredCount} 条数据(总共 ${totalBids} 条)`)
}
}, { deep: true })
}
const fetchData = async () => {
loading.value = true
@@ -392,9 +108,8 @@ const fetchData = async () => {
const [bidsRes, recentRes, highRes, kwRes, sourcesRes, statusRes] = await Promise.all([
axios.get('/api/bids', {
params: {
page: currentPage.value,
limit: pageSize.value,
source: selectedSource.value || undefined
page: 1,
limit: 10
}
}),
axios.get('/api/bids/recent'),
@@ -411,58 +126,13 @@ const fetchData = async () => {
sourceOptions.value = sourcesRes.data
isCrawling.value = statusRes.data.isCrawling
} catch (error) {
ElMessage.error('Failed to fetch data')
console.error('Failed to fetch data:', error)
} finally {
loading.value = false
}
}
const handleCrawl = async () => {
if (isCrawling.value) {
ElMessage.warning('Crawl is already running')
return
}
crawling.value = true
try {
await axios.post('/api/crawler/run')
ElMessage.success('Crawl completed successfully')
fetchData() // Refresh data after crawl
} catch (error) {
ElMessage.error('Failed to run crawl task')
} finally {
crawling.value = false
}
}
const handleAddKeyword = async () => {
if (!form.word) {
ElMessage.warning('Please enter a keyword')
return
}
try {
await axios.post('/api/keywords', form)
ElMessage.success('Keyword added')
dialogVisible.value = false
form.word = ''
form.weight = 1
fetchData()
} catch (error) {
ElMessage.error('Failed to add keyword')
}
}
const handleDeleteKeyword = async (id: string) => {
try {
await axios.delete(`/api/keywords/${id}`)
ElMessage.success('Keyword deleted')
fetchData()
} catch (error) {
ElMessage.error('Failed to delete keyword')
}
}
onMounted(() => {
loadSavedKeywords()
fetchData()
})
</script>
@@ -486,9 +156,4 @@ onMounted(() => {
font-size: 18px;
background-color: #434a50;
}
.card-header {
display: flex;
justify-content: space-between;
align-items: center;
}
</style>
</style>

View File

@@ -0,0 +1,78 @@
<template>
<div>
<div style="display: flex; justify-content: space-between; align-items: center; margin-bottom: 20px;">
<h2 style="margin: 0;">All Bids</h2>
<el-select v-model="selectedSource" placeholder="Filter by Source" clearable style="width: 200px" @change="handleSourceChange">
<el-option
v-for="source in sourceOptions"
:key="source"
:label="source"
:value="source"
/>
</el-select>
</div>
<el-table :data="bids" v-loading="loading" style="width: 100%">
<el-table-column prop="title" label="Title">
<template #default="scope">
<a :href="scope.row.url" target="_blank">{{ scope.row.title }}</a>
</template>
</el-table-column>
<el-table-column prop="source" label="Source" width="200" />
<el-table-column prop="publishDate" label="Date" width="150">
<template #default="scope">{{ formatDate(scope.row.publishDate) }}</template>
</el-table-column>
</el-table>
<el-pagination
v-model:current-page="currentPage"
v-model:page-size="pageSize"
:page-sizes="[10, 20, 50, 100]"
:total="total"
layout="total, sizes, prev, pager, next, jumper"
@current-change="handlePageChange"
@size-change="handleSizeChange"
style="margin-top: 20px; justify-content: flex-end;"
/>
</div>
</template>
<script setup lang="ts">
import { ref } from 'vue'
interface Props {
bids: any[]
sourceOptions: string[]
loading: boolean
total: number
}
const props = defineProps<Props>()
const emit = defineEmits<{
fetch: [page: number, limit: number, source?: string]
}>()
const selectedSource = ref('')
const currentPage = ref(1)
const pageSize = ref(10)
const formatDate = (dateString: string) => {
if (!dateString) return '-'
return new Date(dateString).toLocaleDateString()
}
const handleSourceChange = () => {
currentPage.value = 1
emit('fetch', currentPage.value, pageSize.value, selectedSource.value || undefined)
}
const handlePageChange = (page: number) => {
currentPage.value = page
emit('fetch', currentPage.value, pageSize.value, selectedSource.value || undefined)
}
const handleSizeChange = (size: number) => {
pageSize.value = size
currentPage.value = 1
emit('fetch', currentPage.value, pageSize.value, selectedSource.value || undefined)
}
</script>

View File

@@ -0,0 +1,279 @@
<template>
<div>
<div style="display: flex; justify-content: space-between; align-items: center; margin-bottom: 20px;">
<h2 style="margin: 0;">Dashboard</h2>
<el-button type="primary" :loading="crawling" :disabled="isCrawling" @click="handleCrawl">
<el-icon style="margin-right: 5px"><Refresh /></el-icon>
立刻抓取
</el-button>
</div>
<el-row :gutter="20">
<el-col :span="24">
<el-card class="box-card" shadow="hover">
<template #header>
<div class="card-header">
<span>High Priority Bids</span>
<el-tag type="danger">Top 10</el-tag>
</div>
</template>
<el-table :data="highPriorityBids" style="width: 100%" size="small">
<el-table-column prop="title" label="Title">
<template #default="scope">
<a :href="scope.row.url" target="_blank">{{ scope.row.title }}</a>
</template>
</el-table-column>
<el-table-column prop="source" label="Source" width="240" />
<el-table-column prop="publishDate" label="Date" width="120">
<template #default="scope">{{ formatDate(scope.row.publishDate) }}</template>
</el-table-column>
</el-table>
</el-card>
</el-col>
</el-row>
<el-divider />
<div style="display: flex; justify-content: space-between; align-items: center; margin-bottom: 10px;">
<h3 style="margin: 0;">Today's Bids</h3>
<div style="display: flex; gap: 10px;">
<el-date-picker
v-model="dateRange"
type="daterange"
range-separator="To"
start-placeholder="Start Date"
end-placeholder="End Date"
format="YYYY-MM-DD"
value-format="YYYY-MM-DD"
clearable
style="width: 240px;"
/>
<el-button type="primary" @click="setLast3Days">3天</el-button>
<el-button type="primary" @click="setLast7Days">7天</el-button>
<el-select
v-model="selectedKeywords"
multiple
collapse-tags
collapse-tags-tooltip
placeholder="Filter by Keywords"
clearable
style="width: 300px;"
>
<el-option
v-for="keyword in keywords"
:key="keyword.id"
:label="keyword.word"
:value="keyword.word"
/>
</el-select>
</div>
</div>
<el-table :data="filteredTodayBids" v-loading="loading" style="width: 100%">
<el-table-column prop="title" label="Title">
<template #default="scope">
<a :href="scope.row.url" target="_blank">{{ scope.row.title }}</a>
</template>
</el-table-column>
<el-table-column prop="source" label="Source" width="220" />
<el-table-column prop="publishDate" label="Date" width="150">
<template #default="scope">{{ formatDate(scope.row.publishDate) }}</template>
</el-table-column>
</el-table>
</div>
</template>
<script setup lang="ts">
import { ref, computed, watch } from 'vue'
import axios from 'axios'
import { ElMessage } from 'element-plus'
import { Refresh } from '@element-plus/icons-vue'
interface Props {
todayBids: any[]
highPriorityBids: any[]
keywords: any[]
loading: boolean
isCrawling: boolean
}
const props = defineProps<Props>()
const emit = defineEmits<{
crawl: []
refresh: []
}>()
const selectedKeywords = ref<string[]>([])
const dateRange = ref<[string, string] | null>(null)
const crawling = ref(false)
// 从 localStorage 加载保存的关键字
const loadSavedKeywords = () => {
const saved = localStorage.getItem('selectedKeywords')
if (saved) {
try {
selectedKeywords.value = JSON.parse(saved)
} catch (e) {
console.error('Failed to parse saved keywords:', e)
}
}
}
// 监听关键字变化并保存到 localStorage
watch(selectedKeywords, (newKeywords) => {
localStorage.setItem('selectedKeywords', JSON.stringify(newKeywords))
}, { deep: true })
// 监听日期范围变化并显示提示
watch(dateRange, () => {
const totalBids = props.todayBids.length
const filteredCount = filteredTodayBids.value.length
if (totalBids > 0 && filteredCount < totalBids) {
ElMessage.info(`筛选结果:共 ${filteredCount} 条数据(总共 ${totalBids} 条)`)
}
})
const formatDate = (dateString: string) => {
if (!dateString) return '-'
return new Date(dateString).toLocaleDateString()
}
// 过滤 Today's Bids只显示包含所选关键字的项目并且在日期范围内
const filteredTodayBids = computed(() => {
let result = props.todayBids
// 按关键字筛选
if (selectedKeywords.value.length > 0) {
result = result.filter(bid => {
return selectedKeywords.value.some(keyword =>
bid.title.toLowerCase().includes(keyword.toLowerCase())
)
})
}
// 按日期范围筛选(只限制开始时间,不限制结束时间)
if (dateRange.value && dateRange.value.length === 2) {
const [startDate] = dateRange.value
result = result.filter(bid => {
if (!bid.publishDate) return false
const bidDate = new Date(bid.publishDate)
const start = new Date(startDate)
// 设置时间为当天的开始
start.setHours(0, 0, 0, 0)
return bidDate >= start
})
}
return result
})
// 监听筛选结果变化并显示提示
watch(filteredTodayBids, (newFilteredBids) => {
const totalBids = props.todayBids.length
const filteredCount = newFilteredBids.length
if (totalBids > 0 && filteredCount < totalBids) {
ElMessage.info(`筛选结果:共 ${filteredCount} 条数据(总共 ${totalBids} 条)`)
}
}, { deep: true })
// 设置日期范围为最近3天
const setLast3Days = () => {
const endDate = new Date()
const startDate = new Date()
startDate.setDate(startDate.getDate() - 2) // 最近3天包括今天
const formatDateForPicker = (date: Date) => {
const year = date.getFullYear()
const month = String(date.getMonth() + 1).padStart(2, '0')
const day = String(date.getDate()).padStart(2, '0')
return `${year}-${month}-${day}`
}
dateRange.value = [formatDateForPicker(startDate), formatDateForPicker(endDate)]
console.log('setLast3Days called, todayBids:', props.todayBids.length, 'dateRange:', dateRange.value)
// 直接计算筛选结果并显示提示(只限制开始时间,不限制结束时间)
const start = new Date(startDate)
start.setHours(0, 0, 0, 0)
let result = props.todayBids
result = result.filter(bid => {
if (!bid.publishDate) return false
const bidDate = new Date(bid.publishDate)
return bidDate >= start
})
const totalBids = props.todayBids.length
const filteredCount = result.length
console.log('setLast3Days result, totalBids:', totalBids, 'filteredCount:', filteredCount)
if (totalBids === 0) {
ElMessage.warning('暂无数据,请先抓取数据')
}
}
// 设置日期范围为最近7天
const setLast7Days = () => {
const endDate = new Date()
const startDate = new Date()
startDate.setDate(startDate.getDate() - 6) // 最近7天包括今天
const formatDateForPicker = (date: Date) => {
const year = date.getFullYear()
const month = String(date.getMonth() + 1).padStart(2, '0')
const day = String(date.getDate()).padStart(2, '0')
return `${year}-${month}-${day}`
}
dateRange.value = [formatDateForPicker(startDate), formatDateForPicker(endDate)]
console.log('setLast7Days called, todayBids:', props.todayBids.length, 'dateRange:', dateRange.value)
// 直接计算筛选结果并显示提示(只限制开始时间,不限制结束时间)
const start = new Date(startDate)
start.setHours(0, 0, 0, 0)
let result = props.todayBids
result = result.filter(bid => {
if (!bid.publishDate) return false
const bidDate = new Date(bid.publishDate)
return bidDate >= start
})
const totalBids = props.todayBids.length
const filteredCount = result.length
console.log('setLast7Days result, totalBids:', totalBids, 'filteredCount:', filteredCount)
if (totalBids === 0) {
ElMessage.warning('暂无数据,请先抓取数据')
}
}
const handleCrawl = async () => {
if (props.isCrawling) {
ElMessage.warning('Crawl is already running')
return
}
crawling.value = true
try {
await axios.post('/api/crawler/run')
ElMessage.success('Crawl completed successfully')
emit('refresh') // Refresh data after crawl
} catch (error) {
ElMessage.error('Failed to run crawl task')
} finally {
crawling.value = false
}
}
// 初始化时加载保存的关键字
loadSavedKeywords()
</script>
<style scoped>
.card-header {
display: flex;
justify-content: space-between;
align-items: center;
}
</style>

View File

@@ -1,41 +0,0 @@
<script setup lang="ts">
import { ref } from 'vue'
defineProps<{ msg: string }>()
const count = ref(0)
</script>
<template>
<h1>{{ msg }}</h1>
<div class="card">
<button type="button" @click="count++">count is {{ count }}</button>
<p>
Edit
<code>components/HelloWorld.vue</code> to test HMR
</p>
</div>
<p>
Check out
<a href="https://vuejs.org/guide/quick-start.html#local" target="_blank"
>create-vue</a
>, the official Vue + Vite starter
</p>
<p>
Learn more about IDE Support for Vue in the
<a
href="https://vuejs.org/guide/scaling-up/tooling.html#ide-support"
target="_blank"
>Vue Docs Scaling up Guide</a
>.
</p>
<p class="read-the-docs">Click on the Vite and Vue logos to learn more</p>
</template>
<style scoped>
.read-the-docs {
color: #888;
}
</style>

View File

@@ -0,0 +1,107 @@
<template>
<div>
<div class="card-header" style="display: flex; justify-content: space-between; align-items: center; margin-bottom: 20px;">
<h2>Keyword Management</h2>
<el-button type="primary" @click="dialogVisible = true">Add Keyword</el-button>
</div>
<div v-loading="loading" style="min-height: 200px;">
<el-tag
v-for="keyword in keywords"
:key="keyword.id"
closable
:type="getTagType(keyword.weight)"
@close="handleDeleteKeyword(keyword.id)"
style="margin: 5px;"
>
{{ keyword.word }}
</el-tag>
<el-empty v-if="keywords.length === 0" description="No keywords" />
</div>
<el-dialog v-model="dialogVisible" title="Add Keyword" width="30%">
<el-form :model="form" label-width="120px">
<el-form-item label="Keyword">
<el-input v-model="form.word" />
</el-form-item>
<el-form-item label="Weight">
<el-input-number v-model="form.weight" :min="1" :max="5" />
</el-form-item>
</el-form>
<template #footer>
<span class="dialog-footer">
<el-button @click="dialogVisible = false">Cancel</el-button>
<el-button type="primary" @click="handleAddKeyword">Confirm</el-button>
</span>
</template>
</el-dialog>
</div>
</template>
<script setup lang="ts">
import { ref, reactive } from 'vue'
import axios from 'axios'
import { ElMessage } from 'element-plus'
interface Props {
keywords: any[]
loading: boolean
}
const props = defineProps<Props>()
const emit = defineEmits<{
refresh: []
}>()
const dialogVisible = ref(false)
const form = reactive({
word: '',
weight: 1
})
// 根据 weight 获取 tag 类型
const getTagType = (weight: number) => {
if (weight >= 5) return 'danger'
if (weight >= 4) return 'warning'
if (weight >= 3) return 'primary'
if (weight >= 2) return 'success'
return 'info'
}
const handleAddKeyword = async () => {
if (!form.word) {
ElMessage.warning('Please enter a keyword')
return
}
try {
await axios.post('/api/keywords', form)
ElMessage.success('Keyword added')
dialogVisible.value = false
form.word = ''
form.weight = 1
emit('refresh')
} catch (error) {
ElMessage.error('Failed to add keyword')
}
}
const handleDeleteKeyword = async (id: string) => {
try {
await axios.delete(`/api/keywords/${id}`)
ElMessage.success('Keyword deleted')
emit('refresh')
} catch (error) {
ElMessage.error('Failed to delete keyword')
}
}
</script>
<style scoped>
.card-header {
display: flex;
justify-content: space-between;
align-items: center;
}
</style>