settleFiles.js 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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. const fs = uni.getFileSystemManager(),
  95. basePath = String(`${uni.env.USER_DATA_PATH}/${file.postfix}`);
  96. fs.access({
  97. path: basePath,
  98. success(res) {
  99. fs.getSavedFileList({
  100. success: res => {
  101. console.log("临时文件列表", res)
  102. }
  103. })
  104. },
  105. fail(res) {
  106. fs.mkdir({
  107. dirPath: basePath,
  108. recursive: true,
  109. success(res) {
  110. console.log("创建目录", res)
  111. },
  112. fail(res) {
  113. console.error("创建目录", res)
  114. }
  115. })
  116. }
  117. })
  118. const downloadTask = uni.downloadFile({
  119. url: file.url,
  120. filePath: basePath + '/' + file.document,
  121. timeout: 6000000,
  122. success(res) {
  123. console.log("下载文件", 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,
  184. icon: "none"
  185. })
  186. }
  187. }
  188. })
  189. }
  190. function saveVideo(filePath) {
  191. uni.saveVideoToPhotosAlbum({
  192. filePath,
  193. success(res) {
  194. console.log("保存商品", res)
  195. uni.showModal({
  196. title: '提示',
  197. content: '视频已保存到系统相册',
  198. showCancel: false
  199. })
  200. uni.hideLoading()
  201. clearFile(filePath)
  202. },
  203. fail(err) {
  204. console.log("保存失败", err)
  205. if (err.errno == 103) {
  206. uni.showModal({
  207. title: '提示',
  208. content: '未获取保存相册权限,无法保存!',
  209. confirmText: "前去授权",
  210. success({
  211. confirm
  212. }) {
  213. if (confirm) uni.openSetting({
  214. success(res) {
  215. console.log(res.authSetting["scope.writePhotosAlbum"])
  216. if (res.authSetting["scope.writePhotosAlbum"]) {
  217. saveVideo(filePath)
  218. } else {
  219. uni.showToast({
  220. title: '未获得授权',
  221. icon: "none"
  222. })
  223. }
  224. }
  225. })
  226. }
  227. })
  228. } else {
  229. clearFile(filePath)
  230. uni.showToast({
  231. title: err,
  232. icon: "none"
  233. })
  234. }
  235. }
  236. })
  237. }
  238. function clearFile(filePath) {
  239. let fs = uni.getFileSystemManager()
  240. fs.unlink({
  241. filePath,
  242. success(res) {
  243. console.log("文件删除", res)
  244. },
  245. fail(res) {
  246. console.log("删除失败", res)
  247. }
  248. })
  249. }
  250. },
  251. fail(err) {
  252. console.log("下载失败", err)
  253. uni.showToast({
  254. title: `下载失败:${err.errMsg}`,
  255. icon: "none"
  256. })
  257. if (err.errMsg == 'downloadFile:fail exceed max file size') uni.showToast({
  258. title: '视频体积超大,无法保存!请尝试打开视频长按保存下载',
  259. icon: "none"
  260. })
  261. }
  262. })
  263. downloadTask.onProgressUpdate((res) => {
  264. uni.showLoading({
  265. title: res.progress + `%`,
  266. mask: true
  267. })
  268. if (res.progress == 100) uni.hideLoading()
  269. })
  270. }
  271. module.exports = {
  272. viewMedias,
  273. viewFlies,
  274. formattedFiles,
  275. viewImage,
  276. wxSaveFile
  277. }