123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287 |
- 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) {
- if (file.fileType == undefined) file = formattedFiles([file])[0]
- 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) {
- 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.errMsg,
- icon: "none"
- })
- }
- }
- })
- }
- function saveVideo(filePath) {
- console.log('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.errMsg,
- 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
- }
|