// component/documentItem/documentItem.ts import { httpUtil } from "../../utils/restful" Component({ /** * 组件的属性列表 */ properties: { itemData: null, itemIndex: { type: String, value: '' }, }, /** * 组件的初始数据 */ data: { type: 1, hasDownLoad: false, downLoadProgress: '', progressType: 0, }, lifetimes: { attached: function () { // 在组件实例被从页面节点树添加时执行 this.setData({ type: this.properties.itemData.type }) console.log("itemIndex:", this.properties.itemIndex) }, detached: function () { // 在组件实例被从页面节点树移除时执行 }, }, /** * 组件的方法列表 */ methods: { showActionWindow: function () { this.triggerEvent("showActionWindow", { item: this.properties.itemData, itemIndex: this.properties.itemIndex }) }, downLoadItem: function (progressType, fileType) { console.log("fileType:", fileType) let that = this; that.setData({ progressType: progressType, type: fileType }) //文件下载 httpUtil.wxDownLoadFile(that.data.itemData.url, function (res: any) { // console.log("下载没===", res) that.setDownLoadProgress(res) }).then((res) => { // console.log("下载成功:", res) that.downLoadComplete(res) }).catch((res) => { // console.log("下载失败") }) }, setDownLoadProgress: function (data: any) { if (this.data.progressType == 0) { console.log("设置下载百分比:", data.detail.progress) this.setData({ downLoadProgress: '下载:' + data.detail.progress + '%', hasDownLoad: true }) } else { this.setData({ downLoadProgress: '下载:' + data.progress + '%', hasDownLoad: true }) } }, downLoadComplete: function (data: any) { console.log("下载成功:", data) // console.log("data.detail.data.tempFilePath:", data.detail.data.tempFilePath) console.log("downLoadComplete:", this.data.type) let path = '' if (this.data.progressType == 0) { path = data.detail.data.tempFilePath } else if (this.data.progressType == 1) { path = data.tempFilePath } if (this.data.type == 0) { //0是图片,图片保存到相册 wx.saveImageToPhotosAlbum({ filePath: path, success(res) { wx.showToast({ title: '保存相册成功', icon: 'succes', duration: 1000, mask: true }) } }) } else if (this.data.type == 1) { //1是视频,视频保存到相册 wx.saveVideoToPhotosAlbum({ filePath: path, success(res) { // console.log(res.errMsg) wx.showToast({ title: '保存相册成功', icon: 'succes', duration: 1000, mask: true }) } }) } this.setData({ downLoadProgress: '下载完成' }) }, downLoadError: function (data: any) { console.log("下载失败:", data) this.setData({ downLoadProgress: '下载失败' }) } } })