乐趣区

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%>
<!–end header –>

<!– start banner_x –>
<% include ../public/banner.html%>
<!– end banner_x –>

<script src=”/public/default/js/jqPaginator.js”></script>

<link rel=”stylesheet” href=”/public/default/css/order.css” />

<!– self_info –>
<div class=”grzxbj”>
<div class=”selfinfo center”>
<div class=”lfnav fl”>
<% include ./user_left.html%>
</div>
<div class=”rtcont fr”>
<h1> 我的订单 </h1>

<div class=”uc-content-box”>

<div class=”box-hd”>
<div class=”more clearfix”>
<ul class=”filter-list J_orderType”>
<li class=”first active”><a href=”/user/order”> 全部有效订单 </a></li>
<li><a href=”/user/order?page=<%=page%>&order_status=0″> 待支付 </a></li>
<li><a href=”/user/order?page=<%=page%>&order_status=1″> 已支付 </a></li>
<li><a href=”/user/order?page=<%=page%>&order_status=3″> 待收货 </a></li>
<li><a href=”/user/order?page=<%=page%>&order_status=6″> 已关闭 </a></li>
</ul>
<form id=”J_orderSearchForm” class=”search-form clearfix” action=”#” method=”get”>

<input class=”search-text” type=”search” id=”J_orderSearchKeywords” name=”keywords” autocomplete=”off” placeholder=” 输入商品名称、商品编号、订单号 ”>
<input type=”submit” class=”search-btn iconfont” value=” 搜索 ”>
</form>
</div>
</div>
<div class=”box-bd”>

<%if(list.length>0){%>

<table class=”table”>

<%for(var i=0;i<list.length;i++){%>
<tr <%if(list[i].pay_status==0){%>class=”order_pay”
<%}%>>
<td colspan=”2″>

<div class=”order-summary”>

<h2>

<%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){%>

无效 已取消
<%}%>

</h2>

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

</div>

<%for(var j=0;j<list[i].orderItems.length;j++){%>
<div class=”order-info clearfix”>

<div class=”col_pic”>
<img src=”<%=list[i].orderItems[j].product_img%>” />
</div>
<div class=”col_title”>
<p>
<%=list[i].orderItems[j].product_title%>
</p>
<p>
<%=list[i].orderItems[j].product_price%> 元 ×
<%=list[i].orderItems[j].product_num%>
</p>

</div>
</div>
<%}%>
</td>

<td>
<span>

<%if(list[i].pay_status==1){%>
<a class=”delete btn” href=”/user/orderinfo?id=<%=list[i]._id%>”> 订单详情 </a>
<br>
<br>
<a class=”delete btn” href=”#”> 申请售后 </a>

<%}else{%>
<a class=”delete btn btn-primary” href=”/buy/confirm?id=<%=list[i]._id%>”> 去支付 </a>

<br>
<br>
<a class=”delete btn” href=”/user/orderinfo?id=<%=list[i]._id%>”> 订单详情 </a>
<%}%>

</span>
</td>
</tr>

<%}%>

</table>

<div id=”page” class=”pagination fr”></div>

<%}else{%>

<p style=”text-align:center; padding-top:100px;”> 没有查找到任何订到 </p>
<%}%>
</div>
</div>

<script>
$(‘#page’).jqPaginator({
totalPages: <%=totalPages%>,
visiblePages: 8,
currentPage: <%=page%>,
onPageChange: function(num, type) {
console.log(‘ 当前第 ’ + num + ‘ 页 ’, type);

if (type == ‘change’) {

location.href = “/user/order?page=” + num + ‘&order_status=’ + <%=order_status%>;
}
}
});
</script>

</div>
<div class=”clear”></div>
</div>
</div>
<!– self_info –>

<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>
</body>

</html>
效果
显示订单支付状态

根据订单状态筛选

搜索

退出移动版