utils.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. "use strict";
  2. var __assign = (this && this.__assign) || function () {
  3. __assign = Object.assign || function(t) {
  4. for (var s, i = 1, n = arguments.length; i < n; i++) {
  5. s = arguments[i];
  6. for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
  7. t[p] = s[p];
  8. }
  9. return t;
  10. };
  11. return __assign.apply(this, arguments);
  12. };
  13. Object.defineProperty(exports, "__esModule", { value: true });
  14. exports.chooseFile = exports.isVideoFile = exports.isImageFile = void 0;
  15. var utils_1 = require("../common/utils");
  16. var validator_1 = require("../common/validator");
  17. function isImageFile(item) {
  18. if (item.isImage != null) {
  19. return item.isImage;
  20. }
  21. if (item.type) {
  22. return item.type === 'image';
  23. }
  24. if (item.url) {
  25. return (0, validator_1.isImageUrl)(item.url);
  26. }
  27. return false;
  28. }
  29. exports.isImageFile = isImageFile;
  30. function isVideoFile(item) {
  31. if (item.isVideo != null) {
  32. return item.isVideo;
  33. }
  34. if (item.type) {
  35. return item.type === 'video';
  36. }
  37. if (item.url) {
  38. return (0, validator_1.isVideoUrl)(item.url);
  39. }
  40. return false;
  41. }
  42. exports.isVideoFile = isVideoFile;
  43. function formatImage(res) {
  44. return res.tempFiles.map(function (item) { return (__assign(__assign({}, (0, utils_1.pickExclude)(item, ['path'])), { type: 'image', url: item.path, thumb: item.path })); });
  45. }
  46. function formatVideo(res) {
  47. return [
  48. __assign(__assign({}, (0, utils_1.pickExclude)(res, ['tempFilePath', 'thumbTempFilePath', 'errMsg'])), { type: 'video', url: res.tempFilePath, thumb: res.thumbTempFilePath }),
  49. ];
  50. }
  51. function formatMedia(res) {
  52. return res.tempFiles.map(function (item) { return (__assign(__assign({}, (0, utils_1.pickExclude)(item, ['fileType', 'thumbTempFilePath', 'tempFilePath'])), { type: res.type, url: item.tempFilePath, thumb: res.type === 'video' ? item.thumbTempFilePath : item.tempFilePath })); });
  53. }
  54. function formatFile(res) {
  55. return res.tempFiles.map(function (item) { return (__assign(__assign({}, (0, utils_1.pickExclude)(item, ['path'])), { url: item.path })); });
  56. }
  57. function chooseFile(_a) {
  58. var accept = _a.accept, multiple = _a.multiple, capture = _a.capture, compressed = _a.compressed, maxDuration = _a.maxDuration, sizeType = _a.sizeType, camera = _a.camera, maxCount = _a.maxCount;
  59. return new Promise(function (resolve, reject) {
  60. switch (accept) {
  61. case 'image':
  62. wx.chooseImage({
  63. count: multiple ? Math.min(maxCount, 9) : 1,
  64. sourceType: capture,
  65. sizeType: sizeType,
  66. success: function (res) { return resolve(formatImage(res)); },
  67. fail: reject,
  68. });
  69. break;
  70. case 'media':
  71. wx.chooseMedia({
  72. count: multiple ? Math.min(maxCount, 9) : 1,
  73. sourceType: capture,
  74. maxDuration: maxDuration,
  75. sizeType: sizeType,
  76. camera: camera,
  77. success: function (res) { return resolve(formatMedia(res)); },
  78. fail: reject,
  79. });
  80. break;
  81. case 'video':
  82. wx.chooseVideo({
  83. sourceType: capture,
  84. compressed: compressed,
  85. maxDuration: maxDuration,
  86. camera: camera,
  87. success: function (res) { return resolve(formatVideo(res)); },
  88. fail: reject,
  89. });
  90. break;
  91. default:
  92. wx.chooseMessageFile({
  93. count: multiple ? maxCount : 1,
  94. type: accept,
  95. success: function (res) { return resolve(formatFile(res)); },
  96. fail: reject,
  97. });
  98. break;
  99. }
  100. });
  101. }
  102. exports.chooseFile = chooseFile;