55 lines
1.5 KiB
JavaScript
55 lines
1.5 KiB
JavaScript
const app = getApp()
|
|
|
|
Page({
|
|
data: {
|
|
result: null,
|
|
fileName: '',
|
|
loading: false,
|
|
error: '',
|
|
},
|
|
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',
|
|
extension: ['xlsx'],
|
|
success: (res) => {
|
|
const file = res.tempFiles[0]
|
|
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 {
|
|
const payload = JSON.parse(uploadRes.data)
|
|
this.setData({ result: payload.data || null })
|
|
} catch (e) {
|
|
this.setData({ error: '上传结果解析失败' })
|
|
}
|
|
},
|
|
fail: () => this.setData({ error: '文件上传失败' }),
|
|
complete: () => this.setData({ loading: false }),
|
|
})
|
|
},
|
|
})
|
|
},
|
|
})
|