Browse Source

优化兑换

jason 6 months ago
parent
commit
b895aaa696

+ 29 - 23
application/api/controller/Ledger.php

@@ -6,17 +6,13 @@ use app\api\logic\WelfareLoginc;
 use app\common\controller\Api;
 use app\common\logic\BscApi;
 use app\common\model\LedgerFrozenChangeModel;
+use app\common\model\ProductLists;
 use app\common\model\ProductOrder;
-use app\common\model\ProductsModel;
 use app\common\model\RwaExchangeRecordModel;
 use app\common\model\UserModel;
-use app\common\model\LedgerSmhChangeModel;
-use app\common\model\LedgerTeacChangeModel;
 use app\common\model\LedgerTokenChangeModel;
-use app\common\model\LedgerUsdtChangeModel;
 use app\common\model\LedgerWalletModel;
 use app\common\model\OfflineWithdrawRecordModel;
-use fast\Action;
 use think\Log;
 use think\Env;
 use think\Db;
@@ -219,24 +215,27 @@ class Ledger extends Api
         }
 
         $welfare_config = config('welfare_rede');
-        if (!in_array($coin, $welfare_config['welfare_rede']['currency'])) {
+        if (!in_array($coin, $welfare_config['currency'])) {
             $this->error(__('Invalid parameters'));
         }
 
-        $BscApi = new BscApi($welfare_config['welfare_rede']['contract_address'][$coin]);
-        $result_coin = $BscApi->getTransactionRecordsByAddress($coin_from_address, $welfare_config['welfare_rede']['transfer_address'], 49990000);
+        $BscApi = new BscApi($welfare_config['contract_address'][$coin]);
+        $result_coin = $BscApi->getTransactionRecordsByAddress($coin_from_address, $welfare_config['transfer_address'], 49990000);
         if ($result_coin['code'] == 0) {
             $this->error($result_coin['msg']);
         }
+        Log::info($result_coin, '代币所有转入记录');
         $coin_list = [];
         foreach ($result_coin['data'] as $value) {
-            if ($value['amount'] == $welfare_config['welfare_rede']['currency_price'][$coin]) {
+            if ($value['amount'] == $welfare_config['currency_price'][$coin]) {
                 $coin_list[] = $value;
             }
         }
         if (empty($coin_list)) {
             $this->error('未识别到代币转入记录');
         }
+        Log::info($coin_list, '代币精准转入记录');
+
         $coin_data = [];
         foreach ($coin_list as $item) {
             $check_info = (new RwaExchangeRecordModel())->where('tx_hash', $item['hash'])->find();
@@ -248,21 +247,26 @@ class Ledger extends Api
         if (empty($coin_data)) {
             $this->error('未识别到新代币转入记录');
         }
+        Log::info($coin_list, '代币可用转入记录');
 
-        $BscApi = new BscApi($welfare_config['welfare_rede']['contract_address']['USDT']);
-        $result_usdt = $BscApi->getTransactionRecordsByAddress($usdt_from_address, $welfare_config['welfare_rede']['transfer_address'], 49990000);
+        $BscApi = new BscApi($welfare_config['contract_address']['USDT']);
+        $result_usdt = $BscApi->getTransactionRecordsByAddress($usdt_from_address, $welfare_config['transfer_address'], 49990000);
         if ($result_usdt['code'] == 0) {
             $this->error($result_usdt['msg']);
         }
+        Log::info($result_usdt, 'USDT所有转入记录');
+
         $usdt_list = [];
-        foreach ($usdt_list['data'] as $value) {
-            if ($value['amount'] == $welfare_config['welfare_rede']['currency_price']['USDT']) {
+        foreach ($result_usdt['data'] as $value) {
+            if ($value['amount'] == $welfare_config['currency_price']['USDT']) {
                 $usdt_list[] = $value;
             }
         }
         if (empty($usdt_list)) {
             $this->error('未识别到USDT转入记录');
         }
+        Log::info($usdt_list, 'USDT精准转入记录');
+
         $usdt_data = [];
         foreach ($usdt_list as $item) {
             $check_info = (new RwaExchangeRecordModel())->where('tx_hash', $item['hash'])->find();
@@ -274,6 +278,8 @@ class Ledger extends Api
         if (empty($usdt_data)) {
             $this->error('未识别到新USDT转入记录');
         }
+        Log::info($usdt_list, 'USDT可用入记录');
+
         $order_no = date('YmdHis') . rand(1000, 9999);
         $inster_data = [
             [
@@ -282,7 +288,7 @@ class Ledger extends Api
                 'user_id' => $this->auth->id,
                 'symbol' => $coin,
                 'amount' => $coin_data['amount'],
-                'product_id' => $welfare_config['welfare_rede']['product_id'],
+                'product_id' => $welfare_config['product_id'],
                 'from_address' => $coin_data['from'],
                 'to_address' => $coin_data['to'],
                 'status' => 200,
@@ -294,27 +300,27 @@ class Ledger extends Api
                 'user_id' => $this->auth->id,
                 'symbol' => 'USDT',
                 'amount' => $usdt_data['amount'],
-                'product_id' => $welfare_config['welfare_rede']['product_id'],
+                'product_id' => $welfare_config['product_id'],
                 'from_address' => $usdt_data['from'],
                 'to_address' => $usdt_data['to'],
                 'status' => 200,
                 'create_time' => time(),
             ]
         ];
+        Log::info($inster_data, '插入数据');
 
-        $product_id = $welfare_config['welfare_rede']['product_id'];
-        $product_info =  (new ProductsModel())->where('id', $product_id)->find();
+        $product_id = $welfare_config['product_id'];
+        $product_info =  (new ProductLists())->where('id', $product_id)->find();
         if (empty($product_info)) {
             $this->error('产品不存在');
         }
         try {
             Db::startTrans();
-            (new RwaExchangeRecordModel())->saveAll($inster_data);
-
-            $rs = WelfareLoginc::setUserProductOrder(1, false, $order_no, $product_info['price'], $product_info['product_id'], $this->auth->id, ProductOrder::Exchange);
-            if($rs && $product_info['price'] >= config('min_rwa_price')){
-                (new UserModel())->updateForRwaNum($this->auth->id, UserModel::getByParentId($user_id), 1, '+');
-            }
+            $rs = Db::name('rwa_exchange_record')->fetchSql(false)->insertAll($inster_data);
+//            dump($rs);
+//            $rs = (new RwaExchangeRecordModel())->saveAll($inster_data);
+            //发放产品
+            $rs = WelfareLoginc::setUserProductOrder(1, false, $order_no, 0, $product_info['id'], $this->auth->id, ProductOrder::Exchange);
             Db::commit();
             $this->success('ok', $order_no);
         } catch (Exception $e) {

+ 120 - 4
application/api/controller/Test.php

@@ -3,6 +3,7 @@
 
 namespace app\api\controller;
 
+use app\api\logic\WelfareLoginc;
 use app\common\logic\BscApi;
 use app\common\logic\MyBscApi;
 use app\common\logic\TeamRewards;
@@ -13,6 +14,8 @@ use app\common\model\OfflineRechargeRecordModel;
 use app\api\logic\MarketLogic;
 use app\common\model\ParametersModel;
 use app\common\model\ProductOrder;
+use app\common\model\ProductsModel;
+use app\common\model\RwaExchangeRecordModel;
 use app\common\model\UserModel;
 use app\common\model\UserPathModel;
 use app\common\model\LedgerWalletModel;
@@ -22,6 +25,7 @@ use fast\Asset;
 use fast\Http;
 use fast\RechargeStatus;
 use think\Db;
+use think\Exception;
 use think\Log;
 use think\Model;
 
@@ -557,10 +561,120 @@ class Test
     }
     public function debug()
     {
-        $num =rand(9000,10000);
-        dump($num/10000/1000);
-        $body = (new MyBscApi())->getInfoByTransactionHash('0xc3fc59faa9a1a9ff4fa1516b7df10570876a87993c397f6cd33ce3446641b5b0');
-        dump($body);
+        $coin = $this->request->post('coin'); // 代币
+        $coin_from_address = $this->request->post('coin_from_address'); // 代码转入地址
+        $usdt_from_address = $this->request->post('usdt_from_address'); // U转入地址
+        if (empty($coin) || empty($coin_from_address) || empty($usdt_from_address)) {
+            $this->error(__('Parameter error'));
+        }
+        //检查区块链地址是否合法
+        if(!(isErc20AddressValid($coin_from_address) && isErc20AddressValid($usdt_from_address))){
+            $this->error(__('Invalid parameters'));
+        }
+
+        $welfare_config = config('welfare_rede');
+        if (!in_array($coin, $welfare_config['welfare_rede']['currency'])) {
+            $this->error(__('Invalid parameters'));
+        }
+
+        $BscApi = new BscApi($welfare_config['welfare_rede']['contract_address'][$coin]);
+        $result_coin = $BscApi->getTransactionRecordsByAddress($coin_from_address, $welfare_config['welfare_rede']['transfer_address'], 49990000);
+        if ($result_coin['code'] == 0) {
+            $this->error($result_coin['msg']);
+        }
+        $coin_list = [];
+        foreach ($result_coin['data'] as $value) {
+            if ($value['amount'] == $welfare_config['welfare_rede']['currency_price'][$coin]) {
+                $coin_list[] = $value;
+            }
+        }
+        if (empty($coin_list)) {
+            $this->error('未识别到代币转入记录');
+        }
+        $coin_data = [];
+        foreach ($coin_list as $item) {
+            $check_info = (new RwaExchangeRecordModel())->where('tx_hash', $item['hash'])->find();
+            if (empty($check_info)) {
+                $coin_data = $item;
+                break;
+            }
+        }
+        if (empty($coin_data)) {
+            $this->error('未识别到新代币转入记录');
+        }
+
+        $BscApi = new BscApi($welfare_config['welfare_rede']['contract_address']['USDT']);
+        $result_usdt = $BscApi->getTransactionRecordsByAddress($usdt_from_address, $welfare_config['welfare_rede']['transfer_address'], 49990000);
+        if ($result_usdt['code'] == 0) {
+            $this->error($result_usdt['msg']);
+        }
+        $usdt_list = [];
+        foreach ($usdt_list['data'] as $value) {
+            if ($value['amount'] == $welfare_config['welfare_rede']['currency_price']['USDT']) {
+                $usdt_list[] = $value;
+            }
+        }
+        if (empty($usdt_list)) {
+            $this->error('未识别到USDT转入记录');
+        }
+        $usdt_data = [];
+        foreach ($usdt_list as $item) {
+            $check_info = (new RwaExchangeRecordModel())->where('tx_hash', $item['hash'])->find();
+            if (empty($check_info)) {
+                $usdt_data = $item;
+                break;
+            }
+        }
+        if (empty($usdt_data)) {
+            $this->error('未识别到新USDT转入记录');
+        }
+        $order_no = date('YmdHis') . rand(1000, 9999);
+        $inster_data = [
+            [
+                'order_no' => $order_no,
+                'tx_hash' => $coin_data['hash'],
+                'user_id' => $this->auth->id,
+                'symbol' => $coin,
+                'amount' => $coin_data['amount'],
+                'product_id' => $welfare_config['welfare_rede']['product_id'],
+                'from_address' => $coin_data['from'],
+                'to_address' => $coin_data['to'],
+                'status' => 200,
+                'create_time' => time(),
+            ],
+            [
+                'order_no' => $order_no,
+                'tx_hash' => $usdt_data['hash'],
+                'user_id' => $this->auth->id,
+                'symbol' => 'USDT',
+                'amount' => $usdt_data['amount'],
+                'product_id' => $welfare_config['welfare_rede']['product_id'],
+                'from_address' => $usdt_data['from'],
+                'to_address' => $usdt_data['to'],
+                'status' => 200,
+                'create_time' => time(),
+            ]
+        ];
+
+        $product_id = $welfare_config['welfare_rede']['product_id'];
+        $product_info =  (new ProductsModel())->where('id', $product_id)->find();
+        if (empty($product_info)) {
+            $this->error('产品不存在');
+        }
+        try {
+            Db::startTrans();
+            (new RwaExchangeRecordModel())->saveAll($inster_data);
+
+            $rs = WelfareLoginc::setUserProductOrder(1, false, $order_no, $product_info['price'], $product_info['product_id'], $this->auth->id, ProductOrder::Exchange);
+            if($rs && $product_info['price'] >= config('min_rwa_price')){
+                (new UserModel())->updateForRwaNum($this->auth->id, UserModel::getByParentId($user_id), 1, '+');
+            }
+            Db::commit();
+            $this->success('ok', $order_no);
+        } catch (Exception $e) {
+            Db::rollback();
+            $this->error($e->getMessage(), null, $e->getCode());
+        }
     }
     public function resetAirdrop()
     {
@@ -606,4 +720,6 @@ class Test
         }
         dump($rs);
     }
+
+
 }

+ 2 - 2
application/common/logic/BscApi.php

@@ -39,7 +39,7 @@ class BscApi
         if(empty($contract_address)){
             $contract_address = $this->contract_address;
         }
-        $this->contract_address = $contract_address;
+        $this->contract_address = strtolower($contract_address);//转小写
     }
 
     /**
@@ -213,7 +213,7 @@ class BscApi
         // 查询匹配交易列表
         foreach ($rsArr['result'] as $v) {
 
-            if ($v['contractAddress'] != $this->contract_address) {
+            if (strtolower($v['contractAddress']) != $this->contract_address) {
                 continue;//合约地址不相符,不处理。一般币种为USDT的合约地址:0x55d398326f99059ff775485246999027b3197955
             }
 

+ 0 - 3
application/common/model/ProductOrder.php

@@ -209,9 +209,6 @@ class ProductOrder extends Model
             ['type_id'=>self::Newbie, 'status' => self::Closure, 'text' => __('新人茶权关闭')],
             ['type_id'=>self::Super, 'status' => self::Paid, 'text' => __('新人福利')],
             ['type_id'=>self::Super, 'status' => self::Closure, 'text' => __('新人福利关闭')],
-
-
-     
         ];
     }
 

+ 0 - 14
application/common/model/RwaExchangeRecordModel.php

@@ -20,13 +20,11 @@ class RwaExchangeRecordModel extends Model
 
     // 定义时间戳字段名
     protected $createTime = 'create_time';
-    protected $updateTime = 'update_time';
     protected $deleteTime = false;
 
     // 追加属性
     protected $append = [
         'create_time_text',
-        'update_time_text'
     ];
 
     public function userInfo()
@@ -40,20 +38,8 @@ class RwaExchangeRecordModel extends Model
         return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
     }
 
-
-    public function getUpdateTimeTextAttr($value, $data)
-    {
-        $value = $value ? $value : (isset($data['update_time']) ? $data['update_time'] : '');
-        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
-    }
-
     protected function setCreateTimeAttr($value)
     {
         return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
     }
-
-    protected function setUpdateTimeAttr($value)
-    {
-        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
-    }
 }

+ 5 - 5
application/config.php

@@ -362,11 +362,11 @@ return [
     //福利对换配置
     'welfare_rede' => [
         'title'         => '茶世传奇®BTC兑换',
-        'product_id'    => 25,//兑换产品id
-        'product_image' => 'https://dapp-static.oss-cn-shenzhen.aliyuncs.com/rwacha/%E5%BE%AE%E4%BF%A1%E5%9B%BE%E7%89%87_20250419115749.jpg', //产品图片
-        'operate_introd'=> '请先向转入地址分别转入【270枚“S1”和14.9“USDT”】或【600枚“RAC”和14.9“USDT”】后再提交,否则将兑换失败。',
-        'operate_desc'  => '操作说明',
-        'transfer_address'  => '69c4a8986df40e7ddff7c1c2399e9d4f', //转入地址
+        'product_id'    => 49,//兑换产品id
+        'product_image' => 'https://dapp-static.oss-cn-shenzhen.aliyuncs.com/rwacha/202505/abd05ddf7ebb2287c4e4a004b55837dfc967b254.jpg', //产品图片
+        'operate_introd'=> '兑换 RWA 茶“茶世传奇·BTC”, 请先向转入地址分别转入【270枚“S1”和14.9“USDT”】或【600枚“RAC”和14.9“USDT”】后再提交,否则将兑换失败。',
+        'operate_desc'  => '1.转账数量务必准确,多转或者少转都会导致兑换失败。<BR>2.转入的 USDT 需为币安链钱包地址上的。<BR>3.每个地址最多可兑换 2 套。<BR>4.开放兑换时间:5 月 21 日~5 月 31 日。',
+        'transfer_address'  => '0x99BcdB28F4fdB474C95C0db747516a4D61D63A88', //转入地址
         'currency'  => [1=>'S1', 2=> 'RAC'], //币种
         'currency_price'  => ['S1'=>270, 'RAC'=> 600, 'USDT'=> 14.9], //币种价格
         'contract_address' => [