documentItem.ts 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. // component/documentItem/documentItem.ts
  2. import { httpUtil } from "../../utils/restful"
  3. Component({
  4. /**
  5. * 组件的属性列表
  6. */
  7. properties: {
  8. itemData: null,
  9. itemIndex: {
  10. type: String,
  11. value: ''
  12. },
  13. },
  14. /**
  15. * 组件的初始数据
  16. */
  17. data: {
  18. type: 1,
  19. hasDownLoad: false,
  20. downLoadProgress: '',
  21. progressType: 0,
  22. },
  23. lifetimes: {
  24. attached: function () {
  25. // 在组件实例被从页面节点树添加时执行
  26. this.setData({
  27. type: this.properties.itemData.type
  28. })
  29. console.log("itemIndex:", this.properties.itemIndex)
  30. },
  31. detached: function () {
  32. // 在组件实例被从页面节点树移除时执行
  33. },
  34. },
  35. /**
  36. * 组件的方法列表
  37. */
  38. methods: {
  39. showActionWindow: function () {
  40. this.triggerEvent("showActionWindow", { item: this.properties.itemData, itemIndex: this.properties.itemIndex })
  41. },
  42. downLoadItem: function (progressType, fileType) {
  43. console.log("fileType:", fileType)
  44. let that = this;
  45. that.setData({
  46. progressType: progressType,
  47. type: fileType
  48. })
  49. //文件下载
  50. httpUtil.wxDownLoadFile(that.data.itemData.url, function (res: any) {
  51. // console.log("下载没===", res)
  52. that.setDownLoadProgress(res)
  53. }).then((res) => {
  54. // console.log("下载成功:", res)
  55. that.downLoadComplete(res)
  56. }).catch((res) => {
  57. // console.log("下载失败")
  58. })
  59. },
  60. setDownLoadProgress: function (data: any) {
  61. if (this.data.progressType == 0) {
  62. console.log("设置下载百分比:", data.detail.progress)
  63. this.setData({
  64. downLoadProgress: '下载:' + data.detail.progress + '%',
  65. hasDownLoad: true
  66. })
  67. } else {
  68. this.setData({
  69. downLoadProgress: '下载:' + data.progress + '%',
  70. hasDownLoad: true
  71. })
  72. }
  73. },
  74. downLoadComplete: function (data: any) {
  75. console.log("下载成功:", data)
  76. // console.log("data.detail.data.tempFilePath:", data.detail.data.tempFilePath)
  77. console.log("downLoadComplete:", this.data.type)
  78. let path = ''
  79. if (this.data.progressType == 0) {
  80. path = data.detail.data.tempFilePath
  81. } else if (this.data.progressType == 1) {
  82. path = data.tempFilePath
  83. }
  84. if (this.data.type == 0) {
  85. //0是图片,图片保存到相册
  86. wx.saveImageToPhotosAlbum({
  87. filePath: path,
  88. success(res) {
  89. wx.showToast({
  90. title: '保存相册成功',
  91. icon: 'succes',
  92. duration: 1000,
  93. mask: true
  94. })
  95. }
  96. })
  97. } else if (this.data.type == 1) {
  98. //1是视频,视频保存到相册
  99. wx.saveVideoToPhotosAlbum({
  100. filePath: path,
  101. success(res) {
  102. // console.log(res.errMsg)
  103. wx.showToast({
  104. title: '保存相册成功',
  105. icon: 'succes',
  106. duration: 1000,
  107. mask: true
  108. })
  109. }
  110. })
  111. }
  112. this.setData({
  113. downLoadProgress: '下载完成'
  114. })
  115. },
  116. downLoadError: function (data: any) {
  117. console.log("下载失败:", data)
  118. this.setData({
  119. downLoadProgress: '下载失败'
  120. })
  121. }
  122. }
  123. })