settleFiles.js 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. function formattedFiles(list) {
  2. if (list.length == 0) return [];
  3. let suffixList = {
  4. image: ['png', 'jpg', 'jpeg', 'bmp', 'gif', 'webp', 'svg', 'tiff'],
  5. video: ['mp4', 'ogg', 'webm'],
  6. word: ['doc', 'docx'],
  7. excel: ['xls', 'xlsx'],
  8. ppt: ['ppt', 'pptx'],
  9. txt: ['txt', 'md', 'js', 'json'],
  10. pdf: ['pdf'],
  11. rar: ['7z', 'zip', 'rar', 'kz', 'ace', 'arj', 'bz2', 'cab', 'gz', 'iso', 'jar', 'lzh', 'tar', 'z'],
  12. folder: ['folder']
  13. },
  14. typeList = [];
  15. for (let key in suffixList) typeList.push(key);
  16. for (let i = 0; i < list.length; i++) {
  17. list[i].fileType = 'unknown';
  18. const suffix = list[i].postfix.toLowerCase();
  19. if (suffix != "folder") {
  20. for (var key in suffixList) {
  21. if (suffixList[key].some(value => value == suffix)) list[i].fileType = key;
  22. }
  23. } else {
  24. list[i].fileType = "folder";
  25. }
  26. }
  27. return list;
  28. }
  29. /* 预览媒体 */
  30. function viewMedias(files, index, type) {
  31. // #ifndef MP
  32. if (type == 'image') {
  33. uni.previewImage({
  34. current: index,
  35. urls: files,
  36. loop: true,
  37. })
  38. } else {
  39. window.open(files[index].url)
  40. }
  41. // #endif
  42. // #ifdef MP-WEIXIN
  43. uni.previewMedia({
  44. current: index,
  45. sources: files,
  46. })
  47. // #endif
  48. }
  49. /* 预览图片 */
  50. function viewImage(url) {
  51. // #ifndef MP
  52. uni.previewImage({
  53. current: 0,
  54. urls: [url],
  55. loop: true,
  56. })
  57. // #endif
  58. // #ifdef MP-WEIXIN
  59. uni.previewImage({
  60. urls: [url]
  61. })
  62. // #endif
  63. }
  64. /* 预览文档 */
  65. function viewFlies(item) {
  66. uni.showLoading({
  67. title: '加载中...',
  68. })
  69. uni.downloadFile({
  70. url: item.url,
  71. complete({
  72. statusCode,
  73. tempFilePath
  74. }) {
  75. if (statusCode != 200) return;
  76. uni.openDocument({
  77. filePath: tempFilePath,
  78. fileType: item.postfix,
  79. showMenu: true,
  80. complete({
  81. errMsg
  82. }) {
  83. uni.hideLoading();
  84. if (errMsg != "openDocument:ok") uni.showToast({
  85. title: '打开失败',
  86. icon: "none"
  87. })
  88. }
  89. })
  90. }
  91. })
  92. }
  93. function wxSaveFile(file) {
  94. if (file.fileType == undefined) file = formattedFiles([file])[0]
  95. const fs = uni.getFileSystemManager(),
  96. basePath = String(`${uni.env.USER_DATA_PATH}/${file.postfix}`);
  97. fs.access({
  98. path: basePath,
  99. success(res) {
  100. fs.getSavedFileList({
  101. success: res => {
  102. console.log("临时文件列表", res)
  103. }
  104. })
  105. },
  106. fail(res) {
  107. fs.mkdir({
  108. dirPath: basePath,
  109. recursive: true,
  110. success(res) {
  111. console.log("创建目录", res)
  112. },
  113. fail(res) {
  114. console.error("创建目录", res)
  115. }
  116. })
  117. }
  118. })
  119. const downloadTask = uni.downloadFile({
  120. url: file.url,
  121. filePath: basePath + '/' + file.document,
  122. timeout: 6000000,
  123. success(res) {
  124. if (file.fileType == "video") {
  125. saveVideo(res.filePath)
  126. } else {
  127. saveImage(res.filePath)
  128. }
  129. function shareVideo() {
  130. uni.shareVideoMessage({
  131. videoPath: res.tempFilePath,
  132. complete(res) {
  133. console.log("转发", res)
  134. clearFile(filePath)
  135. /* uni.showToast({
  136. title: `转发失败:${err.errMsg}`,
  137. icon: "none"
  138. }) */
  139. }
  140. })
  141. }
  142. function saveImage(filePath) {
  143. uni.saveImageToPhotosAlbum({
  144. filePath,
  145. success(res) {
  146. console.log("保存图片", res)
  147. uni.showModal({
  148. title: '提示',
  149. content: '文件已保存到系统相册',
  150. showCancel: false
  151. })
  152. uni.hideLoading()
  153. clearFile(filePath)
  154. },
  155. fail(err) {
  156. console.log("保存失败", err)
  157. if (err.errno == 103) {
  158. uni.showModal({
  159. title: '提示',
  160. content: '未获取保存相册权限,无法保存!',
  161. confirmText: "前去授权",
  162. success({
  163. confirm
  164. }) {
  165. if (confirm) uni.openSetting({
  166. success(res) {
  167. console.log(res.authSetting["scope.writePhotosAlbum"])
  168. if (res.authSetting["scope.writePhotosAlbum"]) {
  169. saveVideo(filePath)
  170. } else {
  171. uni.showToast({
  172. title: '未获得授权',
  173. icon: "none"
  174. })
  175. }
  176. }
  177. })
  178. }
  179. })
  180. } else {
  181. clearFile(filePath)
  182. uni.showToast({
  183. title: err.errMsg,
  184. icon: "none"
  185. })
  186. }
  187. }
  188. })
  189. }
  190. function saveVideo(filePath) {
  191. console.log('saveVideo', filePath)
  192. uni.saveVideoToPhotosAlbum({
  193. filePath,
  194. success(res) {
  195. console.log("保存视频", res)
  196. uni.showModal({
  197. title: '提示',
  198. content: '视频已保存到系统相册',
  199. showCancel: false
  200. })
  201. uni.hideLoading()
  202. clearFile(filePath)
  203. },
  204. fail(err) {
  205. console.log("保存失败", err)
  206. if (err.errno == 103) {
  207. uni.showModal({
  208. title: '提示',
  209. content: '未获取保存相册权限,无法保存!',
  210. confirmText: "前去授权",
  211. success({
  212. confirm
  213. }) {
  214. if (confirm) uni.openSetting({
  215. success(res) {
  216. console.log(res.authSetting["scope.writePhotosAlbum"])
  217. if (res.authSetting["scope.writePhotosAlbum"]) {
  218. saveVideo(filePath)
  219. } else {
  220. uni.showToast({
  221. title: '未获得授权',
  222. icon: "none"
  223. })
  224. }
  225. }
  226. })
  227. }
  228. })
  229. } else {
  230. clearFile(filePath)
  231. uni.showToast({
  232. title: err.errMsg,
  233. icon: "none"
  234. })
  235. }
  236. }
  237. })
  238. }
  239. function clearFile(filePath) {
  240. let fs = uni.getFileSystemManager()
  241. fs.unlink({
  242. filePath,
  243. success(res) {
  244. console.log("文件删除", res)
  245. },
  246. fail(res) {
  247. console.log("删除失败", res)
  248. }
  249. })
  250. }
  251. },
  252. fail(err) {
  253. console.log("下载失败", err)
  254. uni.showToast({
  255. title: `下载失败:${err.errMsg}`,
  256. icon: "none"
  257. })
  258. if (err.errMsg == 'downloadFile:fail exceed max file size') uni.showToast({
  259. title: '视频体积超大,无法保存!请尝试打开视频长按保存下载',
  260. icon: "none"
  261. })
  262. }
  263. })
  264. downloadTask.onProgressUpdate((res) => {
  265. uni.showLoading({
  266. title: res.progress + `%`,
  267. mask: true
  268. })
  269. if (res.progress == 100) uni.hideLoading()
  270. })
  271. }
  272. module.exports = {
  273. viewMedias,
  274. viewFlies,
  275. formattedFiles,
  276. viewImage,
  277. wxSaveFile
  278. }