Exchange.php 9.8 KB

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