lrf 2 年 前
コミット
c763c46f00

+ 24 - 0
app/controller/trade/config/.coupon.js

@@ -69,4 +69,28 @@ module.exports = {
       count: true,
     },
   },
+  userView: {
+    parameters: {
+      query: {
+        'meta.createdAt@start': 'meta.createdAt@start',
+        'meta.createdAt@end': 'meta.createdAt@end',
+        shop: 'shop',
+        name: 'name',
+        expire_type: 'expire_type',
+        discount_type: 'discount_type',
+        use_limit: 'use_limit',
+        get_limit: 'get_limit',
+        status: 'status',
+      },
+      // options: {
+      //   "meta.state": 0 // 默认条件
+      // },
+    },
+    options: {
+      query: ['skip', 'limit'],
+      sort: ['meta.createdAt'],
+      desc: true,
+      count: true,
+    },
+  },
 };

+ 72 - 0
app/service/trade/coupon.js

@@ -10,9 +10,81 @@ class CouponService extends CrudService {
   constructor(ctx) {
     super(ctx, 'coupon');
     this.model = this.ctx.model.Trade.Coupon;
+    this.userCouponModel = this.ctx.model.User.UserCoupon;
+  }
+
+  async userView(filter, { skip = 0, limit, sort, desc, projection } = {}) {
+    const customer = _.get(this.ctx, 'user._id');
+    if (customer) throw new BusinessError(ErrorCode.NOT_LOGIN, '未找到用户登陆信息');
+    let list = await this.query(filter, { skip, limit, sort, desc, projection });
+    list = await this.makeShowData(list);
+    // 还需要判断用户是否领取过了
+    for (const coupon of list) {
+      const { get_limit, get_limit_config } = coupon;
+      if (get_limit === 'nolimit') coupon.canGet = true;
+      else {
+        const max = _.get(get_limit_config, 'max', 1);
+        const num = await this.userCouponModel.count({ coupon: coupon._id, customer });
+        if (this.ctx.minus(num, max) > 0) coupon.canGet = false;
+      }
+    }
+    return list;
   }
 
 
+  async makeShowData(couponList) {
+    const dictDataModel = this.ctx.model.Dev.DictData;
+    // 过期字典
+    const expList = await dictDataModel.find({ code: 'coupon_expire_type' });
+    // 减免字典
+    const disList = await dictDataModel.find({ code: 'coupon_discount_type' });
+    // 使用字典
+    const useList = await dictDataModel.find({ code: 'coupon_use_limit' });
+    // 领取字典
+    const getList = await dictDataModel.find({ code: 'coupon_get_limit' });
+    couponList = couponList.map(coupon => {
+      const { _id } = coupon;
+      let obj = { _id, name: _.get(coupon, 'name') };
+      // 失效
+      const expire_type = _.get(coupon, 'expire_type');
+      const expire_type_label = _.get(
+        expList.find(f => f.value === expire_type),
+        'label'
+      );
+      let expire_time = _.get(coupon, `expire_config.${expire_type}`);
+      if (expire_time) {
+        if (expire_type === 'fixed') expire_time = expire_time.join(' 至 ');
+      }
+      obj = { ...obj, expire_type, expire_type_label, expire_time };
+      // 减免
+      const discount_type = _.get(coupon, 'discount_type');
+      const discount_type_label = _.get(
+        disList.find(f => f.value === discount_type),
+        'label'
+      );
+      const discount_config = _.get(coupon, 'discount_config');
+      obj = { ...obj, discount_type, discount_type_label, discount_config };
+      // 使用
+      const use_limit = _.get(coupon, 'use_limit');
+      const use_limit_label = _.get(
+        useList.find(f => f.value === use_limit),
+        'label'
+      );
+      const use_limit_config = _.get(coupon, 'use_limit_config');
+      obj = { ...obj, use_limit, use_limit_label, use_limit_config };
+
+      // 领取
+      const get_limit = _.get(coupon, 'get_limit');
+      const get_limit_label = _.get(
+        getList.find(f => f.value === get_limit),
+        'label'
+      );
+      const get_limit_config = _.get(coupon, 'get_limit_config');
+      obj = { ...obj, get_limit, get_limit_label, get_limit_config };
+      return obj;
+    });
+    return couponList;
+  }
 }
 
 module.exports = CouponService;

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

@@ -7,6 +7,7 @@ const rkey = 'coupon';
 const ckey = 'trade.coupon';
 const keyZh = '优惠券';
 const routes = [
+  { method: 'get', path: `${rkey}/userView`, controller: `${ckey}.userView`, name: `${ckey}userView`, 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}` },