moshaorui 4 дней назад
Родитель
Сommit
58610cbeb9

+ 6 - 1
app/Distributor/Controllers/SmmPostController.php

@@ -214,6 +214,11 @@ class SmmPostController extends AdminDistController
                 return Admin::json()->error(admin_trans_label('select_send_time'));
             }
 
+            //判断是否超过发送限制
+            if (SmmUserAccount::checkAccountCanSendPost($post['account_ids'],getDistributorId()) == false) {
+                return Admin::json()->error(admin_trans_label('account_send_limit'));
+            }
+
             // 從北京時間字符串創建 Carbon 對象
             $sendTime = Carbon::createFromFormat('Y-m-d H:i:s', $post['send_time'], 'Asia/Shanghai');
 
@@ -221,7 +226,7 @@ class SmmPostController extends AdminDistController
             $send_time = $sendTime->setTimezone('UTC');
 
             //保存数据
-            SmmPost::create($post,$send_time,$image_video_url);
+            SmmPost::create($post,$send_time,$image_video_url,getDistributorId());
             // 生成发送记录
             $timer = new TimerSsmPost();
             $timer->createLog();

+ 2 - 2
app/Distributor/Controllers/SmmUserAccountController.php

@@ -46,7 +46,7 @@ class SmmUserAccountController extends AdminDistController
             $grid->disableRowSelector(); // 关键代码
             $grid->column('id')->width(80);
             $grid->column('name')->tree()->display(function ($name) {
-                $num = SmmUserAccount::returnChildCount($this->id);
+                $num = SmmUserAccount::returnChildCount($this->id,getDistributorId());
                 if ($this->parent_id == 0) {
                     if ($num > 0) {
                         return $name . '<span class="badge" style="background:#21b978;margin-left:5px">'. $num. '</span>';
@@ -168,7 +168,7 @@ class SmmUserAccountController extends AdminDistController
             abort(404);
         }
         $ssmUserAccount = new SmmUserAccount();
-        $ssmUserAccount->deleteAccount($id);
+        $ssmUserAccount->deleteAccount($id,getDistributorId());
         header('Content-Type: application/json');
         echo json_encode(['status' => true, 'data' => ['alert' => true,'message' => '删除成功 !']]);
         exit;

+ 2 - 2
app/Distributor/Repositories/SmmPost.php

@@ -18,7 +18,7 @@ class SmmPost extends EloquentRepository
     /*
      * 插入数据
      */
-    public static function create($post,$sendTime,$imageVideoUrl)
+    public static function create($post,$sendTime,$imageVideoUrl,$distId)
     {
         $model = new Model();
     //    $model->send_type = $post['send_type'];
@@ -28,7 +28,7 @@ class SmmPost extends EloquentRepository
         $model->account_ids = implode(',',$post['account_ids']);
         $model->image_video_url = $imageVideoUrl;
         $model->status = 0;
-        $model->dist_id = getDistributorId();
+        $model->dist_id = $distId;
         $model->backup_field1 = json_encode([
             'youtube_category' => $post['youtube_category'],
             'yutube_title' => $post['yutube_title'],

+ 21 - 5
app/Distributor/Repositories/SmmPostLog.php

@@ -74,7 +74,7 @@ class SmmPostLog extends EloquentRepository
 
     /*
      * 计算Twitter月配额,每月只能发送100个帖子,(只限发送帖子,其他接口,每个用户1个请求,15分钟内只允许发送一次)
-     *
+     * 全站统计
      */
     public static function getPostQuota($mediaName = 'Twitter')
     {
@@ -84,20 +84,36 @@ class SmmPostLog extends EloquentRepository
             // 获取当月第一天 00:00:00 的时间戳
             $startOfMonth = Carbon::now()->startOfMonth();
 
-            $count = $log->where('media_name', 'Twitter')
+            $rows = $log->where('media_name', 'Twitter')
                 ->where('created_at', '>=', $startOfMonth->format('Y-m-d H:i:s'))
-                ->count();
-
+                ->get();
+            $count = 0;
+            foreach ($rows as $row) {
+                if ($row->status == 0) {
+                    $count++;
+                } else {
+                    $count = $count + $row->request_count;
+                }
+            }
             return 100 - $count;
 
         } elseif ($mediaName === 'YouTube') {
             // 获取当天 00:00:00 的时间戳
             $startOfDay = Carbon::now()->startOfDay();
 
-            $count = $log->where('media_name', 'YouTube')
+            $rows = $log->where('media_name', 'YouTube')
                 ->where('created_at', '>=', $startOfDay->format('Y-m-d H:i:s'))
                 ->count();
 
+            $count = 0;
+            foreach ($rows as $row) {
+                if ($row->status == 0) {
+                    $count++;
+                } else {
+                    $count = $count + $row->request_count;
+                }
+            }
+
             // 计算可用配额(向下取整)
             $quota = floor((10000 - 1600 * $count) / 1600);
             return max($quota, 0); // 保证最小返回0

+ 37 - 9
app/Distributor/Repositories/SmmUserAccount.php

@@ -72,17 +72,17 @@ class SmmUserAccount extends EloquentRepository
 
 
     /*
-     * 查找所有用户账号(只显示有效的账号)
+     * 查找当前用户下所有用户账号(只显示有效的账号)
      */
     public static function getUserAccounts()
     {
         $model = new Model();
-        $accounts = $model->where('dist_id','>', 0)->where('parent_id','>',0)->where('expires_at','>', Carbon::now())->orderBy('parent_id', 'asc')->get();
+        $accounts = $model->where('dist_id','=', getDistributorId())->where('parent_id','>',0)->where('expires_at','>', Carbon::now())->orderBy('parent_id', 'asc')->get();
         return $accounts;
     }
 
     /*
-     *  查找所有用户账号
+     *  查找所有用户账号 (全站账号)
      */
     public static function getAllYouTubeUserAccounts()
     {
@@ -114,10 +114,10 @@ class SmmUserAccount extends EloquentRepository
     /*
      * 返回子账号数量
      */
-    public static function returnChildCount($id)
+    public static function returnChildCount($id, $distId)
     {
         $model = new Model();
-        $count = $model->where('parent_id', $id)->where('expires_at','>', Carbon::now())->count();
+        $count = $model->where('parent_id', $id)->where('dist_id', $distId)->where('expires_at','>', Carbon::now())->count();
         return $count;
     }
 
@@ -133,11 +133,11 @@ class SmmUserAccount extends EloquentRepository
 
 
 
-    public static function deleteAccount($id)
+    public static function deleteAccount($id, $distId)
     {
         $model = new Model();
         $account = $model->find($id);
-        if ($account && $account->dist_id == getDistributorId() && $account->parent_id > 0) {
+        if ($account && $account->dist_id == $distId && $account->parent_id > 0) {
             $account->delete();
             return true;
         }
@@ -147,11 +147,39 @@ class SmmUserAccount extends EloquentRepository
      /*
       * 查找用户IDS中有无YouTube的帐号
       */
-    public static function findYoutubeAccount($ids)
+    public static function findYoutubeAccount($ids, $distId)
     {
         $model = new Model();
         $youTubeRs = $model->where('name', 'YouTube')->first();
-        $result = $model->where('parent_id', $youTubeRs->id)->whereIn('id', $ids)->get();
+        $result = $model->where('parent_id', $youTubeRs->id)->whereIn('id', $ids)->where('dist_id', $distId)->get();
         return $result;
     }
+
+    /*
+     * 判断用户是否可以发送POST
+     * 根据IDS查找用户账号,找到是否超
+     */
+
+    public static function checkAccountCanSendPost($ids, $distId) {
+        $model = new Model();
+        $result = $model->whereIn('id', $ids)->where('dist_id', $distId)->get();
+        $youTubeCount = 0;
+        $twitterCount = 0;
+        foreach ($result as $row) {
+            if ($row->getParent->name == 'YouTube') {
+                $youTubeCount++;
+            }
+            if ($row->getParent->name == 'Twitter') {
+                $twitterCount++;
+            }
+        }
+        if (SmmPostLog::getPostQuota('YouTube') < $youTubeCount) {
+            return false;
+        }
+        if (SmmPostLog::getPostQuota('Twitter') < $twitterCount) {
+            return false;
+        }
+        return true;
+    }
+
 }

+ 1 - 0
lang/en/global.php

@@ -257,6 +257,7 @@ return [
         'yutube_title_limit' => 'The length of the YouTube title exceeds the limit.',
         'post_message_limit' => 'The length of the post exceeds the limit.',
         'post_message_help' => 'Tweets are limited to 280 characters and will be automatically intercepted if the limit is exceeded.',
+        'account_send_limit' => 'The number of account sendings has reached the limit.',
     ],
     'options' => [
         //

+ 1 - 0
lang/zh_CN/global.php

@@ -268,6 +268,7 @@ return [
         'yutube_title_limit'   => 'Youtube标题长度超过限制',
         'post_message_limit'    => '帖子内容长度超过限制',
         'post_message_help' => '推特仅可输入 280 字符,超限系统将自动截取。',
+        'account_send_limit'    => '帐号发送次数已达上限',
     ],
     'options' => [
         //