改成导excel
This commit is contained in:
@@ -141,46 +141,66 @@ Page({
|
||||
return [headers.join(','), ...rows].join('\n')
|
||||
},
|
||||
|
||||
exportCSV() {
|
||||
exportXLSX() {
|
||||
const items = this.data.items
|
||||
if (!items.length) {
|
||||
wx.showToast({ title: '暂无识别结果可导出', icon: 'none' })
|
||||
return
|
||||
}
|
||||
|
||||
const csvContent = this.generateCSV()
|
||||
const timestamp = new Date().toISOString().slice(0, 19).replace(/[T:]/g, '-')
|
||||
const filename = `batch_detect_${timestamp}.csv`
|
||||
wx.showLoading({ title: '生成文件中...' })
|
||||
|
||||
// 写入临时文件
|
||||
const fs = wx.getFileSystemManager()
|
||||
const tempPath = `${wx.env.USER_DATA_PATH}/${filename}`
|
||||
const app = getApp()
|
||||
const token = app.globalData.token || wx.getStorageSync('token') || ''
|
||||
const baseURL = app.globalData.baseURL || 'http://127.0.0.1:5000/api'
|
||||
|
||||
try {
|
||||
fs.writeFileSync(tempPath, csvContent, 'utf8')
|
||||
wx.showModal({
|
||||
title: '导出成功',
|
||||
content: `CSV文件已生成,是否打开查看?\n文件名:${filename}`,
|
||||
confirmText: '打开',
|
||||
cancelText: '关闭',
|
||||
success: (res) => {
|
||||
if (res.confirm) {
|
||||
wx.openDocument({
|
||||
filePath: tempPath,
|
||||
fileType: 'csv',
|
||||
showMenu: true,
|
||||
fail: (err) => {
|
||||
console.error('打开文件失败', err)
|
||||
wx.showToast({ title: '打开失败,请检查文件管理器', icon: 'none' })
|
||||
}
|
||||
})
|
||||
}
|
||||
wx.request({
|
||||
url: `${baseURL}/spam/export/xlsx`,
|
||||
method: 'POST',
|
||||
data: { items },
|
||||
header: {
|
||||
'Content-Type': 'application/json',
|
||||
Authorization: `Bearer ${token}`
|
||||
},
|
||||
responseType: 'arraybuffer',
|
||||
success(res) {
|
||||
wx.hideLoading()
|
||||
|
||||
if (res.statusCode !== 200) {
|
||||
wx.showToast({ title: '导出失败', icon: 'none' })
|
||||
return
|
||||
}
|
||||
})
|
||||
} catch (err) {
|
||||
console.error('写入文件失败', err)
|
||||
wx.showToast({ title: '导出失败', icon: 'none' })
|
||||
}
|
||||
|
||||
const timestamp = new Date().toISOString().slice(0, 19).replace(/[T:]/g, '-')
|
||||
const filename = `batch_detect_${timestamp}.xlsx`
|
||||
const fs = wx.getFileSystemManager()
|
||||
const tempPath = `${wx.env.USER_DATA_PATH}/${filename}`
|
||||
|
||||
try {
|
||||
fs.writeFileSync(tempPath, res.data)
|
||||
wx.openDocument({
|
||||
filePath: tempPath,
|
||||
fileType: 'xlsx',
|
||||
showMenu: true,
|
||||
success: () => {
|
||||
wx.showToast({ title: '导出成功', icon: 'success' })
|
||||
},
|
||||
fail: (err) => {
|
||||
console.error('打开文件失败', err)
|
||||
wx.showToast({ title: '打开失败', icon: 'none' })
|
||||
}
|
||||
})
|
||||
} catch (err) {
|
||||
console.error('写入文件失败', err)
|
||||
wx.showToast({ title: '导出失败', icon: 'none' })
|
||||
}
|
||||
},
|
||||
fail(err) {
|
||||
wx.hideLoading()
|
||||
console.error('导出请求失败', err)
|
||||
wx.showToast({ title: '导出失败,请检查网络', icon: 'none' })
|
||||
}
|
||||
})
|
||||
},
|
||||
|
||||
copyCSVToClipboard() {
|
||||
|
||||
@@ -77,7 +77,7 @@
|
||||
<view class="card fade-up fade-up-delay-3" wx:if="{{items.length}}">
|
||||
<view class="card-title">明细结果</view>
|
||||
<view class="btn-row" style="margin-bottom: 12rpx;">
|
||||
<button class="btn btn-ghost" bindtap="exportCSV">导出CSV文件</button>
|
||||
<button class="btn btn-ghost" bindtap="exportXLSX">导出Excel文件</button>
|
||||
<button class="btn btn-ghost" bindtap="copyCSVToClipboard">复制CSV内容</button>
|
||||
</view>
|
||||
<view class="list-item" wx:for="{{items}}" wx:key="index">
|
||||
|
||||
Reference in New Issue
Block a user