lrf402788946 4 years ago
parent
commit
15ada85990

+ 38 - 0
app/controller/.carousel.js

@@ -0,0 +1,38 @@
+module.exports = {
+  create: {
+    requestBody: ['name','url','remark']
+  },
+  destory: {
+    params: ["!id"],
+    service: "delete"
+  },
+  update: {
+    params: ["!id"],
+    requestBody: ['name','url','remark']
+  },
+  show: {
+    parameters: {
+      params: ["!id"]
+    },
+    service: "fetch"
+  },
+  index: {
+    parameters: {
+      query: {
+        name: "%name%",
+        "create_time@start": "create_time@start",
+        "create_time@end": "create_time@end",
+      },
+      options: {
+        "meta.status": 0 // 默认条件
+      },
+    },
+   service: "query",
+   options: {
+     query: ["skip", "limit"],
+     sort: ["meta.createdAt"],
+     desc: true,
+     count: true,
+   },
+  },
+}

+ 13 - 0
app/controller/carousel.js

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

+ 16 - 0
app/controller/qrcode.js

@@ -0,0 +1,16 @@
+'use strict';
+const Controller = require('egg').Controller;
+const { CrudController } = require('naf-framework-mongoose/lib/controller');
+
+// 二维码
+class QrcodeController extends Controller {
+  constructor(ctx) {
+    super(ctx);
+    this.service = this.ctx.service.qrcode;
+  }
+  async index() {
+    const res = await this.service.initQRCode(this.ctx.query);
+    this.ctx.ok({ data: res });
+  }
+}
+module.exports = CrudController(QrcodeController, {});

+ 18 - 0
app/model/carousel.js

@@ -0,0 +1,18 @@
+'use strict';
+const Schema = require('mongoose').Schema;
+const moment = require('moment');
+const metaPlugin = require('naf-framework-mongoose/lib/model/meta-plugin');
+// 轮播图表
+const carousel = {
+  url: { type: String },
+  name: { type: String },
+  remark: { type: String, maxLength: 200 },
+  create_time: { type: String, default: moment().format('YYYY-MM-DD HH:mm:ss') },
+};
+const schema = new Schema(carousel, { toJSON: { virtuals: true } });
+schema.index({ id: 1 });
+schema.plugin(metaPlugin);
+module.exports = app => {
+  const { mongoose } = app;
+  return mongoose.model('Carousel', schema, 'carousel');
+};

+ 5 - 0
app/router.js

@@ -7,10 +7,15 @@ module.exports = app => {
   const { router, controller } = app;
   const prefix = '/api/htyd';
   router.get('/', controller.home.index);
+  // 二维码
+  router.get(`${prefix}/qrcode`, controller.qrcode.index);
+  // 登陆
   router.post(`${prefix}/login`, controller.login.login);
+  // 工具函数
   router.post(`${prefix}/util`, controller.util.utilMethod);
   require('./router/card')(app); // 办卡
   require('./router/cash')(app); // 提现
   require('./router/record')(app); // 记录
   require('./router/count')(app); // 统计
+  require('./router/carousel')(app); // 轮播
 };

+ 11 - 0
app/router/carousel.js

@@ -0,0 +1,11 @@
+'use strict';
+/**
+ * @param {Egg.Application} app - egg application
+ */
+module.exports = app => {
+  const prefix = '/api/htyd';
+  const index = 'carousel';
+  const { router, controller } = app;
+  router.resources(index, `${prefix}/${index}`, controller[index]); // index、create、show、destroy
+  router.post(index, `${prefix}/${index}/update/:id`, controller[index].update);
+};

+ 2 - 2
app/schedule/carshow.js

@@ -7,8 +7,8 @@ class CheckCheck extends Subscription {
   // 更改执行时间
   static get schedule() {
     return {
-      // cron: '0 0 0 * * *',
-      interval: '30s',
+      cron: '0 0 0 * * *',
+      // interval: '30s',
       type: 'worker', // 指定所有的 worker 都需要执行
     };
   }

+ 88 - 30
app/service/card.js

@@ -91,9 +91,9 @@ class CardService extends CrudService {
     }
     // 4,判断是129套餐,还是169套餐,如果是129套餐,那就不需要继续了
     if (set === '129') return user;
-    // 根据等级,找下推荐人应该加多少分
-    const { stockholder } = recommender;
-    // 重新计算推荐人用户等级
+    // 5,根据等级,找下推荐人应该加多少分
+    const { stockholder, level } = recommender;
+    // 6,重新计算推荐人用户等级
     const nlevel = await this.reComputedLevel(recommender);
     recommender.level = nlevel;
     const rank = this.getRank().find(f => f.rank === nlevel);
@@ -102,7 +102,7 @@ class CardService extends CrudService {
       return user;
     }
     let { score } = rank;
-    // 需要检查股东奖,是否需要+10
+    // 7,需要检查股东奖,是否需要+10
     if (stockholder) score = score + this.stockholder_point;
     else {
       // 去写检查是不是股东的问题
@@ -118,7 +118,7 @@ class CardService extends CrudService {
         score = score + 10;
       }
     }
-    // 加积分
+    // 8,加积分
     recommender.points = `${recommender.points + score}`;
     await recommender.save();
     const record = _.pick(recommender, [ 'mobile', 'name' ]);
@@ -128,10 +128,14 @@ class CardService extends CrudService {
     try {
       await this.record.create(record);
     } catch (error) {
-      this.logger.error(`积分记录添加失败${JSON.stringify(record)}`);
+      this.logger.error(`line-131-积分记录添加失败${JSON.stringify(record)}`);
     }
-    // TODO 添加推荐人等级检查与同级加分
-    this.toCheckRecommender(recommender._id);
+    // 9,以上为用户创建,计算推荐人等级=>计算推荐人积分=>生成记录
+    //         先算等级,避免分数基础部队
+    // 推荐人升级,进行推荐人等级检查与同级加分(原等级和现等级不同,说明升级了,需要检查下)
+    if (level !== nlevel) this.toCheckRecommender(recommender._id);
+    // 10,给推荐人的推荐人反佣金
+    this.rtrScore(recommender._id, level);
     return user;
   }
   /**
@@ -181,7 +185,7 @@ class CardService extends CrudService {
       const group_number = b.length + c;
       return group_number > this.stockholder_limit;
     } catch (error) {
-      this.ctx.logger.error(`${moment().format('YYYY-MM-DD HH:mm:ss')}-${user.name}-${user.mobile}:股东检查出错`);
+      this.ctx.logger.error(`line-188-${moment().format('YYYY-MM-DD HH:mm:ss')}-${user.name}-${user.mobile}:股东检查出错`);
       return undefined;
     }
   }
@@ -205,12 +209,12 @@ class CardService extends CrudService {
           try {
             await this.record.create(record);
           } catch (error) {
-            this.logger.error(`积分记录添加失败${JSON.stringify(record)}`);
+            this.logger.error(`line-212-积分记录添加失败${JSON.stringify(record)}`);
           }
         }
       }
     } catch (error) {
-      this.ctx.logger.error(`${moment().format('YYYY-MM-DD HH:mm:ss')}-车奖检查出错`);
+      this.ctx.logger.error(`line-217-${moment().format('YYYY-MM-DD HH:mm:ss')}-车奖检查出错`);
       return undefined;
     }
 
@@ -234,30 +238,64 @@ class CardService extends CrudService {
    * @param {String} id 该用户的推荐人id
    */
   async toCheckRecommender(id) {
-    const rec = await this.model.findById(id);
-    if (!rec) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到推荐人信息');
-    const { r_mobile, level } = rec;
-    // TODO 如果之给1次,就在这里判断是否给过,给过就不给了
-    // 查出推荐人的推荐人
-    const r_rec = await this.model.findOne({ mobile: r_mobile });
-    if (!r_rec) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到推荐人的推荐人信息');
-    const { level: rlevel } = r_rec;
-    // 推荐人等级已经计算过了,直接比较
-    if (level === rlevel) {
-      rec.points = rec.points + 20;
-      await rec.save();
-    }
-    const record = _.pick(rec, [ 'mobile', 'name' ]);
-    record.points = rec.points + 20;
-    record.opera = '同级奖励';
-    record.params = { name: user.name, mobile: user.mobile };
-      await this.record.create(record);
+    try {
+      const rec = await this.model.findById(id);
+      if (!rec) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到推荐人信息');
+      const { r_mobile, level } = rec;
+      // 查出推荐人的推荐人
+      const r_rec = await this.model.findOne({ mobile: r_mobile });
+      if (!r_rec) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到推荐人的推荐人信息');
+      const { level: rlevel } = r_rec;
+      // 推荐人等级已经计算过了,直接比较
+      if (level === rlevel) {
+        rec.points = rec.points + 20;
+        await rec.save();
+      }
+      const record = _.pick(rec, [ 'mobile', 'name' ]);
+      try {
+        record.points = 20;
+        record.opera = '同级奖励';
+        await this.record.create(record);
+      } catch (error) {
+        this.logger.error(`line-260-积分记录添加失败${JSON.stringify(record)}`);
+      }
     } catch (error) {
-      this.logger.error(`积分记录添加失败${JSON.stringify(record)}`);
+      this.logger.error(`line-263-捕获检查用户推荐人的推荐人失败:${JSON.stringify(error)}`);
     }
 
   }
 
+  /**
+   * 给该用户的推荐人的推荐人反佣金
+   * @param {String} id 该用户的推荐人id
+   * @param {Number} level 该用户的推荐人原等级,需要按这个等级去反佣
+   */
+  async rtrScore(id, level) {
+    try {
+      const rec = await this.model.findById(id);
+      if (!rec) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到推荐人信息');
+      const { r_mobile } = rec;
+      // 查出推荐人的推荐人
+      const r_rec = await this.model.findOne({ mobile: r_mobile });
+      if (!r_rec) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到推荐人的推荐人信息');
+      // r_level:推荐人的推荐人的等级
+      const { level: r_level } = r_rec;
+      const points = this.getReturns(level, r_level);
+      r_rec.points = r_rec.points + points;
+      await r_rec.save();
+      const record = _.pick(rec, [ 'mobile', 'name' ]);
+      try {
+        record.points = points;
+        record.opera = '直推下级完成业绩反佣金';
+        await this.record.create(record);
+      } catch (error) {
+        this.logger.error(`line-292-积分记录添加失败${JSON.stringify(record)}`);
+      }
+    } catch (error) {
+      this.logger.error(`line-295-捕获检查用户推荐人的推荐人失败:${JSON.stringify(error)}`);
+    }
+  }
+
   /**
    *  等级设置
    * @property rank 等级:1,业务员;2,经理;3一星经理;4,二星经理;5,三星经理;6,四星经理;
@@ -318,6 +356,26 @@ class CardService extends CrudService {
       },
     ];
   }
+  /**
+   * 根据推荐人和推荐人的推荐人等级判断,该如何反給推荐人的推荐人佣金
+   * @param {Number} level 推荐人等级
+   * @param {Number} r_level 推荐人的推荐人等级
+   */
+  getReturns(level, r_level) {
+    // 同级固定反20
+    if (level === r_level) return 20;
+    const levels = [
+      { rank: 1, 2: 100, 3: 150, 4: 200, 5: 250, 6: 300 },
+      { rank: 2, 3: 50, 4: 100, 5: 150, 6: 200 },
+      { rank: 3, 4: 50, 5: 100, 6: 150 },
+      { rank: 4, 5: 50, 6: 100 },
+      { rank: 5, 6: 50 },
+    ];
+    // 根据推荐人等级,获取价格表的指定行
+    const obj = levels.find(f => f.rank === level);
+    if (!obj) throw new BusinessError(ErrorCode.SERVICE_FAULT, '为找到推荐人适合的规则');
+    return _.get(obj, r_level);
+  }
 }
 
 module.exports = CardService;

+ 12 - 0
app/service/carousel.js

@@ -0,0 +1,12 @@
+'use strict';
+const { CrudService } = require('naf-framework-mongoose/lib/service');
+
+// 轮播图
+class CarouselService extends CrudService {
+  constructor(ctx) {
+    super(ctx, 'carousel');
+    this.model = this.ctx.model.Carousel;
+  }
+}
+
+module.exports = CarouselService;

+ 30 - 0
app/service/qrcode.js

@@ -0,0 +1,30 @@
+'use strict';
+const { CrudService } = require('naf-framework-mongoose/lib/service');
+const { BusinessError, ErrorCode } = require('naf-core').Error;
+const QRCode = require('qrcode');
+const UUID = require('uuid');
+const assert = require('assert');
+
+
+// 二维码
+class QrcodeService extends CrudService {
+  constructor(ctx) {
+    super(ctx, 'qrcode');
+    this.model = this.ctx.model.Card;
+    this.uri = 'http://www.baidu.com/';
+  }
+
+  async initQRCode({ id }) {
+    assert(id, '缺少推荐人信息');
+    const user = await this.model.findById(id);
+    if (!user) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '为找到用户信息');
+    const { mobile, name } = user;
+    const url = `${this.uri}?id=${id}&mobile=${mobile}&name=${name}`;
+    const res = await QRCode.toDataURL(url, {
+      width: 200,
+    });
+    return res;
+  }
+}
+
+module.exports = QrcodeService;

+ 2 - 1
package.json

@@ -12,7 +12,8 @@
     "jsonwebtoken": "^8.5.1",
     "lodash": "^4.17.15",
     "moment": "^2.27.0",
-    "naf-framework-mongoose": "^0.6.11"
+    "naf-framework-mongoose": "^0.6.11",
+    "qrcode": "^1.4.4"
   },
   "devDependencies": {
     "autod": "^3.0.1",