lrf il y a 2 ans
Parent
commit
5bac220456

+ 13 - 0
app/controller/trade/cart.js

@@ -0,0 +1,13 @@
+'use strict';
+const meta = require('./config/.cart.js');
+const Controller = require('egg').Controller;
+const { CrudController } = require('naf-framework-mongoose-free/lib/controller');
+
+// 
+class CartController extends Controller {
+  constructor(ctx) {
+    super(ctx);
+    this.service = this.ctx.service.trade.cart;
+  }
+}
+module.exports = CrudController(CartController, meta);

+ 41 - 0
app/controller/trade/config/.cart.js

@@ -0,0 +1,41 @@
+module.exports = {
+  create: {
+    requestBody: ['customer', 'shop', 'goods', 'goodsSpec', 'num'],
+  },
+  destroy: {
+    params: ['!id'],
+    service: 'delete',
+  },
+  update: {
+    params: ['!id'],
+    requestBody: ['customer', 'shop', 'goods', 'goodsSpec', 'num'],
+  },
+  show: {
+    parameters: {
+      params: ['!id'],
+    },
+    service: 'fetch',
+  },
+  index: {
+    parameters: {
+      query: {
+        'meta.createdAt@start': 'meta.createdAt@start',
+        'meta.createdAt@end': 'meta.createdAt@end',
+        customer: 'customer',
+        shop: 'shop',
+        goods: 'goods',
+        goodsSpec: 'goodsSpec',
+      },
+      // options: {
+      //   "meta.state": 0 // 默认条件
+      // },
+    },
+    service: 'query',
+    options: {
+      query: ['skip', 'limit'],
+      sort: ['meta.createdAt'],
+      desc: true,
+      count: true,
+    },
+  },
+};

+ 26 - 0
app/model/trade/cart.js

@@ -0,0 +1,26 @@
+'use strict';
+const Schema = require('mongoose').Schema;
+const metaPlugin = require('naf-framework-mongoose-free/lib/model/meta-plugin');
+
+// 购物车
+const cart = {
+  customer: { type: String, required: false, zh: '顾客', ref: 'User.User' }, //
+  shop: { type: String, required: false, zh: '店铺', ref: 'Shop.Shop' }, //
+  goods: { type: String, required: false, zh: '商品', ref: 'Shop.Goods' }, //
+  goodsSpec: { type: String, required: false, zh: '商品规格', ref: 'Shop.GoodsSpec' }, //
+  num: { type: Number, required: false, zh: '数量' }, //
+};
+const schema = new Schema(cart, { toJSON: { getters: true, virtuals: true } });
+schema.index({ id: 1 });
+schema.index({ 'meta.createdAt': 1 });
+schema.index({ customer: 1 });
+schema.index({ shop: 1 });
+schema.index({ goods: 1 });
+schema.index({ goodsSpec: 1 });
+
+schema.plugin(metaPlugin);
+
+module.exports = app => {
+  const { mongoose } = app;
+  return mongoose.model('Cart', schema, 'cart');
+};

+ 15 - 0
app/service/trade/cart.js

@@ -0,0 +1,15 @@
+'use strict';
+const { CrudService } = require('naf-framework-mongoose-free/lib/service');
+const { BusinessError, ErrorCode } = require('naf-core').Error;
+const _ = require('lodash');
+const assert = require('assert');
+
+// 
+class CartService extends CrudService {
+  constructor(ctx) {
+    super(ctx, 'cart');
+    this.model = this.ctx.model.Trade.Cart;
+  }
+}
+
+module.exports = CartService;

+ 19 - 0
app/z_router/trade/cart.js

@@ -0,0 +1,19 @@
+'use strict';
+// 路由配置
+const path = require('path');
+const regPath = path.resolve('app', 'public', 'routerRegister');
+const routerRegister = require(regPath);
+const rkey = 'cart';
+const ckey = 'trade.cart';
+const keyZh = '购物车';
+const routes = [
+  { method: 'get', path: `${rkey}`, controller: `${ckey}.index`, name: `${ckey}Query`, zh: `${keyZh}列表查询` },
+  { method: 'get', path: `${rkey}/:id`, controller: `${ckey}.show`, name: `${ckey}Show`, zh: `${keyZh}查询` },
+  { method: 'post', path: `${rkey}`, controller: `${ckey}.create`, name: `${ckey}Create`, zh: `创建${keyZh}` },
+  { method: 'post', path: `${rkey}/:id`, controller: `${ckey}.update`, name: `${ckey}Update`, zh: `修改${keyZh}` },
+  { method: 'delete', path: `${rkey}/:id`, controller: `${ckey}.destroy`, name: `${ckey}Delete`, zh: `删除${keyZh}` },
+];
+
+module.exports = app => {
+  routerRegister(app, routes, keyZh, rkey, ckey);
+};

+ 1 - 0
app/z_router/trade/index.js

@@ -5,4 +5,5 @@ module.exports = app => {
   require('./order')(app); // 总订单
   require('./orderDetail')(app); // 订单详情
   require('./afterSale')(app); // 售后
+  require('./cart')(app); // 购物车
 };