shaogen1995 1 Minggu lalu
induk
melakukan
0e81fb4e9d
1 mengubah file dengan 63 tambahan dan 53 penghapusan
  1. 63 53
      src/controller/zChouJiang.ts

+ 63 - 53
src/controller/zChouJiang.ts

@@ -95,68 +95,78 @@ const chouJiang = {
   },
   },
   // 点击抽奖
   // 点击抽奖
   start: async (req: any, res: any) => {
   start: async (req: any, res: any) => {
-    const realName = req.body.realName;
-    // 检查抽奖剩余次数
-    const userInfo: any = await ZUser.findOne({ realName });
-    if (userInfo.num <= 0) return resSend(res, 400, '抽奖次数用完了,快去厕所偷偷撸');
-
-    if (flag) return resSend(res, 400, '别人正在强奸系统,请等待');
+    if (flag) return resSend(res, 0, '别人正在强奸系统,请等待');
     flag = true;
     flag = true;
 
 
-    // 1. 从数据库获取所有奖品
-    const allPrizes = await ZList.find({});
-
-    // 2. 等概率随机选择一个奖品
-    const randomIndex = Math.floor(Math.random() * allPrizes.length);
-    const selectedPrize = allPrizes[randomIndex];
+    try {
+      const realName = req.body.realName;
+      // 检查抽奖剩余次数
+      const userInfo: any = await ZUser.findOne({ realName });
+      if (userInfo.num <= 0) {
+        flag = false;
+        return resSend(res, 400, '抽奖次数用完了,快去厕所偷偷撸');
+      }
 
 
-    // return resSend(res, 0, '666', selectedPrize);
+      // 1. 从数据库获取所有奖品
+      const allPrizes = await ZList.find({});
 
 
-    let title = '';
-    // 3. 更新数据库中的剩余次数(如果是有限制次数的奖品)
-    if (selectedPrize.limit <= 0 || selectedPrize.limit === 999) {
-      // 安慰奖
-      title = '安慰奖';
-    } else {
-      // 不是安慰奖
-      // 使用原子操作减少剩余次数
-      const updatedPrize: any = await ZList.findOneAndUpdate(
-        { _id: selectedPrize._id, remaining: { $gt: 0 } },
-        { $inc: { remaining: -1 }, $set: { updateTime: dayjs().format('YYYY-MM-DD HH:mm:ss') } },
+      // 2. 等概率随机选择一个奖品
+      const randomIndex = Math.floor(Math.random() * allPrizes.length);
+      const selectedPrize = allPrizes[randomIndex];
 
 
-        { new: true, runValidators: true }
-      );
-      title = updatedPrize.name;
-    }
+      let title = '';
+      // 3. 更新数据库中的剩余次数(如果是有限制次数的奖品)
 
 
-    // 4.减少自己的抽奖次数
-    await ZUser.findOneAndUpdate(
-      { realName, num: { $gt: 0 } },
-      {
-        $inc: {
-          num: -1, // 减少抽奖次数
-        },
-        $set: { updateTime: dayjs().format('YYYY-MM-DD HH:mm:ss') },
-      },
-      {
-        new: true,
-        runValidators: true,
+      if (selectedPrize.remaining <= 0) {
+        // 安慰奖
+        title = '安慰奖';
+      } else {
+        // 不是安慰奖
+        // 使用原子操作减少剩余次数
+        const updatedPrize: any = await ZList.findOneAndUpdate(
+          { _id: selectedPrize._id, remaining: { $gt: 0 } },
+          { $inc: { remaining: -1 }, $set: { updateTime: dayjs().format('YYYY-MM-DD HH:mm:ss') } },
+
+          { new: true, runValidators: true }
+        );
+        title = updatedPrize.name;
       }
       }
-    );
-
-    const description = objjj[title];
 
 
-    const resTxt = `${realName}-抽到${title},${description}`;
+      // 4.减少自己的抽奖次数
+      await ZUser.findOneAndUpdate(
+        { realName, num: { $gt: 0 } },
+        {
+          $inc: {
+            num: -1, // 减少抽奖次数
+          },
+          $set: { updateTime: dayjs().format('YYYY-MM-DD HH:mm:ss') },
+        },
+        {
+          new: true,
+          runValidators: true,
+        }
+      );
 
 
-    // 增加一条日志
-    const saveObj = {
-      description: resTxt,
-      createTime: dayjs().format('YYYY-MM-DD HH:mm:ss'),
-    };
-    const newLog = new ZLog(saveObj);
-    newLog.save();
-    flag = false;
-    return resSend(res, 0, resTxt);
+      const description = objjj[title];
+
+      const resTxt = `${realName}-抽到${title},${description}`;
+
+      // 增加一条日志
+      const saveObj = {
+        description: resTxt,
+        createTime: dayjs().format('YYYY-MM-DD HH:mm:ss'),
+      };
+      const newLog = new ZLog(saveObj);
+      newLog
+        .save()
+        .then(() => (flag = false))
+        .catch((err) => (flag = false));
+
+      return resSend(res, 0, resTxt);
+    } catch (error: any) {
+      flag = false;
+      return resSend(res, 400, error.message);
+    }
   },
   },
   // 获取日志列表
   // 获取日志列表
   list: async (req: any, res: any) => {
   list: async (req: any, res: any) => {