index.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. import $store from '@/common/store';
  2. import { showAuthModal, showLoginPage } from '@/common/hooks/useModal';
  3. import { isNumber, isString, isEmpty, startsWith, isObject, isNil, clone } from 'lodash-es';
  4. import throttle from '@/common/helper/throttle';
  5. const _go = (
  6. path,
  7. params = {},
  8. options = {
  9. redirect: false,
  10. },
  11. ) => {
  12. let page = ''; // 跳转页面
  13. let query = ''; // 页面参数
  14. let url = ''; // 跳转页面完整路径
  15. if (isString(path)) {
  16. // 判断跳转类型是 path | 还是http
  17. if (startsWith(path, 'http')) {
  18. // #ifdef H5
  19. window.location = path;
  20. return;
  21. // #endif
  22. // #ifndef H5
  23. page = `/pages/public/webview`;
  24. query = `url=${encodeURIComponent(path)}`;
  25. // #endif
  26. } else {
  27. [page, query] = path.split('?');
  28. }
  29. if (!isEmpty(params)) {
  30. let query2 = paramsToQuery(params);
  31. if (isEmpty(query)) {
  32. query = query2;
  33. } else {
  34. query += '&' + query2;
  35. }
  36. }
  37. }
  38. if (isObject(path)) {
  39. page = path.url;
  40. if (!isNil(path.params)) {
  41. query = paramsToQuery(path.params);
  42. }
  43. }
  44. const nextRoute = ROUTES_MAP[page];
  45. // 未找到指定跳转页面
  46. // mark: 跳转404页
  47. if (!nextRoute) {
  48. console.log(`%c跳转路径参数错误<${page || 'EMPTY'}>`, 'color:red;background:yellow');
  49. return;
  50. }
  51. // 页面登录拦截
  52. if (nextRoute.meta?.auth && !$store('user').isLogin) {
  53. //showAuthModal();
  54. showLoginPage()
  55. return;
  56. }
  57. url = page;
  58. if (!isEmpty(query)) {
  59. url += `?${query}`;
  60. }
  61. // 跳转底部导航
  62. if (TABBAR.includes(page)) {
  63. uni.switchTab({
  64. url,
  65. });
  66. return;
  67. }
  68. // 使用redirect跳转
  69. if (options.redirect) {
  70. uni.redirectTo({
  71. url,
  72. });
  73. return;
  74. }
  75. uni.navigateTo({
  76. url,
  77. });
  78. };
  79. // 限流 防止重复点击跳转
  80. function go(...args) {
  81. throttle(() => {
  82. _go(...args);
  83. });
  84. }
  85. function paramsToQuery(params) {
  86. if (isEmpty(params)) {
  87. return '';
  88. }
  89. // return new URLSearchParams(Object.entries(params)).toString();
  90. let query = [];
  91. for (let key in params) {
  92. query.push(key + '=' + params[key]);
  93. }
  94. return query.join('&');
  95. }
  96. function back() {
  97. // #ifdef H5
  98. history.back();
  99. // #endif
  100. // #ifndef H5
  101. uni.navigateBack();
  102. // #endif
  103. }
  104. function redirect(path, params = {}) {
  105. go(path, params, {
  106. redirect: true,
  107. });
  108. }
  109. // 检测是否有浏览器历史
  110. function hasHistory() {
  111. // #ifndef H5
  112. const pages = getCurrentPages();
  113. if (pages.length > 1) {
  114. return true;
  115. }
  116. return false;
  117. // #endif
  118. // #ifdef H5
  119. return !!history.state.back;
  120. // #endif
  121. }
  122. function getCurrentRoute(field = '') {
  123. let currentPage = getCurrentPage();
  124. // #ifdef MP
  125. currentPage.$page['route'] = currentPage.route;
  126. currentPage.$page['options'] = currentPage.options;
  127. // #endif
  128. if (field !== '') {
  129. return currentPage.$page[field];
  130. } else {
  131. return currentPage.$page;
  132. }
  133. }
  134. function getCurrentPage() {
  135. let pages = getCurrentPages();
  136. return pages[pages.length - 1];
  137. }
  138. function error(errCode, errMsg = '') {
  139. redirect('/pages/public/error', {
  140. errCode,
  141. errMsg,
  142. });
  143. }
  144. export default {
  145. go,
  146. back,
  147. hasHistory,
  148. redirect,
  149. getCurrentPage,
  150. getCurrentRoute,
  151. error,
  152. };