Quellcode durchsuchen

修正节点订阅

zhangjiangbin vor 8 Jahren
Ursprung
Commit
e292e3bec0

+ 8 - 16
app/Console/Commands/AutoDecGoodsTrafficJob.php

@@ -20,28 +20,20 @@ class AutoDecGoodsTrafficJob extends Command
 
     public function handle()
     {
-        $orderGoods = OrderGoods::query()->where('is_expire', 0)->get();
+        $orderGoods = OrderGoods::query()->with(['user', 'goods'])->where('is_expire', 0)->get();
         foreach ($orderGoods as $og) {
-            $goods = Goods::query()->where('id', $og->goods_id)->first();
-            if (empty($goods)) {
+            if (empty($og->goods) || $og->goods->is_del || empty($og->user)) {
                 continue;
             }
 
-            if (date("Y-m-d H:i:s", strtotime("-" . $goods->days . " days")) >= $og->created_at) {
-                $u = User::query()->where('id', $og->user_id)->first();
-                if (empty($u)) {
-                    continue;
+            // 到期自动处理
+            if (date("Y-m-d H:i:s", strtotime("-" . $og->goods->days . " days")) >= $og->created_at) {
+                if ($og->user->transfer_enable - $og->traffic * 1048576 <= 0) {
+                    User::query()->where('id', $og->user_id)->update(['transfer_enable' => 0]);
+                } else {
+                    User::query()->where('id', $og->user_id)->decrement('transfer_enable', $og->traffic * 1048576);
                 }
 
-                // 流量包到期自动扣总流量
-                //if ($goods->type == 1) {
-                    if ($u->transfer_enable - $goods->traffic * 1048576 <= 0) {
-                        User::query()->where('id', $og->user_id)->update(['transfer_enable' => 0]);
-                    } else {
-                        User::query()->where('id', $og->user_id)->decrement('transfer_enable', $goods->traffic * 1048576);
-                    }
-                //}
-
                 OrderGoods::query()->where('id', $og->id)->update(['is_expire' => 1]);
             }
         }

+ 19 - 2
app/Console/Commands/AutoResetUserTrafficJob.php

@@ -2,6 +2,7 @@
 
 namespace App\Console\Commands;
 
+use App\Http\Models\OrderGoods;
 use Illuminate\Console\Command;
 use App\Http\Models\Config;
 use App\Http\Models\User;
@@ -30,8 +31,24 @@ class AutoResetUserTrafficJob extends Command
     public function handle()
     {
         if (self::$config['reset_traffic']) {
-            $user_ids = User::query()->where('pay_way', '<>', 0)->select(['id'])->get();
-            User::query()->whereIn('id', $user_ids)->update(['u' => 0, 'd' => 0]);
+            $userList = User::query()->where('status', '>=', 0)->where('enable', 1)->get();
+            foreach ($userList as $user) {
+                if (!$user->traffic_reset_day) {
+                    continue;
+                }
+
+                // 取出这个用户最后购买的有效套餐
+                $orderGoods = OrderGoods::query()->with(['goods' => function($q) { $q->where('type', 2); }])->where('user_id', $user->id)->where('is_expire', 0)->orderBy('id', 'desc')->first();
+                if (empty($orderGoods) || empty($orderGoods->goods)) {
+                    continue;
+                }
+
+                if ($user->traffic_reset_day == abs(date('d')) && abs(date('m')) == abs(date('m', $orderGoods->created_at))) {
+                    continue;
+                }
+
+                User::query()->where('id', $user->id)->update(['u' => 0, 'd' => 0]);
+            }
         }
 
         Log::info('定时任务:' . $this->description);

+ 1 - 1
app/Console/Commands/AutoStatisticsNodeDailyTrafficJob.php

@@ -20,7 +20,7 @@ class AutoStatisticsNodeDailyTrafficJob extends Command
 
     public function handle()
     {
-        $nodeList = SsNode::query()->where('status', 1)->get();
+        $nodeList = SsNode::query()->where('status', 1)->orderBy('id', 'asc')->get();
         foreach ($nodeList as $node) {
             $this->statisticsByNode($node->id);
         }

+ 1 - 1
app/Console/Commands/AutoStatisticsNodeHourlyTrafficJob.php

@@ -20,7 +20,7 @@ class AutoStatisticsNodeHourlyTrafficJob extends Command
 
     public function handle()
     {
-        $nodeList = SsNode::query()->where('status', 1)->get();
+        $nodeList = SsNode::query()->where('status', 1)->orderBy('id', 'asc')->get();
         foreach ($nodeList as $node) {
             $this->statisticsByNode($node->id);
         }

+ 1 - 1
app/Console/Commands/AutoStatisticsUserDailyTrafficJob.php

@@ -27,7 +27,7 @@ class AutoStatisticsUserDailyTrafficJob extends Command
             $this->statisticsByNode($user->id);
 
             // 统计每个节点产生的流量
-            $nodeList = SsNode::query()->get();
+            $nodeList = SsNode::query()->where('status', 1)->orderBy('id', 'asc')->get();
             foreach ($nodeList as $node) {
                 $this->statisticsByNode($user->id, $node->id);
             }

+ 1 - 1
app/Console/Commands/AutoStatisticsUserHourlyTrafficJob.php

@@ -27,7 +27,7 @@ class AutoStatisticsUserHourlyTrafficJob extends Command
             $this->statisticsByNode($user->id);
 
             // 统计每个节点产生的流量
-            $nodeList = SsNode::query()->get();
+            $nodeList = SsNode::query()->where('status', 1)->orderBy('id', 'asc')->get();
             foreach ($nodeList as $node) {
                 $this->statisticsByNode($user->id, $node->id);
             }

+ 9 - 9
app/Http/Controllers/AdminController.php

@@ -1002,19 +1002,19 @@ class AdminController extends BaseController
         $nodeList = SsNode::query()->paginate(10)->appends($request->except('page'));
         foreach ($nodeList as &$node) {
             // 生成ssr scheme
-            $obfs_param = $node->single ? '' : base64_encode($user->obfs_param);
-            $protocol_param = $node->single ? base64_encode($user->port . ':' . $user->passwd) : base64_encode($user->protocol_param);
+            $obfs_param = $node->single ? '' : $user->obfs_param;
+            $protocol_param = $node->single ? $user->port . ':' . $user->passwd : $user->protocol_param;
 
             $ssr_str = '';
             $ssr_str .= $node->server . ':' . ($node->single ? $node->single_port : $user->port);
             $ssr_str .= ':' . ($node->single ? $node->single_protocol : $user->protocol) . ':' . ($node->single ? $node->single_method : $user->method);
-            $ssr_str .= ':' . ($node->single ? 'tls1.2_ticket_auth' : $user->obfs) . ':' . ($node->single ? base64_encode($node->single_passwd) : base64_encode($user->passwd));
-            $ssr_str .= '/?obfsparam=' . $obfs_param;
-            $ssr_str .= '&protoparam=' . $protocol_param;
-            $ssr_str .= '&remarks=' . base64_encode($node->name);
-            $ssr_str .= '&group=' . base64_encode('节点');
-            //$ssr_str .= '&udpport=0';
-            //$ssr_str .= '&uot=0';
+            $ssr_str .= ':' . ($node->single ? 'tls1.2_ticket_auth' : $user->obfs) . ':' . ($node->single ? $this->base64url_encode($node->single_passwd) : $this->base64url_encode($user->passwd));
+            $ssr_str .= '/?obfsparam=' . $this->base64url_encode($obfs_param);
+            $ssr_str .= '&protoparam=' . $this->base64url_encode($protocol_param);
+            $ssr_str .= '&remarks=' . $this->base64url_encode($node->name);
+            $ssr_str .= '&group=' . $this->base64url_encode('节点');
+            $ssr_str .= '&udpport=0';
+            $ssr_str .= '&uot=0';
             $ssr_str = $this->base64url_encode($ssr_str);
             $ssr_scheme = 'ssr://' . $ssr_str;
 

+ 9 - 7
app/Http/Controllers/SubscribeController.php

@@ -64,18 +64,20 @@ class SubscribeController extends BaseController
         $nodeList = SsNode::query()->where('status', 1)->whereIn('id', $node_ids)->get();
         $scheme = self::$config['subscribe_max'] > 0 ? 'MAX=' . self::$config['subscribe_max'] . "\n" : '';
         foreach ($nodeList as $node) {
-            $obfs_param = $node->single ? '' : base64_encode($user->obfs_param);
-            $protocol_param = $node->single ? base64_encode($user->port . ':' . $user->passwd) : base64_encode($user->protocol_param);
+            $obfs_param = $node->single ? '' : $user->obfs_param;
+            $protocol_param = $node->single ? $user->port . ':' . $user->passwd : $user->protocol_param;
 
             // 生成ssr scheme
             $ssr_str = '';
             $ssr_str .= $node->server . ':' . ($node->single ? $node->single_port : $user->port);
             $ssr_str .= ':' . ($node->single ? $node->single_protocol : $user->protocol) . ':' . ($node->single ? $node->single_method : $user->method);
-            $ssr_str .= ':' . ($node->single ? 'tls1.2_ticket_auth' : $user->obfs) . ':' . base64_encode($user->passwd);
-            $ssr_str .= '/?obfsparam=' . $obfs_param;
-            $ssr_str .= '&protoparam=' . $protocol_param;
-            $ssr_str .= '&remarks=' . base64_encode($node->name);
-            $ssr_str .= '&group=' . base64_encode('VPN');
+            $ssr_str .= ':' . ($node->single ? 'tls1.2_ticket_auth' : $user->obfs) . ':' . $this->base64url_encode($user->passwd);
+            $ssr_str .= '/?obfsparam=' . $this->base64url_encode($obfs_param);
+            $ssr_str .= '&protoparam=' . $this->base64url_encode($protocol_param);
+            $ssr_str .= '&remarks=' . $this->base64url_encode($node->name);
+            $ssr_str .= '&group=' . $this->base64url_encode('VPN');
+            $ssr_str .= '&udpport=0';
+            $ssr_str .= '&uot=0';
             $ssr_str = $this->base64url_encode($ssr_str);
             $scheme .= 'ssr://' . $ssr_str . "\n";
         }

+ 13 - 12
app/Http/Controllers/UserController.php

@@ -71,19 +71,19 @@ class UserController extends BaseController
 
         foreach ($nodeList as &$node) {
             // 生成ssr scheme
-            $obfs_param = $node->single ? '' : base64_encode($user->obfs_param);
-            $protocol_param = $node->single ? base64_encode($user->port . ':' . $user->passwd) : base64_encode($user->protocol_param);
+            $obfs_param = $node->single ? '' : $user->obfs_param;
+            $protocol_param = $node->single ? $user->port . ':' . $user->passwd : $user->protocol_param;
 
             $ssr_str = '';
             $ssr_str .= $node->server . ':' . ($node->single ? $node->single_port : $user->port);
             $ssr_str .= ':' . ($node->single ? $node->single_protocol : $user->protocol) . ':' . ($node->single ? $node->single_method : $user->method);
-            $ssr_str .= ':' . ($node->single ? 'tls1.2_ticket_auth' : $user->obfs) . ':' . ($node->single ? base64_encode($node->single_passwd) : base64_encode($user->passwd));
-            $ssr_str .= '/?obfsparam=' . $obfs_param;
-            $ssr_str .= '&protoparam=' . $protocol_param;
-            $ssr_str .= '&remarks=' . base64_encode($node->name);
-            $ssr_str .= '&group=' . base64_encode('节点');
-            //$ssr_str .= '&udpport=0';
-            //$ssr_str .= '&uot=0';
+            $ssr_str .= ':' . ($node->single ? 'tls1.2_ticket_auth' : $user->obfs) . ':' . ($node->single ? $this->base64url_encode($node->single_passwd) : $this->base64url_encode($user->passwd));
+            $ssr_str .= '/?obfsparam=' . $this->base64url_encode($obfs_param);
+            $ssr_str .= '&protoparam=' . $this->base64url_encode($protocol_param);
+            $ssr_str .= '&remarks=' . $this->base64url_encode($node->name);
+            $ssr_str .= '&group=' . $this->base64url_encode('节点');
+            $ssr_str .= '&udpport=0';
+            $ssr_str .= '&uot=0';
             $ssr_str = $this->base64url_encode($ssr_str);
             $ssr_scheme = 'ssr://' . $ssr_str;
 
@@ -766,7 +766,7 @@ class UserController extends BaseController
                 $userBalanceLogObj->before = $user->balance;
                 $userBalanceLogObj->after = $user->balance - $totalPrice;
                 $userBalanceLogObj->amount = -1 * $totalPrice;
-                $userBalanceLogObj->desc = '购买流量包';
+                $userBalanceLogObj->desc = '购买服务:' . $goods->name;
                 $userBalanceLogObj->created_at = date('Y-m-d H:i:s');
                 $userBalanceLogObj->save();
 
@@ -788,8 +788,9 @@ class UserController extends BaseController
                 // 把商品的流量加到账号上
                 User::query()->where('id', $user['id'])->increment('transfer_enable', $goods->traffic * 1048576);
 
-                // 将商品的有效期加到账号上
-                User::query()->where('id', $user['id'])->update(['expire_time' => date('Y-m-d H:i:s', strtotime("+" . $goods->days . " days")), 'enable' => 1]);
+                // 将商品的有效期和流量自动重置日期加到账号上
+                $traffic_reset_day = $goods->type == 2 ? (in_array(date('d'), [29, 30, 31]) ? 28 : abs(date('d'))) : 0;
+                User::query()->where('id', $user['id'])->update(['traffic_reset_day' => $traffic_reset_day, 'expire_time' => date('Y-m-d H:i:s', strtotime("+" . $goods->days . " days")), 'enable' => 1]);
 
                 // 写入返利日志
                 if ($user->referral_uid) {

+ 4 - 0
app/Http/Models/OrderGoods.php

@@ -24,6 +24,10 @@ class OrderGoods extends Model
         'is_expire'
     ];
 
+    function user() {
+        return $this->hasOne(User::class, 'id', 'user_id');
+    }
+
     function goods() {
         return $this->hasOne(Goods::class, 'id', 'goods_id');
     }

+ 1 - 1
resources/views/admin/system.blade.php

@@ -347,7 +347,7 @@
                                                             <label for="reset_traffic" class="col-md-3 control-label">流量自动重置</label>
                                                             <div class="col-md-9">
                                                                 <input type="checkbox" class="make-switch" @if($reset_traffic) checked @endif id="reset_traffic" data-on-color="success" data-off-color="danger" data-on-text="启用" data-off-text="关闭">
-                                                                <span class="help-block"> 每月1日自动重置账号可用流量 </span>
+                                                                <span class="help-block"> 账号会按其购买套餐的日期自动重置账号可用流量 </span>
                                                             </div>
                                                         </div>
                                                         <div class="col-md-6">

+ 2 - 0
sql/db.sql

@@ -120,6 +120,7 @@ CREATE TABLE `user` (
   `reg_ip` varchar(20) NOT NULL DEFAULT '127.0.0.1' COMMENT '注册IP',
   `last_login` int(11) NOT NULL DEFAULT '0' COMMENT '最后登录时间',
   `referral_uid` int(11) NOT NULL DEFAULT '0' COMMENT '邀请人',
+  `traffic_reset_day` tinyint(4) NOT NULL DEFAULT '0' COMMENT '流量自动重置日,0表示不重置',
   `status` tinyint(4) NOT NULL DEFAULT '0' COMMENT '状态:-1-禁用、0-未激活、1-正常',
   `created_at` datetime DEFAULT NULL,
   `updated_at` datetime DEFAULT NULL,
@@ -127,6 +128,7 @@ CREATE TABLE `user` (
   UNIQUE KEY `port` (`port`)
 ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
 
+
 LOCK TABLES `user` WRITE;
 /*!40000 ALTER TABLE `user` DISABLE KEYS */;
 

+ 4 - 0
sql/update/20171202.sql

@@ -0,0 +1,4 @@
+-- 加入流量重置日
+ALTER TABLE `user`
+ADD COLUMN `traffic_reset_day`  tinyint NOT NULL DEFAULT 0 COMMENT '流量自动重置日,0表示不重置' AFTER `referral_uid`;
+