2025-12-29 20:30:15 +08:00
|
|
|
|
<template>
|
|
|
|
|
|
<van-button
|
|
|
|
|
|
v-if="hasTransactions"
|
2025-12-30 18:49:46 +08:00
|
|
|
|
:type="buttonType"
|
2025-12-29 20:30:15 +08:00
|
|
|
|
size="small"
|
|
|
|
|
|
:loading="loading || saving"
|
|
|
|
|
|
:disabled="loading || saving"
|
|
|
|
|
|
@click="handleClick"
|
|
|
|
|
|
class="smart-classify-btn"
|
|
|
|
|
|
>
|
|
|
|
|
|
<template v-if="!loading && !saving">
|
2025-12-30 18:49:46 +08:00
|
|
|
|
<van-icon :name="buttonIcon" />
|
|
|
|
|
|
<span style="margin-left: 4px;">{{ buttonText }}</span>
|
2025-12-29 20:30:15 +08:00
|
|
|
|
</template>
|
|
|
|
|
|
<template v-else>
|
2025-12-30 18:49:46 +08:00
|
|
|
|
<span>{{ loadingText }}</span>
|
2025-12-29 20:30:15 +08:00
|
|
|
|
</template>
|
|
|
|
|
|
</van-button>
|
|
|
|
|
|
</template>
|
|
|
|
|
|
|
|
|
|
|
|
<script setup>
|
2025-12-30 18:49:46 +08:00
|
|
|
|
import { ref, computed, nextTick } from 'vue'
|
2025-12-29 20:30:15 +08:00
|
|
|
|
import { showToast, closeToast } from 'vant'
|
|
|
|
|
|
import { smartClassify, batchUpdateClassify } from '@/api/transactionRecord'
|
|
|
|
|
|
|
|
|
|
|
|
const props = defineProps({
|
|
|
|
|
|
transactions: {
|
|
|
|
|
|
type: Array,
|
|
|
|
|
|
default: () => []
|
2025-12-29 21:17:18 +08:00
|
|
|
|
},
|
|
|
|
|
|
onBeforeClassify: {
|
|
|
|
|
|
type: Function,
|
|
|
|
|
|
default: null
|
2025-12-29 20:30:15 +08:00
|
|
|
|
}
|
|
|
|
|
|
})
|
|
|
|
|
|
|
2025-12-30 18:49:46 +08:00
|
|
|
|
const emit = defineEmits(['update', 'save', 'notifyDonedTransactionId'])
|
2025-12-29 20:30:15 +08:00
|
|
|
|
|
|
|
|
|
|
const loading = ref(false)
|
|
|
|
|
|
const saving = ref(false)
|
|
|
|
|
|
const classifiedResults = ref([])
|
2025-12-30 18:49:46 +08:00
|
|
|
|
const isAllCompleted = ref(false)
|
2025-12-29 20:30:15 +08:00
|
|
|
|
let toastInstance = null
|
|
|
|
|
|
|
|
|
|
|
|
const hasTransactions = computed(() => {
|
|
|
|
|
|
return props.transactions && props.transactions.length > 0
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
const hasClassifiedResults = computed(() => {
|
2025-12-30 18:49:46 +08:00
|
|
|
|
return isAllCompleted.value && classifiedResults.value.length > 0
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
// 按钮类型
|
|
|
|
|
|
const buttonType = computed(() => {
|
|
|
|
|
|
if (saving.value) return 'warning'
|
|
|
|
|
|
if (loading.value) return 'primary'
|
|
|
|
|
|
if (hasClassifiedResults.value) return 'success'
|
|
|
|
|
|
return 'primary'
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
// 按钮图标
|
|
|
|
|
|
const buttonIcon = computed(() => {
|
|
|
|
|
|
if (hasClassifiedResults.value) return 'success'
|
|
|
|
|
|
return 'fire'
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
// 按钮文字(非加载状态)
|
|
|
|
|
|
const buttonText = computed(() => {
|
|
|
|
|
|
if (hasClassifiedResults.value) return '保存分类'
|
|
|
|
|
|
return '智能分类'
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
// 加载中文字
|
|
|
|
|
|
const loadingText = computed(() => {
|
|
|
|
|
|
if (saving.value) return '保存中...'
|
|
|
|
|
|
if (loading.value) return '分类中...'
|
|
|
|
|
|
return ''
|
2025-12-29 20:30:15 +08:00
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 点击按钮处理
|
|
|
|
|
|
*/
|
|
|
|
|
|
const handleClick = () => {
|
|
|
|
|
|
if (hasClassifiedResults.value) {
|
|
|
|
|
|
handleSaveClassify()
|
|
|
|
|
|
} else {
|
|
|
|
|
|
handleSmartClassify()
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 保存分类结果
|
|
|
|
|
|
*/
|
|
|
|
|
|
const handleSaveClassify = async () => {
|
|
|
|
|
|
try {
|
|
|
|
|
|
saving.value = true
|
|
|
|
|
|
showToast({
|
|
|
|
|
|
message: '正在保存...',
|
|
|
|
|
|
duration: 0,
|
|
|
|
|
|
forbidClick: true,
|
|
|
|
|
|
loadingType: 'spinner'
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
// 准备批量更新数据
|
|
|
|
|
|
const items = classifiedResults.value.map(item => ({
|
|
|
|
|
|
id: item.id,
|
|
|
|
|
|
classify: item.classify,
|
|
|
|
|
|
type: item.type
|
|
|
|
|
|
}))
|
|
|
|
|
|
|
|
|
|
|
|
const response = await batchUpdateClassify(items)
|
|
|
|
|
|
|
|
|
|
|
|
closeToast()
|
|
|
|
|
|
|
|
|
|
|
|
if (response.success) {
|
|
|
|
|
|
showToast({
|
|
|
|
|
|
type: 'success',
|
|
|
|
|
|
message: `保存成功,已更新 ${items.length} 条记录`,
|
|
|
|
|
|
duration: 2000
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
// 清空已分类结果
|
|
|
|
|
|
classifiedResults.value = []
|
2025-12-30 18:49:46 +08:00
|
|
|
|
isAllCompleted.value = false
|
2025-12-29 20:30:15 +08:00
|
|
|
|
|
|
|
|
|
|
// 通知父组件刷新数据
|
|
|
|
|
|
emit('save')
|
|
|
|
|
|
} else {
|
|
|
|
|
|
showToast({
|
|
|
|
|
|
type: 'fail',
|
|
|
|
|
|
message: response.message || '保存失败',
|
|
|
|
|
|
duration: 2000
|
|
|
|
|
|
})
|
|
|
|
|
|
}
|
|
|
|
|
|
} catch (error) {
|
|
|
|
|
|
console.error('保存分类失败:', error)
|
|
|
|
|
|
closeToast()
|
|
|
|
|
|
showToast({
|
|
|
|
|
|
type: 'fail',
|
|
|
|
|
|
message: '保存失败,请重试',
|
|
|
|
|
|
duration: 2000
|
|
|
|
|
|
})
|
|
|
|
|
|
} finally {
|
|
|
|
|
|
saving.value = false
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 处理智能分类
|
|
|
|
|
|
*/
|
|
|
|
|
|
const handleSmartClassify = async () => {
|
|
|
|
|
|
if (!props.transactions || props.transactions.length === 0) {
|
|
|
|
|
|
showToast('没有可分类的交易记录')
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 清空之前的分类结果
|
2025-12-30 18:49:46 +08:00
|
|
|
|
isAllCompleted.value = false
|
2025-12-29 20:30:15 +08:00
|
|
|
|
classifiedResults.value = []
|
|
|
|
|
|
|
2025-12-29 21:17:18 +08:00
|
|
|
|
const batchSize = 30
|
|
|
|
|
|
let processedCount = 0
|
2025-12-29 20:30:15 +08:00
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
loading.value = true
|
|
|
|
|
|
// 清除之前的Toast
|
|
|
|
|
|
if (toastInstance) {
|
|
|
|
|
|
closeToast()
|
|
|
|
|
|
}
|
2025-12-29 21:17:18 +08:00
|
|
|
|
|
|
|
|
|
|
// 等待父组件的 beforeClassify 事件处理完成(如果有返回 Promise) TODO 没有生效
|
|
|
|
|
|
if (props.onBeforeClassify) {
|
|
|
|
|
|
const shouldContinue = await props.onBeforeClassify()
|
|
|
|
|
|
if (shouldContinue === false) {
|
|
|
|
|
|
loading.value = false
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-12-30 18:49:46 +08:00
|
|
|
|
await nextTick()
|
|
|
|
|
|
|
|
|
|
|
|
const allTransactions = props.transactions
|
|
|
|
|
|
const totalCount = allTransactions.length
|
|
|
|
|
|
|
2025-12-29 20:30:15 +08:00
|
|
|
|
toastInstance = showToast({
|
|
|
|
|
|
message: '正在智能分类...',
|
|
|
|
|
|
duration: 0,
|
2025-12-30 18:49:46 +08:00
|
|
|
|
forbidClick: false, // 允许用户点击页面其他地方
|
2025-12-29 20:30:15 +08:00
|
|
|
|
loadingType: 'spinner'
|
|
|
|
|
|
})
|
|
|
|
|
|
|
2025-12-29 21:17:18 +08:00
|
|
|
|
// 分批处理
|
|
|
|
|
|
for (let i = 0; i < allTransactions.length; i += batchSize) {
|
|
|
|
|
|
const batch = allTransactions.slice(i, i + batchSize)
|
|
|
|
|
|
const transactionIds = batch.map(t => t.id)
|
|
|
|
|
|
const currentBatch = Math.floor(i / batchSize) + 1
|
|
|
|
|
|
const totalBatches = Math.ceil(allTransactions.length / batchSize)
|
2025-12-29 20:30:15 +08:00
|
|
|
|
|
2025-12-29 21:17:18 +08:00
|
|
|
|
// 更新批次进度
|
|
|
|
|
|
closeToast()
|
|
|
|
|
|
toastInstance = showToast({
|
|
|
|
|
|
message: `正在处理第 ${currentBatch}/${totalBatches} 批 (${i + 1}-${Math.min(i + batchSize, totalCount)} / ${totalCount})...`,
|
|
|
|
|
|
duration: 0,
|
2025-12-30 18:49:46 +08:00
|
|
|
|
forbidClick: false, // 允许用户点击
|
2025-12-29 21:17:18 +08:00
|
|
|
|
loadingType: 'spinner'
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
const response = await smartClassify(transactionIds)
|
2025-12-29 20:30:15 +08:00
|
|
|
|
|
2025-12-29 21:17:18 +08:00
|
|
|
|
if (!response.ok) {
|
|
|
|
|
|
throw new Error('智能分类请求失败')
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 读取流式响应
|
|
|
|
|
|
const reader = response.body.getReader()
|
|
|
|
|
|
const decoder = new TextDecoder()
|
|
|
|
|
|
let buffer = ''
|
2025-12-30 18:49:46 +08:00
|
|
|
|
let lastUpdateTime = 0
|
|
|
|
|
|
const updateInterval = 300 // 最多每300ms更新一次Toast,减少DOM操作
|
2025-12-29 21:17:18 +08:00
|
|
|
|
|
|
|
|
|
|
while (true) {
|
|
|
|
|
|
const { done, value } = await reader.read()
|
2025-12-29 20:30:15 +08:00
|
|
|
|
|
2025-12-29 21:17:18 +08:00
|
|
|
|
if (done) break
|
|
|
|
|
|
|
|
|
|
|
|
buffer += decoder.decode(value, { stream: true })
|
|
|
|
|
|
|
|
|
|
|
|
// 处理完整的事件(SSE格式:event: type\ndata: data\n\n)
|
|
|
|
|
|
const events = buffer.split('\n\n')
|
|
|
|
|
|
buffer = events.pop() || '' // 保留最后一个不完整的部分
|
|
|
|
|
|
|
|
|
|
|
|
for (const eventBlock of events) {
|
|
|
|
|
|
if (!eventBlock.trim()) continue
|
2025-12-29 20:30:15 +08:00
|
|
|
|
|
2025-12-29 21:17:18 +08:00
|
|
|
|
try {
|
|
|
|
|
|
const lines = eventBlock.split('\n')
|
|
|
|
|
|
let eventType = ''
|
|
|
|
|
|
let eventData = ''
|
2025-12-29 20:30:15 +08:00
|
|
|
|
|
2025-12-29 21:17:18 +08:00
|
|
|
|
for (const line of lines) {
|
|
|
|
|
|
if (line.startsWith('event: ')) {
|
|
|
|
|
|
eventType = line.slice(7).trim()
|
|
|
|
|
|
} else if (line.startsWith('data: ')) {
|
|
|
|
|
|
eventData = line.slice(6).trim()
|
|
|
|
|
|
}
|
2025-12-29 20:30:15 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-12-29 21:17:18 +08:00
|
|
|
|
if (eventType === 'start') {
|
|
|
|
|
|
// 开始分类
|
|
|
|
|
|
closeToast()
|
|
|
|
|
|
toastInstance = showToast({
|
|
|
|
|
|
message: `${eventData} (批次 ${currentBatch}/${totalBatches})`,
|
|
|
|
|
|
duration: 0,
|
2025-12-30 18:49:46 +08:00
|
|
|
|
forbidClick: false, // 允许用户点击
|
2025-12-29 21:17:18 +08:00
|
|
|
|
loadingType: 'spinner'
|
|
|
|
|
|
})
|
2025-12-30 18:49:46 +08:00
|
|
|
|
lastUpdateTime = Date.now()
|
2025-12-29 21:17:18 +08:00
|
|
|
|
} else if (eventType === 'data') {
|
|
|
|
|
|
// 收到分类结果
|
|
|
|
|
|
const data = JSON.parse(eventData)
|
|
|
|
|
|
processedCount++
|
|
|
|
|
|
|
|
|
|
|
|
// 记录分类结果
|
|
|
|
|
|
classifiedResults.value.push({
|
|
|
|
|
|
id: data.id,
|
|
|
|
|
|
classify: data.Classify,
|
|
|
|
|
|
type: data.Type
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
// 实时更新交易记录的分类信息
|
|
|
|
|
|
const index = props.transactions.findIndex(t => t.id === data.id)
|
|
|
|
|
|
if (index !== -1) {
|
|
|
|
|
|
const transaction = props.transactions[index]
|
|
|
|
|
|
transaction.upsetedClassify = data.Classify
|
|
|
|
|
|
transaction.upsetedType = data.Type
|
2025-12-30 18:49:46 +08:00
|
|
|
|
emit('notifyDonedTransactionId', data.id)
|
2025-12-29 21:17:18 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-12-30 18:49:46 +08:00
|
|
|
|
// 限制Toast更新频率,避免频繁的DOM操作
|
|
|
|
|
|
const now = Date.now()
|
|
|
|
|
|
if (now - lastUpdateTime > updateInterval) {
|
|
|
|
|
|
closeToast()
|
|
|
|
|
|
toastInstance = showToast({
|
|
|
|
|
|
message: `已分类 ${processedCount}/${totalCount} 条 (批次 ${currentBatch}/${totalBatches})...`,
|
|
|
|
|
|
duration: 0,
|
|
|
|
|
|
forbidClick: false, // 允许用户点击
|
|
|
|
|
|
loadingType: 'spinner'
|
|
|
|
|
|
})
|
|
|
|
|
|
lastUpdateTime = now
|
|
|
|
|
|
}
|
2025-12-29 21:17:18 +08:00
|
|
|
|
} else if (eventType === 'end') {
|
|
|
|
|
|
// 当前批次完成
|
|
|
|
|
|
console.log(`批次 ${currentBatch}/${totalBatches} 完成`)
|
|
|
|
|
|
} else if (eventType === 'error') {
|
|
|
|
|
|
// 处理错误
|
|
|
|
|
|
throw new Error(eventData || '分类失败')
|
|
|
|
|
|
}
|
|
|
|
|
|
} catch (e) {
|
|
|
|
|
|
console.error('解析SSE事件失败:', e, eventBlock)
|
|
|
|
|
|
throw e
|
2025-12-29 20:30:15 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-12-29 21:17:18 +08:00
|
|
|
|
|
|
|
|
|
|
// 所有批次完成
|
|
|
|
|
|
closeToast()
|
|
|
|
|
|
toastInstance = null
|
2025-12-30 18:49:46 +08:00
|
|
|
|
isAllCompleted.value = true
|
2025-12-29 21:17:18 +08:00
|
|
|
|
showToast({
|
|
|
|
|
|
type: 'success',
|
|
|
|
|
|
message: `分类完成,共处理 ${processedCount} 条记录,请点击"保存分类"按钮保存结果`,
|
|
|
|
|
|
duration: 3000
|
|
|
|
|
|
})
|
2025-12-29 20:30:15 +08:00
|
|
|
|
} catch (error) {
|
|
|
|
|
|
console.error('智能分类失败:', error)
|
|
|
|
|
|
closeToast()
|
|
|
|
|
|
toastInstance = null
|
|
|
|
|
|
showToast({
|
|
|
|
|
|
type: 'fail',
|
|
|
|
|
|
message: '智能分类失败,请重试',
|
|
|
|
|
|
duration: 2000
|
|
|
|
|
|
})
|
|
|
|
|
|
} finally {
|
|
|
|
|
|
loading.value = false
|
|
|
|
|
|
// 确保Toast被清除
|
|
|
|
|
|
if (toastInstance) {
|
|
|
|
|
|
setTimeout(() => {
|
|
|
|
|
|
closeToast()
|
|
|
|
|
|
toastInstance = null
|
|
|
|
|
|
}, 100)
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 重置组件状态
|
|
|
|
|
|
*/
|
|
|
|
|
|
const reset = () => {
|
2025-12-30 18:49:46 +08:00
|
|
|
|
isAllCompleted.value = false
|
2025-12-29 20:30:15 +08:00
|
|
|
|
classifiedResults.value = []
|
|
|
|
|
|
loading.value = false
|
|
|
|
|
|
saving.value = false
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
defineExpose({
|
|
|
|
|
|
reset
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
</script>
|
|
|
|
|
|
|
|
|
|
|
|
<style scoped>
|
|
|
|
|
|
.smart-classify-btn {
|
|
|
|
|
|
display: inline-flex;
|
|
|
|
|
|
align-items: center;
|
|
|
|
|
|
white-space: nowrap;
|
|
|
|
|
|
border-radius: 16px;
|
|
|
|
|
|
padding: 6px 12px;
|
|
|
|
|
|
}
|
|
|
|
|
|
</style>
|