egg(91)–egg之配置前台公共中间件获取数据(系统设置、公共导航、公共nav)

42次阅读

共计 3081 个字符,预计需要花费 8 分钟才能阅读完成。

controller
app/controller/default/index.js
去掉请求顶部导航,中间导航,商品分类的请求
‘use strict’;

const Controller = require(‘egg’).Controller;

class IndexController extends Controller {
async index() {

console.time(‘indextime’);

// 轮播图
var focus=await this.ctx.service.cache.get(‘index_focus’);
if(!focus){
focus=await this.ctx.model.Focus.find({“type”:1});
await this.ctx.service.cache.set(‘index_focus’,focus,60*60);

}

// 手机

var shoujiResult=await this.ctx.service.cache.get(‘index_shoujiResult’);

if(!shoujiResult){
shoujiResult=await this.service.goods.get_category_recommend_goods(‘5bbf058f9079450a903cb77b’,’best’,8);
await this.ctx.service.cache.set(‘index_shoujiResult’,shoujiResult,60*60);

}

// 电视
var dianshiResult=await this.service.goods.get_category_recommend_goods(‘5bbf05ac9079450a903cb77c’,’best’,10);

console.timeEnd(‘indextime’);

await this.ctx.render(‘default/index’,{
focus:focus,
shoujiResult:shoujiResult
});

}
}

module.exports = IndexController;

中间件 middleware
app/middleware/init.js

module.exports = (options,app) => {
return async function init(ctx, next) {

// console.log(app);

// 获取顶部导航的数据
var topNav=await ctx.service.cache.get(‘index_topNav’);
if(!topNav){
topNav=await ctx.model.Nav.find({“position”:1});
await ctx.service.cache.set(‘index_topNav’,topNav,60*60);
}

// 商品分类
var goodsCate=await ctx.service.cache.get(‘index_goodsCate’);
if(!goodsCate){
goodsCate=await ctx.model.GoodsCate.aggregate([

{
$lookup:{
from:’goods_cate’,
localField:’_id’,
foreignField:’pid’,
as:’items’
}
},
{
$match:{
“pid”:’0′
}
}

])
await ctx.service.cache.set(‘index_goodsCate’,goodsCate,60*60);

}

// 中间导航以及关联商品

var middleNav=await ctx.service.cache.get(‘index_middleNav’);
if(!middleNav){
middleNav=await ctx.model.Nav.find({“position”:2});
middleNav=JSON.parse(JSON.stringify(middleNav)); //1、不可扩展对象
for(var i=0;i<middleNav.length;i++){
if(middleNav[i].relation){
// 数据库查找 relation 对应的商品
try{
var tempArr=middleNav[i].relation.replace(/,/g,’,’).split(‘,’);
var tempRelationIds=[];
tempArr.forEach((value)=>{
tempRelationIds.push({
“_id”:app.mongoose.Types.ObjectId(value)
})
})
var relationGoods=await ctx.model.Goods.find({
$or:tempRelationIds
},’title goods_img’);

middleNav[i].subGoods=relationGoods;

}catch(err){//2、如果用户输入了错误的 ObjectID(商品 id)

middleNav[i].subGoods=[];
}
}else{

middleNav[i].subGoods=[];
}
}

await ctx.service.cache.set(‘index_middleNav’,middleNav,60*60);

}

// console.log(middleNav);

ctx.state.topNav=topNav;
ctx.state.goodsCate=goodsCate;
ctx.state.middleNav=middleNav;
// 注意
await next();

};
};

router.js
app/router/default.js
给需要使用顶部导航,中间导航,商品分类,增加中间件过滤
module.exports = app => {
const {router, controller} = app;
// 配置路由中间件
var initMiddleware=app.middleware.init({},app);
router.get(‘/’, initMiddleware,controller.default.index.index);
router.get(‘/plist’,initMiddleware, controller.default.product.list);
router.get(‘/pinfo’,initMiddleware, controller.default.product.info);
router.get(‘/pinfo’,initMiddleware, controller.default.product.info);
router.get(‘/cart’, initMiddleware,controller.default.flow.cart);
// 用户中心
router.get(‘/login’, controller.default.user.login);
router.get(‘/register’, controller.default.user.register);
router.get(‘/user’, controller.default.user.welcome);
router.get(‘/user/order’, controller.default.user.order);
}
效果
顶部导航,中间导航,商品分类的数据

正文完
 0