egg(91)–egg之商品列表数据渲染

33次阅读

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

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>
效果

正文完
 0