Просмотр исходного кода

Weenier 168otc项目部署 0630

wesmiler 3 лет назад
Родитель
Сommit
0cdfabce9b

+ 74 - 0
app/Http/Controllers/Admin/AdvertOrderController.php

@@ -76,4 +76,78 @@ class AdvertOrderController extends Backend
         return $message;
     }
 
+    /**
+     * 购买广告(交易员)
+     * @return array
+     */
+    public function buy()
+    {
+        $params = request()->post();
+        if(!AdvertOrderService::make()->buy($this->userInfo['user_id'], $params)){
+            return returnJson(AdvertOrderService::make()->getError(), false);
+        }else{
+            return returnJson(AdvertOrderService::make()->getError(), true);
+        }
+    }
+
+    /**
+     * 出售广告(交易员)
+     * @return array
+     */
+    public function sell()
+    {
+        return returnJson(1002);
+    }
+
+    /**
+     * 确认付款(交易员的客户订单)
+     * @return array
+     */
+    public function pay()
+    {
+        return returnJson(1002);
+    }
+
+    /**
+     * 确认付款(交易员的个人订单)
+     * @return array
+     */
+    public function businessPay()
+    {
+        return returnJson(1002);
+    }
+
+    /**
+     * 确认收款(交易员客户订单)
+     * @return array
+     */
+    public function collection()
+    {
+        return returnJson(1002);
+    }
+
+    /**
+     * 确认收款(交易员的个人订单)
+     * @return array
+     */
+    public function businessCollection()
+    {
+        return returnJson(1002);
+    }
+
+    /**
+     * 订单异常处理(交易员申请或平台处理)
+     */
+    public function exception()
+    {
+        return returnJson(1002);
+    }
+
+    /**
+     * 订单取消(交易员或平台操作)
+     */
+    public function cancel()
+    {
+
+    }
 }

+ 24 - 0
app/Http/Controllers/Admin/IndexController.php

@@ -245,4 +245,28 @@ class IndexController extends Backend
         }
         return returnJson(MESSAGE_OK, true, is_array($datas)? $datas : []);
     }
+
+    /**
+     * 后台手动归集总钱包
+     * @return array
+     * @throws \Tron\Exceptions\TransactionException
+     * @throws \Tron\Exceptions\TronErrorException
+     */
+    public function trigger()
+    {
+        $type = request()->post('type',1);
+        if($type == 1){
+            if($result = UsdtWalletService::make()->usdtTrcTrigger(true)){
+                return showJson(1002, true, $result);
+            }else{
+                return showJson(UsdtWalletService::make()->getError(), false);
+            }
+        }else{
+            if($result = UsdtWalletService::make()->usdtErcTrigger(true)){
+                return showJson(1002, true, $result);
+            }else{
+                return showJson(UsdtWalletService::make()->getError(), false);
+            }
+        }
+    }
 }

+ 41 - 0
app/Http/Controllers/Api/TaskController.php

@@ -170,6 +170,47 @@ class TaskController extends webApp
         }
     }
 
+    /**
+     * 自动归集TRC总钱包
+     * @return array
+     * @throws \Tron\Exceptions\TransactionException
+     * @throws \Tron\Exceptions\TronErrorException
+     */
+    public function catchTrcTruggerByOtc()
+    {
+        $key = md5('OTC');
+        $checkKey = request()->get('key');
+        if($checkKey != $key){
+            return showJson(1003, false);
+        }
+
+        if($result = UsdtWalletService::make()->usdtTrcTrigger()){
+            return showJson(1002, true, $result);
+        }else{
+            return showJson(UsdtWalletService::make()->getError(), false);
+        }
+    }
+
+    /**
+     * 自动归集ERC总钱包
+     * @return array
+     * @throws \Tron\Exceptions\TransactionException
+     * @throws \Tron\Exceptions\TronErrorException
+     */
+    public function catchErcTruggerByOtc()
+    {
+        $key = md5('OTC');
+        $checkKey = request()->get('key');
+        if($checkKey != $key){
+            return showJson(1003, false);
+        }
+
+        if($result = UsdtWalletService::make()->usdtErcTrigger()){
+            return showJson(1002, true, $result);
+        }else{
+            return showJson(UsdtWalletService::make()->getError(), false);
+        }
+    }
 
     /**
      * 自动取消派单订单任务

+ 8 - 10
app/Services/Common/AdvertOrderService.php

@@ -166,10 +166,6 @@ class AdvertOrderService extends BaseService
         // 验证参数
         $config = \App\Services\ConfigService::make()->getConfigOptionByGroup(5);
         $tradeOpen = isset($config['trade_usdt_open']) ? $config['trade_usdt_open'] : 0;
-        $tradeMinNum = isset($config['trade_min_num']) ? $config['trade_min_num'] : 0;
-        $tradeMaxNum = isset($config['trade_max_num']) ? $config['trade_max_num'] : 0;
-        $trademinMoney = isset($config['trade_min_money']) ? $config['trade_min_money'] : 0;
-        $tradeMaxMoney = isset($config['trade_max_money']) ? $config['trade_max_money'] : 0;
         $tradePrice = isset($config['usdt_buy_price']) ? $config['usdt_buy_price'] : 0;
         $tradeLimitNum = isset($config['trade_no_catch']) ? $config['trade_no_catch'] : 0;
 
@@ -194,24 +190,26 @@ class AdvertOrderService extends BaseService
             return false;
         }
 
-        // 验证数量或金额
+        // 浮动价格计算
         if ($priceType == 2) {
             $price = floatval($tradePrice + $price);
         }
 
+        // 总价
         $total = floatval($price * $num);
+        if($total<=0){
+            $this->error = '4002';
+            return false;
+        }
 
-        // 用户信息
+        // 购买用户信息
         $userInfo = MemberService::make()->getInfo($userId);
         $status = isset($userInfo['status']) ? $userInfo['status'] : 0;
-        $idcardCheck = isset($userInfo['idcard_check']) ? $userInfo['idcard_check'] : 0;
-        $username = isset($userInfo['username']) && $userInfo['username'] ? format_account($userInfo['username']) : '';
-        if ($status != 1) {
+        if (empty($userInfo) || $status != 1) {
             $this->error = '2009';
             return false;
         }
 
-
         // 未处理订单
         $noCatchOrder = $this->checkOrderNoCatch($userId, 1);
         if ($tradeLimitNum > 0 && $noCatchOrder >= $tradeLimitNum) {

+ 152 - 3
app/Services/UsdtWalletService.php

@@ -96,7 +96,6 @@ class UsdtWalletService extends BaseService
         }
     }
 
-
     /**
      * trx 转账
      * @param $to 进账账户
@@ -357,7 +356,6 @@ class UsdtWalletService extends BaseService
         }
     }
 
-
     /**
      * 监听USDT-TRC2.0转账记录并进账
      * @param $userId 用户ID
@@ -752,7 +750,6 @@ class UsdtWalletService extends BaseService
         return ['wif' => $wif, 'hexAddress' => $this->getHexAddress($address), 'address' => $address];
     }
 
-
     /**
      * 获取HASH钱包地址
      * @param $address 钱包地址
@@ -825,4 +822,156 @@ class UsdtWalletService extends BaseService
 
     }
 
+    /**
+     * usdt-trc2.0 归集
+     * @throws \Tron\Exceptions\TransactionException
+     * @throws \Tron\Exceptions\TronErrorException
+     */
+    public function usdtErcTrigger($force = false)
+    {
+
+        return false;
+
+        if (empty($this->config['tron_api_url'])) {
+            $this->error = '2206';
+            return false;
+        }
+
+        $headers = ["TRON-PRO-API-KEY" => $this->config['tron_api_key']];
+        $api = new Api(new Client(['base_uri' => $this->config['tron_api_url'], 'headers' => $headers]));
+        $trcWallet = new TRC20($api, ['contract_address' => $this->config['tron_contract_address'], 'decimals' => 6]);
+        $trxWallet = new TRX($api, ['contract_address' => $this->config['tron_contract_address'], 'decimals' => 6]);
+
+        // 获取钱包参数
+        try {
+
+            // 用收账钱包归集
+            $otcAddress = ConfigService::make()->getConfigByCode('trc_address');
+            $otcAddressPrivate = ConfigService::make()->getConfigByCode('trc_private_key');
+            // 出账手续费钱包
+            $otcOutAddress = ConfigService::make()->getConfigByCode('trc_out_address');
+            $otcOutAddressPrivate = ConfigService::make()->getConfigByCode('trc_out_private_key');
+            $triggerMin = ConfigService::make()->getConfigByCode('trade_trigger_min');
+            $triggerTime = ConfigService::make()->getConfigByCode('trade_trigger_time');
+            $triggerFree = ConfigService::make()->getConfigByCode('trade_trigger_free');
+            $triggerFree = $triggerFree > 0 ? $triggerFree : 8;
+            $triggerMin = $triggerMin > 0 ? $triggerMin : 0.1;
+            $triggerTime = $triggerTime > 0 ? $triggerTime * 86400 : 86400;
+            if (empty($otcAddress) || empty($otcAddressPrivate)) {
+                $this->error = '2203';
+                return false;
+            }
+
+            // 出账钱包
+            if (empty($otcOutAddress) || empty($otcOutAddressPrivate)) {
+                $this->error = '2203';
+                return false;
+            }
+
+            $page = RedisService::get("caches:wallet:transferPage");
+            $page = $page ? $page : 1;
+
+            // 归集时间段为凌晨0点-5点
+            if ((date('H:i') >= '05:00') && !$force) {
+                $this->error = '不在归集时间段';
+                return false;
+            }
+
+            if (RedisService::get("caches:wallet:triggerLock:{$page}")) {
+                $this->error = '不要频繁操作,30秒后重试';
+                return false;
+            }
+
+            // 上锁
+            RedisService::set("caches:wallet:triggerLock:{$page}", 1, rand(10, 30));
+            $addrList = MemberService::make()->getTriggerAddressList($triggerMin, $page, 200);
+            if (empty($addrList)) {
+                RedisService::set("caches:wallet:transferPage", 1, 600);
+                $this->error = '1019';
+                return false;
+            }
+
+
+            // 平台钱包地址
+            $count = 0;
+            $failedCount = 0;
+            $tron = new Tron();
+            $tron->setAddress($otcAddress);
+            $otcAddress = $tron->getAddress();
+            $otcAddressData = new \Tron\Address($otcAddress['base58'], $otcAddressPrivate, $otcAddress['hex']);
+
+            // 平台出账钱包地址
+            $tron->setAddress($otcOutAddress);
+            $otcOutAddress = $tron->getAddress();
+            $otcOutAddressData = new \Tron\Address($otcOutAddress['base58'], $otcOutAddressPrivate, $otcOutAddress['hex']);
+            $cacheKey = "caches:wallet:trigger:";
+            foreach ($addrList as $v) {
+                try {
+                    // 获取子钱包TRC-USDT余额
+                    $userId = isset($v['id']) ? $v['id'] : 0;
+                    $address = isset($v['trc_address']) ? $v['trc_address'] : '';
+                    $hexAddress = isset($v['trc_hexaddress']) ? $v['trc_hexaddress'] : '';
+                    $addressPrivate = isset($v['trc_wif']) ? $v['trc_wif'] : '';
+                    $triggerAddress = new \Tron\Address($address, $addressPrivate, $hexAddress);
+                    // 可归集的USDT余额
+                    $triggerUsdt = $trcWallet->balance($triggerAddress);
+
+                    // USDT 余额低于归集金额,则不归集
+                    if ($triggerUsdt < $triggerMin) {
+                        $failedCount++;
+                        $error = ['data' => $v, 'usdt' => $triggerUsdt, 'triggerMin' => $triggerMin, 'error' => '用户余额不足归集', 'date' => date('Y-m-d H:i:s')];
+                        RedisService::set($cacheKey . "U_{$userId}:error", $error, 7200);
+                        continue;
+                    }
+
+                    // 获取子钱包TRX余额
+                    $triggerTrx = $trxWallet->balance($triggerAddress);
+
+                    // 获取平台出账钱包TRX余额
+                    $otcOutTrxTotal = $trxWallet->balance($otcOutAddressData);
+
+                    // 如果子钱包和平台钱包TRX余额不足手续费,则不归集
+                    if ($triggerTrx < $triggerFree && $otcOutTrxTotal < $triggerFree) {
+                        $failedCount++;
+                        $error = ['data' => $v, 'usdt' => $triggerUsdt, 'triggerMin' => $triggerMin, 'triggerTrx' => $triggerTrx, 'otcTrx' => $otcOutTrxTotal, 'error' => '平台钱包手续费不足', 'date' => date('Y-m-d H:i:s')];
+                        RedisService::set($cacheKey . "U_{$userId}:error", $error, 7200);
+                        continue;
+                    }
+
+                    // 如果子钱包TRX不足手续费8个,平台TRX充足则自动充值后下次调用时归集
+                    if ($triggerTrx < $triggerFree) {
+                        $failedCount++;
+                        $result = $this->trxTransfer($address, $triggerFree);
+                        $error = ['data' => $v, 'usdt' => $triggerUsdt, 'triggerMin' => $triggerMin, 'triggerTrx' => $triggerTrx, 'transfer' => $result, 'error' => '归集钱包手续费不足,先充值', 'date' => date('Y-m-d H:i:s')];
+                        RedisService::set($cacheKey . "U_{$userId}:catch", $error, 7200);
+                        continue;
+                    }
+
+                    // 满足归集条件处理
+                    $result = $trcWallet->transfer($triggerAddress, $otcAddressData, $triggerUsdt);
+                    RedisService::set($cacheKey . "U_{$userId}:result", ['data' => $v, 'result' => $result, 'msg' => '归集已提交', 'date' => date('Y-m-d H:i:s')], 7200);
+                    $count++;
+
+                } catch (\Exception $exception) {
+                    $failedCount++;
+                    RedisService::set($cacheKey . "U_{$userId}:exception", ['data' => $v, 'msg' => $exception->getMessage(), 'date' => date('Y-m-d H:i:s')], 7200);
+                }
+
+            }
+
+            // 超出分页数,下次处理下一页
+            if (count($addrList) >= 200) {
+                RedisService::set("caches:wallet:transferPage", $page + 1, 600);
+            }
+            if ($count > 0) {
+                return ['success' => $count, 'fail' => $failedCount];
+            } else {
+                $this->error = 1021;
+                return false;
+            }
+        } catch (\Exception $exception) {
+            $this->error = $exception->getMessage();
+            return false;
+        }
+    }
 }