utils.js 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. import { pickExclude, isPC, isWxWork } 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 || item.path, thumb: item.tempFilePath || 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: item.fileType, url: item.tempFilePath, thumb: item.fileType === '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. if (isPC || isWxWork) {
  46. wx.chooseImage({
  47. count: multiple ? Math.min(maxCount, 9) : 1,
  48. sourceType: capture,
  49. sizeType,
  50. success: (res) => resolve(formatImage(res)),
  51. fail: reject,
  52. });
  53. }
  54. else {
  55. wx.chooseMedia({
  56. count: multiple ? Math.min(maxCount, 9) : 1,
  57. mediaType: ['image'],
  58. sourceType: capture,
  59. maxDuration,
  60. sizeType,
  61. camera,
  62. success: (res) => resolve(formatImage(res)),
  63. fail: reject,
  64. });
  65. }
  66. break;
  67. case 'media':
  68. wx.chooseMedia({
  69. count: multiple ? Math.min(maxCount, 9) : 1,
  70. mediaType,
  71. sourceType: capture,
  72. maxDuration,
  73. sizeType,
  74. camera,
  75. success: (res) => resolve(formatMedia(res)),
  76. fail: reject,
  77. });
  78. break;
  79. case 'video':
  80. wx.chooseVideo({
  81. sourceType: capture,
  82. compressed,
  83. maxDuration,
  84. camera,
  85. success: (res) => resolve(formatVideo(res)),
  86. fail: reject,
  87. });
  88. break;
  89. default:
  90. wx.chooseMessageFile(Object.assign(Object.assign({ count: multiple ? maxCount : 1, type: accept }, (extension ? { extension } : {})), { success: (res) => resolve(formatFile(res)), fail: reject }));
  91. break;
  92. }
  93. });
  94. }