kydc/batch-upload/batch-upload.js

55 lines
1.5 KiB
JavaScript
Raw Permalink Normal View History

const app = getApp()
Page({
data: {
result: null,
fileName: '',
2026-04-19 11:56:49 +08:00
loading: false,
error: '',
},
2026-04-23 13:27:23 +08:00
downloadTemplate() {
wx.downloadFile({
url: `${app.globalData.baseUrl}/api/v1/entries/template`,
success: (res) => {
if (res.statusCode !== 200) {
this.setData({ error: '模板下载失败' })
return
}
wx.openDocument({
filePath: res.tempFilePath,
fileType: 'xlsx',
fail: () => this.setData({ error: '模板打开失败,请先保存到本地再上传' }),
})
},
fail: () => this.setData({ error: '模板下载失败' }),
})
},
chooseFile() {
wx.chooseMessageFile({
count: 1,
type: 'file',
2026-04-23 13:27:23 +08:00
extension: ['xlsx'],
success: (res) => {
const file = res.tempFiles[0]
2026-04-19 11:56:49 +08:00
this.setData({ fileName: file.name, loading: true, error: '' })
wx.uploadFile({
url: `${app.globalData.baseUrl}/api/v1/entries/upload-file`,
filePath: file.path,
name: 'file',
header: { 'X-Auth-Token': app.globalData.authToken || '' },
success: (uploadRes) => {
try {
2026-04-19 11:56:49 +08:00
const payload = JSON.parse(uploadRes.data)
this.setData({ result: payload.data || null })
} catch (e) {
2026-04-19 11:56:49 +08:00
this.setData({ error: '上传结果解析失败' })
}
},
2026-04-19 11:56:49 +08:00
fail: () => this.setData({ error: '文件上传失败' }),
complete: () => this.setData({ loading: false }),
})
},
})
},
})