lrf 2 rokov pred
rodič
commit
91f6db4e28

+ 0 - 50
app/controller/group/config/.group.js

@@ -1,50 +0,0 @@
-module.exports = {
-  create: {
-    requestBody: ['person_limit', 'shop', 'goods', 'goodsSpec', 'leader', 'persons', 'status'],
-  },
-  destroy: {
-    params: ['!id'],
-    service: 'delete',
-  },
-  update: {
-    params: ['!id'],
-    requestBody: ['person_limit', 'shop', 'goods', 'goodsSpec', 'leader', 'persons', 'status'],
-  },
-  show: {
-    parameters: {
-      params: ['!id'],
-    },
-    service: 'fetch',
-  },
-  index: {
-    parameters: {
-      query: {
-        'meta.createdAt@start': 'meta.createdAt@start',
-        'meta.createdAt@end': 'meta.createdAt@end',
-        shop: 'shop',
-        goods: 'goods._id',
-        goodsSpec: 'goodsSpec._id',
-        person: 'persons.customer',
-        leader: 'leader',
-        status: 'status',
-      },
-      // options: {
-      //   'persons.status': '0', // 默认条件
-      // },
-    },
-    service: 'query',
-    options: {
-      query: ['skip', 'limit'],
-      sort: ['meta.createdAt'],
-      desc: true,
-      count: true,
-    },
-  },
-  getGroup: {
-    parameters: {
-      query: {
-        order_id: 'order_id',
-      },
-    },
-  },
-};

+ 0 - 13
app/controller/group/group.js

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

+ 3 - 2
app/controller/shop/config/.goodsSpec.js

@@ -1,6 +1,6 @@
 module.exports = {
   create: {
-    requestBody: ['leader_price', 'sort', 'file', 'can_group', 'group_config', 'sell_money', 'flow_money', 'freight', 'goods', 'name', 'num', 'status'],
+    requestBody: ['buy_limit', 'limit_num', 'leader_price', 'sort', 'file', 'can_group', 'group_config', 'sell_money', 'flow_money', 'freight', 'goods', 'name', 'num', 'status'],
   },
   destroy: {
     params: ['!id'],
@@ -8,7 +8,7 @@ module.exports = {
   },
   update: {
     params: ['!id'],
-    requestBody: ['leader_price', 'sort', 'file', 'can_group', 'group_config', 'sell_money', 'flow_money', 'freight', 'goods', 'name', 'num', 'status'],
+    requestBody: ['buy_limit', 'limit_num', 'leader_price', 'sort', 'file', 'can_group', 'group_config', 'sell_money', 'flow_money', 'freight', 'goods', 'name', 'num', 'status'],
   },
   show: {
     parameters: {
@@ -26,6 +26,7 @@ module.exports = {
         status: 'status',
         can_group: 'can_group',
         sort: 'sort',
+        buy_limit: 'buy_limit',
       },
       // options: {
       //   "meta.state": 0 // 默认条件

+ 0 - 34
app/model/group/group.js

@@ -1,34 +0,0 @@
-'use strict';
-const Schema = require('mongoose').Schema;
-const metaPlugin = require('naf-framework-mongoose-free/lib/model/meta-plugin');
-const persons = [{
-  customer: '用户',
-  status: '状态', // 0:入团;1:退团
-  join_time: '入团时间',
-  out_time: '退团时间',
-}];
-// 团表
-const group = {
-  shop: { type: String, required: false, zh: '店铺', ref: 'Shop.Shop' }, //
-  goods: { type: Object, required: false, zh: '商品' }, //
-  goodsSpec: { type: Object, required: false, zh: '规格' }, //
-  leader: { type: String, required: false, zh: '团长', ref: 'User.User' }, //
-  persons: { type: Array, required: false, zh: '团员' }, //
-  status: { type: String, required: false, default: '0', zh: '状态' }, // 字典:group_status
-  person_limit: { type: Number, required: false, zh: '人数限制' }, //
-};
-const schema = new Schema(group, { toJSON: { getters: true, virtuals: true } });
-schema.index({ id: 1 });
-schema.index({ 'meta.createdAt': 1 });
-schema.index({ shop: 1 });
-schema.index({ goods: 1 });
-schema.index({ goodsSpec: 1 });
-schema.index({ leader: 1 });
-schema.index({ status: 1 });
-
-schema.plugin(metaPlugin);
-
-module.exports = app => {
-  const { mongoose } = app;
-  return mongoose.model('Group', schema, 'group');
-};

+ 5 - 5
app/model/shop/goodsSpec.js

@@ -9,19 +9,19 @@ const goodsSpec = {
   goods: { type: String, required: false, zh: '商品源', ref: 'Shop.Goods' }, //
   name: { type: String, required: false, zh: '规格名称' }, //
   num: { type: Number, required: false, zh: '库存' }, //
-  status: { type: String, required: false, default: '0', zh: '状态' }, // 字典status
-  can_group: { type: String, required: false, zh: '是否可以团购' }, // 字典:tf
-  group_config: { type: Object, required: false, zh: '团购设置' }, // 团购模块的设置:团购金额:money,需要人数:need_person
+  buy_limit: { type: String, required: false, default: '0', zh: '购买限制' }, // 字典表:buy_limit
+  limit_num: { type: Number, required: false, zh: '限制数量界限' }, //
+  status: { type: String, required: false, default: '0', zh: '状态' }, // 字典:use
   file: { type: Array, required: false, zh: '图片' }, //
-  sort: { type: Number, required: false, default: 0, zh: '排序' }, //
+  sort: { type: Number, required: false, default: '0', zh: '排序' }, //
 };
 const schema = new Schema(goodsSpec, { toJSON: { getters: true, virtuals: true } });
 schema.index({ id: 1 });
 schema.index({ 'meta.createdAt': 1 });
 schema.index({ goods: 1 });
 schema.index({ name: 1 });
+schema.index({ buy_limit: 1 });
 schema.index({ status: 1 });
-schema.index({ can_group: 1 });
 schema.index({ sort: 1 });
 
 schema.plugin(metaPlugin);

+ 0 - 1
app/router.js

@@ -12,7 +12,6 @@ module.exports = app => {
   require('./z_router/system/index')(app); // 系统部分
   require('./z_router/shop/index')(app); // 店铺部分
   require('./z_router/trade/index')(app); // 交易部分
-  require('./z_router/group/index')(app); // 团购部分
 
   require('./z_router/view/index')(app); // 视图部分
   require('./z_router/util')(app); // 工具接口

+ 4 - 4
app/service/trade/cart.js

@@ -83,13 +83,13 @@ class CartService extends CrudService {
     const data = await this.model.findOne(query);
     if (data) {
       const buyNum = this.ctx.plus(data.num, num);
-      const { enough } = await this.checkGoodsNum({ goodsSpecId: goodsSpec, num: buyNum });
-      if (!enough) throw new BusinessError(ErrorCode.SERVICE_FAULT, '库存不足,无法添加至购物车');
+      const { enough, msg } = await this.checkGoodsNum({ goodsSpecId: goodsSpec, num: buyNum });
+      if (!enough) throw new BusinessError(ErrorCode.SERVICE_FAULT, msg);
       data.num = buyNum;
       await data.save();
     } else {
-      const { enough } = await this.checkGoodsNum({ goodsSpecId: goodsSpec, num });
-      if (!enough) throw new BusinessError(ErrorCode.SERVICE_FAULT, '库存不足,无法添加至购物车');
+      const { enough, msg } = await this.checkGoodsNum({ goodsSpecId: goodsSpec, num });
+      if (!enough) throw new BusinessError(ErrorCode.SERVICE_FAULT, msg);
       await this.model.create({ num, ...body });
     }
   }

+ 4 - 6
app/service/trade/order.js

@@ -380,6 +380,7 @@ class OrderService extends CrudService {
             goodsSpec_name: '$name',
             freight: { $toString: '$freight' },
             sell_money: { $toString: '$sell_money' },
+            leader_price: { $toString: '$leader_price' },
             num: { $toDouble: num },
             file: '$goods.file',
             tags: '$goods.tags',
@@ -399,15 +400,12 @@ class OrderService extends CrudService {
     const is_leader = _.get(user, 'is_leader', '1');
     if (is_leader === '0') {
       for (const i of arr) {
-        const { goods_id: goods, goodsSpec_id: spec } = i;
-        const r = await this.goodsConfigModel.findOne({ goods, spec });
-        if (r) {
-          i.leader_price = r.leader_price;
-          i.price = r.leader_price;
+        if (i.leader_price) {
+          i.leader_price = this.ctx.toNumber(i.leader_price);
+          i.price = i.leader_price;
         }
       }
     }
-
     // 平铺数据后,需要处理活动相关部分
     // 经过处理后的数据,会添加act字段,表明与活动有关的信息
     await this.dealAct(arr, actList);

+ 25 - 0
app/service/util/trade.js

@@ -168,6 +168,31 @@ class TradeService extends CrudService {
       result.result = false;
       return result;
     }
+    // 判断是否有购买限制
+    if (_.get(goodsSpecData, 'buy_limit', '0') !== '0') {
+      // 有购买限制,需要检测购买限制
+      const buy_limit = _.get(goodsSpecData, 'buy_limit');
+      const limit_num = _.get(goodsSpecData, 'limit_num', 0);
+      if (buy_limit === '1') {
+        if (num < limit_num) {
+          result.msg = `该规格至少购买 ${limit_num} 份, 购买数量不足`;
+          result.result = false;
+          return result;
+        }
+      } else if (buy_limit === '2') {
+        if (num > limit_num) {
+          result.msg = `该规格至多购买 ${limit_num} 份, 购买数量超过上限`;
+          result.result = false;
+          return result;
+        }
+      } else if (buy_limit === '3') {
+        if (num !== limit_num) {
+          result.msg = `该规格只能购买 ${limit_num} 份`;
+          result.result = false;
+          return result;
+        }
+      }
+    }
     return result;
   }
 

+ 22 - 14
app/service/view/goods.js

@@ -77,9 +77,10 @@ class GoodsService extends CrudService {
               freight: { $toDouble: '$freight' },
               name: 1,
               num: 1,
-              can_group: 1,
-              group_config: 1,
+              buy_limit: 1,
+              limit_num: 1,
               file: 1,
+              leader_price: 1,
             },
           },
         ],
@@ -162,15 +163,18 @@ class GoodsService extends CrudService {
     if (user) {
       const is_leader = _.get(user, 'is_leader', '1');
       if (is_leader === '0') {
-        const list = await this.goodsConfigModel.find({ goods: id }).lean();
-        for (const i of list) {
-          const { spec, leader_price } = i;
-          const r = data.specs.find(f => f._id === spec);
-          if (r) {
-            r.o_sell_money = JSON.parse(r.sell_money);
-            r.leader_price = leader_price;
-            r.sell_money = leader_price;
-          }
+        if (is_leader === '0') {
+          data.specs = data.specs.map(i => {
+            const { sell_money, leader_price } = i;
+            const sm = this.ctx.toNumber(sell_money);
+            const obj = { o_sell_money: sm };
+            if (leader_price) {
+              const lp = this.ctx.toNumber(leader_price);
+              obj.leader_price = lp;
+              obj.sell_money = lp;
+            }
+            return { ...i, ...obj };
+          });
         }
       }
     }
@@ -214,6 +218,7 @@ class GoodsService extends CrudService {
         create_time: 1,
         sell_money: { $toDouble: '$specs.sell_money' },
         flow_money: { $toDouble: '$specs.flow_money' },
+        leader_price: { $toDouble: '$specs.leader_price' },
         num: '$specs.num',
         act_tags: 1,
       },
@@ -232,6 +237,7 @@ class GoodsService extends CrudService {
         file: '$data.file',
         sell_money: '$data.sell_money',
         flow_money: '$data.flow_money',
+        leader_price: '$data.leader_price',
         num: '$data.num',
         create_time: '$data.create_time',
         sort: '$data.sort',
@@ -246,7 +252,8 @@ class GoodsService extends CrudService {
     if (parseInt(limit)) qpipeline.push({ $limit: parseInt(limit) });
     let list = await this.goodsModel.aggregate(qpipeline);
     list = list.map(i => {
-      const obj = _.pick(i, [ 'name', 'file', 'num', 'flow_money', 'sell_money', 'view_num', '_id', 'actTagsShow', 'act_tags' ]);
+      const obj = _.pick(i, [ 'name', 'file', 'num', 'flow_money', 'sell_money', 'view_num', '_id', 'actTagsShow', 'act_tags', 'leader_price' ]);
+      if (obj.leader_price) obj.leader_price = this.ctx.toNumber(obj.leader_price);
       return obj;
     });
     // 处理活动标签
@@ -259,8 +266,8 @@ class GoodsService extends CrudService {
     const user = _.get(this.ctx, 'user');
     if (user) {
       const is_leader = _.get(user, 'is_leader', '1');
-      if (is_leader === '0') {
-        list = await this.searchLeaderPrice(list);
+      if (is_leader === '1') {
+        list = list.map(i => _.omit(i, [ 'leader_price' ]));
       }
     }
     const tpipeline = _.cloneDeep(pipeline);
@@ -352,6 +359,7 @@ class GoodsService extends CrudService {
     return list;
   }
 
+  // 弃用
   async searchLeaderPrice(list) {
     for (const i of list) {
       const { _id: goods } = i;

+ 0 - 20
app/z_router/group/group.js

@@ -1,20 +0,0 @@
-'use strict';
-// 路由配置
-const path = require('path');
-const regPath = path.resolve('app', 'public', 'routerRegister');
-const routerRegister = require(regPath);
-const rkey = 'group';
-const ckey = 'group.group';
-const keyZh = '团';
-const routes = [
-  { method: 'get', path: `${rkey}/getGroup`, controller: `${ckey}.getGroup`, name: `${ckey}getGroup`, zh: `${keyZh}获取订单信息` },
-  { 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);
-};

+ 0 - 6
app/z_router/group/index.js

@@ -1,6 +0,0 @@
-/**
- * @param {Egg.Application} app - egg application
- */
-module.exports = app => {
-  require('./group')(app); // 团
-};