index.js 3.5 KB

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