index.js 6.7 KB

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