wzp
2022-02-22 46083a2914a62c1835612fb0872d7fdc76d7bb9d
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
module.exports =
/******/ (function(modules) { // webpackBootstrap
/******/     // The module cache
/******/     var installedModules = {};
/******/
/******/     // The require function
/******/     function __webpack_require__(moduleId) {
/******/
/******/         // Check if module is in cache
/******/         if(installedModules[moduleId]) {
/******/             return installedModules[moduleId].exports;
/******/         }
/******/         // Create a new module (and put it into the cache)
/******/         var module = installedModules[moduleId] = {
/******/             i: moduleId,
/******/             l: false,
/******/             exports: {}
/******/         };
/******/
/******/         // Execute the module function
/******/         modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
/******/
/******/         // Flag the module as loaded
/******/         module.l = true;
/******/
/******/         // Return the exports of the module
/******/         return module.exports;
/******/     }
/******/
/******/
/******/     // expose the modules object (__webpack_modules__)
/******/     __webpack_require__.m = modules;
/******/
/******/     // expose the module cache
/******/     __webpack_require__.c = installedModules;
/******/
/******/     // define getter function for harmony exports
/******/     __webpack_require__.d = function(exports, name, getter) {
/******/         if(!__webpack_require__.o(exports, name)) {
/******/             Object.defineProperty(exports, name, { enumerable: true, get: getter });
/******/         }
/******/     };
/******/
/******/     // define __esModule on exports
/******/     __webpack_require__.r = function(exports) {
/******/         if(typeof Symbol !== 'undefined' && Symbol.toStringTag) {
/******/             Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
/******/         }
/******/         Object.defineProperty(exports, '__esModule', { value: true });
/******/     };
/******/
/******/     // create a fake namespace object
/******/     // mode & 1: value is a module id, require it
/******/     // mode & 2: merge all properties of value into the ns
/******/     // mode & 4: return value when already ns object
/******/     // mode & 8|1: behave like require
/******/     __webpack_require__.t = function(value, mode) {
/******/         if(mode & 1) value = __webpack_require__(value);
/******/         if(mode & 8) return value;
/******/         if((mode & 4) && typeof value === 'object' && value && value.__esModule) return value;
/******/         var ns = Object.create(null);
/******/         __webpack_require__.r(ns);
/******/         Object.defineProperty(ns, 'default', { enumerable: true, value: value });
/******/         if(mode & 2 && typeof value != 'string') for(var key in value) __webpack_require__.d(ns, key, function(key) { return value[key]; }.bind(null, key));
/******/         return ns;
/******/     };
/******/
/******/     // getDefaultExport function for compatibility with non-harmony modules
/******/     __webpack_require__.n = function(module) {
/******/         var getter = module && module.__esModule ?
/******/             function getDefault() { return module['default']; } :
/******/             function getModuleExports() { return module; };
/******/         __webpack_require__.d(getter, 'a', getter);
/******/         return getter;
/******/     };
/******/
/******/     // Object.prototype.hasOwnProperty.call
/******/     __webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); };
/******/
/******/     // __webpack_public_path__
/******/     __webpack_require__.p = "";
/******/
/******/
/******/     // Load entry module and return exports
/******/     return __webpack_require__(__webpack_require__.s = 0);
/******/ })
/************************************************************************/
/******/ ([
/* 0 */
/***/ (function(module, exports, __webpack_require__) {
 
module.exports = __webpack_require__(1);
 
 
/***/ }),
/* 1 */
/***/ (function(module, __webpack_exports__, __webpack_require__) {
 
"use strict";
// ESM COMPAT FLAG
__webpack_require__.r(__webpack_exports__);
 
// EXPORTS
__webpack_require__.d(__webpack_exports__, "default", function() { return /* binding */ lib_uniRouteGuards; });
 
// CONCATENATED MODULE: ./src/lib/install.js
/**
 * Vue.use 插件安装
 * @param {Object} Vue
 * @param {*} opts
 */
function install(Vue, opts = {}) {}
 
// CONCATENATED MODULE: ./src/lib/hackRoute.js
/**
 * hack uniapp的路由函数b
 * @param {Function} callback
 * @return {never}
 */
const hackUniRoute = function (callback) {
    // 路由跳转的函数key值
    const UNI_ROUTE_ACTIONS = ['navigateTo', 'redirectTo', 'reLaunch', 'switchTab', 'navigateBack'];
 
    // 函数缓存容器
    const cacheFunc = {};
 
    // 保存原函数引用
    UNI_ROUTE_ACTIONS.forEach((key) => {
        cacheFunc[key] = uni[key];
    });
 
    // 重写方法
    UNI_ROUTE_ACTIONS.forEach((key) => {
        uni[key] = (opts, routeGuardsOpts) => {
            // 取消拦截并直接运行
            if (routeGuardsOpts === false) {
                cacheFunc[key](opts);
            } else {
                // 处理所有钩子
                const defaultOpts = { action: key };
                const newOpts = Object.assign(defaultOpts, opts);
                const actionFunc = function (customOpts) {
                    const lastOpts = Object.assign(newOpts, customOpts);
 
                    cacheFunc[lastOpts.action](lastOpts);
                };
 
                callback.call(this, newOpts, actionFunc);
            }
        };
    });
};
 
// CONCATENATED MODULE: ./src/lib/utils.js
/**
 * 控制台打印内容
 * @param {string} msg 内容
 * @param {string} action ['log'] 打印类型
 * @param {never}
 */
const print = function (msg, action = 'log') {
    console[action]('[route-guards] ' + msg);
};
 
/**
 * 判断错误对象是否是由`Error`对象实例化出来的
 * @param {Error|Object} errObj
 * @return {boolean}
 */
const isError = function (errObj) {
    return Object.prototype.toString.call(errObj).includes('Error');
};
 
/**
 * 获取并封装当前路由栈的信息
 * @return {Object}
 */
const getCurStack = function () {
    const stackAll = getCurrentPages();
    const stackLen = stackAll.length;
 
    // 跳过路由栈为空的情况(App端)
    if (stackLen === 0) {
        return false;
    }
 
    const curStack = stackAll[stackLen - 1];
    const from = { url: '/' + curStack.route };
 
    return from;
};
 
/**
 * 注册 钩子
 * @param {Function[]} list 钩子列表
 * @param {Function} callback 回调函数
 * @returns {Function} 用于注销当前注册钩子的闭包函数
 */
const registerHook = function (list, callback) {
    list.push(callback);
 
    return () => {
        const index = list.indexOf(callback);
 
        if (index !== -1) list.splice(index, 1);
    };
};
 
// CONCATENATED MODULE: ./src/lib/handleHooks.js
 
 
/**
 * 处理 全局钩子队列
 * @param {Object} to
 * @param {Function} uniRunRoute 被hack的uniapp路由方法
 */
const handleGlobalHooksQueue = function (to, uniRunRoute) {
    // 跳过 h5环境中, 调用系统的tabbar功能('tabbar')或系统的navbar上的返回功能('backbutton'), 会触发uni的路由方法
    if (['tabBar', 'backbutton'].includes(to.from)) return uniRunRoute();
 
    // 获取当前路由栈信息
    const from = getCurStack();
 
    // 跳过 app端 首次进入页面会调用uni路由方法, 导致获取当前路由栈(from)为空,所有直接运行,不进行拦截
    if (from === false) return uniRunRoute();
 
    iteratorHook(
        this.beforeHooks,
        handleNextPipe.bind(this),
        () => {
            uniRunRoute();
            handleAfterHook.call(this, to, from);
        },
        {
            to,
            from,
            uniRunRoute
        }
    );
};
 
/**
 * 处理 全局后置钩子
 * @param {Object} to
 * @param {Object} from
 */
const handleAfterHook = function (to, from) {
    this.afterHooks.forEach((hook) => {
        hook(to, from);
    });
};
 
/**
 * 处理 错误信息
 * @param {Object|string} err 错误信息、错误栈
 */
const handleAbort = function (err) {
    if (this.errorCbs.length > 0) {
        this.errorCbs.forEach((cb) => {
            cb(err);
        });
    } else {
        print('error:' + err, 'error');
    }
};
 
/**
 * 遍历并运行 钩子
 * @param {Function[]} hookQueue 钩子队列
 * @param {Function} everyCb 每次遍历都会运行的回调函数
 * @param {Function} endCb 队列运行结束后运行的回调函数
 * @param {Object} hookOpts 钩子运行需要的参数
 */
const iteratorHook = function (hookQueue, everyCb, endCb, hookOpts) {
    const step = (i) => {
        // 队列运行结束,运行回调函数
        if (i >= hookQueue.length) {
            endCb.call(this);
        } else {
            // 遍历运行钩子
            everyCb.call(this, hookQueue[i], hookOpts, (val) => {
                // 结束钩子遍历
                if (val === false) return;
 
                step(++i);
            });
        }
    };
 
    step(0);
};
 
/**
 * 处理 有next参数的钩子(前置钩子)
 * @param {Function} hookCb 钩子函数
 * @param {Object} hookOpts 钩子运行需要的参数
 * @param {Function} iteratorNextHook 运行下一个钩子
 */
const handleNextPipe = function (hookCb, hookOpts, iteratorNextHook) {
    hookCb(hookOpts.to, hookOpts.from, (nextVal) => {
        try {
            // next(false) or next(new Error('xxx')) 中断当前的路径跳转,或中断且注册错误回调
            if (nextVal === false || isError(nextVal)) {
                handleAbort.call(this, nextVal);
            }
 
            // next('/pages/a') or next({ url: '/pages/a' }) 修改 路由
            else if (
                typeof nextVal === 'string' ||
                (typeof nextVal === 'object' && typeof nextVal.url === 'string')
            ) {
                // 处理字符串路径
                typeof nextVal === 'string' && (nextVal = { url: nextVal });
 
                hookOpts.uniRunRoute(nextVal);
                handleAfterHook.call(this, hookOpts.to, hookOpts.from);
 
                // 更新引用,替换原来的`url`字段数据
                hookOpts.to = Object.assign(hookOpts.to, nextVal);
 
                // 结束钩子遍历
                iteratorNextHook(false);
            }
 
            // next() 运行下一个管道(next)
            else {
                iteratorNextHook();
            }
        } catch (err) {
            handleAbort.call(this, err);
        }
    });
};
 
// CONCATENATED MODULE: ./src/lib/index.js
 
 
 
 
 
class lib_uniRouteGuards {
    constructor() {
        // 初始化数据
        this.beforeHooks = [];
        this.afterHooks = [];
        this.errorCbs = [];
        hackUniRoute.call(this, handleGlobalHooksQueue);
    }
 
    /**
     * 注册 全局前置守卫
     * @param {Function} callback 回调函数
     */
    beforeEach(callback) {
        return registerHook(this.beforeHooks, callback);
    }
 
    /**
     * 注册 全局后置守卫
     * @param {Function} callback 回调函数
     */
    afterEach(callback) {
        return registerHook(this.afterHooks, callback);
    }
 
    /**
     * 注册 错误回调
     * @param {Function} errCb 错误回调函数
     */
    onError(errCb) {
        return registerHook(this.errorCbs, errCb);
    }
}
 
// 添加 Vue.use 功能
lib_uniRouteGuards.install = install;
 
 
/***/ })
/******/ ]);