Exchange.php 9.1 KB

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