Exchange.php 11 KB

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