utils.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. //处理头像名称
  2. export const getUserName = (name) => {
  3. const leng = name.length;
  4. const pattern = new RegExp(/[0-9a-z]/i);
  5. if (pattern.test(name)) {
  6. return name.substr(leng - 3, 3)
  7. } else {
  8. return name.substr(leng - 2, 2)
  9. }
  10. }
  11. //选择图片文件
  12. export const chooseImage = (count) => {
  13. return new Promise((resolve) => {
  14. uni.chooseImage({
  15. count: count,
  16. sizeType: ['original'],
  17. sourceType: ['album', 'camera'],
  18. success: ({tempFiles}) => {
  19. resolve(tempFiles);
  20. },
  21. fail: () => {
  22. resolve([]);
  23. }
  24. });
  25. })
  26. }
  27. //选择视频文件
  28. export const chooseVideo = () => {
  29. return new Promise((resolve) => {
  30. uni.chooseVideo({
  31. sourceType: ['camera', 'album'],
  32. success: (res) => {
  33. resolve(res);
  34. },
  35. fail: () => {
  36. resolve({});
  37. }
  38. });
  39. })
  40. }
  41. export const filterSize = (size) => {
  42. if (!size) return '';
  43. if (size < pow1024(1)) return size + ' B';
  44. if (size < pow1024(2)) return (size / pow1024(1)).toFixed(2) + ' KB';
  45. if (size < pow1024(3)) return (size / pow1024(2)).toFixed(2) + ' MB';
  46. if (size < pow1024(4)) return (size / pow1024(3)).toFixed(2) + ' GB';
  47. return (size / pow1024(4)).toFixed(2) + ' TB'
  48. }
  49. // 求次幂
  50. function pow1024(num) {
  51. return Math.pow(1024, num)
  52. }