utils.js 3.0KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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, }) {
  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. sourceType: capture,
  57. maxDuration,
  58. sizeType,
  59. camera,
  60. success: (res) => resolve(formatMedia(res)),
  61. fail: reject,
  62. });
  63. break;
  64. case 'video':
  65. wx.chooseVideo({
  66. sourceType: capture,
  67. compressed,
  68. maxDuration,
  69. camera,
  70. success: (res) => resolve(formatVideo(res)),
  71. fail: reject,
  72. });
  73. break;
  74. default:
  75. wx.chooseMessageFile({
  76. count: multiple ? maxCount : 1,
  77. type: accept,
  78. success: (res) => resolve(formatFile(res)),
  79. fail: reject,
  80. });
  81. break;
  82. }
  83. });
  84. }