previewFile.js 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. wx.previewMedia({
  44. current: index,
  45. sources: files,
  46. })
  47. // #endif
  48. }
  49. /* 预览文档 */
  50. function viewFlies(item) {
  51. uni.showLoading({
  52. title: '加载中...',
  53. })
  54. uni.downloadFile({
  55. url: item.url,
  56. complete({
  57. statusCode,
  58. tempFilePath
  59. }) {
  60. if (statusCode != 200) return;
  61. uni.openDocument({
  62. filePath: tempFilePath,
  63. fileType: item.postfix,
  64. showMenu: true,
  65. complete({
  66. errMsg
  67. }) {
  68. uni.hideLoading();
  69. if (errMsg != "openDocument:ok") uni.showToast({
  70. title: '打开失败',
  71. icon: "none"
  72. })
  73. }
  74. })
  75. }
  76. })
  77. }
  78. module.exports = {
  79. viewMedias,
  80. viewFlies,
  81. formattedFiles
  82. }