1. 微信小程序 全局路由拦截
function loginCheck(pageObj) {
if (pageObj.onLoad) {
let _onLoad = pageObj.onLoad;
pageObj.onLoad = function (options) {
if(wx.getStorageSync('userinfo')) {
let currentInstance = getPageInstance();
_onLoad.call(currentInstance, options);
} else {
wx.redirectTo({
url: "/pages/login/index"
});
}
}
}
return pageObj;
}
function getPageInstance() {
var pages = getCurrentPages();
return pages[pages.length - 1];
}
exports.loginCheck = loginCheck;
2. 在需要使用的页面 中使用就行了
/
const filter = require('../../utils/filter');
Page(filter.loginCheck({
onLoad: function (options) {
},
}));