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