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<subCatesIds.length;i++){
tempArr.push({
"cate_id":subCatesIds[i]._id
})
}
var goodsList=await this.ctx.model.Goods.find({
$or:tempArr
},'_id title price sub_title goods_img shop_price');
}
var tpl=curentCate[0].template?curentCate[0].template:'default/product_list.html';
await this.ctx.render(tpl,{
goodsList:goodsList
});
}
async info() {
await this.ctx.render('default/product_info.html');
}
}
module.exports = ProductController;
view
app/view/default/index.html
<a href="/plist?cid=<%=goodsCate[i]._id%>"><%=goodsCate[i].title%></a>
app/view/default/product_list.html
<% include ./public/header.html%>
<!--end header -->
<!-- start banner_x -->
<% include ./public/banner.html%>
<!-- end banner_x -->
<!-- start danpin -->
<div class="danpin center">
<div class="biaoti center">小米手机</div>
<div class="main center">
<%for(var i=0;i<goodsList.length;i++){%>
<div class="mingxing fl mb20" style="border:2px solid #fff;width:230px;cursor:pointer;" onmouseout="this.style.border='2px solid #fff'" onmousemove="this.style.border='2px solid red'">
<div class="sub_mingxing">
<a href="/pinfo?id=<%=goodsList[i]._id%>" target="_blank"><img src="<%=goodsList[i].goods_img%>" alt="" /></a>
</div>
<div class="pinpai"><a href="./xiangqing.html" target="_blank"><%=goodsList[i].title%></a></div>
<div class="jiage"><%=goodsList[i].shop_price%>元</div>
</div>
<%}%>
</div>
</div>
<footer class="mt20 center">
<div class="mt20">小米商城|MIUI|米聊|多看书城|小米路由器|视频电话|小米天猫店|小米淘宝直营店|小米网盟|小米移动|隐私政策|Select Region</div>
<div>©mi.com 京ICP证110507号 京ICP备10046444号 京公网安备11010802020134号 京网文[2014]0059-0009号</div>
<div>违法和不良信息举报电话:185-0130-1238,本网站所列数据,除特殊说明,所有数据均出自我司实验室测试</div>
</footer>
<!-- end danpin -->
</body>
</html>
效果


**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。