html页面判断是否登录,egg(103)--egg之定义公共的中间件判断用户是否登录以及去结算页面制作...
判斷用戶是否登錄
中間件
app/middleware/userauth.js
module.exports = (options, app) => {
return async function init(ctx, next) {
//判斷前臺(tái)用戶是否登錄 如果登錄可以進(jìn)入 ( 去結(jié)算 用戶中心) 如果沒有登錄直接跳轉(zhuǎn)到登錄
var userinfo = ctx.service.cookies.get('userinfo');
if (userinfo && userinfo._id && userinfo.phone) {
//判斷數(shù)據(jù)庫里面有沒有當(dāng)前用戶
var userResutl = await ctx.model.User.find({ "_id": userinfo._id, "phone": userinfo.phone });
if (userResutl && userResutl.length > 0) {
//注意
await next();
} else {
ctx.redirect('/login');
}
} else {
ctx.redirect('/login');
}
};
};
router
var userauthMiddleware = app.middleware.userauth({}, app);
router.get('/buy/checkout', initMiddleware, userauthMiddleware, controller.default.buy.checkout);
}
效果
點(diǎn)擊結(jié)算,如果沒有登錄,就跳轉(zhuǎn)到登錄頁面
購物車到結(jié)算頁面
controller
app/controller/default/buy.js
async checkout() {
var orderList = [];
var allPrice = 0;
var cartList = this.service.cookies.get('cartList');
if (cartList && cartList.length > 0) {
for (var i = 0; i < cartList.length; i++) {
if (cartList[i].checked) {
orderList.push(cartList[i]);
allPrice += cartList[i].price * cartList[i].num;
}
}
await this.ctx.render('/default/checkout.html', {
orderList: orderList,
allPrice: allPrice
})
} else {
this.ctx.redirect('/cart')
}
}
view
app/view/default/checkout.html
元 x
元
應(yīng)付總額:
元
效果
總結(jié)
以上是生活随笔為你收集整理的html页面判断是否登录,egg(103)--egg之定义公共的中间件判断用户是否登录以及去结算页面制作...的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: CSS3简介、新增选择器、属性选择器、伪
- 下一篇: java scanner以回车结束_请问