egg(114)--egg之订单页面切换选中

router

    router.get('/user/welcome', initMiddleware, userauthMiddleware, controller.default.user.welcome);

    router.get('/user/order', initMiddleware, userauthMiddleware, controller.default.user.order);

    router.get('/user/orderinfo', initMiddleware, userauthMiddleware, controller.default.user.orderinfo);

中间件

传递当前url

appmiddlewareuserauth.js
ctx.state.url = url.parse(ctx.request.url).pathname;

controller

appcontrollerdefaultuser.js
'use strict';

const Controller = require('egg').Controller;

class UserController extends Controller {

    async welcome() {
        await this.ctx.render('default/user/welcome.html');
    }

    async order() {
        const uid = this.ctx.service.cookies.get('userinfo')._id;
        const page = this.ctx.request.query.page || 1;
        var json = { "uid": uid }; //查询当前用户下面的所有订单
        const pageSize = 2;
        // 总数量
        const totalNum = await this.ctx.model.Order.find(json).countDocuments();


        //聚合管道要注意顺序

        const result = await this.ctx.model.Order.aggregate([{
                $lookup: {
                    from: 'order_item',
                    localField: '_id',
                    foreignField: 'order_id',
                    as: 'orderItems',
                },
            },
            {
                $sort: { "add_time": -1 }
            },
            {
                $match: { "uid": this.app.mongoose.Types.ObjectId(uid) } //条件
            },
            {
                $skip: (page - 1) * pageSize,
            },
            {
                $limit: pageSize,
            }
        ]);

        await this.ctx.render('default/user/order.html', {
            list: result,
            totalPages: Math.ceil(totalNum / pageSize),
            page,
        });


    }

    async orderinfo() {
        // this.ctx.body = '用户订单';
        await this.ctx.render('default/user/order_info.html');
    }


    async address() {
        this.ctx.body = '收货地址';

    }
}

module.exports = UserController;

view

左侧导航

appviewdefaultuseruser_left.html
用户中心

欢迎页

appviewdefaultuserwelcome.html
<% include  ./user_left.html%>
 
<% include ./user_left.html%>

2152135723

晚上好

修改个人信息 > 2152135723

订单页

 <% include  ./user_left.html%>

订单详情页

<% include  ./user_left.html%>

效果

欢迎页

egg(114)--egg之订单页面切换选中_第1张图片

订单页

egg(114)--egg之订单页面切换选中_第2张图片

订单详情页

egg(114)--egg之订单页面切换选中_第3张图片

你可能感兴趣的:(egg(114)--egg之订单页面切换选中)