|
@@ -0,0 +1,286 @@
|
|
|
+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
|
|
|
+ uni.previewMedia({
|
|
|
+ current: index,
|
|
|
+ sources: files,
|
|
|
+ })
|
|
|
+ // #endif
|
|
|
+}
|
|
|
+
|
|
|
+/* 预览图片 */
|
|
|
+function viewImage(url) {
|
|
|
+ // #ifndef MP
|
|
|
+ uni.previewImage({
|
|
|
+ current: 0,
|
|
|
+ urls: [url],
|
|
|
+ loop: true,
|
|
|
+ })
|
|
|
+ // #endif
|
|
|
+ // #ifdef MP-WEIXIN
|
|
|
+ uni.previewImage({
|
|
|
+ urls: [url]
|
|
|
+ })
|
|
|
+ // #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"
|
|
|
+ })
|
|
|
+ }
|
|
|
+ })
|
|
|
+ }
|
|
|
+ })
|
|
|
+}
|
|
|
+
|
|
|
+function wxSaveFile(file) {
|
|
|
+ const fs = uni.getFileSystemManager(),
|
|
|
+ basePath = String(`${uni.env.USER_DATA_PATH}/${file.postfix}`);
|
|
|
+ fs.access({
|
|
|
+ path: basePath,
|
|
|
+ success(res) {
|
|
|
+ fs.getSavedFileList({
|
|
|
+ success: res => {
|
|
|
+ console.log("临时文件列表", res)
|
|
|
+ }
|
|
|
+ })
|
|
|
+ },
|
|
|
+ fail(res) {
|
|
|
+ fs.mkdir({
|
|
|
+ dirPath: basePath,
|
|
|
+ recursive: true,
|
|
|
+ success(res) {
|
|
|
+ console.log("创建目录", res)
|
|
|
+ },
|
|
|
+ fail(res) {
|
|
|
+ console.error("创建目录", res)
|
|
|
+ }
|
|
|
+ })
|
|
|
+ }
|
|
|
+ })
|
|
|
+ const downloadTask = uni.downloadFile({
|
|
|
+ url: file.url,
|
|
|
+ filePath: basePath + '/' + file.document,
|
|
|
+ timeout: 6000000,
|
|
|
+ success(res) {
|
|
|
+ console.log("下载文件", res)
|
|
|
+ if (file.fileType == "video") {
|
|
|
+ saveVideo(res.filePath)
|
|
|
+ } else {
|
|
|
+ saveImage(res.filePath)
|
|
|
+ }
|
|
|
+
|
|
|
+ function shareVideo() {
|
|
|
+ uni.shareVideoMessage({
|
|
|
+ videoPath: res.tempFilePath,
|
|
|
+ complete(res) {
|
|
|
+ console.log("转发", res)
|
|
|
+ clearFile(filePath)
|
|
|
+ /* uni.showToast({
|
|
|
+ title: `转发失败:${err.errMsg}`,
|
|
|
+ icon: "none"
|
|
|
+ }) */
|
|
|
+ }
|
|
|
+ })
|
|
|
+ }
|
|
|
+
|
|
|
+ function saveImage(filePath) {
|
|
|
+ uni.saveImageToPhotosAlbum({
|
|
|
+ filePath,
|
|
|
+ success(res) {
|
|
|
+ console.log("保存图片", res)
|
|
|
+ uni.showModal({
|
|
|
+ title: '提示',
|
|
|
+ content: '图片已保存到系统相册',
|
|
|
+ showCancel: false
|
|
|
+ })
|
|
|
+ uni.hideLoading()
|
|
|
+ clearFile(filePath)
|
|
|
+ },
|
|
|
+ fail(err) {
|
|
|
+ console.log("保存失败", err)
|
|
|
+ if (err.errno == 103) {
|
|
|
+ uni.showModal({
|
|
|
+ title: '提示',
|
|
|
+ content: '未获取保存相册权限,无法保存!',
|
|
|
+ confirmText: "前去授权",
|
|
|
+ success({
|
|
|
+ confirm
|
|
|
+ }) {
|
|
|
+ if (confirm) uni.openSetting({
|
|
|
+ success(res) {
|
|
|
+ console.log(res.authSetting["scope.writePhotosAlbum"])
|
|
|
+ if (res.authSetting["scope.writePhotosAlbum"]) {
|
|
|
+ saveVideo(filePath)
|
|
|
+ } else {
|
|
|
+ uni.showToast({
|
|
|
+ title: '未获得授权',
|
|
|
+ icon: "none"
|
|
|
+ })
|
|
|
+ }
|
|
|
+ }
|
|
|
+ })
|
|
|
+ }
|
|
|
+ })
|
|
|
+ } else {
|
|
|
+ clearFile(filePath)
|
|
|
+ uni.showToast({
|
|
|
+ title: err,
|
|
|
+ icon: "none"
|
|
|
+ })
|
|
|
+ }
|
|
|
+ }
|
|
|
+ })
|
|
|
+ }
|
|
|
+
|
|
|
+ function saveVideo(filePath) {
|
|
|
+ uni.saveVideoToPhotosAlbum({
|
|
|
+ filePath,
|
|
|
+ success(res) {
|
|
|
+ console.log("保存商品", res)
|
|
|
+ uni.showModal({
|
|
|
+ title: '提示',
|
|
|
+ content: '视频已保存到系统相册',
|
|
|
+ showCancel: false
|
|
|
+ })
|
|
|
+ uni.hideLoading()
|
|
|
+ clearFile(filePath)
|
|
|
+ },
|
|
|
+ fail(err) {
|
|
|
+ console.log("保存失败", err)
|
|
|
+ if (err.errno == 103) {
|
|
|
+ uni.showModal({
|
|
|
+ title: '提示',
|
|
|
+ content: '未获取保存相册权限,无法保存!',
|
|
|
+ confirmText: "前去授权",
|
|
|
+ success({
|
|
|
+ confirm
|
|
|
+ }) {
|
|
|
+ if (confirm) uni.openSetting({
|
|
|
+ success(res) {
|
|
|
+ console.log(res.authSetting["scope.writePhotosAlbum"])
|
|
|
+ if (res.authSetting["scope.writePhotosAlbum"]) {
|
|
|
+ saveVideo(filePath)
|
|
|
+ } else {
|
|
|
+ uni.showToast({
|
|
|
+ title: '未获得授权',
|
|
|
+ icon: "none"
|
|
|
+ })
|
|
|
+ }
|
|
|
+ }
|
|
|
+ })
|
|
|
+ }
|
|
|
+ })
|
|
|
+ } else {
|
|
|
+ clearFile(filePath)
|
|
|
+ uni.showToast({
|
|
|
+ title: err,
|
|
|
+ icon: "none"
|
|
|
+ })
|
|
|
+ }
|
|
|
+ }
|
|
|
+ })
|
|
|
+ }
|
|
|
+
|
|
|
+ function clearFile(filePath) {
|
|
|
+ let fs = uni.getFileSystemManager()
|
|
|
+ fs.unlink({
|
|
|
+ filePath,
|
|
|
+ success(res) {
|
|
|
+ console.log("文件删除", res)
|
|
|
+ },
|
|
|
+ fail(res) {
|
|
|
+ console.log("删除失败", res)
|
|
|
+ }
|
|
|
+ })
|
|
|
+ }
|
|
|
+
|
|
|
+ },
|
|
|
+ fail(err) {
|
|
|
+ console.log("下载失败", err)
|
|
|
+ uni.showToast({
|
|
|
+ title: `下载失败:${err.errMsg}`,
|
|
|
+ icon: "none"
|
|
|
+ })
|
|
|
+ if (err.errMsg == 'downloadFile:fail exceed max file size') uni.showToast({
|
|
|
+ title: '视频体积超大,无法保存!请尝试打开视频长按保存下载',
|
|
|
+ icon: "none"
|
|
|
+ })
|
|
|
+ }
|
|
|
+ })
|
|
|
+ downloadTask.onProgressUpdate((res) => {
|
|
|
+ uni.showLoading({
|
|
|
+ title: res.progress + `%`,
|
|
|
+ mask: true
|
|
|
+ })
|
|
|
+ if (res.progress == 100) uni.hideLoading()
|
|
|
+ })
|
|
|
+}
|
|
|
+module.exports = {
|
|
|
+ viewMedias,
|
|
|
+ viewFlies,
|
|
|
+ formattedFiles,
|
|
|
+ viewImage,
|
|
|
+ wxSaveFile
|
|
|
+}
|