Exchange.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. <?php
  2. namespace app\api\controller;
  3. use app\api\logic\WelfareLoginc;
  4. use app\common\controller\Api;
  5. use app\common\logic\BscApi;
  6. use app\common\model\ProductLists;
  7. use app\common\model\LedgerFrozenChangeModel;
  8. use app\common\model\ProductOrder;
  9. use app\common\model\ProductsModel;
  10. use app\common\model\RwaExchangeRecordModel;
  11. use app\common\model\LedgerWalletModel;
  12. use fast\Action;
  13. use fast\Asset;
  14. use think\Db;
  15. use think\Exception;
  16. use think\Log;
  17. //Teac兑换
  18. class Exchange extends Api
  19. {
  20. protected string $lan = '';
  21. public function _initialize()
  22. {
  23. parent::_initialize();
  24. $this->lan = $this->request->getLan();
  25. }
  26. //Ledger/getWelfareRedeConfig Exchange
  27. //Ledger/submitWelfare
  28. //Rwa福利兑换配置
  29. public function getWelfareRedeConfig(){
  30. $this->success('ok', config('welfare_rede'));
  31. }
  32. //Rwa兑换
  33. public function submitWelfare(LedgerWalletModel $ledgerWalletModel)
  34. {
  35. $coin = $this->request->post('coin'); // 代币
  36. $coin_from_address = $this->request->post('coin_from_address'); // 代码转入地址
  37. $usdt_from_address = $this->request->post('usdt_from_address'); // U转入地址
  38. if (empty($coin) || empty($coin_from_address) || empty($usdt_from_address)) {
  39. $this->error(__('Parameter error'));
  40. }
  41. //检查区块链地址是否合法
  42. if(!(isErc20AddressValid($coin_from_address) && isErc20AddressValid($usdt_from_address))){
  43. $this->error(__('Invalid parameters'));
  44. }
  45. $welfare_config = config('welfare_rede');
  46. if (!in_array($coin, $welfare_config['currency'])) {
  47. $this->error(__('Invalid parameters'));
  48. }
  49. $check_user = (new RwaExchangeRecordModel())->where('user_id', $this->auth->id)->where('status', 200)->count();
  50. if($check_user >= 4){
  51. $this->error('每人限制兑换两套,您已达上限');
  52. }
  53. $BscApi = new BscApi($welfare_config['contract_address'][$coin]);
  54. $result_coin = $BscApi->getTransactionRecordsByAddress($coin_from_address, $welfare_config['transfer_address'], 49990000);
  55. if ($result_coin['code'] == 0) {
  56. $this->error($result_coin['msg']);
  57. }
  58. Log::info($result_coin, '代币所有转入记录');
  59. $coin_list = [];
  60. foreach ($result_coin['data'] as $value) {
  61. if ($value['amount'] == $welfare_config['currency_price'][$coin]) {
  62. $coin_list[] = $value;
  63. }
  64. }
  65. if (empty($coin_list)) {
  66. $this->error('未识别到代币转入记录');
  67. }
  68. Log::info($coin_list, '代币精准转入记录');
  69. $coin_data = [];
  70. foreach ($coin_list as $item) {
  71. $check_info = (new RwaExchangeRecordModel())->where('tx_hash', $item['hash'])->find();
  72. if (empty($check_info)) {
  73. $coin_data = $item;
  74. break;
  75. }
  76. }
  77. if (empty($coin_data)) {
  78. $this->error('未识别到新代币转入记录');
  79. }
  80. Log::info($coin_list, '代币可用转入记录');
  81. $BscApi = new BscApi($welfare_config['contract_address']['USDT']);
  82. $result_usdt = $BscApi->getTransactionRecordsByAddress($usdt_from_address, $welfare_config['transfer_address'], 49990000);
  83. if ($result_usdt['code'] == 0) {
  84. $this->error($result_usdt['msg']);
  85. }
  86. Log::info($result_usdt, 'USDT所有转入记录');
  87. $usdt_list = [];
  88. foreach ($result_usdt['data'] as $value) {
  89. if ($value['amount'] == $welfare_config['currency_price']['USDT']) {
  90. $usdt_list[] = $value;
  91. }
  92. }
  93. if (empty($usdt_list)) {
  94. $this->error('未识别到USDT转入记录');
  95. }
  96. Log::info($usdt_list, 'USDT精准转入记录');
  97. $usdt_data = [];
  98. foreach ($usdt_list as $item) {
  99. $check_info = (new RwaExchangeRecordModel())->where('tx_hash', $item['hash'])->find();
  100. if (empty($check_info)) {
  101. $usdt_data = $item;
  102. break;
  103. }
  104. }
  105. if (empty($usdt_data)) {
  106. $this->error('未识别到新USDT转入记录');
  107. }
  108. Log::info($usdt_list, 'USDT可用入记录');
  109. $order_no = date('YmdHis') . rand(1000, 9999);
  110. $inster_data = [
  111. [
  112. 'order_no' => $order_no,
  113. 'tx_hash' => $coin_data['hash'],
  114. 'user_id' => $this->auth->id,
  115. 'symbol' => $coin,
  116. 'amount' => $coin_data['amount'],
  117. 'product_id' => $welfare_config['product_id'],
  118. 'from_address' => $coin_data['from'],
  119. 'to_address' => $coin_data['to'],
  120. 'status' => 200,
  121. 'create_time' => time(),
  122. ],
  123. [
  124. 'order_no' => $order_no,
  125. 'tx_hash' => $usdt_data['hash'],
  126. 'user_id' => $this->auth->id,
  127. 'symbol' => 'USDT',
  128. 'amount' => $usdt_data['amount'],
  129. 'product_id' => $welfare_config['product_id'],
  130. 'from_address' => $usdt_data['from'],
  131. 'to_address' => $usdt_data['to'],
  132. 'status' => 200,
  133. 'create_time' => time(),
  134. ]
  135. ];
  136. Log::info($inster_data, '插入数据');
  137. $product_id = $welfare_config['product_id'];
  138. $product_info = (new ProductLists())->where('id', $product_id)->find();
  139. if (empty($product_info)) $this->error('产品不存在');
  140. try {
  141. Db::startTrans();
  142. $rs = Db::name('rwa_exchange_record')->fetchSql(false)->insertAll($inster_data);
  143. //添加标记茶宝记录
  144. $ledgerWalletModel->changeWalletAccount($this->auth->id, Asset::FROZEN, $usdt_data['amount'], LedgerFrozenChangeModel::RwaExchangeRecord, 0);
  145. //发放产品
  146. $rs = WelfareLoginc::setUserProductOrder(1, false, $order_no, 0, $product_info['id'], $this->auth->id, ProductOrder::RwaExchange);
  147. Db::commit();
  148. $this->success('ok', $order_no);
  149. } catch (Exception $e) {
  150. Db::rollback();
  151. $this->error($e->getMessage(), null, $e->getCode());
  152. }
  153. }
  154. //Teac兑换列表
  155. public function getTeacList(ProductLists $productLists){
  156. $list = $productLists::where('is_teac', 1)
  157. ->where('status', 1)
  158. ->field('id,teac_price,thum,'.$this->lan.'_name as name')
  159. ->order('weigh desc')
  160. ->paginate($this->pageSize);
  161. $this->success('ok', $list);
  162. }
  163. //Teac兑换详情
  164. public function getTeacDetail(ProductLists $productLists){
  165. $ids = $this->request->post('product_id/d', 0);
  166. $list = $productLists::where('is_teac', 1)
  167. ->where('id', $ids)
  168. ->field('id,teac_price,thum,'.$this->lan.'_name as name,details')
  169. ->find();
  170. $this->success('ok', $list);
  171. }
  172. //Teac兑换购买
  173. public function getTeacOrder(ProductLists $productLists){
  174. $list = $productLists::where('is_teac', 1)
  175. ->field('id,teac_price,thum,'.$this->lan.'_name as name')
  176. ->order('weigh desc')
  177. ->find();
  178. $this->success('ok', $list);
  179. }
  180. }