function formattedFiles(list) { if (list.length == 0) return []; let suffixList = { image: ['png', 'jpg', 'jpeg', 'bmp', 'gif', 'webp', 'svg', 'tiff'], video: ['mp4', 'ogg', 'webm'], word: ['doc', 'docx'], excel: ['xls', 'xlsx'], ppt: ['ppt', 'pptx'], txt: ['txt', 'md', 'js', 'json'], pdf: ['pdf'], rar: ['7z', 'zip', 'rar', 'kz', 'ace', 'arj', 'bz2', 'cab', 'gz', 'iso', 'jar', 'lzh', 'tar', 'z'], folder: ['folder'] }, typeList = []; for (let key in suffixList) typeList.push(key); for (let i = 0; i < list.length; i++) { list[i].fileType = 'unknown'; const suffix = list[i].postfix.toLowerCase(); if (suffix != "folder") { for (var key in suffixList) { if (suffixList[key].some(value => value == suffix)) list[i].fileType = key; } } else { list[i].fileType = "folder"; } } return list; } /* 预览媒体 */ function viewMedias(files, index, type) { // #ifndef MP if (type == 'image') { uni.previewImage({ current: index, urls: files, loop: true, }) } else { window.open(files[index].url) } // #endif // #ifdef MP-WEIXIN wx.previewMedia({ current: index, sources: files, }) // #endif } /* 预览文档 */ function viewFlies(item) { uni.showLoading({ title: '加载中...', }) uni.downloadFile({ url: item.url, complete({ statusCode, tempFilePath }) { if (statusCode != 200) return; uni.openDocument({ filePath: tempFilePath, fileType: item.postfix, showMenu: true, complete({ errMsg }) { uni.hideLoading(); if (errMsg != "openDocument:ok") uni.showToast({ title: '打开失败', icon: "none" }) } }) } }) } module.exports = { viewMedias, viewFlies, formattedFiles }