Txmx.php 12 KB

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