utils.js 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. //选择图片文件
  2. export const chooseImage = (count) => {
  3. return new Promise((resolve) => {
  4. uni.chooseImage({
  5. count: count,
  6. sizeType: ['original'],
  7. sourceType: ['album', 'camera'],
  8. success: ({tempFiles}) => {
  9. resolve(tempFiles);
  10. },
  11. fail: () => {
  12. resolve([]);
  13. }
  14. });
  15. })
  16. }
  17. //选择视频文件
  18. export const chooseVideo = () => {
  19. return new Promise((resolve) => {
  20. uni.chooseVideo({
  21. sourceType: ['camera', 'album'],
  22. success: (res) => {
  23. resolve(res);
  24. },
  25. fail: () => {
  26. resolve({});
  27. }
  28. });
  29. })
  30. }
  31. export const filterSize = (size) => {
  32. if (!size) return '';
  33. if (size < pow1024(1)) return size + ' B';
  34. if (size < pow1024(2)) return (size / pow1024(1)).toFixed(2) + ' KB';
  35. if (size < pow1024(3)) return (size / pow1024(2)).toFixed(2) + ' MB';
  36. if (size < pow1024(4)) return (size / pow1024(3)).toFixed(2) + ' GB';
  37. return (size / pow1024(4)).toFixed(2) + ' TB'
  38. }
  39. // 求次幂
  40. function pow1024(num) {
  41. return Math.pow(1024, num)
  42. }