My_Files.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. <template>
  2. <view class="media">
  3. <!-- 图片 -->
  4. <navigator url="#" class="item" v-for="(item, index) in files.images" :key="item.attachmentid">
  5. <image :src="item.url" mode="aspectFill" @click="viewMedias(index, 'image')" />
  6. <image v-if="isDelete" class="delete" src="../static/img/delete.png" @click.stop="handleDeleteFile(item)" />
  7. </navigator>
  8. <!-- 视频 -->
  9. <navigator url="#" class="item" v-for="(item, index) in files.videos" :key="item.attachmentid">
  10. <image :src="item.subfiles[0].url" mode="aspectFill" />
  11. <view class="shade" @click="viewMedias(index, 'video')"><van-icon size='48rpx' name="play" /></view>
  12. <image v-if="isDelete" class="delete" src="../static/img/delete.png" @click.stop="handleDeleteFile(item)" />
  13. </navigator>
  14. <!-- 文件 -->
  15. <navigator url="#" class="item file" v-for="item in files.files" :key="item.attachmentid" @click="viewFlies(item)">
  16. <image class="image" :src="item.cover" mode="heightFix" />
  17. <view class="name">{{ item.document }}</view>
  18. <image v-if="isDelete" class="delete" src="../static/img/delete.png" @click.stop="handleDeleteFile(item)" />
  19. </navigator>
  20. </view>
  21. </template>
  22. <script>
  23. export default {
  24. props: {
  25. isDelete: {
  26. type: Boolean
  27. },
  28. onDeteleFiles: {
  29. type: Function
  30. },
  31. strict: {
  32. type: Boolean,
  33. default: true
  34. },
  35. aDeletion: {
  36. type: Boolean,
  37. },
  38. attinfos: Array
  39. },
  40. watch: {
  41. attinfos: {
  42. handler(newVal, oldVal) {
  43. this.handleFiles(newVal)
  44. },
  45. immediate: true,
  46. }
  47. },
  48. data() {
  49. return {
  50. files: {
  51. images: [],
  52. viewImages: [],
  53. videos: [],
  54. viewVideos: [],
  55. files: []
  56. },
  57. }
  58. },
  59. methods: {
  60. /* 格式化附件 */
  61. formattedFiles(list) {
  62. if (list.length == 0) return [];
  63. let suffixList = {
  64. image: ['png', 'jpg', 'jpeg', 'bmp', 'gif', 'webp', 'svg', 'tiff'],
  65. video: ['mp4', 'ogg', 'webm'],
  66. word: ['doc', 'docx'],
  67. excel: ['xls', 'xlsx'],
  68. ppt: ['ppt', 'pptx'],
  69. txt: ['txt', 'md', 'js', 'json'],
  70. pdf: ['pdf'],
  71. rar: ['7z', 'zip', 'rar', 'kz', 'ace', 'arj', 'bz2', 'cab', 'gz', 'iso', 'jar', 'lzh', 'tar', 'z'],
  72. folder: ['"folder"']
  73. },
  74. typeList = [];
  75. for (let key in suffixList) typeList.push(key);
  76. for (let i = 0; i < list.length; i++) {
  77. list[i].fileType = 'unknown';
  78. list[i].cover = `/static/file/unknown.png`
  79. const suffix = list[i].postfix.toLowerCase();
  80. if (suffix != "folder") {
  81. for (var key in suffixList) {
  82. if (suffixList[key].some(value => value == suffix)) {
  83. list[i].fileType = key;
  84. if (key == 'image') {
  85. list[i].cover = list[i].url;
  86. } else if (typeList.includes(key)) {
  87. list[i].cover = `/static/file/${key}.png`;
  88. }
  89. }
  90. }
  91. } else {
  92. list[i].fileType = "folder";
  93. list[i].cover = `/static/file/folder.png`
  94. }
  95. }
  96. return list;
  97. },
  98. /* 删除文件 */
  99. handleDeleteFile(item) {
  100. let that = this;
  101. if (this.strict) {
  102. uni.showModal({
  103. title: '提示',
  104. content: '是否确定删除该附件',
  105. complete: ({ confirm }) => {
  106. if (confirm) start()
  107. }
  108. })
  109. } else {
  110. start()
  111. }
  112. function start() {
  113. if (that.aDeletion) return that.$emit("onDeteleFiles", {
  114. deleteid: item.linksid,
  115. attachmentid: item.attachmentid,
  116. ...that.getFiles()
  117. })
  118. that.$Http.basic({
  119. "classname": "system.attachment.Attachment",
  120. "method": "deleteFileLink",
  121. "content": {
  122. "linksids": [item.linksid]
  123. }
  124. }).then(res => {
  125. if (that.cutoff(res.msg)) return;
  126. let files = that.files;
  127. /* switch (item.fileType) {
  128. case "image":
  129. files.images = files.images.filter(v => v.url != item.url);
  130. files.viewImages = files.viewImages.filter(v => v.url != item.url);
  131. break;
  132. case "video":
  133. files.videos = files.videos.filter(v => v.url != item.url);
  134. files.viewVideos = files.viewVideos.filter(v => v.url != item.url);
  135. break;
  136. default:
  137. files.files = files.files.filter(v => v.attachmentid != item.attachmentid);
  138. break;
  139. };
  140. that.files = files; */
  141. that.$emit("onDeteleFiles", {
  142. deleteid: item.linksid,
  143. attachmentid: item.attachmentid,
  144. ...that.getFiles()
  145. })
  146. });
  147. }
  148. },
  149. /* 处理附件 */
  150. handleFiles(arr, init = false) {
  151. let files = init ? {
  152. images: [],
  153. viewImages: [],
  154. videos: [],
  155. viewVideos: [],
  156. files: []
  157. } : this.files,
  158. list = this.formattedFiles(arr);
  159. list.forEach(v => {
  160. switch (v.fileType) {
  161. case "video":
  162. files.videos.push(v)
  163. files.viewVideos.push({
  164. url: v.url,
  165. type: "video",
  166. poster: v.subfiles[0].url
  167. })
  168. break;
  169. case "image":
  170. files.images.push(v)
  171. files.viewImages.push({
  172. url: v.url,
  173. type: "image"
  174. })
  175. break;
  176. default:
  177. files.files.push(v)
  178. break;
  179. }
  180. });
  181. this.files = files;
  182. },
  183. /* 初始化数据 */
  184. initData() {
  185. this.files = {
  186. images: [],
  187. viewImages: [],
  188. videos: [],
  189. viewVideos: [],
  190. files: []
  191. }
  192. },
  193. /* 返回数据ID数组 用来换绑数据 */
  194. getFiles() {
  195. let data = {
  196. attachmentids: [],
  197. list: [],
  198. },
  199. files = this.files;
  200. files.files.forEach(v => {
  201. data.attachmentids.push(v.attachmentid);
  202. data.list.push(v);
  203. })
  204. files.images.forEach(v => {
  205. data.attachmentids.push(v.attachmentid);
  206. data.list.push(v);
  207. })
  208. files.videos.forEach(v => {
  209. data.attachmentids.push(v.attachmentid);
  210. data.list.push(v);
  211. });
  212. return data
  213. },
  214. /* 预览媒体 */
  215. viewMedias(index, type) {
  216. // #ifndef MP
  217. console.log(this.files.viewVideos)
  218. if (type == 'image') {
  219. uni.previewImage({
  220. current: index,
  221. urls: this.files.viewImages,
  222. loop: true,
  223. })
  224. } else {
  225. window.open(this.files.viewVideos[index].url)
  226. }
  227. // #endif
  228. // #ifdef MP-WEIXIN
  229. wx.previewMedia({
  230. current: index,
  231. sources: type == 'image' ? this.files.viewImages : this.files.viewVideos,
  232. })
  233. // #endif
  234. },
  235. /* 预览文档 */
  236. viewFlies(item) {
  237. console.log(item)
  238. uni.showLoading({
  239. title: '加载中...',
  240. })
  241. uni.downloadFile({
  242. url: item.url,
  243. complete({
  244. statusCode,
  245. tempFilePath
  246. }) {
  247. if (statusCode != 200) return;
  248. uni.openDocument({
  249. filePath: tempFilePath,
  250. fileType: item.postfix,
  251. showMenu: true,
  252. complete({
  253. errMsg
  254. }) {
  255. uni.hideLoading();
  256. if (errMsg != "openDocument:ok") uni.showToast({
  257. title: '打开失败',
  258. icon: "none"
  259. })
  260. }
  261. })
  262. }
  263. })
  264. },
  265. deleteAll() {
  266. let linksids = this.getFiles().list.map(v => v.linksid);
  267. if (linksids.length) this.$Http.basic({
  268. "classname": "system.attachment.Attachment",
  269. "method": "deleteFileLink",
  270. "content": {
  271. linksids
  272. }
  273. }).then(res => {
  274. console.log("删除所有未保存附件", linksids, res)
  275. if (res.msg != '成功') return uni.showToast({
  276. title: res.data,
  277. icon: "none"
  278. });
  279. });
  280. }
  281. }
  282. }
  283. </script>
  284. <style lang="scss" scoped>
  285. .media {
  286. width: 100%;
  287. display: flex;
  288. flex-wrap: wrap;
  289. box-sizing: border-box;
  290. .item {
  291. position: relative;
  292. width: 62px;
  293. height: 62px;
  294. margin-right: 6px;
  295. margin-bottom: 6px;
  296. border-radius: 4px;
  297. overflow: hidden;
  298. image {
  299. width: 100%;
  300. height: 100%;
  301. }
  302. .delete {
  303. position: absolute;
  304. width: 20px;
  305. height: 20px;
  306. right: 0;
  307. top: 0;
  308. }
  309. .shade {
  310. display: flex;
  311. align-items: center;
  312. justify-content: center;
  313. position: absolute;
  314. width: 100%;
  315. height: 100%;
  316. background: rgba(0, 0, 0, 0.2);
  317. top: 0;
  318. left: 0;
  319. color: #ffffff;
  320. }
  321. }
  322. .item:nth-child(5n) {
  323. margin-right: 0;
  324. }
  325. .file {
  326. display: flex;
  327. flex-direction: column;
  328. border: 1px solid #CCCCCC;
  329. box-sizing: border-box;
  330. align-items: center;
  331. .name {
  332. width: 60px;
  333. font-size: 12px;
  334. font-family: PingFang SC-Regular, PingFang SC;
  335. color: #333333;
  336. margin-top: 10px;
  337. overflow: hidden;
  338. white-space: nowrap;
  339. text-overflow: ellipsis;
  340. }
  341. .image {
  342. height: 29px;
  343. margin-top: 12rpx;
  344. }
  345. }
  346. }
  347. </style>