Selaa lähdekoodia

查询订单详情视图

lrf 2 vuotta sitten
vanhempi
commit
2b53d30814

+ 5 - 0
app/controller/view/config/.order.js

@@ -0,0 +1,5 @@
+module.exports = {
+  getOrder: {
+    params: ['!id'],
+  },
+};

+ 2 - 1
app/controller/view/order.js

@@ -1,4 +1,5 @@
 'use strict';
+const meta = require('./config/.order.js');
 const Controller = require('egg').Controller;
 const { CrudController } = require('naf-framework-mongoose-free/lib/controller');
 
@@ -14,4 +15,4 @@ class OrderController extends Controller {
     this.ctx.ok(data);
   }
 }
-module.exports = CrudController(OrderController, {});
+module.exports = CrudController(OrderController, meta);

+ 16 - 12
app/service/view/order.js

@@ -9,11 +9,18 @@ class OrderService extends CrudService {
   constructor(ctx) {
     super(ctx, 'order');
     this.orderModel = this.ctx.model.Trade.Order;
+    this.orderDetailModel = this.ctx.model.Trade.OrderDetail;
     this.shopModel = this.ctx.model.Shop.Shop;
     this.goodsRateModel = this.ctx.model.Shop.GoodsRate;
     this.afterSaleModel = this.ctx.model.Trade.AfterSale;
   }
 
+  async getOrder({ id }) {
+    const num = await this.orderModel.count({ _id: id });
+    if (num > 0) return this.ctx.service.trade.order.fetch({ id });
+    return this.ctx.service.trade.orderDetail.fetch({ id });
+  }
+
   async allOrder(query = {}) {
     const { skip, limit } = query;
     const customer = _.get(this.ctx, 'user._id');
@@ -59,18 +66,15 @@ class OrderService extends CrudService {
       let obj = {};
       if (pay) {
         // 这个是order表的数据,需要用order的计算方式
-        const buy_num_total = goods.reduce(
-          (p, n) => {
-            let num = 0;
-            if (_.get(n, 'is_set') === '0') num = _.get(n, 'buy_num');
-            else {
-              const goods = _.get(n, 'goods', []);
-              num = goods.reduce((np, nn) => this.ctx.plus(np, nn.buy_num), 0);
-            }
-            return this.ctx.plus(p, num);
-          },
-          0
-        );
+        const buy_num_total = goods.reduce((p, n) => {
+          let num = 0;
+          if (_.get(n, 'is_set') === '0') num = _.get(n, 'buy_num');
+          else {
+            const goods = _.get(n, 'goods', []);
+            num = goods.reduce((np, nn) => this.ctx.plus(np, nn.buy_num), 0);
+          }
+          return this.ctx.plus(p, num);
+        }, 0);
         obj.buy_num_total = buy_num_total;
         obj.real_pay = _.get(i, 'pay.pay_money');
         const shopData = _.pick(_.head(goods), [ 'shop', 'shop_name' ]);

+ 5 - 1
app/z_router/view/order.js

@@ -6,7 +6,11 @@ const routerRegister = require(regPath);
 const rkey = 'viewOrder';
 const ckey = 'view.order';
 const keyZh = '订单相关视图';
-const routes = [{ method: 'get', path: `${rkey}`, controller: `${ckey}.allOrder`, name: `${ckey}allOrder`, zh: '全部订单' }];
+
+const routes = [
+  { method: 'get', path: `${rkey}`, controller: `${ckey}.allOrder`, name: `${ckey}allOrder`, zh: '全部订单' },
+  { method: 'get', path: `${rkey}/getOrder/:id`, controller: `${ckey}.getOrder`, name: `${ckey}getOrder`, zh: '订单查询' },
+];
 
 module.exports = app => {
   routerRegister(app, routes, keyZh, rkey, ckey);