Browse Source

1、抽奖接口逻辑实现;

Lawsun_M 1 year ago
parent
commit
7502588a17
3 changed files with 71 additions and 34 deletions
  1. 52 25
      app/controller/home/user.js
  2. 0 0
      app/service/BaseUtils.js
  3. 19 9
      app/service/shop.js

+ 52 - 25
app/controller/home/user.js

@@ -234,7 +234,7 @@ module.exports = class UserController extends shopController {
         // include: [
         //   { model: that.app.model.ProxyApplyLogs, as: 'proxyApplyLogs', attributes: [ 'verify_status' ] },
         // ],
-        attributes: [ 'user_id', 'account_name', 'nickname', 'headimgurl', 'openid', 'intergral', 'is_proxy', 'partner_id', 'is_office', 'is_family' ],
+        attributes: [ 'user_id', 'account_name', 'nickname', 'headimgurl', 'openid', 'intergral', 'is_proxy', 'partner_id', 'is_office', 'is_family', 'lucky_time' ],
         raw: true,
       });
       if (!user) throw new Error('登录失败,请检查账号密码是否正确');
@@ -326,7 +326,7 @@ module.exports = class UserController extends shopController {
     // console.log('==========inviteCode========== : ' + inviter_id);
     const options = {
       where: { openid: userInfo.openid },
-      attributes: [ 'user_id', 'account_name', 'nickname', 'headimgurl', 'openid', 'partner_id', 'is_office', 'is_family' ],
+      attributes: [ 'user_id', 'account_name', 'nickname', 'headimgurl', 'openid', 'partner_id', 'is_office', 'is_family', 'lucky_time' ],
       raw: true,
     };
     let user = await that.useModel.findOne(options);
@@ -428,7 +428,7 @@ module.exports = class UserController extends shopController {
         offset: (data.page - 1) * data.limit,
         limit: data.limit,
         where: { user_id: data.user_id },
-        order: [[ 'log_id', 'desc' ]],
+        order: [ [ 'log_id', 'desc' ] ],
         attributes: [ 'log_id', 'log_desc', 'change_time', 'money', 'type', 'inviter_id', 'inviter_name', 'inviter_img' ],
       });
       const result = await that.service.base.select(selectBean, that.app.model.UsersMoneyLogs, '查询资金明细失败,请稍候重试', true, true);
@@ -449,7 +449,7 @@ module.exports = class UserController extends shopController {
       const selectBean = await that.app.comoBean.instance(data, {
         // offset: (data.page - 1) * data.limit, limit: data.limit,
         where: { user_id: data.user_id },
-        order: [[ 'log_id', 'desc' ]],
+        order: [ [ 'log_id', 'desc' ] ],
         attributes: [ 'log_desc', 'create_time', 'commission', 'type', 'inviter_id', 'inviter_name', 'inviter_img' ],
       });
       // 2022/11/28 直接查询所有数据 不分页
@@ -470,7 +470,7 @@ module.exports = class UserController extends shopController {
       const data = await that.ctx.validate(that.userMoneyValidate, await that.ctx.getParse());
       const selectBean = await that.app.comoBean.instance(data, {
         where: { user_id: data.user_id },
-        order: [[ 'log_id', 'desc' ]],
+        order: [ [ 'log_id', 'desc' ] ],
       });
       // 2022/11/28 直接查询所有数据 不分页
       const result = await that.service.base.select(selectBean, that.app.model.DinnerCoinLogs, '查询分佣明细失败,请稍候重试', false, true);
@@ -543,7 +543,7 @@ module.exports = class UserController extends shopController {
         partner_id: { [seq.Op.in]: [ businessInfo.partner_id, 1 ] },
         expired: false,
       },
-      attributes: [[ seq.fn('sum', seq.col('account')), 'account' ]],
+      attributes: [ [ seq.fn('sum', seq.col('account')), 'account' ] ],
     });
     const result = await that.service.base.select(selectBean, that.app.model.DinnerCoins, '查询餐饮币账户余额失败,请稍候重试', false, false);
     const res = JSON.parse(JSON.stringify(result));
@@ -600,7 +600,7 @@ module.exports = class UserController extends shopController {
         create_time: { [seq.Op.lte]: endDateTime },
         account: { [seq.Op.gte]: 0 },
       },
-      attributes: [[ seq.fn('sum', seq.col('account')), 'account' ]],
+      attributes: [ [ seq.fn('sum', seq.col('account')), 'account' ] ],
     });
     const coinResult = await that.service.base.select(selectBean, that.app.model.DinnerCoinLogs, '查询商家可提现餐币金额失败,请稍候重试', false, false);
     const selectBean2 = await that.app.comoBean.instance({}, {
@@ -610,7 +610,7 @@ module.exports = class UserController extends shopController {
         account: { [seq.Op.lte]: 0 },
         type: { [seq.Op.ne]: -1 },
       },
-      attributes: [[ seq.fn('sum', seq.col('account')), 'account' ]],
+      attributes: [ [ seq.fn('sum', seq.col('account')), 'account' ] ],
     });
     const cashResult = await that.service.base.select(selectBean2, that.app.model.DinnerCoinLogs, '查询商家可提现餐币金额失败,请稍候重试', false, false);
     const coinRes = JSON.parse(JSON.stringify(coinResult));
@@ -654,7 +654,7 @@ module.exports = class UserController extends shopController {
             partner_id: { [seq.Op.in]: [ transferParams.partner_id, 1 ] },
             expired: false,
           },
-          order: [[ 'partner_id', 'desc' ], [ 'ori_partner', 'desc' ]],
+          order: [ [ 'partner_id', 'desc' ], [ 'ori_partner', 'desc' ] ],
         });
         // 2023/2/28 查询所有指定商家餐币
         const result = await that.service.base.select(selectBean, that.app.model.DinnerCoins, '查询餐饮币账户失败,请稍候重试', false, true);
@@ -859,12 +859,12 @@ module.exports = class UserController extends shopController {
     // 2022/11/28 查询7天前的所得分佣总和 以及所有时间得提现总和 再求和 即为 可提现金额
     const selectBean = await that.app.comoBean.instance({ user_id }, {
       where: { user_id, create_time: { [seq.Op.lte]: endDateTime }, commission: { [seq.Op.gte]: 0 } },
-      attributes: [ 'user_id', [ seq.fn('sum', seq.col('commission')), 'all_commission' ]],
+      attributes: [ 'user_id', [ seq.fn('sum', seq.col('commission')), 'all_commission' ] ],
     });
     const commissionResult = await that.service.base.select(selectBean, that.app.model.UsersCommissionLogs, '查询佣金失败,请稍候重试', false, false);
     const selectBean2 = await that.app.comoBean.instance({ user_id }, {
       where: { user_id, commission: { [seq.Op.lte]: 0 }, type: { [seq.Op.ne]: -1 } }, // type-1提现失败类型
-      attributes: [ 'user_id', [ seq.fn('sum', seq.col('commission')), 'all_cash' ]],
+      attributes: [ 'user_id', [ seq.fn('sum', seq.col('commission')), 'all_cash' ] ],
     });
     const cashResult = await that.service.base.select(selectBean2, that.app.model.UsersCommissionLogs, '查询佣金失败,请稍候重试', false, false);
     const commRes = JSON.parse(JSON.stringify(commissionResult));
@@ -950,7 +950,7 @@ module.exports = class UserController extends shopController {
           user_id: data.user_id,
           type: { [seq.Op.in]: [ 1, 2 ] },
         },
-        order: [[ 'type', 'asc' ]],
+        order: [ [ 'type', 'asc' ] ],
       });
       const result = await that.service.base.select(selectBean, that.app.model.UsersMoneyLogs, '新用户福利查询失败,请稍候重试', true, true);
       return that.ctx.appJson(that.app.szjcomo.appResult('SUCCESS', result, false));
@@ -967,27 +967,54 @@ module.exports = class UserController extends shopController {
     const that = this;
     try {
       const data = await that.ctx.validate(that.luckyValidate, await that.ctx.anyParse());
-      // 2023/11/17 todo:  用户今天抽奖校验
+      // 2023/11/17   用户今天是否已经抽奖
       const user = await that.useModel.findOne({
         where: { user_id: data.user_id },
       });
-
-      // 2023/11/17 todo: 发起概率抽奖 发放奖项 (红包上限)
-      const seq = that.app.Sequelize;
-      const selectBean = await that.app.comoBean.instance(data, {
-        offset: (data.page - 1) * data.limit, limit: data.limit, where: {
-          user_id: data.user_id,
-          type: { [seq.Op.in]: [ 1, 2 ] },
-        },
-        order: [[ 'type', 'asc' ]],
-      });
-      const result = await that.service.base.select(selectBean, that.app.model.UsersMoneyLogs, '新用户福利查询失败,请稍候重试', true, true);
-      return that.ctx.appJson(that.app.szjcomo.appResult('SUCCESS', result, false));
+      const isTodayLucky = await that.service.baseUtils.isToday(user.lucky_time);
+      // const isTodayLucky = false;
+      if (isTodayLucky) {
+        throw new Error('您今天已经抽奖了哦,明天再来吧!');
+      } else {
+        // 2023/11/17 : 发起概率抽奖 发放奖项 (todo 红包上限)
+        const prizes = [
+          { reward: 6, weight: 4 },
+          { reward: 1, weight: 1 },
+          { reward: 10, weight: 30 },
+          { reward: 8, weight: 5 },
+          { reward: 30, weight: 10 },
+          { reward: 20, weight: 50 }
+        ];
+        const prizeAward = await that.lottery(prizes);
+        // 2023/11/21  发放红包奖励
+        // console.log(prizeAward);
+        await that.service.shop.userMoneyAdd(user.user_id, prizeAward.reward, null, '每日抽奖红包', 0, 4);
+        return that.ctx.appJson(that.app.szjcomo.appResult('SUCCESS', prizeAward, false));
+      }
     } catch (err) {
       return that.ctx.appJson(that.app.szjcomo.appResult(err.message));
     }
   }
 
+  //权重抽奖函数
+  async lottery(prizes) {
+    let total = 0;
+    let percent;
+    //下标标记数组
+    let index = [];
+    for (let i = 0; i < prizes.length; i++) {
+      //判断元素的权重,为了实现小数权重,先将所有的值放大100倍
+      percent = 'undefined' != typeof (prizes[i].weight) ? prizes[i].weight : 0;
+      for (let j = 0; j < percent; j++) {
+        index.push(i);
+      }
+      total += percent;
+    }
+    //随机数值
+    let rand = Math.floor(Math.random() * total);
+    return prizes[index[rand]];
+  };
+
 
   /**
    * 更新用户信息

+ 0 - 0
app/utils/BaseUtils.js → app/service/BaseUtils.js


+ 19 - 9
app/service/shop.js

@@ -33,7 +33,7 @@ class ShopService extends BaseService {
   async payConfig(attributes = []) {
     const that = this;
     const res = await that.app.model.PaysConfig.findAll({
-      order: [[ 'pay_sort', 'asc' ]],
+      order: [ [ 'pay_sort', 'asc' ] ],
       attributes: [ 'pay_id', 'pay_name', 'pay_logo', 'pay_params_index', 'is_default' ].concat(attributes),
       where: { pay_status: 1 },
     });
@@ -75,7 +75,7 @@ class ShopService extends BaseService {
     const seq = that.app.Sequelize;
     const selectBean = await that.app.comoBean.instance({}, {
       where: { user_id, expired: false },
-      attributes: [[ seq.fn('sum', seq.col('account')), 'account' ]],
+      attributes: [ [ seq.fn('sum', seq.col('account')), 'account' ] ],
     });
     const res = await that.service.base.select(selectBean, that.app.model.DinnerCoins, '查询餐饮币账户余额失败,请稍候重试', false, false);
     const resultTemp = JSON.parse(JSON.stringify(result));
@@ -108,7 +108,7 @@ class ShopService extends BaseService {
    * @param transaction
    * @param log_desc
    * @param {Number} admin_id 管理员id
-   * @param {Number} type 充值类型 0管理员充值;1新用户注册奖励;2受邀注册奖励;3分享邀请新用户奖励
+   * @param {Number} type 充值类型 0管理员充值;1新用户注册奖励;2受邀注册奖励;3分享邀请新用户奖励;4每日抽奖红包
    * @param {Number} inviter_id 分享邀请者用户id
    * @param inviter_name
    * @param inviter_img
@@ -117,13 +117,23 @@ class ShopService extends BaseService {
   async userMoneyAdd(user_id, money = 0, transaction = null, log_desc = '用户余额增加'
     , admin_id = 0, type = 0, inviter_id = -1, inviter_name = '', inviter_img = '') {
     const that = this;
-    const updateBean = await that.app.comoBean.instance({
-      money: that.app.Sequelize.literal('money + ' + money),
-      update_time: that.app.szjcomo.date('Y-m-d H:i:s'),
-      admin_id,
-    }, { where: { user_id }, transaction });
+    let updateBean;
+    if (type === 4) {
+      updateBean = await that.app.comoBean.instance({
+        money: that.app.Sequelize.literal('money + ' + money),
+        update_time: that.app.szjcomo.date('Y-m-d H:i:s'),
+        lucky_time: that.app.szjcomo.date('Y-m-d H:i:s'),
+        admin_id,
+      }, { where: { user_id }, transaction });
+    } else {
+      updateBean = await that.app.comoBean.instance({
+        money: that.app.Sequelize.literal('money + ' + money),
+        update_time: that.app.szjcomo.date('Y-m-d H:i:s'),
+        admin_id,
+      }, { where: { user_id }, transaction });
+    }
     // 2022/9/27 用户余额更新 用户表szj_users
-    const result = await that.update(updateBean, that.app.model.Users, '用户余额更新失败,请稍候重试');
+    const result = await that.update(updateBean, that.app.model.Users, '用户红包余额更新失败,请稍候重试');
     // 2022/9/27 添加余额变化记录szj_users_money_logs
     await that.userMoneyLog(user_id, money, transaction, log_desc, admin_id, type, inviter_id, inviter_name, inviter_img);
     return result;