tools.js 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. import router from '@/sheep/router';
  2. export default {
  3. /**
  4. * 打电话
  5. * @param {String<Number>} phoneNumber - 数字字符串
  6. */
  7. callPhone(phoneNumber = '') {
  8. let num = phoneNumber.toString();
  9. uni.makePhoneCall({
  10. phoneNumber: num,
  11. fail(err) {
  12. console.log('makePhoneCall出错', err);
  13. },
  14. });
  15. },
  16. /**
  17. * 微信头像
  18. * @param {String} url -图片地址
  19. */
  20. checkMPUrl(url) {
  21. // #ifdef MP
  22. if (
  23. url.substring(0, 4) === 'http' &&
  24. url.substring(0, 5) !== 'https' &&
  25. url.substring(0, 12) !== 'http://store' &&
  26. url.substring(0, 10) !== 'http://tmp' &&
  27. url.substring(0, 10) !== 'http://usr'
  28. ) {
  29. url = 'https' + url.substring(4, url.length);
  30. }
  31. // #endif
  32. return url;
  33. },
  34. /**
  35. * getUuid 生成唯一id
  36. */
  37. getUuid(len = 32, firstU = true, radix = null) {
  38. const chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'.split('');
  39. const uuid = [];
  40. radix = radix || chars.length;
  41. if (len) {
  42. // 如果指定uuid长度,只是取随机的字符,0|x为位运算,能去掉x的小数位,返回整数位
  43. for (let i = 0; i < len; i++) uuid[i] = chars[0 | (Math.random() * radix)];
  44. } else {
  45. let r;
  46. // rfc4122标准要求返回的uuid中,某些位为固定的字符
  47. uuid[8] = uuid[13] = uuid[18] = uuid[23] = '-';
  48. uuid[14] = '4';
  49. for (let i = 0; i < 36; i++) {
  50. if (!uuid[i]) {
  51. r = 0 | (Math.random() * 16);
  52. uuid[i] = chars[i == 19 ? (r & 0x3) | 0x8 : r];
  53. }
  54. }
  55. }
  56. // 移除第一个字符,并用u替代,因为第一个字符为数值时,该guuid不能用作id或者class
  57. if (firstU) {
  58. uuid.shift();
  59. return `u${uuid.join('')}`;
  60. }
  61. return uuid.join('');
  62. },
  63. };