// 请求 下载表格 接口
exportExcel (e) {
let that = this
let id = that.getPageOptions().id
let fileName = `【${that.data.holderInfo.nameC}】 ${that.data.bookingInfo.area}-${that.data.productInfo.company}-${that.data.productInfo.plan}`
wx.showLoading({
title: '加载中...',
mask: true
})
Util.httpRequest('Get', `${Config.exportExcel}/${id}/excel`, {}, 'arraybuffer').then(res => {
console.log(res)
wx.hideLoading()
// that.createCatalog(res.data, fileName) // 创建存放目录
}).catch(err => {
console.log(err)
})
},
// 创建存放目录
createCatalog (arraybuffer, fileName) {
const rootPath = wx.env.USER_DATA_PATH
const cachePath = rootPath + '/openFileCache'
const manage = wx.getFileSystemManager()
let newFileName = fileName + '.xlsx'
// 判断是否已存在自定义目录
manage.access({
path: cachePath,
success: res => {
console.log(res)
let readdir = manage.readdirSync(cachePath)
console.log(readdir)
for (let path of readdir) {
manage.unlinkSync(cachePath + '/' + path)
}
this.openFileEv(cachePath, newFileName, arraybuffer)
},
fail: err => {
console.log(err)
// 新建自定义目录
manage.mkdir({
dirPath: cachePath,
recursive: true,
success: res => {
console.log('创建成功')
this.openFileEv(cachePath, newFileName)
},
fail: err => {
console.log('创建失败')
}
})
}
})
},
// 打开文件
openFileEv (cachePath, newFileName, arraybuffer) {
const manage = wx.getFileSystemManager()
// 显示加载
wx.showLoading({
title: '加载中...',
mask: true
})
manage.writeFile({ // 写文件
filePath: cachePath + '/' + newFileName, // wx.env.USER_DATA_PATH 指定临时文件存入的路径,后面字符串自定义
data: arraybuffer,
encoding: "binary", //二进制流文件必须是 binary
success: res => {
// 打开文档
wx.openDocument({
filePath: cachePath + '/' + newFileName,
showMenu: true,
success: function (res) {
console.log('打开文档成功')
},
fail: function () {
console.log('打开失败')
}
})
}
})
},