tools.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. import {
  2. router
  3. } from '@/common/router'
  4. export default {
  5. /**
  6. * 跳转再封装,主要是为了兼容外链。
  7. * @param {String} path - 跳转路径
  8. * @param {isTabbar} isTabbar - 是否是底部导航
  9. */
  10. routerTo(path, isTabbar) {
  11. if (path) {
  12. // 是否跳转外部链接
  13. if (~path.indexOf('http') || ~path.indexOf('www')) {
  14. // #ifdef H5
  15. window.location = path;
  16. // #endif
  17. // #ifndef H5
  18. router.push({
  19. path: '/pages/public/webview',
  20. query: {
  21. 'webviewPath': path
  22. }
  23. })
  24. // #endif
  25. return false
  26. }
  27. if (isTabbar) {
  28. router.replaceAll(path)
  29. } else {
  30. path.includes('/pages/index') && !path.includes('/pages/index/view') ? router.replaceAll(path) : router
  31. .push(path)
  32. }
  33. } else {
  34. console.log(`%cerr:没有填写跳转路径`, 'color:green;background:yellow');
  35. }
  36. },
  37. /**
  38. * 图片处理-预览图片
  39. * @param {Array} urls - 图片列表
  40. * @param {Number} current - 首个预览下标
  41. */
  42. previewImage(urls = [], current = 0) {
  43. uni.previewImage({
  44. urls: urls,
  45. current: current,
  46. indicator: 'default',
  47. loop: true,
  48. fail(err) {
  49. console.log('previewImage出错', urls, err)
  50. },
  51. })
  52. },
  53. /**
  54. * 数据分组
  55. * @param {Array} oArr - 原数组列表
  56. * @param {Number} length - 单个数组长度
  57. * @return {Array} arr - 分组后的新数组
  58. */
  59. splitData(oArr = [], length = 1) {
  60. let arr = [];
  61. let minArr = [];
  62. oArr.forEach(c => {
  63. if (minArr.length === length) {
  64. minArr = [];
  65. }
  66. if (minArr.length === 0) {
  67. arr.push(minArr);
  68. }
  69. minArr.push(c);
  70. });
  71. return arr;
  72. },
  73. /**
  74. * 剩余时间格式化
  75. * @param {Number} t - 剩余多少秒
  76. * @return {Object} format - 格式后的天时分秒对象
  77. */
  78. format(t) {
  79. let format = {
  80. d: '00',
  81. h: '00',
  82. m: '00',
  83. s: '00'
  84. };
  85. if (t > 0) {
  86. let d = Math.floor(t / 86400);
  87. let h = Math.floor((t / 3600) % 24);
  88. let m = Math.floor((t / 60) % 60);
  89. let s = Math.floor(t % 60);
  90. format.d = d < 10 ? '0' + d : d;
  91. format.h = h < 10 ? '0' + h : h;
  92. format.m = m < 10 ? '0' + m : m;
  93. format.s = s < 10 ? '0' + s : s;
  94. }
  95. return format;
  96. },
  97. /**
  98. * 打电话
  99. * @param {String<Number>} phoneNumber - 数字字符串
  100. */
  101. callPhone(phoneNumber = '') {
  102. let num = phoneNumber.toString()
  103. uni.makePhoneCall({
  104. phoneNumber: num,
  105. fail(err) {
  106. console.log('makePhoneCall出错', err)
  107. },
  108. });
  109. },
  110. /**
  111. * 微信头像
  112. * @param {String} url -图片地址
  113. */
  114. checkMPUrl(url) {
  115. // #ifdef MP
  116. if (
  117. url.substring(0, 4) === 'http' &&
  118. url.substring(0, 5) !== 'https' &&
  119. url.substring(0, 12) !== 'http://store' &&
  120. url.substring(0, 10) !== 'http://tmp' &&
  121. url.substring(0, 10) !== 'http://usr'
  122. ) {
  123. url = 'https' + url.substring(4, url.length);
  124. }
  125. // #endif
  126. return url;
  127. },
  128. }