Exchange.php 9.5 KB

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