egg(115)--egg之订单页面筛选,搜索

model

修改order_item,增加uid

appmodelorder_item.js
module.exports = app => {
    const mongoose = app.mongoose;
    const Schema = mongoose.Schema;

    const d = new Date();
    const OrderItem = new Schema({
        uid: { type: Schema.Types.ObjectId },
        order_id: { type: Schema.Types.ObjectId },
        product_title: { type: String },
        product_id: { type: Schema.Types.ObjectId },
        product_img: { type: String },
        product_price: { type: Number },
        product_num: { type: Number },
        add_time: {
            type: Number,
            default: d.getTime(),
        }
    });

    return mongoose.model('OrderItem', OrderItem, 'order_item');
};

controller

appcontrollerdefaultbuy.js
'use strict';

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

class BuyController extends Controller {

    // 去结算
    async checkout() {

        // 获取购物车选中的商品

        let orderList = [];
        let allPrice = 0;
        let cartList = this.service.cookies.get('cartList');


        //签名防止重复提交订单
        var orderSign = await this.service.tools.md5(await this.service.tools.getRandomNum());
        this.ctx.session.orderSign = orderSign;


        if (cartList && cartList.length > 0) {

            for (let i = 0; i < cartList.length; i++) {

                if (cartList[i].checked) {
                    orderList.push(cartList[i]);

                    allPrice += cartList[i].price * cartList[i].num;
                }

            }

            // 获取当前用户的所有收货地址

            const uid = this.ctx.service.cookies.get('userinfo')._id;
            const addressList = await this.ctx.model.Address.find({ uid }).sort({ default_address: -1 });


            await this.ctx.render('default/checkout.html', {
                orderList,
                allPrice,
                addressList,
                orderSign: orderSign
            });

        } else {
            // 恶意操作
            this.ctx.redirect('/cart');
        }


    }


    //提交订单


    async doOrder() {

        /*
          1、获取收货地址信息

          2、需要获取购买商品的信息

          3、把这些信息  放在订单表  
                
          4、删除购物车里面的数据    
        */

        /*防止提交重复订单*/
        var orderSign = this.ctx.request.body.orderSign;
        if (orderSign != this.ctx.session.orderSign) {
            return false;
        }
        this.ctx.session.orderSign = null;


        const uid = this.ctx.service.cookies.get('userinfo')._id;
        let addressResult = await this.ctx.model.Address.find({ "uid": uid, "default_address": 1 });
        let cartList = this.service.cookies.get('cartList');
        if (addressResult && addressResult.length > 0 && cartList && cartList.length > 0) {

            var all_price = 0;

            let orderList = cartList.filter((value) => {
                if (value.checked) {

                    all_price += value.price * value.num;
                    return value;
                }
            })

            //执行提交订单的操作

            let order_id = await this.service.tools.getOrderId();
            let name = addressResult[0].name;
            let phone = addressResult[0].phone;
            let address = addressResult[0].address;
            let zipcode = addressResult[0].zipcode;
            let pay_status = 0;
            let pay_type = '';
            let order_status = 0;


            let orderModel = new this.ctx.model.Order({ order_id, uid, name, phone, address, zipcode, pay_status, pay_type, order_status, all_price });
            let orderResult = await orderModel.save();

            if (orderResult && orderResult._id) {


                //增加商品信息

                for (let i = 0; i < orderList.length; i++) {

                    let json = {

                        "uid": uid,
                        "order_id": orderResult._id, //订单id
                        "product_title": orderList[i].title,
                        "product_id": orderList[i]._id,
                        "product_img": orderList[i].goods_img,
                        "product_price": orderList[i].price,
                        "product_num": orderList[i].num
                    }

                    let orderItemModel = new this.ctx.model.OrderItem(json);
                    await orderItemModel.save();

                }


                //删除购物车中已经购买的商品             

                var unCheckedCartList = cartList.filter((value) => {
                    if (!value.checked) {
                        return value;
                    }
                })

                this.service.cookies.set('cartList', unCheckedCartList);


                this.ctx.redirect('/buy/confirm?id=' + orderResult._id);


            } else {
                this.ctx.redirect('/buy/checkout');
            }

        } else {


            this.ctx.redirect('/buy/checkout');
        }









        console.log('提交订单');

    }

    // 确认订单  支付
    async confirm() {


        var id = this.ctx.request.query.id;


        var orderResult = await this.ctx.model.Order.find({ "_id": id });

        if (orderResult && orderResult.length > 0) {


            //获取商品

            var orderItemResult = await this.ctx.model.OrderItem.find({ "order_id": id });


            await this.ctx.render('default/confirm.html', {
                orderResult: orderResult[0],
                orderItemResult: orderItemResult,
                id: id

            });

        } else {
            //错误
            this.ctx.redirect('/');
        }

    }


    //执行多次
    async getOrderPayStatus() {

        /*

         1、获取订单号
    
         2、查询当前订单的支付状态

         3、如果支付 返回成功   如果没有支付返回失败信息


        */

        var id = this.ctx.request.query.id;

        if (id) {
            try {

                var orderReuslt = await this.ctx.model.Order.find({ "_id": id });
                if (orderReuslt && orderReuslt[0].pay_status == 1 && orderReuslt[0].order_status == 1) {
                    this.ctx.body = {
                        success: true,
                        message: '已支付'
                    }

                } else {
                    this.ctx.body = {
                        success: false,
                        message: '未支付'
                    }
                }

            } catch (error) {

                this.ctx.body = {
                    success: false,
                    message: '未支付'
                }
            }

        } else {
            this.ctx.body = {
                success: false,
                message: '未支付'
            }

        }

    }
}

module.exports = BuyController;
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 order_status = this.ctx.request.query.order_status || -1;
        var keywords = this.ctx.request.query.keywords;




        var json = { "uid": this.app.mongoose.Types.ObjectId(uid) }; //查询当前用户下面的所有订单

        //筛选
        if (order_status != -1) {
            json = Object.assign(json, { "order_status": parseInt(order_status) });
        }

        //搜索

        if (keywords) {

            var orderItemJson = Object.assign({ "uid": this.app.mongoose.Types.ObjectId(uid) }, { "product_title": { $regex: new RegExp(keywords) } });

            var orderItemResult = await this.ctx.model.OrderItem.find(orderItemJson);

            if (orderItemResult.length > 0) {

                var tempArr = [];
                orderItemResult.forEach(value => {
                    tempArr.push({
                        _id: value.order_id
                    });
                });


                json = Object.assign(json, {
                        $or: tempArr
                    })
                    /*            
                     { uid: 5c10c2dfd702ac47bc58ab45,
                      '$or':
                      [ { _id: 5c41955b10f6400bb0c850ab },
                        { _id: 5c42a48be6389d22a4396833 } ] }

                    */

            } else {
                json = Object.assign(json, {
                    $or: [{ 1: -1 }]
                })
            }

        }

        console.log("aaa")
        console.log(JSON.stringify(json));


        const pageSize = 5;
        // 总数量
        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: json //条件
            },
            {
                $skip: (page - 1) * pageSize,
            },
            {
                $limit: pageSize,
            }
        ]);

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


    }

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


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

    }
}

module.exports = UserController;

view

appviewdefaultuserorder.html
<% include  ../public/header.html%>
    

    
    <% include  ../public/banner.html%>
        

        

        


        
        
<% include ./user_left.html%>

我的订单

<%if(list.length>0){%> <%for(var i=0;i class="order_pay" <%}%>> <%}%>

<%if(list[i].order_status==0){%> 已下单 未支付 <%}else if(list[i].order_status==1){%> 已付款 <%}else if(list[i].order_status==2){%> 已配货 <%}else if(list[i].order_status==3){%> 已发货 <%}else if(list[i].order_status==4){%> 交易成功 <%}else if(list[i].order_status==5){%> 已退货 <%}else if(list[i].order_status==6){%> 无效 已取消 <%}%>

<%=helper.formatTime(list[i].add_time) %> | <%=list[i].name%> | 订单号: <%=list[i].order_id%> | 在线支付 实付金额: <%=list[i].all_price%>元

<%for(var j=0;j

<%=list[i].orderItems[j].product_title%>

<%=list[i].orderItems[j].product_price%>元 × <%=list[i].orderItems[j].product_num%>

<%}%>
<%if(list[i].pay_status==1){%> 订单详情

申请售后 <%}else{%> 去支付

订单详情 <%}%>
<%}else{%>

没有查找到任何订到

<%}%>
小米商城|MIUI|米聊|多看书城|小米路由器|视频电话|小米天猫店|小米淘宝直营店|小米网盟|小米移动|隐私政策|Select Region
©mi.com 京ICP证110507号 京ICP备10046444号 京公网安备11010802020134号 京网文[2014]0059-0009号
违法和不良信息举报电话:185-0130-1238,本网站所列数据,除特殊说明,所有数据均出自我司实验室测试

效果

显示订单支付状态

根据订单状态筛选

搜索

你可能感兴趣的:(eggjs)