utils.js 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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.path, thumb: item.path })));
  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.chooseImage({
  46. count: multiple ? Math.min(maxCount, 9) : 1,
  47. sourceType: capture,
  48. sizeType,
  49. success: (res) => resolve(formatImage(res)),
  50. fail: reject,
  51. });
  52. break;
  53. case 'media':
  54. wx.chooseMedia({
  55. count: multiple ? Math.min(maxCount, 9) : 1,
  56. mediaType,
  57. sourceType: capture,
  58. maxDuration,
  59. sizeType,
  60. camera,
  61. success: (res) => resolve(formatMedia(res)),
  62. fail: reject,
  63. });
  64. break;
  65. case 'video':
  66. wx.chooseVideo({
  67. sourceType: capture,
  68. compressed,
  69. maxDuration,
  70. camera,
  71. success: (res) => resolve(formatVideo(res)),
  72. fail: reject,
  73. });
  74. break;
  75. default:
  76. wx.chooseMessageFile(Object.assign(Object.assign({ count: multiple ? maxCount : 1, type: accept }, (extension ? { extension } : {})), { success: (res) => resolve(formatFile(res)), fail: reject }));
  77. break;
  78. }
  79. });
  80. }