jason 6 сар өмнө
parent
commit
e605853ee9

+ 12 - 1
application/api/common.php

@@ -60,4 +60,15 @@ function getLangKey( $lan = 'zh'): int
 {
     $toLang = ['zh' => 0, 'en' => 1];
     return $toLang[$lan];
-}
+}
+//判断是否是erc20地址
+function isErc20AddressValid($address) {
+    // 去掉地址的 "0x" 前缀
+    $address = trim($address);
+    if (substr($address, 0, 2) == '0x') {
+        $address = substr($address, 2);
+    }
+
+    // 检查地址是否符合以太坊地址的基本格式
+    return preg_match('/^[0-9a-fA-F]{40}$/', $address) === 1;
+}

+ 123 - 0
application/api/controller/Ledger.php

@@ -2,8 +2,13 @@
 
 namespace app\api\controller;
 
+use app\api\logic\WelfareLoginc;
 use app\common\controller\Api;
+use app\common\logic\BscApi;
 use app\common\model\LedgerFrozenChangeModel;
+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;
@@ -200,6 +205,124 @@ class Ledger extends Api
         $this->success('ok', config('welfare_rede'));
     }
 
+    public function submitWelfare()
+    {
+        $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());
+        }
+    }
+
 
     /**
      * 提现自动打款回调

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

@@ -30,16 +30,16 @@ class BscApi
      */
     private string $valueScale = '1000000000000000000';
 
-    public function __construct() {
+    public function __construct($contract_address = '') {
         // 初始化逻辑代码
         $key = Env::get('rental.bsc_api_key');
         if(!empty($key)){
             $this->bsc_api_key = $key;
         }
-        $this->contract_address = Env::get('rental.contract_address');
-        if(empty($this->contract_address)){
-            $this->contract_address = '0x55d398326f99059ff775485246999027b3197955';
+        if(empty($contract_address)){
+            $contract_address = $this->contract_address;
         }
+        $this->contract_address = $contract_address;
     }
 
     /**

+ 2 - 1
application/common/model/ProductOrder.php

@@ -43,7 +43,8 @@ class ProductOrder extends Model
     const Airdrop           = 5; //空投
     const Synthesi          = 6; //合成
     const Buying            = 7; //求购
-    
+    const Exchange          = 8; //兑换
+
     // 订单状态 '已下单', 1=>'已付款', 2=>'已转让', 3=>'提货', 4=>'已取消', 5=>'完成'];
     public $order_status = [
         '-1'                    => '全部',

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

@@ -0,0 +1,59 @@
+<?php
+
+namespace app\common\model;
+
+use Exception;
+use fast\Action;
+use fast\Asset;
+use fast\RechargeOrderType;
+use fast\RechargeStatus;
+use think\Db;
+use think\Model;
+
+class RwaExchangeRecordModel extends Model
+{
+
+    protected $name = "rwa_exchange_record";
+
+    // 自动写入时间戳字段
+    protected $autoWriteTimestamp = 'int';
+
+    // 定义时间戳字段名
+    protected $createTime = 'create_time';
+    protected $updateTime = 'update_time';
+    protected $deleteTime = false;
+
+    // 追加属性
+    protected $append = [
+        'create_time_text',
+        'update_time_text'
+    ];
+
+    public function userInfo()
+    {
+        return $this->hasOne(UserModel::class, 'id', 'user_id');
+    }
+
+    public function getCreateTimeTextAttr($value, $data)
+    {
+        $value = $value ? $value : (isset($data['create_time']) ? $data['create_time'] : '');
+        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);
+    }
+}

+ 8 - 1
application/config.php

@@ -362,11 +362,18 @@ 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', //转入地址
         'currency'  => [1=>'S1', 2=> 'RAC'], //币种
-        'currency_price'  => ['S1'=>20, 'RAC'=> 30, 'USDT'=> 40], //币种价格
+        'currency_price'  => ['S1'=>270, 'RAC'=> 600, 'USDT'=> 14.9], //币种价格
+        'contract_address' => [
+            'S1' => '0x6536A3A1E65E4618Cbe175E167Cd25887fCA4fa0',
+            'RAC' => '0x024a9ad0ecfaa8b3566d3310affb358379a55e7a',
+            'USDT' => '0x55d398326f99059ff775485246999027b3197955',
+        ],
+
     ]
 ];