utils.js 1.9 KB

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