Txmx.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. <?php
  2. namespace app\admin\controller\general;
  3. use app\admin\model\Admin;
  4. use app\common\controller\Backend;
  5. use app\common\model\GasLog;
  6. use blockchain\TronService;
  7. use blockchain\Web3Service;
  8. use fast\Random;
  9. use think\Config;
  10. use think\Log;
  11. use think\Session;
  12. use think\Validate;
  13. use app\common\model\Txjl;
  14. use think\Db;
  15. use Tron\Address;
  16. /**
  17. * 个人配置
  18. *
  19. * @icon fa fa-user
  20. */
  21. class Txmx extends Backend
  22. {
  23. /**
  24. * 查看
  25. */
  26. public function index()
  27. {
  28. //设置过滤方法
  29. $this->request->filter(['strip_tags']);
  30. if ($this->request->isAjax()) {
  31. $model = model('Txjl');
  32. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  33. $total = $model
  34. ->where($where)
  35. ->where('admin_id', $this->auth->id)
  36. ->order($sort, $order)
  37. ->count();
  38. $list = $model
  39. ->where($where)
  40. ->where('admin_id', $this->auth->id)
  41. ->order($sort, $order)
  42. ->limit($offset, $limit)
  43. ->select();
  44. $result = array("total" => $total, "rows" => $list);
  45. return json($result);
  46. }
  47. $admin = Admin::get($this->auth->id);
  48. Session::set("admin", $admin->toArray());
  49. return $this->view->fetch();
  50. }
  51. /**
  52. * 更新个人信息
  53. */
  54. public function update()
  55. {
  56. if ($this->request->isPost()) {
  57. $params = $this->request->post("row/a");
  58. if ($params) {
  59. $site=config('site');
  60. $money=$params['money'];
  61. if($money<$site['zstx']){
  62. $this->error($site['txbz']);
  63. }
  64. if(!$params['type']){
  65. $this->error('请填写提现类型');
  66. }
  67. if(!$params['name']){
  68. $this->error('请填写提现名字');
  69. }
  70. if(!$params['cord']){
  71. $this->error('请填写提现帐号');
  72. }
  73. $user_id=$this->auth->id;
  74. $vadmin=Db::name('admin')->where('id',$user_id)->find();
  75. if ($vadmin && $money != 0) {
  76. if($vadmin['money']<$money){
  77. $this->error('余额不足');
  78. }
  79. Db::startTrans();
  80. try {
  81. $moneys=$money;
  82. //更新会员信息
  83. $before = $vadmin['money'];
  84. $after = $vadmin['money'] - $moneys;
  85. Db::name('admin') ->where('id',$user_id)->setDec('money', $moneys);
  86. //写入日志
  87. Db::name('admin_money_log')->insertGetId(['admin_id' =>$user_id, 'money' => $moneys,'createtime' => time(),'before' => $before, 'after' => $after, 'memo' => '用户提现']);
  88. $site['txsxf']=isset($site['txsxf'])?$site['txsxf']:0;
  89. $sxf=$money*$site['txsxf'];
  90. $up=[
  91. 'admin_id' => $user_id,
  92. 'type' => $params['type'],
  93. 'name' => $params['name'],
  94. 'cord' => $params['cord'],
  95. 'money' => $money,
  96. 'moneydz' => $money-$sxf,
  97. 'sxf' => $sxf,
  98. 'iscl' => 1,
  99. 'islx' => 2,
  100. 'memo' => '后台用户提现'
  101. ];
  102. //写入提现记录
  103. Txjl::create($up);
  104. $admin = Admin::get($this->auth->id);
  105. Session::set("admin", $admin->toArray());
  106. Db::commit();
  107. $this->success('提交成功');
  108. }catch (Exception $e){
  109. Db::rollback();
  110. $this->error($e->getMessage());
  111. }
  112. }else{
  113. $this->error(__('金额不对'));
  114. }
  115. }
  116. $this->error();
  117. }
  118. return;
  119. }
  120. // 归集(把所有用户的USDT归集到指定地址)
  121. public function summary(){
  122. $blockchain = config('blockchain');
  123. $trcUsdtAddress=$blockchain['trc']['tokens']['usdt']['address'];
  124. $trcgasAddress = $blockchain['trc']['tokens']['usdt']['gas_address'];
  125. $bscUsdtAddress = $blockchain['bsc']['tokens']['usdt']['address'];
  126. $bscgasAddress = $blockchain['bsc']['tokens']['usdt']['gas_address'];
  127. $list = Db::name('user_usdt_address')->where('last_recharge','>',0)->select();
  128. foreach ($list as $key => $value) {
  129. $trx_money_info = [
  130. 'address' => $value['trx_address'],
  131. 'hexAddress' => $value['trx_16_address'],
  132. 'privateKey' => $value['trx_key'],
  133. ];
  134. // 创建 Address 对象
  135. $trcGasAddress = new Address(
  136. $trcgasAddress // 地址字符串
  137. );
  138. $this->trx_summary($trx_money_info,$trcUsdtAddress,$trcgasAddress);
  139. $bsc_money_info = [
  140. 'address' => $value['bsc_address'],
  141. 'privateKey' => $value['bsc_address '],
  142. ];
  143. $this->bsc_summary($bsc_money_info,$bscUsdtAddress,$bscgasAddress );
  144. }
  145. }
  146. public function trx_summary(array $money_info, string $summary_address, Address $gas_address): bool
  147. {
  148. //实例化TRON
  149. $service = TronService::instance('usdt');
  150. //实例化出款地址[有Usdt余额的用户钱包]
  151. $address = new Address($money_info['address'], $money_info['privateKey'], $money_info['hexAddress']);
  152. //获取代币余额
  153. $balance = $service->tokenBalance($money_info['address']);
  154. var_dump($balance);die();
  155. //USDT余额大于0,触发归集
  156. if ($balance > 0) {
  157. //查询地址中主币【GAS费余额】
  158. $trx_balance = $service->balance($money_info['address']);
  159. if ($trx_balance < 30) {
  160. //如果主币小于30个,即gas费不够【BSC的gas费需要比较少,大约0.001个bnb就不少了,按照实际情况设置】
  161. //计算要达到30个trx需要再转几个给用户地址
  162. $trade_trx = ceil(30 - $trx_balance);
  163. //处理手续费转账,从系统地址分配gas给用户
  164. //查询系统地址的TRX余额
  165. $gas_trx_balance = $service->balance($gas_address->trcgasAddress);
  166. //系统地址TRX余额不足,退出处理
  167. if ($gas_trx_balance < $trade_trx + 2) {
  168. Log::error('手续费账户余额不足');
  169. return false;
  170. }
  171. try {
  172. //执行转账
  173. $res = $service->transfer($gas_address, $address, $trade_trx);
  174. GasLog::create(['to_address' => $address, 'num' =>$trade_trx , 'type' =>'trx']);
  175. if (isset($res->txID)) {
  176. Log::error('转账TRX交易哈希:' . $res->txID);
  177. return true;
  178. } else {
  179. Log::error('转账TRX失败');
  180. return false;
  181. }
  182. } catch (\Exception $e) {
  183. Log::error('转账TRX失败' . $e->getMessage());
  184. return false;
  185. }
  186. } else {
  187. //账户内TRX充足,执行USDT转账交易
  188. try {
  189. //将账户内的USDT余额转入归集指定地址
  190. $res = $service->tokenTransfer($address, new Address($summary_address), $balance);
  191. @file_put_contents('trasfer.txt', json_encode($res), FILE_APPEND);
  192. if ($res['status']) {
  193. Log::error('转账交易哈希:' . $res['data']->txID);
  194. return true;
  195. } else {
  196. Log::error('转账失败' . $res['msg']);
  197. return false;
  198. }
  199. } catch (\Exception $e) {
  200. Log::error('转账失败' . $e->getMessage());
  201. return false;
  202. }
  203. }
  204. }
  205. return true;
  206. }
  207. public function bsc_summary(array $money_info, string $summary_address, Address $gas_address): bool
  208. {
  209. //实例化TRON
  210. $service = Web3Service::instance('bsc', 'usdt', '');
  211. //实例化出款地址[有Usdt余额的用户钱包]
  212. $address = new Address($money_info['address'], $money_info['privateKey'], $money_info['hexAddress']);
  213. //获取代币余额
  214. $balance = $service->getTokenBalance($money_info['address']);
  215. //USDT余额大于0,触发归集
  216. if ($balance > 0) {
  217. //查询地址中主币【GAS费余额】
  218. $trx_balance = $service->getBalance($money_info['address']);
  219. if ($trx_balance < 0.001) {
  220. //如果主币小于30个,即gas费不够【BSC的gas费需要比较少,大约0.001个bnb就不少了,按照实际情况设置】
  221. //计算要达到30个trx需要再转几个给用户地址
  222. $trade_trx = ceil(30 - $trx_balance);
  223. //处理手续费转账,从系统地址分配gas给用户
  224. //查询系统地址的TRX余额
  225. $gas_trx_balance = $service->getBalance($gas_address);
  226. //系统地址TRX余额不足,退出处理
  227. if ($gas_trx_balance < $trade_trx + 2) {
  228. Log::error('手续费账户余额不足');
  229. return false;
  230. }
  231. try {
  232. //执行转账
  233. $res = $service->transfer($address, $trade_trx);
  234. GasLog::create(['to_address' => $address, 'num' =>$trade_trx , 'type' =>'bsc']);
  235. if (isset($res->txID)) {
  236. Log::error('转账BSC交易哈希:' . $res->txID);
  237. return true;
  238. } else {
  239. Log::error('转账BSC失败');
  240. return false;
  241. }
  242. } catch (\Exception $e) {
  243. Log::error('转账BSC失败' . $e->getMessage());
  244. return false;
  245. }
  246. } else {
  247. //账户内TRX充足,执行USDT转账交易
  248. try {
  249. //将账户内的USDT余额转入归集指定地址
  250. $res = $service->transferToken($address, new Address($summary_address), $balance);
  251. @file_put_contents('trasfer.txt', json_encode($res), FILE_APPEND);
  252. if ($res['status']) {
  253. Log::error('转账交易哈希:' . $res['data']->txID);
  254. return true;
  255. } else {
  256. Log::error('转账失败' . $res['msg']);
  257. return false;
  258. }
  259. } catch (\Exception $e) {
  260. Log::error('转账失败' . $e->getMessage());
  261. return false;
  262. }
  263. }
  264. }
  265. return true;
  266. }
  267. }