t-comm
Version:
专业、稳定、纯粹的工具库
103 lines (101 loc) • 3.63 kB
JavaScript
/**
* 拦截路由
*
* @example
* ```ts
* uniHookRouter({
* navigateToHooks: [
* () => console.log('1')
* ],
* navigateBackHooks: [
* () => console.log('2')
* ],
* redirectToHooks: [
* () => console.log('3')
* ],
* debug: true,
* })
* ```
*/
function uniHookRouter(_ref) {
var navigateToHooks = _ref.navigateToHooks,
navigateBackHooks = _ref.navigateBackHooks,
redirectToHooks = _ref.redirectToHooks,
_ref$tryUniInterCepto = _ref.tryUniInterCeptor,
tryUniInterCeptor = _ref$tryUniInterCepto === void 0 ? true : _ref$tryUniInterCepto,
_ref$debug = _ref.debug,
debug = _ref$debug === void 0 ? false : _ref$debug;
var originNavigateTo = uni.navigateTo;
var originNavigateBack = uni.navigateBack;
var originReplaceTo = uni.redirectTo;
var toDebug = function toDebug(name, callbacks) {
return {
invoke: function invoke() {
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
args[_key] = arguments[_key];
}
callbacks === null || callbacks === void 0 ? void 0 : callbacks.forEach(function (cb) {
return cb === null || cb === void 0 ? void 0 : cb.apply(void 0, args);
});
if (debug) {
console.log(">>> uniHookRouter ".concat(name, " invoke"));
}
},
success: function success() {
if (debug) {
console.log(">>> uniHookRouter ".concat(name, " success"));
}
},
fail: function fail() {
if (debug) {
console.log(">>> uniHookRouter ".concat(name, " fail"));
}
},
complete: function complete() {
if (debug) {
console.log(">>> uniHookRouter ".concat(name, " complete"));
}
}
};
};
if (tryUniInterCeptor && typeof uni.addInterceptor === 'function') {
uni.addInterceptor('navigateTo', Object.assign({}, toDebug('navigateTo', navigateToHooks)));
uni.addInterceptor('navigateBack', Object.assign({}, toDebug('navigateBack', navigateBackHooks)));
uni.addInterceptor('redirectTo', Object.assign({}, toDebug('redirectTo', redirectToHooks)));
return;
}
if (originNavigateTo) {
uni.navigateTo = function () {
for (var _len2 = arguments.length, args = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
args[_key2] = arguments[_key2];
}
navigateToHooks === null || navigateToHooks === void 0 ? void 0 : navigateToHooks.forEach(function (cb) {
return cb === null || cb === void 0 ? void 0 : cb.apply(void 0, args);
});
originNavigateTo.apply(void 0, args);
};
}
if (originNavigateBack) {
uni.navigateBack = function () {
for (var _len3 = arguments.length, args = new Array(_len3), _key3 = 0; _key3 < _len3; _key3++) {
args[_key3] = arguments[_key3];
}
navigateBackHooks === null || navigateBackHooks === void 0 ? void 0 : navigateBackHooks.forEach(function (cb) {
return cb === null || cb === void 0 ? void 0 : cb.apply(void 0, args);
});
originNavigateBack.apply(void 0, args);
};
}
if (originReplaceTo) {
uni.redirectTo = function () {
for (var _len4 = arguments.length, args = new Array(_len4), _key4 = 0; _key4 < _len4; _key4++) {
args[_key4] = arguments[_key4];
}
redirectToHooks === null || redirectToHooks === void 0 ? void 0 : redirectToHooks.forEach(function (cb) {
return cb === null || cb === void 0 ? void 0 : cb.apply(void 0, args);
});
originReplaceTo.apply(void 0, args);
};
}
}
export { uniHookRouter };