index.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. const _Http = getApp().globalData.http;
  2. import {
  3. fileList
  4. } from "../../utils/matchingFeilType";
  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. onDeteleFiles: {
  21. type: Function
  22. },
  23. padding: {
  24. type: String,
  25. value: '0 30rpx'
  26. }
  27. },
  28. methods: {
  29. /* 预览媒体 */
  30. viewMedias(e) {
  31. const {
  32. index,
  33. type
  34. } = e.currentTarget.dataset;
  35. wx.previewMedia({
  36. current: index,
  37. sources: type == 'image' ? this.data.files.viewImages : this.data.files.viewVideos,
  38. })
  39. },
  40. /* 预览文档 */
  41. viewFlies(e) {
  42. const {
  43. item
  44. } = e.currentTarget.dataset;
  45. wx.showLoading({
  46. title: '加载中...',
  47. })
  48. wx.downloadFile({
  49. url: item.url,
  50. complete({
  51. statusCode,
  52. tempFilePath
  53. }) {
  54. if (statusCode != 200) return;
  55. wx.openDocument({
  56. filePath: tempFilePath,
  57. fileType: item.postfix,
  58. showMenu: true,
  59. complete({
  60. errMsg
  61. }) {
  62. wx.hideLoading();
  63. if (errMsg != "openDocument:ok") wx.showToast({
  64. title: '打开失败',
  65. icon: "none"
  66. })
  67. }
  68. })
  69. }
  70. })
  71. },
  72. /* 删除文件 */
  73. handleDeleteFile(e) {
  74. let item = e.currentTarget.dataset.item || e.currentTarget.dataset.item;
  75. _Http.basic({
  76. "classname": "system.attachment.Attachment",
  77. "method": "deleteFileLink",
  78. "content": {
  79. "linksids": [item.linksid]
  80. }
  81. }).then(res => {
  82. if (res.msg != '成功') return wx.showToast({
  83. title: res.data,
  84. icon: "none"
  85. });
  86. let files = this.data.files;
  87. switch (item.fileType) {
  88. case "image":
  89. files.images = files.images.filter(v => v.url != item.url);
  90. files.viewImages = files.viewImages.filter(v => v.url != item.url);
  91. break;
  92. case "video":
  93. files.videos = files.videos.filter(v => v.url != item.url);
  94. files.viewVideos = files.viewVideos.filter(v => v.url != item.url);
  95. break;
  96. default:
  97. files.files = files.files.filter(v => v.attachmentid != item.attachmentid);
  98. break;
  99. };
  100. this.setData({
  101. files
  102. });
  103. this.triggerEvent("onDeteleFiles", this.getFiles())
  104. });
  105. },
  106. /* 处理附件 */
  107. handleFiles(arr, init = false) {
  108. let files = init ? {
  109. images: [],
  110. viewImages: [],
  111. videos: [],
  112. viewVideos: [],
  113. files: []
  114. } : this.data.files,
  115. list = fileList(arr);
  116. list.forEach(v => {
  117. switch (v.fileType) {
  118. case "video":
  119. files.videos.push(v)
  120. files.viewVideos.push({
  121. url: v.url,
  122. type: "video",
  123. poster: v.subfiles[0].url
  124. })
  125. break;
  126. case "image":
  127. files.images.push(v)
  128. files.viewImages.push({
  129. url: v.url,
  130. type: "image"
  131. })
  132. break;
  133. default:
  134. files.files.push(v)
  135. break;
  136. }
  137. });
  138. this.setData({
  139. files
  140. })
  141. },
  142. /* 初始化数据 */
  143. initData() {
  144. this.setData({
  145. files: {
  146. images: [],
  147. viewImages: [],
  148. videos: [],
  149. viewVideos: [],
  150. files: []
  151. }
  152. })
  153. },
  154. /* 返回数据ID数组 用来换绑数据 */
  155. getFiles() {
  156. let data = {
  157. attachmentids: [],
  158. list: [],
  159. },
  160. files = this.data.files;
  161. files.files.forEach(v => {
  162. data.attachmentids.push(v.attachmentid);
  163. data.list.push(v);
  164. })
  165. files.images.forEach(v => {
  166. data.attachmentids.push(v.attachmentid);
  167. data.list.push(v);
  168. })
  169. files.videos.forEach(v => {
  170. data.attachmentids.push(v.attachmentid);
  171. data.list.push(v);
  172. });
  173. return data
  174. },
  175. deleteAll() {
  176. let linksids = this.getFiles().list.map(v => v.linksid);
  177. if (linksids.length) _Http.basic({
  178. "classname": "system.attachment.Attachment",
  179. "method": "deleteFileLink",
  180. "content": {
  181. linksids
  182. }
  183. }).then(res => {
  184. console.log("删除所有未保存附件", linksids, res)
  185. if (res.msg != '成功') return wx.showToast({
  186. title: res.data,
  187. icon: "none"
  188. });
  189. });
  190. }
  191. }
  192. })