tool.js 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. import {
  2. getUrlParams
  3. } from './utils.js'
  4. /**
  5. * 全局uni api二次封装工具
  6. */
  7. //统一消息提醒
  8. export const msg = (title, icon = 'none', duration = 1500, mask = false) => {
  9. //统一提示方便全局修改
  10. if (Boolean(title) === false) {
  11. return;
  12. }
  13. uni.showToast({
  14. title,
  15. duration,
  16. mask,
  17. icon
  18. });
  19. }
  20. //showLoading状态
  21. export const loading = (title) => {
  22. uni.showLoading({
  23. title: title,
  24. mask: true
  25. });
  26. }
  27. export const hide = () => {
  28. uni.hideLoading();
  29. }
  30. //确认模态框封装
  31. export async function showModal(content = "", color = "#108DFC", confirmText = "确定") {
  32. let res = await uni.showModal({
  33. title: '提示',
  34. content: content,
  35. confirmColor: color,
  36. confirmText: confirmText
  37. })
  38. if (!res[1].confirm) {
  39. return false;
  40. }
  41. return true;
  42. }
  43. // 函数用于获取上一页页面栈的实例
  44. export const prePage = (index = 2) => {
  45. return getCurrentPageCustom(index);
  46. }
  47. // 函数用于获取当前页面栈的实例
  48. export const currentPage = (index = 1) => {
  49. return getCurrentPageCustom(index);
  50. }
  51. // 获取自定义页面栈
  52. function getCurrentPageCustom(index) {
  53. let pages = getCurrentPages(); // 函数用于获取当前页面栈的实例
  54. let prePage = pages[pages.length - index];
  55. // #ifdef H5
  56. return prePage;
  57. // #endif
  58. return prePage.$vm;
  59. }
  60. /**
  61. * 页面滚动到指定位置
  62. */
  63. export const pageScrollTo = (id, duration = 300) => {
  64. // 获取某元素距离顶部的距离
  65. // #ifdef H5
  66. let res = document.querySelector(id)
  67. let scrollTop = res.offsetTop;
  68. uni.pageScrollTo({
  69. scrollTop: scrollTop,
  70. });
  71. // #endif
  72. // #ifdef MP-WEIXIN
  73. let query = uni.createSelectorQuery();
  74. query.select(id).boundingClientRect(res => {
  75. let scrollTop = res.top;
  76. console.log(res);
  77. // 开始滚动
  78. uni.pageScrollTo({
  79. scrollTop: scrollTop,
  80. duration: duration
  81. });
  82. }).exec()
  83. // #endif
  84. }