Skip to content

Commit

Permalink
Add order api implementations
Browse files Browse the repository at this point in the history
  • Loading branch information
aojiaotage committed Aug 31, 2019
1 parent c2c7474 commit 24dfaea
Show file tree
Hide file tree
Showing 6 changed files with 108 additions and 13 deletions.
93 changes: 83 additions & 10 deletions app/controller/order.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,56 @@

'use strict';

const moment = require('moment');

const { Controller } = require('egg');

class OrderController extends Controller {

async orderCheckOut() {

const userId = this.ctx.session.user.id;

const cartItems = await this.ctx.service.cart.listItems(
{ user_id: userId });

const address = (await this.ctx.service.address.listAddresses(
{ user_ids: [userId], limit: 1 }))[0].toJSON();

address.address_id = address.id;

this.ctx.body = {
code: 0,
status: 200,
data: {
cartlist: {
items: cartItems.map(i => {
return {
goodsId: i.goods_id,
image_url: i.img_url,
num: i.nums,
short_name: i.name,
subtotal: i.price * i.nums,
};
}),
},
address,
paymethod: [
{
checked: '1',
subtitle: '支付订单',
type: 'weixin_wap',
value: '微信支付',
},
],
},
};
}

async createNewOrder() {
const { products, addressId, bestShipTime } = this.ctx.request.body;

let goods = await this.ctx.service.goods.listGoods({ids: products.map(p => p.id)});
let goods = await this.ctx.service.goods.listGoods(
{ ids: products.map(p => p.id) });

goods = goods.map(g => {
const p = products.find(e => e.id.toString() === g.id.toString());
Expand All @@ -20,18 +62,17 @@ class OrderController extends Controller {
return gObj;
});

const { address } = await this.ctx.service.address.getAddressDetail(addressId);
const { address } = await this.ctx.service.address.getAddressDetail(
addressId);
// TODO 发票信息
// const { invoice } = await this.ctx.service.invoice.getInvoiceDetail(invoiceId);

// TODO 配送价格
// const expense = await this.ctx.service.shipping.getShippingExpense(invoiceId);


// TODO 检查库存
// this.ctx.service.inventory.checkInventory(goods);


const created = await this.ctx.service.order.createNewOrder({
user_id: this.ctx.session.user.id,
goods,
Expand All @@ -51,11 +92,30 @@ class OrderController extends Controller {
async listOrders() {
const { query } = this.ctx.request;
const orders = await this.ctx.service.order.listOrders(query);

const list = orders.map(o => {
return {
order_id: o.id,
order_status: o.status,
goods_amount: o.goods_amount,
add_time: moment(o.created_at).format('YYYY/MM/DD HH:mm:SS'),
order_status_info: '等待付款',
product: o.subOrders.map(s => {
return {
goods_id: s.goods_id,
image_url: '//i1.mifile.cn/a1/pms_1527060327.66235934!180x1800.jpg,//i1.mifile.cn/a1/pms_1527735134.03584233!180x1800.jpg,//i1.mifile.cn/a1/pms_1501236937.96732594!180x1800.jpg',
product_name: s.name,
product_count: s.nums,
};
}),
};
});

this.ctx.body = {
code: 0,
status: 200,
data: {
orders,
list,
},
};
}
Expand All @@ -65,13 +125,26 @@ class OrderController extends Controller {
const query = {
ids: [id],
};
const [order] = await this.ctx.service.order.listOrders(query);
const [o] = await this.ctx.service.order.listOrders(query);
const obj = {
order_id: o.id,
order_status: o.status,
goods_amount: o.goods_amount,
add_time: moment(o.created_at).format('YYYY/MM/DD HH:mm:SS'),
order_status_info: '等待付款',
product: o.subOrders.map(s => {
return {
goods_id: s.goods_id,
image_url: '//i1.mifile.cn/a1/pms_1527060327.66235934!180x1800.jpg,//i1.mifile.cn/a1/pms_1527735134.03584233!180x1800.jpg,//i1.mifile.cn/a1/pms_1501236937.96732594!180x1800.jpg',
product_name: s.name,
product_count: s.nums,
};
}),
};
this.ctx.body = {
code: 0,
status: 200,
data: {
order,
},
data: obj,
};
}

Expand Down
8 changes: 7 additions & 1 deletion app/model/cart.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ module.exports = app => {
};

Model.listItems = async query => {
const { ids, last_id, sort, limit } = query;
const { ids, last_id, sort, limit, user_id } = query;

const sequelizeQuery = {};

Expand All @@ -87,6 +87,12 @@ module.exports = app => {
};
}

if (user_id) {
sequelizeQuery.where.user_id = {
[Op.eq]: user_id,
};
}

sequelizeQuery.limit = limit || 20;

const carts = await Model.findAll(sequelizeQuery);
Expand Down
8 changes: 7 additions & 1 deletion app/model/sub_order.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ module.exports = app => {
};

Model.listOrders = async query => {
const { ids, last_id, sort, limit } = query;
const { ids, last_id, sort, limit, order_ids } = query;

const sequelizeQuery = {};

Expand All @@ -85,6 +85,12 @@ module.exports = app => {
};
}

if (order_ids) {
sequelizeQuery.where.order_id = {
[Op.in]: order_ids,
};
}

sequelizeQuery.limit = limit || 20;

const orders = await Model.findAll(sequelizeQuery);
Expand Down
1 change: 1 addition & 0 deletions app/router/order.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@ module.exports = app => {
router.get('/api/v1/order/:id', controller.order.getOrderById);
router.patch('/api/v1/order/:id', controller.order.updateOrderById);
router.post('/api/v1/order', controller.order.createNewOrder);
router.get('/api/v1/orderCheckOut', controller.order.orderCheckOut);

};
10 changes: 9 additions & 1 deletion app/service/order.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,15 @@ class Order extends Service {
}

async listOrders(query) {
const orders = await this.app.model.Order.listOrders(query);
let orders = await this.app.model.Order.listOrders(query);
const order_ids = orders.map(o => o.id);
const subOrders = await this.app.model.SubOrder.listOrders({ order_ids });
orders = orders.map(o => {
const obj = o.toJSON();
obj.subOrders = subOrders.filter(
s => s.order_id.toString() === o.id.toString());
return obj;
});
return orders;
}

Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"egg-scripts": "^2.11.0",
"egg-sequelize": "^4.3.1",
"egg-session": "^3.1.0",
"moment": "^2.24.0",
"pg": "7.8.0",
"uuid": "^3.3.2"
},
Expand Down

0 comments on commit 24dfaea

Please sign in to comment.