index.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. const _Http = getApp().globalData.http;
  2. import {
  3. fileList
  4. } from "../../utils/FormatTheAttachment";
  5. Component({
  6. properties: {
  7. files: {
  8. type: Object,
  9. value: {
  10. images: [],
  11. viewImages: [],
  12. videos: [],
  13. viewVideos: [],
  14. files: []
  15. },
  16. },
  17. delete: {
  18. type: Boolean
  19. },
  20. deleteCallBack: {
  21. type: Function
  22. }
  23. },
  24. methods: {
  25. /* 预览媒体 */
  26. viewMedias(e) {
  27. const {
  28. index,
  29. type
  30. } = e.currentTarget.dataset;
  31. wx.previewMedia({
  32. current: index,
  33. sources: type == 'image' ? this.data.files.viewImages : this.data.files.viewVideos,
  34. })
  35. },
  36. /* 预览文档 */
  37. viewFlies(e) {
  38. const {
  39. item
  40. } = e.currentTarget.dataset;
  41. wx.showLoading({
  42. title: getApp().globalData.Language.getMapText('加载中...'),
  43. })
  44. wx.downloadFile({
  45. url: item.url,
  46. complete({
  47. statusCode,
  48. tempFilePath
  49. }) {
  50. if (statusCode != 200) return;
  51. wx.openDocument({
  52. filePath: tempFilePath,
  53. fileType: item.postfix,
  54. showMenu: true,
  55. complete({
  56. errMsg
  57. }) {
  58. wx.hideLoading();
  59. if (errMsg != "openDocument:ok") getApp().globalData.Language.showToast('打开失败')
  60. }
  61. })
  62. }
  63. })
  64. },
  65. /* 删除文件 */
  66. handleDeleteFile(e) {
  67. let item = e.currentTarget.dataset.item || e.currentTarget.dataset.item;
  68. _Http.basic({
  69. "classname": "system.attachment.Attachment",
  70. "method": "deleteFileLink",
  71. "content": {
  72. "linksids": [item.linksid]
  73. }
  74. }).then(res => {
  75. if (res.code != '1') return wx.showToast({
  76. title: res.msg,
  77. icon: "none"
  78. });
  79. let files = this.data.files;
  80. switch (item.fileType) {
  81. case "image":
  82. files.images = files.images.filter(v => v.url != item.url);
  83. files.viewImages = files.viewImages.filter(v => v.url != item.url);
  84. break;
  85. case "video":
  86. files.videos = files.videos.filter(v => v.url != item.url);
  87. files.viewVideos = files.viewVideos.filter(v => v.url != item.url);
  88. break;
  89. default:
  90. files.files = files.files.filter(v => v.attachmentid != item.attachmentid);
  91. break;
  92. };
  93. this.setData({
  94. files
  95. });
  96. this.triggerEvent("deleteCallBack")
  97. })
  98. },
  99. /* 处理附件 */
  100. handleFiles(arr, init = false) {
  101. let files = init ? {
  102. images: [],
  103. viewImages: [],
  104. videos: [],
  105. viewVideos: [],
  106. files: []
  107. } : this.data.files,
  108. list = fileList(arr);
  109. list.forEach(v => {
  110. switch (v.fileType) {
  111. case "video":
  112. files.videos.push(v)
  113. files.viewVideos.push({
  114. url: v.url,
  115. type: "video",
  116. poster: v.subfiles[0].url
  117. })
  118. break;
  119. case "image":
  120. files.images.push(v)
  121. files.viewImages.push({
  122. url: v.url,
  123. type: "image"
  124. })
  125. break;
  126. default:
  127. files.files.push(v)
  128. break;
  129. }
  130. });
  131. this.setData({
  132. files
  133. })
  134. },
  135. /* 初始化数据 */
  136. initData() {
  137. this.setData({
  138. files: {
  139. images: [],
  140. viewImages: [],
  141. videos: [],
  142. viewVideos: [],
  143. files: []
  144. }
  145. })
  146. },
  147. }
  148. })