FormatTheAttachment.js 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. function fileList(list) {
  2. let suffixList = {
  3. image: ['png', 'jpg', 'jpeg', 'bmp', 'gif', 'webp', 'svg', 'tiff'],
  4. video: ['mp4', 'ogg', 'webm'],
  5. word: ['doc', 'docx'],
  6. excel: ['xls', 'xlsx'],
  7. ppt: ['ppt', 'pptx'],
  8. txt: ['txt', 'md', 'js', 'json'],
  9. pdf: ['pdf'],
  10. rar: ['7z', 'zip', 'rar', 'kz', 'ace', 'arj', 'bz2', 'cab', 'gz', 'iso', 'jar', 'lzh', 'tar', 'z'],
  11. folder: ['"folder"']
  12. },
  13. typeList = [];
  14. for (let key in suffixList) typeList.push(key);
  15. for (let i = 0; i < list.length; i++) {
  16. // 处理文件URL,确保是完整的URL
  17. list[i].url = getImageUrl(list[i].url);
  18. list[i].fileType = 'unknown';
  19. list[i].cover = `/static/image/file/unknown.png`
  20. const suffix = list[i].postfix.toLowerCase();
  21. if (suffix != "folder") {
  22. for (let key in suffixList) {
  23. if (suffixList[key].some(value => value == suffix)) {
  24. list[i].fileType = key;
  25. if (key == 'image') {
  26. list[i].cover = list[i].url;
  27. } else if (typeList.includes(key)) {
  28. list[i].cover = `/static/image/file/${key}.png`;
  29. }
  30. }
  31. }
  32. } else {
  33. list[i].fileType = "folder";
  34. list[i].cover = `/static/image/file/folder.png`
  35. }
  36. // 处理子文件的URL
  37. if (list[i].subfiles && list[i].subfiles.length > 0) {
  38. list[i].subfiles.forEach(subfile => {
  39. subfile.url = getImageUrl(subfile.url);
  40. });
  41. }
  42. }
  43. return list;
  44. };
  45. //得到缩略图或者压缩图 getType默认得到缩略图传true得到压缩图
  46. function getSpecifiedImage(obj, getType = false) {
  47. obj.url = getImageUrl(obj.url)
  48. try {
  49. let type = getType ? 'compressed' : 'thumbnail';
  50. let imgObj = obj.subfiles.find(v => v.type == type);
  51. return getImageUrl(imgObj.url || obj.url);
  52. } catch (error) {
  53. return getImageUrl(obj.url);
  54. }
  55. }
  56. // 判断图片是本地还是云存储
  57. function getImageUrl(url) {
  58. const _Http = getApp().globalData.http;
  59. if (!url) return '';
  60. //判断url中是否存在http,没有的话要拼接 this.baseUrl
  61. if (!/^https?:\/\//.test(url)) {
  62. url = _Http.baseUrl + url;
  63. }
  64. return url;
  65. }
  66. module.exports = {
  67. fileList,
  68. getSpecifiedImage,
  69. getImageUrl
  70. }