egg(106)--egg之提交订单,生成订单和子订单,删除购物车

流程

  1. 购物车中选中商品,点击去结算
  2. 获取收货地址信息
  3. 需要获取购买商品的信息
  4. 把这些信息 放在订单表
  5. 删除购物车里面的数据

model

order

app/model/order.js
module.exports = app => {
    const mongoose = app.mongoose;
    const Schema = mongoose.Schema;

    const d = new Date();
    const Order = new Schema({
        uid: { type: Schema.Types.ObjectId },
        all_price: { type: Number },
        order_id: { type: Number },
        name: { type: String },
        phone: { type: Number },
        address: { type: String },
        zipcode: { type: String },
        pay_status: { type: Number }, // 支付状态: 0 表示未支付     1 已经支付
        pay_type: { type: String }, // 支付类型: alipay    wechat  
        order_status: { // 订单状态: 0 已下单  1 已付款  2 已配货  3、发货   4、交易成功   5、退货     6、取消      
            type: Number
        },
        add_time: {
            type: Number,
            default: d.getTime()
        }
    });

    return mongoose.model('Order', Order, 'order');
};

order_item

app/model/order_item.js
module.exports = app => {
    const mongoose = app.mongoose;
    const Schema = mongoose.Schema;

    const d = new Date();
    const OrderItem = new Schema({
        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');
};

router

    //去结算
    router.get('/buy/checkout', initMiddleware, userauthMiddleware, controller.default.buy.checkout);
    //确认订单去支付
    router.get('/buy/confirm', initMiddleware, userauthMiddleware, controller.default.buy.confirm);
    //提交订单
    router.post('/buy/doOrder', initMiddleware, userauthMiddleware, controller.default.buy.doOrder);

去结算

controller

app/controller/default/buy.js
    async checkout() {

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

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


        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,
            });

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


    }

提交订单

controller

app/controller/default/buy.js
    async doOrder() {
        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, 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 = {
                        "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('/checkout');
            }
        } else {

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

确认订单,去支付

app/controller/default/buy.js
    async confirm() {
        await this.ctx.render('default/confirm.html');
    }

效果

  1. 购物车中选中商品,点击去结算
  2. 订单确认,点击立即下单
  3. 生成订单,选择付款
  4. 购物车中,删除刚才选择的商品,留下没有选中的商品

第一步

clipboard.png

第二步

clipboard.png

第三步

clipboard.png

第四步

clipboard.png

数据库中查看数据

order

clipboard.png

order_item

clipboard.png

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值