utils.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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) =>
  29. Object.assign(Object.assign({}, pickExclude(item, ['path'])), {
  30. type: 'image',
  31. url: item.path,
  32. thumb: item.path,
  33. })
  34. );
  35. }
  36. function formatVideo(res) {
  37. return [
  38. Object.assign(
  39. Object.assign(
  40. {},
  41. pickExclude(res, ['tempFilePath', 'thumbTempFilePath', 'errMsg'])
  42. ),
  43. { type: 'video', url: res.tempFilePath, thumb: res.thumbTempFilePath }
  44. ),
  45. ];
  46. }
  47. function formatMedia(res) {
  48. return res.tempFiles.map((item) =>
  49. Object.assign(
  50. Object.assign(
  51. {},
  52. pickExclude(item, ['fileType', 'thumbTempFilePath', 'tempFilePath'])
  53. ),
  54. {
  55. type: res.type,
  56. url: item.tempFilePath,
  57. thumb:
  58. res.type === 'video' ? item.thumbTempFilePath : item.tempFilePath,
  59. }
  60. )
  61. );
  62. }
  63. function formatFile(res) {
  64. return res.tempFiles.map((item) =>
  65. Object.assign(Object.assign({}, pickExclude(item, ['path'])), {
  66. url: item.path,
  67. })
  68. );
  69. }
  70. export function chooseFile({
  71. accept,
  72. multiple,
  73. capture,
  74. compressed,
  75. maxDuration,
  76. sizeType,
  77. camera,
  78. maxCount,
  79. }) {
  80. return new Promise((resolve, reject) => {
  81. switch (accept) {
  82. case 'image':
  83. wx.chooseImage({
  84. count: multiple ? Math.min(maxCount, 9) : 1,
  85. sourceType: capture,
  86. sizeType,
  87. success: (res) => resolve(formatImage(res)),
  88. fail: reject,
  89. });
  90. break;
  91. case 'media':
  92. wx.chooseMedia({
  93. count: multiple ? Math.min(maxCount, 9) : 1,
  94. sourceType: capture,
  95. maxDuration,
  96. sizeType,
  97. camera,
  98. success: (res) => resolve(formatMedia(res)),
  99. fail: reject,
  100. });
  101. break;
  102. case 'video':
  103. wx.chooseVideo({
  104. sourceType: capture,
  105. compressed,
  106. maxDuration,
  107. camera,
  108. success: (res) => resolve(formatVideo(res)),
  109. fail: reject,
  110. });
  111. break;
  112. default:
  113. wx.chooseMessageFile({
  114. count: multiple ? maxCount : 1,
  115. type: accept,
  116. success: (res) => resolve(formatFile(res)),
  117. fail: reject,
  118. });
  119. break;
  120. }
  121. });
  122. }