doge hai 3 días
pai
achega
44128c6f40
Modificáronse 2 ficheiros con 102 adicións e 24 borrados
  1. 2 2
      app/Http/Controllers/admin/Sync.php
  2. 100 22
      app/Services/CollectService.php

+ 2 - 2
app/Http/Controllers/admin/Sync.php

@@ -15,8 +15,8 @@ class Sync extends Controller
 {
     public function collect()
     {
-        CollectService::syncCollectStay();
-        return $this->success();
+        $result = CollectService::syncCollectStay();
+        return $this->success($result);
     }
 
     public function recharge()

+ 100 - 22
app/Services/CollectService.php

@@ -8,6 +8,7 @@ use App\Models\Collect;
 use Illuminate\Support\Facades\DB;
 use Illuminate\Support\Collection;
 use Illuminate\Support\Facades\Cache;
+use Illuminate\Support\Facades\Log;
 use App\Helpers\TronHelper;
 use App\Services\WalletService;
 
@@ -141,35 +142,112 @@ class CollectService extends BaseService
      */    
     public static function syncCollectStay()
     {
+        $result = [
+            'threshold' => self::$THRESHOLD,
+            'to_address' => null,
+            'pending_count' => 0,
+            'handled_count' => 0,
+            'success_count' => 0,
+            'fail_count' => 0,
+            'items' => [],
+        ];
+
         $to_address = self::getUsdtAddress();   // 转账的接收地址
         $trx_private_key = self::getTrxPrivateKey(); // 获取TRX能量的秘钥
-        if($to_address && $trx_private_key){
-            $list = self::findAll(['status' => self::model()::STATUS_STAY ,'amount' => self::$THRESHOLD]);
-            foreach($list as $k => $v){
-                $data = [];
-                $wallets = WalletService::findOne(['address' => $v['from_address']]);
-                $privateKey = $wallets['private_key'];
-
-                $trxBalance = TronHelper::getTrxBalance($v['from_address']);
-                if($trxBalance < 10){
-                    TronHelper::sendTrx($trx_private_key,$v['from_address'],10);
-                }
-
-                $transferResult = TronHelper::transferUSDT($privateKey,$to_address,$v['amount']);
-               
-                $data['to_address'] = $to_address;
-                if($transferResult['success']){
-                    $data['txid'] = $transferResult['txid'];
-                    $data['remark'] = 'success';
-                    $data['status'] = self::model()::STATUS_START;
-                }else{
-                    $data['remark'] = $transferResult['error'];
-                }
+        $result['to_address'] = $to_address;
+
+        Log::info('syncCollectStay start', [
+            'threshold' => self::$THRESHOLD,
+            'to_address' => $to_address,
+            'has_trx_private_key' => !empty($trx_private_key),
+        ]);
+
+        if (!$to_address || !$trx_private_key) {
+            $result['message'] = '归集配置不完整';
+            Log::warning('syncCollectStay skipped: missing config', [
+                'to_address' => $to_address,
+                'has_trx_private_key' => !empty($trx_private_key),
+            ]);
+            return $result;
+        }
+
+        $list = self::findAll(['status' => self::model()::STATUS_STAY ,'amount' => self::$THRESHOLD]);
+        $result['pending_count'] = $list->count();
+
+        if ($list->isEmpty()) {
+            $result['message'] = '没有待归集记录';
+            Log::info('syncCollectStay finished: no pending collects', $result);
+            return $result;
+        }
+
+        foreach($list as $k => $v){
+            $item = [
+                'id' => $v['id'],
+                'from_address' => $v['from_address'],
+                'amount' => $v['amount'],
+                'status' => 'pending',
+            ];
+            $data = [];
+            $wallets = WalletService::findOne(['address' => $v['from_address']]);
+
+            if (empty($wallets) || empty($wallets['private_key'])) {
+                $item['status'] = 'wallet_not_found';
+                $item['error'] = '未找到归集钱包私钥';
+                $data['remark'] = $item['error'];
                 $data['updated_at'] = now();
                 self::model()::where(self::getWhere(['id' => $v['id']]))->update($data);
+                $result['fail_count']++;
+                $result['handled_count']++;
+                $result['items'][] = $item;
+                Log::warning('syncCollectStay wallet missing', $item);
+                continue;
             }
+
+            $privateKey = $wallets['private_key'];
+            $trxBalance = TronHelper::getTrxBalance($v['from_address']);
+            $item['trx_balance'] = $trxBalance;
+
+            if($trxBalance < 10){
+                $trxResult = TronHelper::sendTrx($trx_private_key,$v['from_address'],10);
+                $item['trx_topup_result'] = $trxResult;
+                Log::info('syncCollectStay topup trx', [
+                    'from_address' => $v['from_address'],
+                    'trx_balance' => $trxBalance,
+                    'result' => $trxResult,
+                ]);
+            }
+
+            $transferResult = TronHelper::transferUSDT($privateKey,$to_address,$v['amount']);
+            $item['transfer_result'] = $transferResult;
+
+            $data['to_address'] = $to_address;
+            if(is_array($transferResult) && !empty($transferResult['success'])){
+                $data['txid'] = $transferResult['txid'] ?? '';
+                $data['remark'] = 'success';
+                $data['status'] = self::model()::STATUS_START;
+                $item['status'] = 'success';
+                $item['txid'] = $data['txid'];
+                $result['success_count']++;
+                Log::info('syncCollectStay transfer success', $item);
+            }else{
+                $error = is_array($transferResult)
+                    ? ($transferResult['error'] ?? 'USDT归集失败')
+                    : (is_string($transferResult) ? $transferResult : 'USDT归集失败');
+                $data['remark'] = $error;
+                $item['status'] = 'failed';
+                $item['error'] = $error;
+                $result['fail_count']++;
+                Log::warning('syncCollectStay transfer failed', $item);
+            }
+            $data['updated_at'] = now();
+            self::model()::where(self::getWhere(['id' => $v['id']]))->update($data);
+            $result['handled_count']++;
+            $result['items'][] = $item;
         }
 
+        Log::info('syncCollectStay finished', $result);
+        return $result;
+
     }
 
     /**