| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358 |
- <?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;
- }
- // public function summary(){
- // $blockchain = config('blockchain');
- // $trcUsdtAddress=$blockchain['trc']['tokens']['usdt']['address'];
- // $trcgasAddress = $blockchain['trc']['tokens']['usdt']['gas_address'];
- // $bscUsdtAddress = $blockchain['bsc']['tokens']['usdt']['address'];
- // $bscgasAddress = $blockchain['bsc']['tokens']['usdt']['gas_address'];
- // $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 );
- // }
- // }
- // 归集(把所有用户的USDT归集到指定地址)
- public function summary()
- {
- // $blockchain = config('blockchain');
- //
- // // TRC 配置
- // $trcUsdtAddress = $blockchain['trc']['tokens']['usdt']['address'];
- // $trcGasAddress = new Address(
- // $blockchain['trc']['tokens']['usdt']['gas_address'],
- // '',
- // ''
- // );
- //
- // // BSC 配置
- // $bscUsdtAddress = $blockchain['bsc']['tokens']['usdt']['address'];
- // $bscGasAddress = new Address(
- // $blockchain['bsc']['tokens']['usdt']['gas_address'],
- // '',
- // ''
- // );
- $site = config('site');
- $trcUsdtAddress = $site['trxdz']['trx_address'];
- $trcGasAddress = new Address(
- $site['trxdz']['trx_gas_address'],
- $site['trxdz']['trx_gastrx_private_key'],
- $site['trxdz']['trx_hex_gas_address']
- );
- $bscUsdtAddress = $site['bscdz']['bsc_address'];
- $bscGasAddress = new Address(
- $site['bscdz']['bsc_gas_address'],
- $site['bscdz']['bsc_gas_private_key'],
- ''
- );
- $list = Db::name('user_usdt_address')->where('last_recharge', '>', 0)->select();
- try {
- foreach ($list as $value) {
- // TRX 归集
- $this->trx_summary(
- [
- 'address' => $value['trx_address'],
- 'hexAddress' => $value['trx_16_address'],
- 'privateKey' => $value['trx_key'],
- ],
- $trcUsdtAddress,
- $trcGasAddress
- );
- @file_put_contents("usdt.txt", "-456\r\n", 8);
- // BSC 归集
- $this->bsc_summary(
- [
- 'address' => $value['bsc_address'],
- 'privateKey' => $value['bsc_key'], // 确保字段名正确
- ],
- $bscUsdtAddress,
- $bscGasAddress
- );
- }
- @file_put_contents("usdt.txt", "- 到这了吗\r\n", 8);
- // $this->success('转账完成');
- } catch (\Exception $e) {
- Log::error('归集失败' . $e->getMessage());
- return false;
- }
- @file_put_contents("usdt.txt", "- 这里呢\r\n", 8);
- return '完成';
- }
- // 转出的用户地址 转入地址 手续费处理地址
- public function trx_summary(array $money_info, string $summary_address, Address $gas_address)
- {
- //实例化TRON
- $service = TronService::instance('usdt');
- @file_put_contents("usdt.txt", json_encode($service) . "-trx的实例化\r\n", 8);
- //实例化出款地址[有Usdt余额的用户钱包]
- $address = new Address($money_info['address'], $money_info['privateKey'], $money_info['hexAddress']);
- //获取代币余额
- $balance = $service->tokenBalance($money_info['address']);
- @file_put_contents("usdt.txt", $balance . "- trxd交易链usdt余额\r\n", 8);
- //USDT余额大于0,触发归集
- if ($balance > 0) {
- //查询地址中主币【GAS费余额】
- $trx_balance = $service->balance($money_info['address']);
- @file_put_contents("usdt.txt", $trx_balance . "- gas余额\r\n", 8);
- $gas_address_arr = get_object_vars($gas_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_arr['address']);
- //系统地址TRX余额不足,退出处理
- if ($gas_trx_balance < $trade_trx + 2) {
- Log::error('手续费账户余额不足');
- return false;
- }
- try {
- //执行转账 form to
- $res = $service->transfer($gas_address, $address, $trade_trx);
- @file_put_contents("usdt.txt", json_encode($res) . "-转账TRX手续费\r\n", 8);
- 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("usdt.txt", json_encode($res) . "-转账usdt\r\n", 8);
- @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)
- {
- @file_put_contents("usdt.txt", "-123\r\n", 8);
- //实例化TRON
- $service = Web3Service::instance('bsc', 'usdt', '');
- // $service = Web3Service::instance('bsc', 'tusd', '');
- @file_put_contents("usdt.txt", json_encode($service) . "-bsc的实例化\r\n", 8);
- //实例化出款地址[有Usdt余额的用户钱包]
- $address = new Address($money_info['address'], $money_info['privateKey'], '');
- //获取代币余额
- $balance = $service->getTokenBalance($money_info['address']);
- @file_put_contents("usdt.txt", $balance . "-bsc的usdt余额\r\n", 8);
- //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(0.001 - $trx_balance);
- //处理手续费转账,从系统地址分配gas给用户
- //查询系统地址的TRX余额
- $gas_address_arr = get_object_vars($gas_address);
- $gas_trx_balance = $service->getBalance($gas_address_arr['address']);
- @file_put_contents("usdt.txt", $gas_trx_balance . "-bsc的账户内gas余额\r\n", 8);
- //系统地址TRX余额不足,退出处理
- if ($gas_trx_balance < $trade_trx + 2) {
- Log::error('手续费账户余额不足');
- return false;
- }
- try {
- //执行转账
- $res = $service->transfer($money_info['address'], $trade_trx);
- @file_put_contents("usdt.txt", json_encode($res) . "-转账bsc手续费\r\n", 8);
- GasLog::create(['to_address' => $money_info['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($summary_address, $balance);
- @file_put_contents("usdt.txt", json_encode($res) . "bsc-转账usdt\r\n", 8);
- @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;
- }
- }
|