egg(92)--egg之商品列表数据渲染

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>

效果

clipboard.png

clipboard.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值