utils.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', {
  3. value: true
  4. });
  5. exports.isPromise = exports.isObject = exports.isFunction = exports.chooseFile = exports.isVideo = exports.isImageFile = void 0;
  6. var IMAGE_REGEXP = /\.(jpeg|jpg|gif|png|svg|webp|jfif|bmp|dpg)/i;
  7. function isImageUrl(url) {
  8. return IMAGE_REGEXP.test(url);
  9. }
  10. function isImageFile(item) {
  11. if (item.type) {
  12. return item.type.indexOf('image') === 0;
  13. }
  14. if (item.path) {
  15. return isImageUrl(item.path);
  16. }
  17. if (item.url) {
  18. return isImageUrl(item.url);
  19. }
  20. return false;
  21. }
  22. exports.isImageFile = isImageFile;
  23. function isVideo(res, accept) {
  24. return accept === 'video';
  25. }
  26. exports.isVideo = isVideo;
  27. function chooseFile(_a) {
  28. var accept = _a.accept;
  29. var multiple = _a.multiple;
  30. var capture = _a.capture;
  31. var compressed = _a.compressed;
  32. var maxDuration = _a.maxDuration;
  33. var sizeType = _a.sizeType;
  34. var camera = _a.camera;
  35. var maxCount = _a.maxCount;
  36. switch (accept) {
  37. case 'image':
  38. return new Promise(function (resolve, reject) {
  39. uni.chooseImage({
  40. count: multiple ? Math.min(maxCount, 9) : 1,
  41. sourceType: capture,
  42. sizeType: sizeType,
  43. success: resolve,
  44. fail: reject
  45. });
  46. });
  47. case 'media':
  48. return new Promise(function (resolve, reject) {
  49. uni.chooseMedia({
  50. count: multiple ? Math.min(maxCount, 9) : 1,
  51. sourceType: capture,
  52. maxDuration: maxDuration,
  53. sizeType: sizeType,
  54. camera: camera,
  55. success: resolve,
  56. fail: reject
  57. });
  58. });
  59. case 'video':
  60. return new Promise(function (resolve, reject) {
  61. uni.chooseVideo({
  62. sourceType: capture,
  63. compressed: compressed,
  64. maxDuration: maxDuration,
  65. camera: camera,
  66. success: resolve,
  67. fail: reject
  68. });
  69. });
  70. default:
  71. return new Promise(function (resolve, reject) {
  72. uni.chooseMessageFile({
  73. count: multiple ? maxCount : 1,
  74. type: 'file',
  75. success: resolve,
  76. fail: reject
  77. });
  78. });
  79. }
  80. }
  81. exports.chooseFile = chooseFile;
  82. function isFunction(val) {
  83. return typeof val === 'function';
  84. }
  85. exports.isFunction = isFunction;
  86. function isObject(val) {
  87. return val !== null && typeof val === 'object';
  88. }
  89. exports.isObject = isObject;
  90. function isPromise(val) {
  91. return isObject(val) && isFunction(val.then) && isFunction(val.catch);
  92. }
  93. exports.isPromise = isPromise;