lrf402788946 4 år sedan
förälder
incheckning
4be6a313f0
5 ändrade filer med 106 tillägg och 1 borttagningar
  1. 22 0
      app/controller/car_show.js
  2. 1 0
      app/router.js
  3. 11 0
      app/router/car_show.js
  4. 1 1
      app/schedule/carshow.js
  5. 71 0
      app/service/car_show.js

+ 22 - 0
app/controller/car_show.js

@@ -0,0 +1,22 @@
+'use strict';
+const Controller = require('egg').Controller;
+const { CrudController } = require('naf-framework-mongoose/lib/controller');
+
+// 车奖
+class Car_showController extends Controller {
+  constructor(ctx) {
+    super(ctx);
+    this.service = this.ctx.service.carShow;
+  }
+
+  async checkUser() {
+    await this.service.checkUser(this.ctx.query);
+    this.ctx.ok();
+  }
+
+  async getCarShow() {
+    await this.service.getCarShow(this.ctx.query);
+    this.ctx.ok();
+  }
+}
+module.exports = CrudController(Car_showController, {});

+ 1 - 0
app/router.js

@@ -16,6 +16,7 @@ module.exports = app => {
   router.get(`${prefix}/wxpay/auth`, controller.wxpay.toAuth);
   router.get(`${prefix}/wxpay/cash`, controller.wxpay.cash);
   require('./router/card')(app); // 办卡
+  require('./router/car_show')(app); // 车奖
   require('./router/cash')(app); // 提现
   require('./router/record')(app); // 记录
   require('./router/count')(app); // 统计

+ 11 - 0
app/router/car_show.js

@@ -0,0 +1,11 @@
+'use strict';
+/**
+ * @param {Egg.Application} app - egg application
+ */
+module.exports = app => {
+  const prefix = '/api/htyd';
+  const index = 'carShow';
+  const { router, controller } = app;
+  router.get(index, `${prefix}/${index}/check`, controller[index].checkUser);
+  router.get(index, `${prefix}/${index}/getShow`, controller[index].getCarShow);
+};

+ 1 - 1
app/schedule/carshow.js

@@ -15,7 +15,7 @@ class CheckCheck extends Subscription {
 
   // subscribe 是真正定时任务执行时被运行的函数
   async subscribe() {
-    await this.ctx.service.card.checkCarshow();
+    // await this.ctx.service.card.checkCarshow();
   }
 }
 module.exports = CheckCheck;

+ 71 - 0
app/service/car_show.js

@@ -0,0 +1,71 @@
+'use strict';
+const { CrudService } = require('naf-framework-mongoose/lib/service');
+const { BusinessError, ErrorCode } = require('naf-core').Error;
+const _ = require('lodash');
+const assert = require('assert');
+
+// 车奖
+class Car_showService extends CrudService {
+  constructor(ctx) {
+    super(ctx);
+    this.record = this.ctx.model.Record;
+    this.model = this.ctx.model.Card;
+    /**
+     * @constant Number 车奖的积分 default:131400
+     */
+    this.car_point = 131400;
+    /**
+     * @constant Number 车奖的等级界限,前x级不是能获得车奖 default:4
+     */
+    this.car_show_limit_level = 4;
+    /**
+     * @constant Number 车奖的B梯队等级界限 default:4
+     */
+    this.car_show_b_limit_level = 4;
+    /**
+     * @constant Number 车奖的B梯队等级界限的人数 default:5
+     */
+    this.car_show_b_limit_person = 5;
+  }
+
+  /**
+   * 检查用户是否符合车奖条件
+   * @param {String} {id} 用户id
+   */
+  async checkUser({ id }) {
+    assert(id, '缺少用户信息');
+    const user = await this.model.findById(id);
+    if (!user) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到用户信息');
+    const { car_show, mobile, level } = user;
+    if (car_show) throw new BusinessError(ErrorCode.BUSINESS, '您已领取车奖,不能重复领取');
+    if (level < this.car_show_limit_level) throw new BusinessError(ErrorCode.BUSINESS, '未到达领取车奖等级');
+    const b = await this.model.count({ r_mobile: mobile, level: { $gte: this.car_show_b_limit_level } });
+    if (b < this.car_show_b_limit_person) throw new BusinessError(ErrorCode.BUSINESS, '该用户下满足条件的人数少于5人');
+    return true;
+  }
+
+  /**
+   * 用户领取车奖
+   * @param {String} {id} 用户id
+   */
+  async getCarShow({ id }) {
+    assert(id, '缺少用户信息');
+    const r = await this.checkUser({ id });
+    if (!r) throw new BusinessError(ErrorCode.BUSINESS, '用户不满足车奖要求');
+    const user = await this.model.findById(id);
+    user.car_show = true;
+    user.points = user.points + this.car_point;
+    await user.save();
+    // 添加积分记录
+    const record = _.pick(user, [ 'mobile', 'name' ]);
+    record.points = this.car_point;
+    record.opera = '2';
+    try {
+      await this.record.create(record);
+    } catch (error) {
+      this.logger.error(`line-202-积分记录添加失败${JSON.stringify(record)}`);
+    }
+  }
+}
+
+module.exports = Car_showService;