Txmx.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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. var_dump($value);die();
  130. $trx_money_info = [
  131. 'address' => $value['trx_address'],
  132. 'hexAddress' => $value['trx_16_address'],
  133. 'privateKey' => $value['trx_key '],
  134. ];
  135. $this->trx_summary($trx_money_info,$trcUsdtAddress,$trcgasAddress );
  136. $bsc_money_info = [
  137. 'address' => $value['bsc_address'],
  138. 'privateKey' => $value['bsc_address '],
  139. ];
  140. $this->bsc_summary($bsc_money_info,$bscUsdtAddress,$bscgasAddress );
  141. }
  142. }
  143. public function trx_summary(array $money_info, string $summary_address, Address $gas_address): bool
  144. {
  145. //实例化TRON
  146. $service = TronService::instance('usdt');
  147. //实例化出款地址[有Usdt余额的用户钱包]
  148. $address = new Address($money_info['address'], $money_info['privateKey'], $money_info['hexAddress']);
  149. //获取代币余额
  150. $balance = $service->tokenBalance($money_info['address']);
  151. var_dump($balance);die();
  152. //USDT余额大于0,触发归集
  153. if ($balance > 0) {
  154. //查询地址中主币【GAS费余额】
  155. $trx_balance = $service->balance($money_info['address']);
  156. if ($trx_balance < 30) {
  157. //如果主币小于30个,即gas费不够【BSC的gas费需要比较少,大约0.001个bnb就不少了,按照实际情况设置】
  158. //计算要达到30个trx需要再转几个给用户地址
  159. $trade_trx = ceil(30 - $trx_balance);
  160. //处理手续费转账,从系统地址分配gas给用户
  161. //查询系统地址的TRX余额
  162. $gas_trx_balance = $service->balance($gas_address);
  163. //系统地址TRX余额不足,退出处理
  164. if ($gas_trx_balance < $trade_trx + 2) {
  165. Log::error('手续费账户余额不足');
  166. return false;
  167. }
  168. try {
  169. //执行转账
  170. $res = $service->transfer($gas_address, $address, $trade_trx);
  171. GasLog::create(['to_address' => $address, 'num' =>$trade_trx , 'type' =>'trx']);
  172. if (isset($res->txID)) {
  173. Log::error('转账TRX交易哈希:' . $res->txID);
  174. return true;
  175. } else {
  176. Log::error('转账TRX失败');
  177. return false;
  178. }
  179. } catch (\Exception $e) {
  180. Log::error('转账TRX失败' . $e->getMessage());
  181. return false;
  182. }
  183. } else {
  184. //账户内TRX充足,执行USDT转账交易
  185. try {
  186. //将账户内的USDT余额转入归集指定地址
  187. $res = $service->tokenTransfer($address, new Address($summary_address), $balance);
  188. @file_put_contents('trasfer.txt', json_encode($res), FILE_APPEND);
  189. if ($res['status']) {
  190. Log::error('转账交易哈希:' . $res['data']->txID);
  191. return true;
  192. } else {
  193. Log::error('转账失败' . $res['msg']);
  194. return false;
  195. }
  196. } catch (\Exception $e) {
  197. Log::error('转账失败' . $e->getMessage());
  198. return false;
  199. }
  200. }
  201. }
  202. return true;
  203. }
  204. public function bsc_summary(array $money_info, string $summary_address, Address $gas_address): bool
  205. {
  206. //实例化TRON
  207. $service = Web3Service::instance('bsc', 'usdt', '');
  208. //实例化出款地址[有Usdt余额的用户钱包]
  209. $address = new Address($money_info['address'], $money_info['privateKey'], $money_info['hexAddress']);
  210. //获取代币余额
  211. $balance = $service->getTokenBalance($money_info['address']);
  212. //USDT余额大于0,触发归集
  213. if ($balance > 0) {
  214. //查询地址中主币【GAS费余额】
  215. $trx_balance = $service->getBalance($money_info['address']);
  216. if ($trx_balance < 0.001) {
  217. //如果主币小于30个,即gas费不够【BSC的gas费需要比较少,大约0.001个bnb就不少了,按照实际情况设置】
  218. //计算要达到30个trx需要再转几个给用户地址
  219. $trade_trx = ceil(30 - $trx_balance);
  220. //处理手续费转账,从系统地址分配gas给用户
  221. //查询系统地址的TRX余额
  222. $gas_trx_balance = $service->getBalance($gas_address);
  223. //系统地址TRX余额不足,退出处理
  224. if ($gas_trx_balance < $trade_trx + 2) {
  225. Log::error('手续费账户余额不足');
  226. return false;
  227. }
  228. try {
  229. //执行转账
  230. $res = $service->transfer($address, $trade_trx);
  231. GasLog::create(['to_address' => $address, 'num' =>$trade_trx , 'type' =>'bsc']);
  232. if (isset($res->txID)) {
  233. Log::error('转账BSC交易哈希:' . $res->txID);
  234. return true;
  235. } else {
  236. Log::error('转账BSC失败');
  237. return false;
  238. }
  239. } catch (\Exception $e) {
  240. Log::error('转账BSC失败' . $e->getMessage());
  241. return false;
  242. }
  243. } else {
  244. //账户内TRX充足,执行USDT转账交易
  245. try {
  246. //将账户内的USDT余额转入归集指定地址
  247. $res = $service->transferToken($address, new Address($summary_address), $balance);
  248. @file_put_contents('trasfer.txt', json_encode($res), FILE_APPEND);
  249. if ($res['status']) {
  250. Log::error('转账交易哈希:' . $res['data']->txID);
  251. return true;
  252. } else {
  253. Log::error('转账失败' . $res['msg']);
  254. return false;
  255. }
  256. } catch (\Exception $e) {
  257. Log::error('转账失败' . $e->getMessage());
  258. return false;
  259. }
  260. }
  261. }
  262. return true;
  263. }
  264. }