UNPKG

4.91 kBJavaScriptView Raw
1import { __awaiter } from 'tslib';
2import { addLeadingSlash } from '@tarojs/runtime';
3import { EventChannel } from '@tarojs/shared';
4import { parsePath } from 'history';
5import { history, prependBasename } from './history.js';
6import { RouterConfig } from './router/index.js';
7import stacks from './router/stack.js';
8import { routesAlias } from './utils/index.js';
9
10const routeEvtChannel = EventChannel.routeChannel;
11function processNavigateUrl(option) {
12 var _a;
13 const pathPieces = parsePath(option.url);
14 // 处理相对路径
15 if ((_a = pathPieces.pathname) === null || _a === void 0 ? void 0 : _a.includes('./')) {
16 const parts = routesAlias.getOrigin(history.location.pathname).split('/');
17 parts.pop();
18 pathPieces.pathname.split('/').forEach((item) => {
19 if (item === '.')
20 return;
21 item === '..' ? parts.pop() : parts.push(item);
22 });
23 pathPieces.pathname = parts.join('/');
24 }
25 // 确保是 / 开头的路径
26 pathPieces.pathname = addLeadingSlash(pathPieces.pathname);
27 // 处理自定义路由
28 pathPieces.pathname = routesAlias.getAlias(addLeadingSlash(pathPieces.pathname));
29 // 处理 basename
30 pathPieces.pathname = prependBasename(pathPieces.pathname);
31 // hack fix history v5 bug: https://github.com/remix-run/history/issues/814
32 if (!pathPieces.search)
33 pathPieces.search = '';
34 return pathPieces;
35}
36function navigate(option, method) {
37 return __awaiter(this, void 0, void 0, function* () {
38 return new Promise((resolve, reject) => {
39 stacks.method = method;
40 const { success, complete, fail } = option;
41 const unListen = history.listen(() => {
42 const res = { errMsg: `${method}:ok` };
43 if (method === 'navigateTo') {
44 res.eventChannel = routeEvtChannel;
45 routeEvtChannel.addEvents(option.events);
46 }
47 success === null || success === void 0 ? void 0 : success(res);
48 complete === null || complete === void 0 ? void 0 : complete(res);
49 resolve(res);
50 unListen();
51 });
52 try {
53 if ('url' in option) {
54 const pathPieces = processNavigateUrl(option);
55 const state = { timestamp: Date.now() };
56 if (method === 'navigateTo') {
57 // Note: 由于 spa 会针对弱网情况下,短时间内多次跳转同一个页面跳转加了锁,后续如果有用户反馈返回无效,那可能是这个问题
58 history.push(pathPieces, state);
59 }
60 else if (method === 'redirectTo' || method === 'switchTab') {
61 history.replace(pathPieces, state);
62 }
63 else if (method === 'reLaunch') {
64 stacks.delta = stacks.length;
65 history.replace(pathPieces, state);
66 }
67 }
68 else if (method === 'navigateBack') {
69 stacks.delta = option.delta;
70 if (stacks.length > option.delta) {
71 history.go(-option.delta);
72 }
73 else {
74 history.go(1 - stacks.length);
75 }
76 }
77 }
78 catch (error) {
79 const res = { errMsg: `${method}:fail ${error.message || error}` };
80 fail === null || fail === void 0 ? void 0 : fail(res);
81 complete === null || complete === void 0 ? void 0 : complete(res);
82 if (fail || complete) {
83 return resolve(res);
84 }
85 else {
86 return reject(res);
87 }
88 }
89 });
90 });
91}
92function navigateTo(option) {
93 return navigate(option, 'navigateTo');
94}
95function redirectTo(option) {
96 return navigate(option, 'redirectTo');
97}
98function navigateBack(option = { delta: 1 }) {
99 if (!option.delta || option.delta < 1) {
100 option.delta = 1;
101 }
102 return navigate(option, 'navigateBack');
103}
104function switchTab(option) {
105 return navigate(option, 'switchTab');
106}
107function reLaunch(option) {
108 return navigate(option, 'reLaunch');
109}
110function getCurrentPages() {
111 if (process.env.NODE_ENV !== 'production' && RouterConfig.mode === 'multi') {
112 console.warn('多页面路由模式不支持使用 getCurrentPages 方法!');
113 }
114 const pages = stacks.get();
115 return pages.map(e => { var _a; return (Object.assign(Object.assign({}, e), { route: ((_a = e.path) === null || _a === void 0 ? void 0 : _a.replace(/\?.*/g, '')) || '' })); });
116}
117
118export { getCurrentPages, navigateBack, navigateTo, reLaunch, redirectTo, switchTab };