index.js 3.5 KB

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