index.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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. 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. // wx.switchTab: url 不支持 queryString
  66. // 设置全局变量
  67. const params = queryToParams(query);
  68. $store('app').setParamsForTabbar(params);
  69. // 请记得在业务代码里使用完后,清理掉全局状态,避免影响下次跳转
  70. uni.switchTab({
  71. url: page,
  72. });
  73. return;
  74. }
  75. // 使用redirect跳转
  76. if (options.redirect) {
  77. uni.redirectTo({
  78. url,
  79. });
  80. return;
  81. }
  82. uni.navigateTo({
  83. url,
  84. });
  85. };
  86. // 限流 防止重复点击跳转
  87. function go(...args) {
  88. throttle(() => {
  89. _go(...args);
  90. });
  91. }
  92. function paramsToQuery(params) {
  93. if (isEmpty(params)) {
  94. return '';
  95. }
  96. // return new URLSearchParams(Object.entries(params)).toString();
  97. let query = [];
  98. for (let key in params) {
  99. query.push(key + '=' + params[key]);
  100. }
  101. return query.join('&');
  102. }
  103. function queryToParams(query) {
  104. if (isEmpty(query)) {
  105. return {};
  106. }
  107. let params = {};
  108. let pairs = query.split('&');
  109. for (let i = 0; i < pairs.length; i++) {
  110. let pair = pairs[i].split('=');
  111. params[decodeURIComponent(pair[0])] = decodeURIComponent(pair[1] || '');
  112. }
  113. return params;
  114. }
  115. function back() {
  116. // #ifdef H5
  117. history.back();
  118. // #endif
  119. // #ifndef H5
  120. uni.navigateBack();
  121. // #endif
  122. }
  123. function redirect(path, params = {}) {
  124. go(path, params, {
  125. redirect: true,
  126. });
  127. }
  128. // 检测是否有浏览器历史
  129. function hasHistory() {
  130. // #ifndef H5
  131. const pages = getCurrentPages();
  132. if (pages.length > 1) {
  133. return true;
  134. }
  135. return false;
  136. // #endif
  137. // #ifdef H5
  138. return !!history.state.back;
  139. // #endif
  140. }
  141. function getCurrentRoute(field = '') {
  142. let currentPage = getCurrentPage();
  143. // #ifdef MP
  144. currentPage.$page['route'] = currentPage.route;
  145. currentPage.$page['options'] = currentPage.options;
  146. // #endif
  147. if (field !== '') {
  148. return currentPage.$page[field];
  149. } else {
  150. return currentPage.$page;
  151. }
  152. }
  153. function getCurrentPage() {
  154. let pages = getCurrentPages();
  155. return pages[pages.length - 1];
  156. }
  157. function handleAction(path) {
  158. const action = path.split(':');
  159. switch (action[1]) {
  160. case 'showShareModal':
  161. showShareModal();
  162. break;
  163. }
  164. }
  165. function error(errCode, errMsg = '') {
  166. redirect('/pages/public/error', {
  167. errCode,
  168. errMsg,
  169. });
  170. }
  171. export default {
  172. go,
  173. back,
  174. hasHistory,
  175. redirect,
  176. getCurrentPage,
  177. getCurrentRoute,
  178. error,
  179. };