149 lines
3.8 KiB
PHP
149 lines
3.8 KiB
PHP
<?php
|
|
|
|
namespace App\Console\Commands;
|
|
|
|
use App\Models\Order;
|
|
use App\ServicePay\PayApiProvidesTrait;
|
|
use Carbon\Carbon;
|
|
use Illuminate\Console\Command;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Facades\Log;
|
|
use Ixudra\Curl\Facades\Curl;
|
|
|
|
class UsdtAddrGetList extends Command
|
|
{
|
|
use PayApiProvidesTrait;
|
|
/**
|
|
* The name and signature of the console command.
|
|
*
|
|
* @var string
|
|
*/
|
|
protected $signature = 'command:usdt_addr_get_list';
|
|
|
|
/**
|
|
* The console command description.
|
|
*
|
|
* @var string
|
|
*/
|
|
protected $description = '获得usdt地址列表';
|
|
|
|
/**
|
|
* Create a new command instance.
|
|
*
|
|
* @return void
|
|
*/
|
|
public function __construct()
|
|
{
|
|
parent::__construct();
|
|
}
|
|
|
|
private $pay_service;
|
|
private $pay_gate;
|
|
/**
|
|
* Execute the console command.
|
|
*
|
|
* @return mixed
|
|
*/
|
|
public function handle()
|
|
{
|
|
$this->checkNeedUpdateAddr();
|
|
}
|
|
|
|
/**
|
|
* 请求ustd的地址
|
|
* @return void
|
|
*
|
|
*/
|
|
private function checkNeedUpdateAddr(){
|
|
$this->setPayService('1017');
|
|
if (is_null($this->pay_service)) {
|
|
Log::error('通道不存在或支付类型错误 1017');
|
|
return;
|
|
}
|
|
|
|
$this->pay_gate = $this->pay_service->getGateway("1017");
|
|
if (is_null($this->pay_gate)) {
|
|
Log::error('通道不存在 1017');
|
|
return;
|
|
}
|
|
Log::info($this->pay_gate);
|
|
$this->pay_service->payConfig('1007');
|
|
Log::info($this->pay_service->pay_config);
|
|
|
|
//查找地址数据库
|
|
$addr_list = \App\Models\UsdtAddr::all();
|
|
if(count($addr_list) > 0){
|
|
Log::info(count($addr_list));
|
|
}
|
|
//没有地址数据,请求接口更新数据
|
|
$addr = $this->reqAddrList();
|
|
if(empty($addr)){
|
|
Log::error("没有获得地址数据");
|
|
return;
|
|
}
|
|
|
|
//写入数据库
|
|
foreach ($addr as $key => $value) {
|
|
$data['addr'] = $value;
|
|
$data['ustd_type'] = "trc";
|
|
$this->insertAddr($data);
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
}
|
|
|
|
private function reqAddrList(){
|
|
$createUrl='http://api.etwallet.net/wallet'.'/v1/api/address/batch';
|
|
|
|
//商户号
|
|
$accountId = $this->pay_service->pay_config['mch_id'];
|
|
//商户秘钥
|
|
$key = $this->pay_service->pay_config['token'];
|
|
Log::info($key);
|
|
$payData = [
|
|
'num' => 2,//请求钱包地址数量
|
|
'nonce' => strval(mt_rand(10000,99999)),//随机数
|
|
'merchantId' => $accountId, //商户id
|
|
'timestamp' => get_total_millisecond(),// //异步回调地址
|
|
];
|
|
|
|
$sign= $this->pay_service->sign($payData,$key,[]);
|
|
$payData['sign'] = $sign;
|
|
|
|
$res = Curl::to($createUrl)->withData($payData)->post();
|
|
Log::info('下单接口请求内容' . json_encode($payData, 256), []);
|
|
Log::info('返回内容' . json_encode($res, 256), []);
|
|
|
|
$res = is_array($res) ? $res : json_decode($res, true);
|
|
if (is_array($res) && isset($res['code']) && $res['code'] == 0) {
|
|
$data = $res['data'];
|
|
//$data = json_decode($data, true);
|
|
return explode(',',$data['addressList']);
|
|
} else {
|
|
$msg = $res['msg'] ?? '未知错误';
|
|
Log::info('错误' . $msg, []);
|
|
}
|
|
return null;
|
|
}
|
|
|
|
private function insertAddr($addr)
|
|
{
|
|
DB::beginTransaction();
|
|
$addrModel = new \App\Models\UsdtAddr();
|
|
foreach ($addr as $k => $v) {
|
|
$addrModel->$k = $v;
|
|
}
|
|
$r = $addrModel->save();
|
|
|
|
if ($r) {
|
|
DB::commit();
|
|
return 1;
|
|
} else {
|
|
DB::rollBack();
|
|
return false;
|
|
}
|
|
}
|
|
}
|