fix
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
<template>
|
||||
<van-button
|
||||
v-if="hasTransactions"
|
||||
:type="hasClassifiedResults ? 'success' : 'primary'"
|
||||
:type="buttonType"
|
||||
size="small"
|
||||
:loading="loading || saving"
|
||||
:disabled="loading || saving"
|
||||
@@ -9,17 +9,17 @@
|
||||
class="smart-classify-btn"
|
||||
>
|
||||
<template v-if="!loading && !saving">
|
||||
<van-icon :name="hasClassifiedResults ? 'success' : 'fire'" />
|
||||
<span style="margin-left: 4px;">{{ hasClassifiedResults ? '保存分类' : '智能分类' }}</span>
|
||||
<van-icon :name="buttonIcon" />
|
||||
<span style="margin-left: 4px;">{{ buttonText }}</span>
|
||||
</template>
|
||||
<template v-else>
|
||||
{{ saving ? '保存中...' : '分类中...' }}
|
||||
<span>{{ loadingText }}</span>
|
||||
</template>
|
||||
</van-button>
|
||||
</template>
|
||||
|
||||
<script setup>
|
||||
import { ref, computed } from 'vue'
|
||||
import { ref, computed, nextTick } from 'vue'
|
||||
import { showToast, closeToast } from 'vant'
|
||||
import { smartClassify, batchUpdateClassify } from '@/api/transactionRecord'
|
||||
|
||||
@@ -34,11 +34,12 @@ const props = defineProps({
|
||||
}
|
||||
})
|
||||
|
||||
const emit = defineEmits(['update', 'save', 'beforeClassify'])
|
||||
const emit = defineEmits(['update', 'save', 'notifyDonedTransactionId'])
|
||||
|
||||
const loading = ref(false)
|
||||
const saving = ref(false)
|
||||
const classifiedResults = ref([])
|
||||
const isAllCompleted = ref(false)
|
||||
let toastInstance = null
|
||||
|
||||
const hasTransactions = computed(() => {
|
||||
@@ -46,7 +47,34 @@ const hasTransactions = computed(() => {
|
||||
})
|
||||
|
||||
const hasClassifiedResults = computed(() => {
|
||||
return classifiedResults.value.length > 0
|
||||
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 ''
|
||||
})
|
||||
|
||||
/**
|
||||
@@ -93,6 +121,7 @@ const handleSaveClassify = async () => {
|
||||
|
||||
// 清空已分类结果
|
||||
classifiedResults.value = []
|
||||
isAllCompleted.value = false
|
||||
|
||||
// 通知父组件刷新数据
|
||||
emit('save')
|
||||
@@ -126,10 +155,9 @@ const handleSmartClassify = async () => {
|
||||
}
|
||||
|
||||
// 清空之前的分类结果
|
||||
isAllCompleted.value = false
|
||||
classifiedResults.value = []
|
||||
|
||||
const allTransactions = props.transactions
|
||||
const totalCount = allTransactions.length
|
||||
const batchSize = 30
|
||||
let processedCount = 0
|
||||
|
||||
@@ -149,10 +177,15 @@ const handleSmartClassify = async () => {
|
||||
}
|
||||
}
|
||||
|
||||
await nextTick()
|
||||
|
||||
const allTransactions = props.transactions
|
||||
const totalCount = allTransactions.length
|
||||
|
||||
toastInstance = showToast({
|
||||
message: '正在智能分类...',
|
||||
duration: 0,
|
||||
forbidClick: true,
|
||||
forbidClick: false, // 允许用户点击页面其他地方
|
||||
loadingType: 'spinner'
|
||||
})
|
||||
|
||||
@@ -168,7 +201,7 @@ const handleSmartClassify = async () => {
|
||||
toastInstance = showToast({
|
||||
message: `正在处理第 ${currentBatch}/${totalBatches} 批 (${i + 1}-${Math.min(i + batchSize, totalCount)} / ${totalCount})...`,
|
||||
duration: 0,
|
||||
forbidClick: true,
|
||||
forbidClick: false, // 允许用户点击
|
||||
loadingType: 'spinner'
|
||||
})
|
||||
|
||||
@@ -182,6 +215,8 @@ const handleSmartClassify = async () => {
|
||||
const reader = response.body.getReader()
|
||||
const decoder = new TextDecoder()
|
||||
let buffer = ''
|
||||
let lastUpdateTime = 0
|
||||
const updateInterval = 300 // 最多每300ms更新一次Toast,减少DOM操作
|
||||
|
||||
while (true) {
|
||||
const { done, value } = await reader.read()
|
||||
@@ -216,9 +251,10 @@ const handleSmartClassify = async () => {
|
||||
toastInstance = showToast({
|
||||
message: `${eventData} (批次 ${currentBatch}/${totalBatches})`,
|
||||
duration: 0,
|
||||
forbidClick: true,
|
||||
forbidClick: false, // 允许用户点击
|
||||
loadingType: 'spinner'
|
||||
})
|
||||
lastUpdateTime = Date.now()
|
||||
} else if (eventType === 'data') {
|
||||
// 收到分类结果
|
||||
const data = JSON.parse(eventData)
|
||||
@@ -237,16 +273,21 @@ const handleSmartClassify = async () => {
|
||||
const transaction = props.transactions[index]
|
||||
transaction.upsetedClassify = data.Classify
|
||||
transaction.upsetedType = data.Type
|
||||
emit('notifyDonedTransactionId', data.id)
|
||||
}
|
||||
|
||||
// 更新进度
|
||||
closeToast()
|
||||
toastInstance = showToast({
|
||||
message: `已分类 ${processedCount}/${totalCount} 条 (批次 ${currentBatch}/${totalBatches})...`,
|
||||
duration: 0,
|
||||
forbidClick: true,
|
||||
loadingType: 'spinner'
|
||||
})
|
||||
// 限制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
|
||||
}
|
||||
} else if (eventType === 'end') {
|
||||
// 当前批次完成
|
||||
console.log(`批次 ${currentBatch}/${totalBatches} 完成`)
|
||||
@@ -265,6 +306,7 @@ const handleSmartClassify = async () => {
|
||||
// 所有批次完成
|
||||
closeToast()
|
||||
toastInstance = null
|
||||
isAllCompleted.value = true
|
||||
showToast({
|
||||
type: 'success',
|
||||
message: `分类完成,共处理 ${processedCount} 条记录,请点击"保存分类"按钮保存结果`,
|
||||
@@ -295,6 +337,7 @@ const handleSmartClassify = async () => {
|
||||
* 重置组件状态
|
||||
*/
|
||||
const reset = () => {
|
||||
isAllCompleted.value = false
|
||||
classifiedResults.value = []
|
||||
loading.value = false
|
||||
saving.value = false
|
||||
|
||||
Reference in New Issue
Block a user