Browse Source

格式化代码

jason 1 month ago
parent
commit
0b8d00f6af
1 changed files with 247 additions and 248 deletions
  1. 247 248
      application/api/controller/Exchange.php

+ 247 - 248
application/api/controller/Exchange.php

@@ -23,265 +23,264 @@ use think\Log;
 class Exchange extends Api
 {
 
-      protected string $lan = '';
-
-      public function _initialize()
-      {
-            parent::_initialize();
-            $this->lan = $this->request->getLan();
-      }
-
-  
-      //Rwa福利兑换配置
-      public function getWelfareRedeList(ProductWelfareRede $productWelfareRede){
-
-            $list = $productWelfareRede
-                  ->where('status', $productWelfareRede::Normal)
-                  ->order('weigh desc')
-                  ->paginate($this->pageSize);
-            $this->success('ok', $list);
-      }
-
-
-      
-      //Rwa福利兑换详情
-      public function getWelfareRedeDetail(ProductWelfareRede $productWelfareRede){
-
-            $ids = $this->request->post('ids');
-            if (empty($ids)) $this->error(__('Parameter error'));
-            $welfare = $productWelfareRede::get($ids);
-            $data_info = [
-                'title' => $welfare['title'],
-                'product_image' => $welfare['product_image'],
-                'transfer_address' => $welfare['transfer_address'],
-                'usdt_num' => round($welfare['usdt_num'],2),
-                'is_token' => $welfare['is_token'],
-                'token_name' => $welfare['token_name'],
-                'token_num' => round($welfare['token_num'], 2),
-                'is_chabao' => $welfare['is_chabao'],
-                'chabao_num' => round($welfare['chabao_num'],2)
-            ];
-            $this->success('ok', ['data'=>$data_info, 'desc'=> $welfare['describe']]);
-      }
-
-
-      //Rwa兑换
-      public function submitWelfare(LedgerWalletModel $ledgerWalletModel, ProductWelfareRede $productWelfareRede)
-      {
-          $ids = $this->request->post('ids'); // 代币ids
-          $type_id           = $this->request->post('type_id');           // 类型0U 1茶宝
-          $coin_from_address = $this->request->post('coin_from_address'); // 代码转入地址
-          $usdt_from_address = $this->request->post('usdt_from_address'); // U转入地址
-          if (empty($ids) ) {
-              $this->error(__('Parameter error'));
-          }
-          //检查区块链地址是否合法
-          if(!empty($usdt_from_address) &&  !(isErc20AddressValid($usdt_from_address))){
-              $this->error(__('Invalid parameters'));
-          }
-          //福利兑换记录
-          $welfare_config = $productWelfareRede::where('id',$ids)->where('status', $productWelfareRede::Normal)->find();
-          if (!$welfare_config) $this->error(__('Invalid parameters'));
-          //检查是否在开启时间之内
-          if(time() < $welfare_config['start_time'] || time() > $welfare_config['end_time']) $this->error(__('不在兑换时间范围内'));
-          
-          //检查是否已兑换过
-          if($welfare_config['limit_num'] > 0){
-              $check_user = (new RwaExchangeRecordModel())
-                  ->where('user_id', $this->auth->id)
-                  ->where('welfare_id', $ids)
-                  ->count();
-              if($check_user >= $welfare_config['limit_num']) $this->error('每人限制兑换' .$welfare_config['limit_num'].'套,您已达上限');
-          }
-          $token = 0; 
-          $bonus = 0; //推广u或茶宝数量
-          $order_no = date('YmdHis') . rand(1000, 9999);
-          $inster_data = [
-              'order_no' => $order_no,
-              'user_id' => $this->auth->id,
-              'welfare_id' => $welfare_config['id'],
-              'product_id' => $welfare_config['product_id'],
-              'from_address_usdt' => $usdt_from_address,
-              'amount_usdt' => $welfare_config['usdt_num'],
-              'to_address' => $welfare_config['transfer_address'],
-              'create_time' => time()
-          ];
-          //有代币参与时
-          if($welfare_config->is_token == 1 && empty($type_id)){
-              if (empty($coin_from_address)) {
-                  $this->error(__('Parameter error'));
-              }
-              //检查区块链地址是否合法
-              if(!(isErc20AddressValid($coin_from_address))){
-                  $this->error(__('Invalid parameters'));
-              }
-
-              $BscApi = new BscApi($welfare_config->token_address);
-              $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->token_num) {
-                      $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_token', $item['hash'])->find();
-                  if (empty($check_info)) {
-                      $coin_data = $item;//获取未兑换过的代币转入记录
-                      break;
-                  }
-              }
-              if (empty($coin_data)) {
-                  $this->error('未识别到新代币转入记录');
-              }
-
-              Log::info($coin_list, '代币可用转入记录');
-
-              //拼装代币转入记录
-              $inster_data['token_name'] = $welfare_config->token_name;
-              $inster_data['from_address_token'] = $coin_data['from'];
-              $inster_data['amount_token'] = $coin_data['amount'];
-              $inster_data['tx_hash_token'] = $coin_data['hash'];
+    protected string $lan = '';
+
+    public function _initialize()
+    {
+        parent::_initialize();
+        $this->lan = $this->request->getLan();
+    }
+
+
+    //Rwa福利兑换配置
+    public function getWelfareRedeList(ProductWelfareRede $productWelfareRede){
+
+        $list = $productWelfareRede
+            ->where('status', $productWelfareRede::Normal)
+            ->order('weigh desc')
+            ->paginate($this->pageSize);
+        $this->success('ok', $list);
+    }
+
+
+
+    //Rwa福利兑换详情
+    public function getWelfareRedeDetail(ProductWelfareRede $productWelfareRede){
+
+        $ids = $this->request->post('ids');
+        if (empty($ids)) $this->error(__('Parameter error'));
+        $welfare = $productWelfareRede::get($ids);
+        $data_info = [
+            'title' => $welfare['title'],
+            'product_image' => $welfare['product_image'],
+            'transfer_address' => $welfare['transfer_address'],
+            'usdt_num' => round($welfare['usdt_num'],2),
+            'is_token' => $welfare['is_token'],
+            'token_name' => $welfare['token_name'],
+            'token_num' => round($welfare['token_num'], 2),
+            'is_chabao' => $welfare['is_chabao'],
+            'chabao_num' => round($welfare['chabao_num'],2)
+        ];
+        $this->success('ok', ['data'=>$data_info, 'desc'=> $welfare['describe']]);
+    }
+
+
+    //Rwa兑换
+    public function submitWelfare(LedgerWalletModel $ledgerWalletModel, ProductWelfareRede $productWelfareRede)
+    {
+        $ids = $this->request->post('ids'); // 代币ids
+        $type_id           = $this->request->post('type_id');           // 类型0U 1茶宝
+        $coin_from_address = $this->request->post('coin_from_address'); // 代码转入地址
+        $usdt_from_address = $this->request->post('usdt_from_address'); // U转入地址
+        if (empty($ids) ) {
+            $this->error(__('Parameter error'));
+        }
+        //检查区块链地址是否合法
+        if(!empty($usdt_from_address) &&  !(isErc20AddressValid($usdt_from_address))){
+            $this->error(__('Invalid parameters'));
+        }
+        //福利兑换记录
+        $welfare_config = $productWelfareRede::where('id',$ids)->where('status', $productWelfareRede::Normal)->find();
+        if (!$welfare_config) $this->error(__('Invalid parameters'));
+        //检查是否在开启时间之内
+        if(time() < $welfare_config['start_time'] || time() > $welfare_config['end_time']) $this->error(__('不在兑换时间范围内'));
+
+        //检查是否已兑换过
+        if($welfare_config['limit_num'] > 0){
+            $check_user = (new RwaExchangeRecordModel())
+                ->where('user_id', $this->auth->id)
+                ->where('welfare_id', $ids)
+                ->count();
+            if($check_user >= $welfare_config['limit_num']) $this->error('每人限制兑换' .$welfare_config['limit_num'].'套,您已达上限');
+        }
+        $token = 0;
+        $bonus = 0; //推广u或茶宝数量
+        $order_no = date('YmdHis') . rand(1000, 9999);
+        $inster_data = [
+            'order_no' => $order_no,
+            'user_id' => $this->auth->id,
+            'welfare_id' => $welfare_config['id'],
+            'product_id' => $welfare_config['product_id'],
+            'from_address_usdt' => $usdt_from_address,
+            'amount_usdt' => $welfare_config['usdt_num'],
+            'to_address' => $welfare_config['transfer_address'],
+            'create_time' => time()
+        ];
+        //有代币参与时
+        if($welfare_config->is_token == 1 && empty($type_id)) {
+            if (empty($coin_from_address)) {
+                $this->error(__('Parameter error'));
             }
-            //U支付
-            if(empty($type_id) && empty($welfare_config->is_token)){
-                //USDT转入记录
-                $BscApi = new BscApi('0x55d398326f99059ff775485246999027b3197955');
-                $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 ($result_usdt['data'] as $value) {
-                    if ($value['amount'] == $welfare_config->usdt_num) {
-                        $usdt_list[] = $value;
-                        $bonus += $value['amount'];
-                    }
+            //检查区块链地址是否合法
+            if (!(isErc20AddressValid($coin_from_address))) {
+                $this->error(__('Invalid parameters'));
+            }
+
+            $BscApi = new BscApi($welfare_config->token_address);
+            $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->token_num) {
+                    $coin_list[] = $value;
                 }
-                if (empty($usdt_list)) {
-                    $this->error('未识别到USDT转入记录');
+            }
+            if (empty($coin_list)) {
+                $this->error('未识别到代币转入记录');
+            }
+            Log::info($coin_list, '代币精准转入记录');
+
+            $coin_data = [];
+            foreach ($coin_list as $item) {
+                $check_info = (new RwaExchangeRecordModel())->where('tx_hash_token', $item['hash'])->find();
+                if (empty($check_info)) {
+                    $coin_data = $item;//获取未兑换过的代币转入记录
+                    break;
                 }
-                Log::info($usdt_list, 'USDT精准转入记录');
-
-                $usdt_data = [];
-                foreach ($usdt_list as $item) {
-                    $check_info = (new RwaExchangeRecordModel())->where('tx_hash_usdt', $item['hash'])->find();
-                    if (empty($check_info)) {
-                        $usdt_data = $item;
-                        break;
-                    }
+            }
+            if (empty($coin_data)) {
+                $this->error('未识别到新代币转入记录');
+            }
+
+            Log::info($coin_list, '代币可用转入记录');
+
+            //拼装代币转入记录
+            $inster_data['token_name'] = $welfare_config->token_name;
+            $inster_data['from_address_token'] = $coin_data['from'];
+            $inster_data['amount_token'] = $coin_data['amount'];
+            $inster_data['tx_hash_token'] = $coin_data['hash'];
+        }
+        //U支付
+        if(empty($type_id) && empty($welfare_config->is_token)){
+            //USDT转入记录
+            $BscApi = new BscApi('0x55d398326f99059ff775485246999027b3197955');
+            $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 ($result_usdt['data'] as $value) {
+                if ($value['amount'] == $welfare_config->usdt_num) {
+                    $usdt_list[] = $value;
+                    $bonus += $value['amount'];
                 }
-                if (empty($usdt_data)) {
-                    $this->error('未识别到新USDT转入记录');
+            }
+            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_usdt', $item['hash'])->find();
+                if (empty($check_info)) {
+                    $usdt_data = $item;
+                    break;
                 }
-                Log::info($usdt_list, 'USDT可用入记录');
-
-                //记录
-                $inster_data['tx_hash_usdt'] = $usdt_data['hash'];
-                Log::info($inster_data, '插入数据');
             }
-            
-            if($type_id == 1){
-                $inster_data['amount_chabao'] =$token= $bonus= $welfare_config->chabao_num;
-
+            if (empty($usdt_data)) {
+                $this->error('未识别到新USDT转入记录');
             }
-          try {
-              Db::startTrans();
-              $rs = Db::name('rwa_exchange_record')->fetchSql(false)->insert($inster_data);
-              Log::info($rs, '插入状态');
-
-
-              //赠送标记茶宝
-              if($welfare_config['send_chabao'] > 0){
-                  $ledgerWalletModel->changeWalletAccount($this->auth->id, Asset::FROZEN, $welfare_config['send_chabao'], LedgerFrozenChangeModel::RwaExchangeRecord, 0);
-              }
-
-              //扣除茶宝
-              if($token > 0){
-                  $ledgerWalletModel->changeWalletAccount($this->auth->id, Asset::TOKEN, -$token, LedgerTokenChangeModel::RwaExchangeRecord, 0);
-              }
-              //推广奖励
-              if($bonus > 0 && !empty($welfare_config->is_promotion) && $welfare_config->promotion_product > 0){
-                  ProductWelfareRede::sendPromotionReward($this->auth->id, explode(',', $welfare_config->product_id), $welfare_config->promotion_product, $bonus, $welfare_config->promotion_one,$welfare_config->promotion_two, $welfare_config->promotion_three);
-              }
-              //发放产品奖励
-              $rs =WelfareLoginc::setUserWelfareProduct($this->auth->id, $welfare_config['product_id'], ProductOrder::RwaExchange);
-              Log::info($rs, '发放产品奖励状态');
-
-              Db::commit();
-              $this->success('ok', $order_no);
-          } catch (Exception $e) {
-              Db::rollback();
-              $this->error($e->getMessage(), null, $e->getCode());
-          }
-      }
-
-      //Teac兑换列表
-      public function getTeacList(ProductLists $productLists){
-             
-            $list = $productLists::where('is_teac', 1)
-                  ->where('status', 1)
-                  ->field('id,teac_price,thum,'.$this->lan.'_name as name')
-                  ->order('weigh desc')
-                  ->paginate($this->pageSize);
-            $this->success('ok', $list);
-      }
-
-      //Teac兑换详情
-      public function getTeacDetail(ProductLists $productLists){
-             
-            $ids = $this->request->post('product_id/d', 0);
-            $list = $productLists::where('is_teac', 1)
-                  ->where('id', $ids)->where('status', 1)
-                  ->field('id,teac_price,images,'.$this->lan.'_name as name,details')
-                  ->find();
-            $this->success('ok', $list);
-      }
-
-      //Teac兑换购买
-      public function getTeacOrder(ProductLists $productLists, LedgerWalletModel $ledgerWalletModel, ProductOrder $productOrder){
-             
-            $params = $this->request->post();
-            $validate = \think\Loader::validate('Exchange');
-            if(!$validate->scene('order')->check($params)) $this->error($validate->getError());
-
-            $rows = $productLists::where('id', $params['product_id'])->where('is_teac', 1)->where('status', 1)->find();
-            if(empty($rows)) throw new Exception(__("产品暂未开放"));
-            // 启动事务
-            Db::startTrans();
-            try {
+            Log::info($usdt_list, 'USDT可用入记录');
 
-                  $teac = $ledgerWalletModel::getWalletTeac($this->auth->id);
+            //记录
+            $inster_data['tx_hash_usdt'] = $usdt_data['hash'];
+            Log::info($inster_data, '插入数据');
+        }
+
+        if($type_id == 1){
+            $inster_data['amount_chabao'] =$token= $bonus= $welfare_config->chabao_num;
+        }
+        try {
+            Db::startTrans();
+            $rs = Db::name('rwa_exchange_record')->fetchSql(false)->insert($inster_data);
+            Log::info($rs, '插入状态');
 
-                  $amount = bcmul($params['stock'], $rows->teac_price, 2);
-                  if(bccomp($amount, $teac, 2) > 0) throw new Exception(__("Teac购买金额足"), 15001);
-                  
-                  //添加产品
-                  $productOrder::setPopularNoAreaOrder($params['stock'], 0, 0, $params['product_id'], $this->auth->id, $productOrder::TeacExchange);
 
-                  // 更新Teac和账变
-                  $ledgerWalletModel->changeWalletAccount($this->auth->id, Asset::TEAC, -$amount, LedgerTeacChangeModel::Exchange, 0);
+            //赠送标记茶宝
+            if($welfare_config['send_chabao'] > 0){
+                $ledgerWalletModel->changeWalletAccount($this->auth->id, Asset::FROZEN, $welfare_config['send_chabao'], LedgerFrozenChangeModel::RwaExchangeRecord, 0);
+            }
 
-                  // 提交事务
-                  Db::commit();
-            } catch (Exception $e) {
-                  // 回滚事务
-                  Db::rollback();
-                  $this->error($e->getMessage(), null, $e->getCode());
+            //扣除茶宝
+            if($token > 0){
+                $ledgerWalletModel->changeWalletAccount($this->auth->id, Asset::TOKEN, -$token, LedgerTokenChangeModel::RwaExchangeRecord, 0);
+            }
+            //推广奖励
+            if($bonus > 0 && !empty($welfare_config->is_promotion) && $welfare_config->promotion_product > 0){
+                ProductWelfareRede::sendPromotionReward($this->auth->id, explode(',', $welfare_config->product_id), $welfare_config->promotion_product, $bonus, $welfare_config->promotion_one,$welfare_config->promotion_two, $welfare_config->promotion_three);
             }
-     
-            $this->success('ok');
-      }
+            //发放产品奖励
+            $rs =WelfareLoginc::setUserWelfareProduct($this->auth->id, $welfare_config['product_id'], ProductOrder::RwaExchange);
+            Log::info($rs, '发放产品奖励状态');
+
+            Db::commit();
+            $this->success('ok', $order_no);
+        } catch (Exception $e) {
+            Db::rollback();
+            $this->error($e->getMessage(), null, $e->getCode());
+        }
+    }
+
+    //Teac兑换列表
+    public function getTeacList(ProductLists $productLists){
+
+        $list = $productLists::where('is_teac', 1)
+            ->where('status', 1)
+            ->field('id,teac_price,thum,'.$this->lan.'_name as name')
+            ->order('weigh desc')
+            ->paginate($this->pageSize);
+        $this->success('ok', $list);
+    }
+
+    //Teac兑换详情
+    public function getTeacDetail(ProductLists $productLists){
+
+        $ids = $this->request->post('product_id/d', 0);
+        $list = $productLists::where('is_teac', 1)
+            ->where('id', $ids)->where('status', 1)
+            ->field('id,teac_price,images,'.$this->lan.'_name as name,details')
+            ->find();
+        $this->success('ok', $list);
+    }
+
+    //Teac兑换购买
+    public function getTeacOrder(ProductLists $productLists, LedgerWalletModel $ledgerWalletModel, ProductOrder $productOrder){
+
+        $params = $this->request->post();
+        $validate = \think\Loader::validate('Exchange');
+        if(!$validate->scene('order')->check($params)) $this->error($validate->getError());
+
+        $rows = $productLists::where('id', $params['product_id'])->where('is_teac', 1)->where('status', 1)->find();
+        if(empty($rows)) throw new Exception(__("产品暂未开放"));
+        // 启动事务
+        Db::startTrans();
+        try {
+
+            $teac = $ledgerWalletModel::getWalletTeac($this->auth->id);
+
+            $amount = bcmul($params['stock'], $rows->teac_price, 2);
+            if(bccomp($amount, $teac, 2) > 0) throw new Exception(__("Teac购买金额足"), 15001);
+
+            //添加产品
+            $productOrder::setPopularNoAreaOrder($params['stock'], 0, 0, $params['product_id'], $this->auth->id, $productOrder::TeacExchange);
+
+            // 更新Teac和账变
+            $ledgerWalletModel->changeWalletAccount($this->auth->id, Asset::TEAC, -$amount, LedgerTeacChangeModel::Exchange, 0);
+
+            // 提交事务
+            Db::commit();
+        } catch (Exception $e) {
+            // 回滚事务
+            Db::rollback();
+            $this->error($e->getMessage(), null, $e->getCode());
+        }
+
+        $this->success('ok');
+    }
 
 }