router.js
router.get('/plist',initMiddleware, controller.default.product.list);
controller.js
app/controller/default/product.js
'use strict';const Controller = require('egg').Controller;class ProductController extends Controller { async list() { var cid=this.ctx.request.query.cid; //根据分类id获取当前的分类新 var curentCate=await this.ctx.model.GoodsCate.find({"_id":cid}); console.log(JSON.stringify(curentCate)) //判断是否是顶级分类 if(curentCate[0].pid!=0){ // 二级分类 var goodsList=await this.ctx.model.Goods.find({"cate_id":cid},'_id title price sub_title goods_img shop_price'); console.log(goodsList); }else{ //顶级分类 获取当前顶级分类下面的所有的子分类 var subCatesIds=await this.ctx.model.GoodsCate.find({"pid":this.app.mongoose.Types.ObjectId(cid)},'_id'); var tempArr=[]; for(var i=0;i
view
app/view/default/index.html
<%=goodsCate[i].title%>
app/view/default/product_list.html
<% include ./public/header.html%> <% include ./public/banner.html%> <%for(var i=0;i
<%=goodsList[i].shop_price%>元
<%}%>
效果