2025-12-25 15:40:50 +08:00
|
|
|
|
<template>
|
2025-12-27 21:15:26 +08:00
|
|
|
|
<div class="page-container-flex smart-classification">
|
2025-12-25 15:40:50 +08:00
|
|
|
|
<van-nav-bar
|
|
|
|
|
|
title="智能分类"
|
|
|
|
|
|
left-text="返回"
|
|
|
|
|
|
left-arrow
|
|
|
|
|
|
@click-left="onClickLeft"
|
|
|
|
|
|
/>
|
|
|
|
|
|
|
2025-12-27 21:15:26 +08:00
|
|
|
|
<div class="scroll-content" style="padding-top: 5px;">
|
2025-12-25 15:40:50 +08:00
|
|
|
|
<!-- 统计信息 -->
|
|
|
|
|
|
<div class="stats-info">
|
2025-12-29 20:30:15 +08:00
|
|
|
|
<span class="stats-label">未分类账单 </span>
|
2025-12-30 17:02:30 +08:00
|
|
|
|
<span class="stats-value">{{ unclassifiedCount }} 条,共计 {{ totalGroups }} 组</span>
|
2025-12-25 15:40:50 +08:00
|
|
|
|
</div>
|
|
|
|
|
|
|
2025-12-30 17:02:30 +08:00
|
|
|
|
<!-- 分组列表组件 -->
|
|
|
|
|
|
<ReasonGroupList
|
|
|
|
|
|
ref="groupListRef"
|
|
|
|
|
|
:selectable="true"
|
|
|
|
|
|
@data-loaded="handleDataLoaded"
|
|
|
|
|
|
@data-changed="handleDataChanged"
|
|
|
|
|
|
/>
|
2025-12-28 10:23:57 +08:00
|
|
|
|
|
|
|
|
|
|
<!-- 底部安全距离 -->
|
|
|
|
|
|
<div style="height: calc(50px + env(safe-area-inset-bottom, 0px))"></div>
|
2025-12-25 15:40:50 +08:00
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
|
|
<!-- 底部操作按钮 -->
|
2026-01-05 19:07:10 +08:00
|
|
|
|
<div class="bottom-button">
|
2025-12-25 15:40:50 +08:00
|
|
|
|
<van-button
|
|
|
|
|
|
type="primary"
|
|
|
|
|
|
:loading="classifying"
|
2025-12-30 17:02:30 +08:00
|
|
|
|
:disabled="selectedCount === 0"
|
2026-01-06 13:45:39 +08:00
|
|
|
|
round
|
2025-12-25 15:40:50 +08:00
|
|
|
|
@click="startClassify"
|
|
|
|
|
|
class="action-btn"
|
|
|
|
|
|
>
|
2025-12-30 17:02:30 +08:00
|
|
|
|
{{ classifying ? '分类中...' : `开始分类 (${selectedCount}组)` }}
|
2025-12-25 15:40:50 +08:00
|
|
|
|
</van-button>
|
|
|
|
|
|
|
|
|
|
|
|
<van-button
|
|
|
|
|
|
type="success"
|
|
|
|
|
|
:disabled="!hasChanges || classifying"
|
2026-01-06 13:45:39 +08:00
|
|
|
|
round
|
2025-12-25 15:40:50 +08:00
|
|
|
|
@click="saveClassifications"
|
|
|
|
|
|
class="action-btn"
|
|
|
|
|
|
>
|
|
|
|
|
|
保存分类
|
|
|
|
|
|
</van-button>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
</template>
|
|
|
|
|
|
|
|
|
|
|
|
<script setup>
|
2026-01-01 11:58:21 +08:00
|
|
|
|
import { ref, computed, onMounted, nextTick } from 'vue'
|
2025-12-25 15:40:50 +08:00
|
|
|
|
import { useRouter } from 'vue-router'
|
|
|
|
|
|
import { showToast, showLoadingToast, closeToast, showConfirmDialog } from 'vant'
|
|
|
|
|
|
import {
|
|
|
|
|
|
getUnclassifiedCount,
|
|
|
|
|
|
smartClassify,
|
2025-12-29 20:30:15 +08:00
|
|
|
|
batchUpdateClassify
|
2025-12-25 15:40:50 +08:00
|
|
|
|
} from '@/api/transactionRecord'
|
2025-12-30 17:02:30 +08:00
|
|
|
|
import ReasonGroupList from '@/components/ReasonGroupList.vue'
|
2025-12-25 15:40:50 +08:00
|
|
|
|
|
|
|
|
|
|
const router = useRouter()
|
2025-12-30 17:02:30 +08:00
|
|
|
|
const groupListRef = ref(null)
|
2025-12-25 15:40:50 +08:00
|
|
|
|
const unclassifiedCount = ref(0)
|
2025-12-30 17:02:30 +08:00
|
|
|
|
const totalGroups = ref(0)
|
2025-12-25 15:40:50 +08:00
|
|
|
|
const classifying = ref(false)
|
|
|
|
|
|
const hasChanges = ref(false)
|
2025-12-30 17:02:30 +08:00
|
|
|
|
const classifyBuffer = ref('')
|
2026-01-01 11:58:21 +08:00
|
|
|
|
const suppressDataChanged = ref(false)
|
2025-12-25 15:40:50 +08:00
|
|
|
|
|
2025-12-30 17:02:30 +08:00
|
|
|
|
// 计算已选中的数量
|
|
|
|
|
|
const selectedCount = computed(() => {
|
|
|
|
|
|
if (!groupListRef.value) return 0
|
|
|
|
|
|
return groupListRef.value.getSelectedReasons().size
|
|
|
|
|
|
})
|
2025-12-25 15:40:50 +08:00
|
|
|
|
|
|
|
|
|
|
// 加载未分类账单数量
|
|
|
|
|
|
const loadUnclassifiedCount = async () => {
|
|
|
|
|
|
try {
|
|
|
|
|
|
const res = await getUnclassifiedCount()
|
|
|
|
|
|
if (res.success) {
|
|
|
|
|
|
unclassifiedCount.value = res.data
|
|
|
|
|
|
}
|
|
|
|
|
|
} catch (error) {
|
|
|
|
|
|
console.error('获取未分类数量失败', error)
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-12-30 17:02:30 +08:00
|
|
|
|
// 处理数据加载完成
|
|
|
|
|
|
const handleDataLoaded = ({ groups, total }) => {
|
|
|
|
|
|
totalGroups.value = total
|
|
|
|
|
|
// 默认全选所有分组
|
|
|
|
|
|
if (groupListRef.value) {
|
|
|
|
|
|
groupListRef.value.selectAll()
|
2025-12-29 20:30:15 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-12-30 17:02:30 +08:00
|
|
|
|
// 处理数据变更
|
|
|
|
|
|
const handleDataChanged = async () => {
|
2026-01-01 11:58:21 +08:00
|
|
|
|
if (suppressDataChanged.value) {
|
|
|
|
|
|
suppressDataChanged.value = false
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-12-30 17:02:30 +08:00
|
|
|
|
await loadUnclassifiedCount()
|
|
|
|
|
|
hasChanges.value = false
|
2025-12-29 20:30:15 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-12-30 17:02:30 +08:00
|
|
|
|
const onClickLeft = () => {
|
|
|
|
|
|
if (hasChanges.value) {
|
|
|
|
|
|
showConfirmDialog({
|
|
|
|
|
|
title: '提示',
|
|
|
|
|
|
message: '有未保存的分类结果,确定要离开吗?',
|
|
|
|
|
|
}).then(() => {
|
|
|
|
|
|
router.back()
|
|
|
|
|
|
}).catch(() => {})
|
|
|
|
|
|
} else {
|
|
|
|
|
|
router.back()
|
2025-12-29 20:30:15 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-12-25 15:40:50 +08:00
|
|
|
|
// 开始智能分类
|
|
|
|
|
|
const startClassify = async () => {
|
2025-12-30 17:02:30 +08:00
|
|
|
|
if (!groupListRef.value) return
|
|
|
|
|
|
|
|
|
|
|
|
// 获取所有选中分组
|
|
|
|
|
|
const selectedGroups = groupListRef.value.getList(true)
|
|
|
|
|
|
|
2025-12-29 20:30:15 +08:00
|
|
|
|
// 获取所有选中分组的账单ID
|
|
|
|
|
|
const idsToClassify = []
|
2025-12-30 17:02:30 +08:00
|
|
|
|
for (const group of selectedGroups) {
|
|
|
|
|
|
idsToClassify.push(...group.transactionIds)
|
2025-12-29 20:30:15 +08:00
|
|
|
|
}
|
2025-12-26 15:21:31 +08:00
|
|
|
|
|
|
|
|
|
|
if (idsToClassify.length === 0) {
|
2025-12-29 20:30:15 +08:00
|
|
|
|
showToast('请先选择要分类的账单组')
|
2025-12-25 15:40:50 +08:00
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-12-26 18:03:52 +08:00
|
|
|
|
showLoadingToast({
|
2025-12-26 15:21:31 +08:00
|
|
|
|
message: '智能分类中...',
|
|
|
|
|
|
forbidClick: true,
|
|
|
|
|
|
duration: 0
|
|
|
|
|
|
})
|
|
|
|
|
|
|
2025-12-25 15:40:50 +08:00
|
|
|
|
classifying.value = true
|
2025-12-30 17:02:30 +08:00
|
|
|
|
classifyBuffer.value = ''
|
2025-12-25 15:40:50 +08:00
|
|
|
|
|
2025-12-29 20:30:15 +08:00
|
|
|
|
// 用于存储分类结果的临时对象
|
2025-12-30 17:02:30 +08:00
|
|
|
|
const classifyResults = new Map()
|
2025-12-29 20:30:15 +08:00
|
|
|
|
|
2025-12-25 15:40:50 +08:00
|
|
|
|
try {
|
2025-12-26 15:21:31 +08:00
|
|
|
|
const response = await smartClassify(idsToClassify)
|
2025-12-25 15:40:50 +08:00
|
|
|
|
|
|
|
|
|
|
if (!response.ok) {
|
|
|
|
|
|
throw new Error(`HTTP error! status: ${response.status}`)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const reader = response.body.getReader()
|
|
|
|
|
|
const decoder = new TextDecoder()
|
|
|
|
|
|
let buffer = ''
|
|
|
|
|
|
|
|
|
|
|
|
while (true) {
|
|
|
|
|
|
const { done, value } = await reader.read()
|
|
|
|
|
|
|
|
|
|
|
|
if (done) break
|
|
|
|
|
|
|
|
|
|
|
|
buffer += decoder.decode(value, { stream: true })
|
|
|
|
|
|
const lines = buffer.split('\n\n')
|
|
|
|
|
|
buffer = lines.pop() || ''
|
|
|
|
|
|
|
|
|
|
|
|
for (const line of lines) {
|
|
|
|
|
|
if (!line.trim()) continue
|
|
|
|
|
|
|
|
|
|
|
|
const eventMatch = line.match(/^event: (.+)$/m)
|
|
|
|
|
|
const dataMatch = line.match(/^data: (.+)$/m)
|
|
|
|
|
|
|
|
|
|
|
|
if (eventMatch && dataMatch) {
|
|
|
|
|
|
const eventType = eventMatch[1]
|
|
|
|
|
|
const data = dataMatch[1]
|
|
|
|
|
|
|
2025-12-29 20:30:15 +08:00
|
|
|
|
handleSSEEvent(eventType, data, classifyResults)
|
2025-12-25 15:40:50 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
} catch (error) {
|
|
|
|
|
|
console.error('智能分类失败', error)
|
|
|
|
|
|
showToast(`分类失败: ${error.message}`)
|
|
|
|
|
|
} finally {
|
|
|
|
|
|
classifying.value = false
|
2025-12-26 15:21:31 +08:00
|
|
|
|
classifyBuffer.value = ''
|
|
|
|
|
|
closeToast()
|
2025-12-25 15:40:50 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 处理SSE事件
|
2025-12-29 20:30:15 +08:00
|
|
|
|
const handleSSEEvent = (eventType, data, classifyResults) => {
|
2026-01-01 11:58:21 +08:00
|
|
|
|
console.log('收到事件:', eventType, data)
|
2025-12-25 15:40:50 +08:00
|
|
|
|
if (eventType === 'data') {
|
|
|
|
|
|
try {
|
2025-12-26 15:21:31 +08:00
|
|
|
|
classifyBuffer.value += data
|
2025-12-25 15:40:50 +08:00
|
|
|
|
|
2025-12-26 15:21:31 +08:00
|
|
|
|
let startIndex = 0
|
|
|
|
|
|
while (startIndex < classifyBuffer.value.length) {
|
|
|
|
|
|
const openBrace = classifyBuffer.value.indexOf('{', startIndex)
|
|
|
|
|
|
if (openBrace === -1) {
|
|
|
|
|
|
classifyBuffer.value = ''
|
|
|
|
|
|
break
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
let braceCount = 0
|
|
|
|
|
|
let closeBrace = -1
|
|
|
|
|
|
for (let i = openBrace; i < classifyBuffer.value.length; i++) {
|
|
|
|
|
|
if (classifyBuffer.value[i] === '{') braceCount++
|
|
|
|
|
|
else if (classifyBuffer.value[i] === '}') {
|
|
|
|
|
|
braceCount--
|
|
|
|
|
|
if (braceCount === 0) {
|
|
|
|
|
|
closeBrace = i
|
|
|
|
|
|
break
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (closeBrace !== -1) {
|
|
|
|
|
|
const jsonStr = classifyBuffer.value.substring(openBrace, closeBrace + 1)
|
|
|
|
|
|
|
2025-12-25 15:40:50 +08:00
|
|
|
|
try {
|
|
|
|
|
|
const result = JSON.parse(jsonStr)
|
2025-12-30 17:02:30 +08:00
|
|
|
|
if (result.id && groupListRef.value) {
|
2025-12-29 20:30:15 +08:00
|
|
|
|
classifyResults.set(result.id, {
|
2026-01-01 11:58:21 +08:00
|
|
|
|
classify: result.Classify || '',
|
|
|
|
|
|
type: result.Type !== undefined ? result.Type : null
|
2025-12-29 20:30:15 +08:00
|
|
|
|
})
|
|
|
|
|
|
|
2025-12-30 17:02:30 +08:00
|
|
|
|
// 更新组件内的分组显示状态
|
|
|
|
|
|
const groups = groupListRef.value.getList()
|
|
|
|
|
|
for (const group of groups) {
|
2025-12-29 20:30:15 +08:00
|
|
|
|
if (group.transactionIds.includes(result.id)) {
|
2026-01-01 11:58:21 +08:00
|
|
|
|
group.sampleClassify = result.Classify || ''
|
|
|
|
|
|
if (result.Type !== undefined && result.Type !== null) {
|
|
|
|
|
|
group.sampleType = result.Type
|
2025-12-29 20:30:15 +08:00
|
|
|
|
}
|
|
|
|
|
|
hasChanges.value = true
|
|
|
|
|
|
break
|
2025-12-26 15:21:31 +08:00
|
|
|
|
}
|
2025-12-25 15:40:50 +08:00
|
|
|
|
}
|
2026-01-01 11:58:21 +08:00
|
|
|
|
// 更新回组件(内部更新时抑制 data-changed 处理)
|
|
|
|
|
|
suppressDataChanged.value = true
|
2025-12-30 17:02:30 +08:00
|
|
|
|
groupListRef.value.setList(groups)
|
2026-01-01 11:58:21 +08:00
|
|
|
|
// 确保在子组件内部事件触发后恢复标志并保持 hasChanges
|
|
|
|
|
|
nextTick(() => {
|
|
|
|
|
|
suppressDataChanged.value = false
|
|
|
|
|
|
hasChanges.value = true
|
|
|
|
|
|
})
|
2025-12-25 15:40:50 +08:00
|
|
|
|
}
|
|
|
|
|
|
} catch (e) {
|
2025-12-26 15:21:31 +08:00
|
|
|
|
console.error('JSON解析失败:', e)
|
2025-12-25 15:40:50 +08:00
|
|
|
|
}
|
2025-12-26 15:21:31 +08:00
|
|
|
|
|
|
|
|
|
|
classifyBuffer.value = classifyBuffer.value.substring(closeBrace + 1)
|
2025-12-29 20:30:15 +08:00
|
|
|
|
startIndex = 0
|
2025-12-26 15:21:31 +08:00
|
|
|
|
} else {
|
|
|
|
|
|
break
|
2025-12-25 15:40:50 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
} catch (error) {
|
|
|
|
|
|
console.error('解析分类结果失败', error)
|
|
|
|
|
|
}
|
2025-12-26 15:21:31 +08:00
|
|
|
|
} else if (eventType === 'start') {
|
|
|
|
|
|
showToast(data)
|
2025-12-25 15:40:50 +08:00
|
|
|
|
} else if (eventType === 'end') {
|
2025-12-26 15:21:31 +08:00
|
|
|
|
classifyBuffer.value = ''
|
2025-12-25 15:40:50 +08:00
|
|
|
|
showToast('分类完成')
|
|
|
|
|
|
} else if (eventType === 'error') {
|
2025-12-26 15:21:31 +08:00
|
|
|
|
classifyBuffer.value = ''
|
2025-12-25 15:40:50 +08:00
|
|
|
|
showToast(data)
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 保存分类
|
|
|
|
|
|
const saveClassifications = async () => {
|
2025-12-30 17:02:30 +08:00
|
|
|
|
if (!groupListRef.value) return
|
|
|
|
|
|
|
2025-12-29 20:30:15 +08:00
|
|
|
|
// 收集所有已分类的账单
|
2025-12-30 17:02:30 +08:00
|
|
|
|
const groups = groupListRef.value.getList()
|
2025-12-29 20:30:15 +08:00
|
|
|
|
const itemsToUpdate = []
|
2025-12-30 17:02:30 +08:00
|
|
|
|
|
|
|
|
|
|
for (const group of groups) {
|
2025-12-29 20:30:15 +08:00
|
|
|
|
if (group.sampleClassify) {
|
|
|
|
|
|
// 为该分组的所有账单添加分类
|
|
|
|
|
|
for (const id of group.transactionIds) {
|
|
|
|
|
|
itemsToUpdate.push({
|
|
|
|
|
|
id: id,
|
|
|
|
|
|
classify: group.sampleClassify,
|
|
|
|
|
|
type: group.sampleType
|
|
|
|
|
|
})
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-12-25 15:40:50 +08:00
|
|
|
|
|
|
|
|
|
|
if (itemsToUpdate.length === 0) {
|
|
|
|
|
|
showToast('没有需要保存的分类')
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-12-26 18:03:52 +08:00
|
|
|
|
showLoadingToast({
|
2025-12-25 15:40:50 +08:00
|
|
|
|
message: '保存中...',
|
|
|
|
|
|
forbidClick: true,
|
|
|
|
|
|
duration: 0
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
const res = await batchUpdateClassify(itemsToUpdate)
|
|
|
|
|
|
if (res.success) {
|
|
|
|
|
|
showToast('保存成功')
|
|
|
|
|
|
hasChanges.value = false
|
|
|
|
|
|
// 重新加载数据
|
|
|
|
|
|
await loadUnclassifiedCount()
|
2025-12-30 17:02:30 +08:00
|
|
|
|
await groupListRef.value.refresh()
|
2025-12-25 15:40:50 +08:00
|
|
|
|
} else {
|
|
|
|
|
|
showToast(res.message || '保存失败')
|
|
|
|
|
|
}
|
|
|
|
|
|
} catch (error) {
|
|
|
|
|
|
console.error('保存失败', error)
|
|
|
|
|
|
showToast('保存失败')
|
|
|
|
|
|
} finally {
|
|
|
|
|
|
closeToast()
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-12-30 17:02:30 +08:00
|
|
|
|
onMounted(async () => {
|
|
|
|
|
|
await loadUnclassifiedCount()
|
|
|
|
|
|
// 触发组件加载数据
|
|
|
|
|
|
if (groupListRef.value) {
|
|
|
|
|
|
await groupListRef.value.loadData()
|
|
|
|
|
|
}
|
2025-12-25 15:40:50 +08:00
|
|
|
|
})
|
|
|
|
|
|
</script>
|
|
|
|
|
|
|
|
|
|
|
|
<style scoped>
|
|
|
|
|
|
/* 统计信息 */
|
|
|
|
|
|
.stats-info {
|
2025-12-30 17:02:30 +08:00
|
|
|
|
padding: 12px 12px 0 16px;
|
2025-12-25 15:40:50 +08:00
|
|
|
|
font-size: 14px;
|
|
|
|
|
|
color: #969799;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
.stats-value {
|
|
|
|
|
|
font-weight: 500;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
.action-btn {
|
|
|
|
|
|
flex: 1;
|
|
|
|
|
|
height: 44px;
|
|
|
|
|
|
}
|
2025-12-26 18:03:52 +08:00
|
|
|
|
|
|
|
|
|
|
/* 设置页面容器背景色 */
|
|
|
|
|
|
:deep(.van-nav-bar) {
|
|
|
|
|
|
background: transparent !important;
|
|
|
|
|
|
}
|
2025-12-25 15:40:50 +08:00
|
|
|
|
</style>
|