utils.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. 'use strict';
  2. var __assign =
  3. (this && this.__assign) ||
  4. function () {
  5. __assign =
  6. Object.assign ||
  7. function (t) {
  8. for (var s, i = 1, n = arguments.length; i < n; i++) {
  9. s = arguments[i];
  10. for (var p in s)
  11. if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
  12. }
  13. return t;
  14. };
  15. return __assign.apply(this, arguments);
  16. };
  17. Object.defineProperty(exports, '__esModule', { value: true });
  18. exports.chooseFile = exports.isVideoFile = exports.isImageFile = void 0;
  19. var utils_1 = require('../common/utils');
  20. var validator_1 = require('../common/validator');
  21. function isImageFile(item) {
  22. if (item.isImage != null) {
  23. return item.isImage;
  24. }
  25. if (item.type) {
  26. return item.type === 'image';
  27. }
  28. if (item.url) {
  29. return validator_1.isImageUrl(item.url);
  30. }
  31. return false;
  32. }
  33. exports.isImageFile = isImageFile;
  34. function isVideoFile(item) {
  35. if (item.isVideo != null) {
  36. return item.isVideo;
  37. }
  38. if (item.type) {
  39. return item.type === 'video';
  40. }
  41. if (item.url) {
  42. return validator_1.isVideoUrl(item.url);
  43. }
  44. return false;
  45. }
  46. exports.isVideoFile = isVideoFile;
  47. function formatImage(res) {
  48. return res.tempFiles.map(function (item) {
  49. return __assign(__assign({}, utils_1.pickExclude(item, ['path'])), {
  50. type: 'image',
  51. url: item.path,
  52. thumb: item.path,
  53. });
  54. });
  55. }
  56. function formatVideo(res) {
  57. return [
  58. __assign(
  59. __assign(
  60. {},
  61. utils_1.pickExclude(res, [
  62. 'tempFilePath',
  63. 'thumbTempFilePath',
  64. 'errMsg',
  65. ])
  66. ),
  67. { type: 'video', url: res.tempFilePath, thumb: res.thumbTempFilePath }
  68. ),
  69. ];
  70. }
  71. function formatMedia(res) {
  72. return res.tempFiles.map(function (item) {
  73. return __assign(
  74. __assign(
  75. {},
  76. utils_1.pickExclude(item, [
  77. 'fileType',
  78. 'thumbTempFilePath',
  79. 'tempFilePath',
  80. ])
  81. ),
  82. {
  83. type: res.type,
  84. url: item.tempFilePath,
  85. thumb:
  86. res.type === 'video' ? item.thumbTempFilePath : item.tempFilePath,
  87. }
  88. );
  89. });
  90. }
  91. function formatFile(res) {
  92. return res.tempFiles.map(function (item) {
  93. return __assign(__assign({}, utils_1.pickExclude(item, ['path'])), {
  94. url: item.path,
  95. });
  96. });
  97. }
  98. function chooseFile(_a) {
  99. var accept = _a.accept,
  100. multiple = _a.multiple,
  101. capture = _a.capture,
  102. compressed = _a.compressed,
  103. maxDuration = _a.maxDuration,
  104. sizeType = _a.sizeType,
  105. camera = _a.camera,
  106. maxCount = _a.maxCount;
  107. return new Promise(function (resolve, reject) {
  108. switch (accept) {
  109. case 'image':
  110. wx.chooseImage({
  111. count: multiple ? Math.min(maxCount, 9) : 1,
  112. sourceType: capture,
  113. sizeType: sizeType,
  114. success: function (res) {
  115. return resolve(formatImage(res));
  116. },
  117. fail: reject,
  118. });
  119. break;
  120. case 'media':
  121. wx.chooseMedia({
  122. count: multiple ? Math.min(maxCount, 9) : 1,
  123. sourceType: capture,
  124. maxDuration: maxDuration,
  125. sizeType: sizeType,
  126. camera: camera,
  127. success: function (res) {
  128. return resolve(formatMedia(res));
  129. },
  130. fail: reject,
  131. });
  132. break;
  133. case 'video':
  134. wx.chooseVideo({
  135. sourceType: capture,
  136. compressed: compressed,
  137. maxDuration: maxDuration,
  138. camera: camera,
  139. success: function (res) {
  140. return resolve(formatVideo(res));
  141. },
  142. fail: reject,
  143. });
  144. break;
  145. default:
  146. wx.chooseMessageFile({
  147. count: multiple ? maxCount : 1,
  148. type: accept,
  149. success: function (res) {
  150. return resolve(formatFile(res));
  151. },
  152. fail: reject,
  153. });
  154. break;
  155. }
  156. });
  157. }
  158. exports.chooseFile = chooseFile;