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

23次阅读

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

流程

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

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’);
}
效果

购物车中选中商品,点击去结算

订单确认,点击立即下单

生成订单,选择付款
购物车中,删除刚才选择的商品,留下没有选中的商品

第一步

第二步

第三步

第四步

数据库中查看数据
order

order_item

正文完
 0