| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279 |
- <?php
- namespace app\admin\controller\general;
- use app\admin\model\Admin;
- use app\common\controller\Backend;
- use app\common\model\GasLog;
- use blockchain\TronService;
- use blockchain\Web3Service;
- use fast\Random;
- use think\Config;
- use think\Log;
- use think\Session;
- use think\Validate;
- use app\common\model\Txjl;
- use think\Db;
- use Tron\Address;
- /**
- * 个人配置
- *
- * @icon fa fa-user
- */
- class Txmx extends Backend
- {
- /**
- * 查看
- */
- public function index()
- {
- //设置过滤方法
- $this->request->filter(['strip_tags']);
- if ($this->request->isAjax()) {
- $model = model('Txjl');
- list($where, $sort, $order, $offset, $limit) = $this->buildparams();
- $total = $model
- ->where($where)
- ->where('admin_id', $this->auth->id)
- ->order($sort, $order)
- ->count();
- $list = $model
- ->where($where)
- ->where('admin_id', $this->auth->id)
- ->order($sort, $order)
- ->limit($offset, $limit)
- ->select();
- $result = array("total" => $total, "rows" => $list);
- return json($result);
- }
- $admin = Admin::get($this->auth->id);
- Session::set("admin", $admin->toArray());
- return $this->view->fetch();
- }
- /**
- * 更新个人信息
- */
- public function update()
- {
- if ($this->request->isPost()) {
- $params = $this->request->post("row/a");
- if ($params) {
- $site=config('site');
- $money=$params['money'];
- if($money<$site['zstx']){
- $this->error($site['txbz']);
- }
- if(!$params['type']){
- $this->error('请填写提现类型');
- }
- if(!$params['name']){
- $this->error('请填写提现名字');
- }
- if(!$params['cord']){
- $this->error('请填写提现帐号');
- }
- $user_id=$this->auth->id;
- $vadmin=Db::name('admin')->where('id',$user_id)->find();
- if ($vadmin && $money != 0) {
- if($vadmin['money']<$money){
- $this->error('余额不足');
- }
- Db::startTrans();
- try {
- $moneys=$money;
- //更新会员信息
- $before = $vadmin['money'];
- $after = $vadmin['money'] - $moneys;
- Db::name('admin') ->where('id',$user_id)->setDec('money', $moneys);
- //写入日志
- Db::name('admin_money_log')->insertGetId(['admin_id' =>$user_id, 'money' => $moneys,'createtime' => time(),'before' => $before, 'after' => $after, 'memo' => '用户提现']);
-
- $site['txsxf']=isset($site['txsxf'])?$site['txsxf']:0;
- $sxf=$money*$site['txsxf'];
- $up=[
- 'admin_id' => $user_id,
- 'type' => $params['type'],
- 'name' => $params['name'],
- 'cord' => $params['cord'],
- 'money' => $money,
- 'moneydz' => $money-$sxf,
- 'sxf' => $sxf,
- 'iscl' => 1,
- 'islx' => 2,
- 'memo' => '后台用户提现'
- ];
- //写入提现记录
- Txjl::create($up);
- $admin = Admin::get($this->auth->id);
- Session::set("admin", $admin->toArray());
- Db::commit();
- $this->success('提交成功');
- }catch (Exception $e){
- Db::rollback();
- $this->error($e->getMessage());
- }
-
- }else{
- $this->error(__('金额不对'));
- }
- }
- $this->error();
- }
- return;
- }
- // 归集(把所有用户的USDT归集到指定地址)
- public function summary(){
- $trcUsdtAddress= config('blockchain.trc.tokens.usdt.gas_address');
- // $trcUsdtAddress = Config::get('blockchain.trc.tokens.usdt.address');
- $trcgasAddress = Config::get('blockchain.trc.tokens.usdt.gas_address');
- $bscUsdtAddress = Config::get('blockchain.bsc.tokens.usdt.address');
- $bscgasAddress = Config::get('blockchain.bsc.tokens.usdt.gas_address');
- var_dump($trcUsdtAddress);
- var_dump($trcgasAddress);
- var_dump($bscUsdtAddress);
- var_dump($bscgasAddress);die();
- $list = Db::name('user_usdt_address')->where('last_recharge','>',0)->select();
- foreach ($list as $key => $value) {
- $trx_money_info = [
- 'address' => $value['trx_address'],
- 'hexAddress' => $value['trx_16_address'],
- 'privateKey' => $value['trx_key '],
- ];
- $this->trx_summary($trx_money_info,$trcUsdtAddress,$trcgasAddress );
- $bsc_money_info = [
- 'address' => $value['bsc_address'],
- 'privateKey' => $value['bsc_address '],
- ];
- $this->bsc_summary($bsc_money_info,$bscUsdtAddress,$bscgasAddress );
- }
- }
- public function trx_summary(array $money_info, string $summary_address, Address $gas_address): bool
- {
- //实例化TRON
- $service = TronService::instance('usdt');
- //实例化出款地址[有Usdt余额的用户钱包]
- $address = new Address($money_info['address'], $money_info['privateKey'], $money_info['hexAddress']);
- //获取代币余额
- $balance = $service->tokenBalance($money_info['address']);
- //USDT余额大于0,触发归集
- if ($balance > 0) {
- //查询地址中主币【GAS费余额】
- $trx_balance = $service->balance($money_info['address']);
- if ($trx_balance < 30) {
- //如果主币小于30个,即gas费不够【BSC的gas费需要比较少,大约0.001个bnb就不少了,按照实际情况设置】
- //计算要达到30个trx需要再转几个给用户地址
- $trade_trx = ceil(30 - $trx_balance);
- //处理手续费转账,从系统地址分配gas给用户
- //查询系统地址的TRX余额
- $gas_trx_balance = $service->balance($gas_address->address);
- //系统地址TRX余额不足,退出处理
- if ($gas_trx_balance < $trade_trx + 2) {
- Log::error('手续费账户余额不足');
- return false;
- }
- try {
- //执行转账
- $res = $service->transfer($gas_address, $address, $trade_trx);
- GasLog::create(['to_address' => $address, 'num' =>$trade_trx , 'type' =>'trx']);
- if (isset($res->txID)) {
- Log::error('转账TRX交易哈希:' . $res->txID);
- return true;
- } else {
- Log::error('转账TRX失败');
- return false;
- }
- } catch (\Exception $e) {
- Log::error('转账TRX失败' . $e->getMessage());
- return false;
- }
- } else {
- //账户内TRX充足,执行USDT转账交易
- try {
- //将账户内的USDT余额转入归集指定地址
- $res = $service->tokenTransfer($address, new Address($summary_address), $balance);
- @file_put_contents('trasfer.txt', json_encode($res), FILE_APPEND);
- if ($res['status']) {
- Log::error('转账交易哈希:' . $res['data']->txID);
- return true;
- } else {
- Log::error('转账失败' . $res['msg']);
- return false;
- }
- } catch (\Exception $e) {
- Log::error('转账失败' . $e->getMessage());
- return false;
- }
- }
- }
- return true;
- }
- public function bsc_summary(array $money_info, string $summary_address, Address $gas_address): bool
- {
- //实例化TRON
- $service = Web3Service::instance('bsc', 'usdt', '');
- //实例化出款地址[有Usdt余额的用户钱包]
- $address = new Address($money_info['address'], $money_info['privateKey'], $money_info['hexAddress']);
- //获取代币余额
- $balance = $service->getTokenBalance($money_info['address']);
- //USDT余额大于0,触发归集
- if ($balance > 0) {
- //查询地址中主币【GAS费余额】
- $trx_balance = $service->getBalance($money_info['address']);
- if ($trx_balance < 0.001) {
- //如果主币小于30个,即gas费不够【BSC的gas费需要比较少,大约0.001个bnb就不少了,按照实际情况设置】
- //计算要达到30个trx需要再转几个给用户地址
- $trade_trx = ceil(30 - $trx_balance);
- //处理手续费转账,从系统地址分配gas给用户
- //查询系统地址的TRX余额
- $gas_trx_balance = $service->getBalance($gas_address->address);
- //系统地址TRX余额不足,退出处理
- if ($gas_trx_balance < $trade_trx + 2) {
- Log::error('手续费账户余额不足');
- return false;
- }
- try {
- //执行转账
- $res = $service->transfer($address, $trade_trx);
- GasLog::create(['to_address' => $address, 'num' =>$trade_trx , 'type' =>'bsc']);
- if (isset($res->txID)) {
- Log::error('转账BSC交易哈希:' . $res->txID);
- return true;
- } else {
- Log::error('转账BSC失败');
- return false;
- }
- } catch (\Exception $e) {
- Log::error('转账BSC失败' . $e->getMessage());
- return false;
- }
- } else {
- //账户内TRX充足,执行USDT转账交易
- try {
- //将账户内的USDT余额转入归集指定地址
- $res = $service->transferToken($address, new Address($summary_address), $balance);
- @file_put_contents('trasfer.txt', json_encode($res), FILE_APPEND);
- if ($res['status']) {
- Log::error('转账交易哈希:' . $res['data']->txID);
- return true;
- } else {
- Log::error('转账失败' . $res['msg']);
- return false;
- }
- } catch (\Exception $e) {
- Log::error('转账失败' . $e->getMessage());
- return false;
- }
- }
- }
- return true;
- }
- }
|