utils.js 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. import { pickExclude } from '../common/utils';
  2. import { isImageUrl, isVideoUrl } from '../common/validator';
  3. export function isImageFile(item) {
  4. if (item.isImage != null) {
  5. return item.isImage;
  6. }
  7. if (item.type) {
  8. return item.type === 'image';
  9. }
  10. if (item.url) {
  11. return isImageUrl(item.url);
  12. }
  13. return false;
  14. }
  15. export function isVideoFile(item) {
  16. if (item.isVideo != null) {
  17. return item.isVideo;
  18. }
  19. if (item.type) {
  20. return item.type === 'video';
  21. }
  22. if (item.url) {
  23. return isVideoUrl(item.url);
  24. }
  25. return false;
  26. }
  27. function formatImage(res) {
  28. return res.tempFiles.map((item) => (Object.assign(Object.assign({}, pickExclude(item, ['path'])), { type: 'image', url: item.tempFilePath, thumb: item.tempFilePath })));
  29. }
  30. function formatVideo(res) {
  31. return [
  32. Object.assign(Object.assign({}, pickExclude(res, ['tempFilePath', 'thumbTempFilePath', 'errMsg'])), { type: 'video', url: res.tempFilePath, thumb: res.thumbTempFilePath }),
  33. ];
  34. }
  35. function formatMedia(res) {
  36. return res.tempFiles.map((item) => (Object.assign(Object.assign({}, pickExclude(item, ['fileType', 'thumbTempFilePath', 'tempFilePath'])), { type: res.type, url: item.tempFilePath, thumb: res.type === 'video' ? item.thumbTempFilePath : item.tempFilePath })));
  37. }
  38. function formatFile(res) {
  39. return res.tempFiles.map((item) => (Object.assign(Object.assign({}, pickExclude(item, ['path'])), { url: item.path })));
  40. }
  41. export function chooseFile({ accept, multiple, capture, compressed, maxDuration, sizeType, camera, maxCount, mediaType, extension, }) {
  42. return new Promise((resolve, reject) => {
  43. switch (accept) {
  44. case 'image':
  45. wx.chooseMedia({
  46. count: multiple ? Math.min(maxCount, 9) : 1,
  47. mediaType: ['image'],
  48. sourceType: capture,
  49. maxDuration,
  50. sizeType,
  51. camera,
  52. success: (res) => resolve(formatImage(res)),
  53. fail: reject,
  54. });
  55. break;
  56. case 'media':
  57. wx.chooseMedia({
  58. count: multiple ? Math.min(maxCount, 9) : 1,
  59. mediaType,
  60. sourceType: capture,
  61. maxDuration,
  62. sizeType,
  63. camera,
  64. success: (res) => resolve(formatMedia(res)),
  65. fail: reject,
  66. });
  67. break;
  68. case 'video':
  69. wx.chooseVideo({
  70. sourceType: capture,
  71. compressed,
  72. maxDuration,
  73. camera,
  74. success: (res) => resolve(formatVideo(res)),
  75. fail: reject,
  76. });
  77. break;
  78. default:
  79. wx.chooseMessageFile(Object.assign(Object.assign({ count: multiple ? maxCount : 1, type: accept }, (extension ? { extension } : {})), { success: (res) => resolve(formatFile(res)), fail: reject }));
  80. break;
  81. }
  82. });
  83. }