Exchange.php 10 KB

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