Exchange.php 11 KB

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