index.js
4.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
/*
* @Author: 胡杨
* @Date: 2019-04-25 11:14:48
* @Last Modified by: 胡杨
* @Last Modified time: 2019-04-25 11:14:48
*/
import qs from 'qs';
function isObject(o) {
// 是否对象
return Object.prototype.toString.call(o).slice(8, -1) === 'Object'
}
export class Router {
/**
* @class
* */
constructor() {
this.interceptor = {
before: null,
after: null
}
this.instance = null
}
static getInstance() {
if (!this.instance) {
this.instance = new Router()
}
return this.instance
}
__before(options) {
if (this.interceptor.before) {
let newOptions = this.interceptor.before(options)
if (newOptions) {
return newOptions
}
}
return options
}
__after(options) {
if (this.interceptor.after) {
let newOptions = this.interceptor.after(options)
if (newOptions) {
return newOptions
}
}
return options
}
/**
* 增加参数到地址
* */
addParams(url, params) {
let new_url;
if (url.indexOf('?') === -1) {
new_url = `${url}?${params}`;
} else {
new_url = url.replace('?', `?${params}&`);
}
return new_url;
}
/**
* 保留当前页面,跳转到应用内的某个页面,使用navigateBack可以返回到原页面。
* @description navigateTo, redirectTo 只能打开非 tabBar 页面。
* @param {Object} options
* @param {String} [options.url] - 地址
* @param {String} [options.animationType] - 动画样式
* @param {Number} [options.animationDuration] - 动画时长
* @param {Function} [options.success] - 执行成功
* @param {Function} [options.fail] - 执行失败
* @param {Function} [options.complete] - 执行结束
* @return {Object}
*/
navigateTo(
options = {
url: '',
params: {},
animationType: 'pop-in',
animationDuration: 300,
success: () => {},
fail: () => {},
complete: () => {}
}
) {
let new_options = this.__before(options);
if (new_options.url) {
if (isObject(new_options.params)) {
new_options.params = qs.stringify(new_options.params);
new_options.url = this.addParams(new_options.url, new_options.params)
}
uni.navigateTo(new_options);
}
return this.__after(new_options)
}
/**
* 关闭当前页面,跳转到应用内的某个页面。
* @description navigateTo, redirectTo 只能打开非 tabBar 页面。
* @param {*} options
*/
redirectTo(
options = {
url: '',
success: () => {},
fail: () => {},
complete: () => {}
}
) {
let new_options = this.__before(options);
if (new_options.url) {
if (isObject(new_options.params)) {
new_options.params = qs.stringify(new_options.params);
new_options.url = this.addParams(new_options.url, new_options.params)
}
uni.redirectTo(new_options);
}
return this.__after(new_options)
}
/**
* 关闭所有页面,打开到应用内的某个页面。
* @description reLaunch 可以打开任意页面。
* @param {*} options
*/
reLaunch(
options = {
url: '',
success: () => {},
fail: () => {},
complete: () => {}
}
) {
let new_options = this.__before(options);
if (new_options.url) {
if (isObject(new_options.params)) {
new_options.params = qs.stringify(new_options.params);
new_options.url = this.addParams(new_options.url, new_options.params)
}
uni.reLaunch(new_options);
}
return this.__after(new_options)
}
/**
* 跳转到 tabBar 页面,并关闭其他所有非 tabBar 页面。
* @description switchTab 只能打开 tabBar 页面。
* @param {*} options
*/
switchTab(
options = {
url: '',
success: () => {},
fail: () => {},
complete: () => {}
}
) {
let new_options = this.__before(options);
if (new_options.url) {
if (isObject(new_options.params)) {
new_options.params = qs.stringify(new_options.params);
new_options.url = this.addParams(new_options.url, new_options.params)
}
uni.switchTab(new_options);
}
return this.__after(new_options)
}
/**
* 关闭当前页面,返回上一页面或多级页面。可通过 getCurrentPages() 获取当前的页面栈,决定需要返回几层。
* @param {*} options
*/
navigateBack(
options = {
delta: 1,
animationType: 'pop-out',
animationDuration: 300
}
) {
let new_options = this.__before(options);
if (new_options.url) {
if (isObject(new_options.params)) {
new_options.params = qs.stringify(new_options.params);
new_options.url = this.addParams(new_options.url, new_options.params)
}
uni.navigateBack(new_options);
}
return this.__after(new_options)
}
}
let _myRouter = Router.getInstance();
export default _myRouter;