feat: 批量检测支持上传TXT文件

- 新增文件选择功能,支持TXT格式
- 自动读取文件内容并逐行拆解
- 显示已选文件名和文本条数
- 保留手动输入方式作为备选

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
刘正航
2026-05-01 16:59:49 +08:00
parent f7fdc635c7
commit 00ead01cb8
2 changed files with 64 additions and 5 deletions

View File

@@ -3,6 +3,8 @@ const { request } = require('../../utils/request')
Page({
data: {
inputText: '',
fileName: '',
lineCount: 0,
loading: false,
summary: null,
items: []
@@ -24,6 +26,43 @@ Page({
.filter((line) => line.length >= 2)
},
chooseFile() {
wx.chooseMessageFile({
count: 1,
type: 'file',
extension: ['txt'],
success: (res) => {
const file = res.tempFiles[0]
const fs = wx.getFileSystemManager()
try {
const content = fs.readFileSync(file.path, 'utf8')
const lines = content
.split('\n')
.map((line) => line.trim())
.filter((line) => line.length >= 2)
this.setData({
inputText: lines.join('\n'),
fileName: file.name,
lineCount: lines.length
})
wx.showToast({ title: `已读取 ${lines.length} 条文本`, icon: 'success' })
} catch (err) {
console.error('读取文件失败', err)
wx.showToast({ title: '文件读取失败', icon: 'none' })
}
},
fail: (err) => {
console.error('选择文件失败', err)
if (err.errMsg !== 'chooseMessageFile:fail cancel') {
wx.showToast({ title: '请选择TXT文件', icon: 'none' })
}
}
})
},
async submit() {
if (this.data.loading) return
const items = this.parseLines()