Przeglądaj źródła

refactor: checkin reward

M1Screw 1 rok temu
rodzic
commit
a735ef87da

+ 0 - 5
config/.config.example.php

@@ -54,11 +54,6 @@ $_ENV['mail_filter']        = 0;            //0: 关闭; 1: 白名单模式; 2;
 $_ENV['mail_filter_list']   = [];
 
 //已注册用户设置---------------------------------------------------------------------------------------
-#基础
-$_ENV['enable_checkin']             = true;         //是否啓用簽到功能
-$_ENV['checkinMin']                 = 1;            //用户签到最少流量 单位MB
-$_ENV['checkinMax']                 = 50;           //用户签到最多流量
-
 #高级
 $_ENV['class_expire_reset_traffic'] = 0;            //等级到期时重置为的流量值,单位GB,小于0时不重置
 $_ENV['enable_kill']                = true;         //是否允许用户注销账户

+ 30 - 0
config/settings.json

@@ -1429,6 +1429,36 @@
         "default": "0",
         "mark": "通知用户新IP登录"
     },
+    {
+        "id": null,
+        "item": "enable_checkin",
+        "value": "0",
+        "class": "feature",
+        "is_public": 1,
+        "type": "bool",
+        "default": "0",
+        "mark": "签到开关"
+    },
+    {
+        "id": null,
+        "item": "checkin_min",
+        "value": "1",
+        "class": "feature",
+        "is_public": 1,
+        "type": "int",
+        "default": "1",
+        "mark": "签到最少流量"
+    },
+    {
+        "id": null,
+        "item": "checkin_max",
+        "value": "50",
+        "class": "feature",
+        "is_public": 1,
+        "type": "int",
+        "default": "50",
+        "mark": "签到最多流量"
+    },
     {
         "id": null,
         "item": "enable_forced_replacement",

+ 35 - 0
resources/views/tabler/admin/setting/feature.tpl

@@ -36,6 +36,9 @@
                                 <li class="nav-item">
                                     <a href="#log" class="nav-link" data-bs-toggle="tab">用户日志</a>
                                 </li>
+                                <li class="nav-item">
+                                    <a href="#checkin" class="nav-link" data-bs-toggle="tab">签到</a>
+                                </li>
                             </ul>
                         </div>
                         <div class="card-body">
@@ -173,6 +176,38 @@
                                         </div>
                                     </div>
                                 </div>
+                                <div class="tab-pane" id="checkin">
+                                    <div class="card-body">
+                                        <div class="form-group mb-3 row">
+                                            <label class="form-label col-3 col-form-label">启用签到</label>
+                                            <div class="col">
+                                                <select id="enable_checkin" class="col form-select"
+                                                        value="{$settings['enable_checkin']}">
+                                                    <option value="0" {if ! $settings['enable_checkin']}selected{/if}>
+                                                        关闭
+                                                    </option>
+                                                    <option value="1" {if $settings['enable_checkin']}selected{/if}>开启
+                                                    </option>
+                                                </select>
+                                            </div>
+                                        </div>
+                                        <div class="form-group mb-3 row">
+                                            <label class="form-label col-3 col-form-label">签到最少流量(MB)</label>
+                                            <div class="col">
+                                                <input id="checkin_min" type="text" class="form-control"
+                                                       value="{$settings['checkin_min']}">
+                                            </div>
+                                        </div>
+                                        <div class="form-group mb-3 row">
+                                            <label class="form-label col-3 col-form-label">签到最多流量(MB)</label>
+                                            <div class="col">
+                                                <input id="checkin_max" type="text"
+                                                       class="form-control"
+                                                       value="{$settings['checkin_max']}">
+                                            </div>
+                                        </div>
+                                    </div>
+                                </div>
                             </div>
                         </div>
                     </div>

+ 5 - 5
resources/views/tabler/user/index.tpl

@@ -531,7 +531,7 @@
                         </div>
                     </div>
                 </div>
-                {if $config['enable_checkin']}
+                {if $public_setting['enable_checkin']}
                     <div class="col-lg-6 col-sm-12">
                         <div class="card">
                             <div class="card-stamp">
@@ -543,14 +543,14 @@
                                 <h3 class="card-title">每日签到</h3>
                                 <p>
                                     签到可领取
-                                    {if $config['checkinMin'] !== $config['checkinMax']}
+                                    {if $public_setting['checkin_min'] !== $public_setting['checkin_max']}
                                         &nbsp;
-                                        <code>{$config['checkinMin']} MB</code>
+                                        <code>{$public_setting['checkin_min']} MB</code>
-                                        <code>{$config['checkinMax']} MB</code>
+                                        <code>{$public_setting['checkin_max']} MB</code>
                                         范围内的流量
                                     {else}
-                                        <code>{$config['checkinMin']} MB</code>
+                                        <code>{$public_setting['checkin_min']} MB</code>
                                     {/if}
                                 </p>
                                 <p>

+ 3 - 0
src/Controllers/Admin/Setting/FeatureController.php

@@ -21,6 +21,9 @@ final class FeatureController extends BaseController
         'notify_new_subscribe',
         'login_log',
         'notify_new_login',
+        'enable_checkin',
+        'checkin_min',
+        'checkin_max',
     ];
 
     /**

+ 7 - 6
src/Controllers/UserController.php

@@ -13,6 +13,7 @@ use App\Models\OnlineLog;
 use App\Models\Payback;
 use App\Services\Auth;
 use App\Services\Captcha;
+use App\Services\Reward;
 use App\Services\Subscribe;
 use App\Utils\ResponseHelper;
 use App\Utils\Tools;
@@ -139,7 +140,7 @@ final class UserController extends BaseController
 
     public function checkin(ServerRequest $request, Response $response, array $args): Response|ResponseInterface
     {
-        if (! $_ENV['enable_checkin']) {
+        if (! Config::obtain('enable_checkin') || ! $this->user->isAbleToCheckin()) {
             return ResponseHelper::error($response, '暂时还不能签到');
         }
 
@@ -151,17 +152,17 @@ final class UserController extends BaseController
             }
         }
 
-        $checkin = $this->user->checkin();
+        $traffic = Reward::issueCheckinReward($this->user->id);
 
-        if (! $checkin['ok']) {
-            return ResponseHelper::error($response, (string) $checkin['msg']);
+        if (! $traffic) {
+            return ResponseHelper::error($response, '签到失败');
         }
 
         return $response->withJson([
             'ret' => 1,
-            'msg' => $checkin['msg'],
+            'msg' => '获得了 ' . $traffic . 'MB 流量',
             'data' => [
-                'last-checkin-time' => $this->user->lastCheckInTime(),
+                'last-checkin-time' => Tools::toDateTime(time()),
             ],
         ]);
     }

+ 1 - 31
src/Models/User.php

@@ -7,7 +7,6 @@ namespace App\Models;
 use App\Services\IM;
 use App\Utils\Hash;
 use App\Utils\Tools;
-use Exception;
 use GuzzleHttp\Exception\GuzzleException;
 use Illuminate\Database\Eloquent\Collection;
 use Illuminate\Database\Query\Builder;
@@ -16,7 +15,6 @@ use Telegram\Bot\Exceptions\TelegramSDKException;
 use function date;
 use function is_null;
 use function md5;
-use function random_int;
 use function round;
 use function time;
 use const PHP_EOL;
@@ -292,7 +290,7 @@ final class User extends Model
      */
     public function isAbleToCheckin(): bool
     {
-        return date('Ymd') !== date('Ymd', $this->last_check_in_time);
+        return date('Ymd') !== date('Ymd', $this->last_check_in_time) && ! $this->is_shadow_banned;
     }
 
     /**
@@ -348,34 +346,6 @@ final class User extends Model
         return $this->delete();
     }
 
-    /**
-     * 签到
-     */
-    public function checkin(): array
-    {
-        $return = [
-            'ok' => true,
-        ];
-
-        if (! $this->isAbleToCheckin() || $this->is_shadow_banned) {
-            $return['ok'] = false;
-            $return['msg'] = '签到失败,请稍后再试';
-        } else {
-            try {
-                $traffic = random_int((int) $_ENV['checkinMin'], (int) $_ENV['checkinMax']);
-            } catch (Exception $e) {
-                $traffic = 0;
-            }
-
-            $this->transfer_enable += Tools::toMB($traffic);
-            $this->last_check_in_time = time();
-            $this->save();
-            $return['msg'] = '获得了 ' . $traffic . 'MB 流量.';
-        }
-
-        return $return;
-    }
-
     public function unbindIM(): bool
     {
         $this->im_type = 0;

+ 14 - 3
src/Services/Bot/Telegram/Callback.php

@@ -11,6 +11,7 @@ use App\Models\OnlineLog;
 use App\Models\Payback;
 use App\Models\SubscribeLog;
 use App\Models\User;
+use App\Services\Reward;
 use App\Services\Subscribe;
 use App\Utils\Tools;
 use GuzzleHttp\Exception\GuzzleException;
@@ -981,10 +982,20 @@ final class Callback
      */
     public function userCheckin(): void
     {
-        $checkin = $this->user->checkin();
+        if ($this->user->isAbleToCheckin()) {
+            $traffic = Reward::issueCheckinReward($this->user->id);
+
+            if (! $traffic) {
+                $msg = '签到失败';
+            } else {
+                $msg = '获得了 ' . $traffic . 'MB 流量.';
+            }
+        } else {
+            $msg = '你今天已经签到过了';
+        }
 
         $this->answerCallbackQuery([
-            'text' => $checkin['msg'],
+            'text' => $msg,
             'show_alert' => true,
         ]);
         // 回送信息
@@ -1006,7 +1017,7 @@ final class Callback
         }
 
         $this->replyWithMessage([
-            'text' => $temp['text'] . PHP_EOL . PHP_EOL . $checkin['msg'],
+            'text' => $temp['text'] . PHP_EOL . PHP_EOL . $msg,
             'reply_to_message_id' => $this->message_id,
             'parse_mode' => 'Markdown',
             'reply_markup' => json_encode(

+ 13 - 2
src/Services/Bot/Telegram/Commands/CheckinCommand.php

@@ -6,6 +6,7 @@ namespace App\Services\Bot\Telegram\Commands;
 
 use App\Models\Config;
 use App\Services\Bot\Telegram\Message;
+use App\Services\Reward;
 use Telegram\Bot\Actions;
 use Telegram\Bot\Commands\Command;
 
@@ -64,11 +65,21 @@ final class CheckinCommand extends Command
                 ]
             );
         } else {
-            $checkin = $user->checkin();
+            if ($user->isAbleToCheckin()) {
+                $traffic = Reward::issueCheckinReward($this->user->id);
+
+                if (! $traffic) {
+                    $msg = '签到失败';
+                } else {
+                    $msg = '获得了 ' . $traffic . 'MB 流量.';
+                }
+            } else {
+                $msg = '你今天已经签到过了';
+            }
             // 回送信息
             $response = $this->replyWithMessage(
                 [
-                    'text' => $checkin['msg'],
+                    'text' => $msg,
                     'parse_mode' => 'Markdown',
                     'reply_to_message_id' => $message->getMessageId(),
                 ]

+ 33 - 0
src/Services/Reward.php

@@ -9,6 +9,9 @@ use App\Models\Payback;
 use App\Models\User;
 use App\Models\UserMoneyLog;
 use App\Utils\Tools;
+use Exception;
+use function random_int;
+use function time;
 
 final class Reward
 {
@@ -114,4 +117,34 @@ final class Reward
             }
         }
     }
+
+    public static function issueCheckinReward($user_id): int|false
+    {
+        $user = (new User())->where('id', $user_id)->first();
+
+        if ($user === null) {
+            return false;
+        }
+
+        $checkin_min = Config::obtain('checkin_min');
+        $checkin_max = Config::obtain('checkin_max');
+
+        if ($checkin_min === $checkin_max) {
+            $traffic = $checkin_min;
+        } else {
+            try {
+                $traffic = random_int($checkin_min, $checkin_max);
+            } catch (Exception $e) {
+                $traffic = 0;
+            }
+        }
+
+        if ($traffic !== 0) {
+            $user->transfer_enable += Tools::toMB($traffic);
+            $user->last_check_in_time = time();
+            $user->save();
+        }
+
+        return $traffic;
+    }
 }

+ 0 - 4
src/Services/View.php

@@ -63,10 +63,6 @@ final class View
             'appName' => $_ENV['appName'],
             'baseUrl' => $_ENV['baseUrl'],
 
-            'enable_checkin' => $_ENV['enable_checkin'],
-            'checkinMin' => $_ENV['checkinMin'],
-            'checkinMax' => $_ENV['checkinMax'],
-
             'jump_delay' => $_ENV['jump_delay'],
 
             'enable_kill' => $_ENV['enable_kill'],