123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137 |
- // 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: '下载失败'
- })
- }
- }
- })
|